Higher Order Blog

home

Stomple: JMS via WebSockets

09 May 2010

I've spent a couple of days working on Stomple (pronounced "stumble"). Stomple aims to be an easy to use, full-featured and robust Stomp client for JavaScript. You can watch a screencast of the transactional chat example. Screencast of transactional chat. What is Stomp? Stomp provides an interoperable wire format so that any of the available Stomp Clients can communicate with any Stomp Message Broker to provide easy and widespread messaging interop among languages, platforms and brokers. You can use any of the Stomp Clients to work with any JMS compliant message broker, for example HornetQ. There are Stomp clients for many languages, and Stomple adds JavaScript via WebSockets. This means that you can send and receive JMS messages directly in the browser (when it supports WebSockets). Stomple is inspired by Jeff Mesnil's cool stomp-websocket, but I wanted more customization, feature-detection, a nicer API, and more robustness (e.g. timeouts) (and I wanted to write it myself :)). Just like Jeff's client, Stomple is not directly a Stomp client since it runs over WebSockets which requires a handshake between the browser's client and the server. This is supported by HornetQ and ActiveMQ (details in Jeff's docs: http://www.jmesnil.net/stomp-websocket/doc/).
This library is not a pure Stomp client. It is aimed to run on the Web Sockets protocol which is not TCP. Basically, the Web Sockets protocol requires a handshake between the browser client and the server to ensure the browser's "same-origin" security model remains in effect. This means that this library can not connect to regular Stomp brokers since they would not understand the handshake initiated by the Web Socket which is not part of the Stomp protocol and would likely reject the connection.
I'll post more documentation soon, but stomple should be quite simple to use.

if (Stomple) {//feature detection: is stomple available?
    Stomple.debug = true;//enable debug logging of messages
    var client = Stomple.create_client({
        url: "ws://localhost:61614/stomp", 
        destination: "jms.topic.chat",
        login: "guest",
        passcode: "guest"
    });
    client.subscribe({
        handler: function(f) {
            console.log('received frame');
            console.log(f);
            console.log("Message: "+f.body);
            console.log("'this' is bound to 'scope'");
            console.log("Session:"+this.session);
            client.send({
                success: function() {},//handle successful send
                failure: function() {},//handle failed send
                timeout: 5000//wait max 5 seconds before failing
            });
        },
        scope: client,
        success: function() {//did subscription succeed?
            console.log("sub ok..");
        },    
        failure: function() {//did subscription fail?
            console.log("sub fail");
        } 
    });
} 
Check it out on Github: http://github.com/krukow/stomple. More info to come ;)