Wednesday, September 12, 2012

Vertx, SockJS, Heroku and AC

Vertx, SockJS, Heroku and Application Craft

There's an exciting application development tool that caught my attention: http://vertx.io/.  It's pretty impressive, as it states on it's home page it's an "Effortless asynchronous application development for the modern web and enterprise".

What I found interesting is that it's a JVM based solution and supports multiple languages, namely Java, Groovy, Javascript, Python, and Ruby.  It uses a different threading model then typical JEE does - your app runs in it's own thread and only one thread.  So you can program as if your single threaded.  Concurrency is not an issue.   Refer to the web site for more information.

For purposes of this demo, I wanted to demonstrate one aspect that Vertx provides which is SockJS.  SockJS is a browser Javascript library that provides a WebSocket-like object.

Vertx provides a SockJS server implementation.  So it's a breeze to intercommunicate between browsers and mobile apps!   I think a demo would help see how powerful this is.  Watch this

The following code is all that my vertx server contains:

load('vertx.js');

var server = vertx.createHttpServer();

// Create a SockJS bridge which lets everything through
vertx.createSockJSServer(server).bridge({prefix: "/eventbus"}, [{}], [{}]);

//Start the server, list to the port defined in the environment and 
//accept requests from any host.
//When running locally, set the PORT to say 8080
//It's purpose has more to with running on Heroku
server.listen(vertx.env['PORT'],'0.0.0.0');

The client code is pretty simple too:
 
 var eb = null;
  var firstMessage = true;
  var firstSubscribe = true;
  
  /**
   * Publish a message to a topic
   */
  function publish(address,  message) {
    if (eb) {
      var json = {text: message};
      eb.publish(address, json);
    }
  }
    /**
     * Subscribe to a topic
     */
  function subscribe(address) {
    if (eb) {
        eb.registerHandler(address, function(msg,replyTo) {
            app.setValue('receivedTxtArea',app.getValue('receivedTxtArea') + (!firstMessage ? '' : '\n') + msg.text);
            firstMessage = false;
        });
        app.setValue('subscribedTxtArea',app.getValue('subscribedTxtArea') + (firstSubscribe ? '' : '\n') + address);
        firstSubscribe = false;
    }
  }
    /**
     * Close connection
     */
  function closeConn() {
    if (eb) {
      eb.close();
    }
  }
  /** 
   * Open a connection
   * Note it's using heroku
   */
  function openConn() {
    if (!eb) {
        eb = new vertx.EventBus("http://fast-mountain-7331.herokuapp.com/eventbus");
        eb.onopen = function() {
        app.setValue('connectStatus',"Connected");
      };

      eb.onclose = function() {
        app.setValue('connectStatus',"Not Connected");
        eb = null;
      };
    }
  }


You can access the AC app and associated code for Vertx from GitHub: https://github.com/bartonhammond/vertx-ac

To get Vertx running on Heroku follow this guide http://fbflex.wordpress.com/2012/05/02/running-vert-x-applications-on-heroku/ but with the following change:

Use this command instead as it supports having a "server.js" rather then a "server.groovy".  Also, you need to have a Procfile as you see within my github.

heroku create --stack cedar --buildpack https://github.com/bartonhammond/heroku-buildpack-vertx-jdk7.git
The best way to see the changes is upload the VertxAC.zip from my GitHub account and import it into AC. One note of interest is shown below - include the SockJS client files: