So with HTML5 advancing and browsers starting to ship with WebSockets (Chrome 4+, Opera 10.60, Safari 5 and soon Firefox 4) it’s time to start thinking about how to leverage this technology. One of the great things about WebSockets is that they work very similarly to standard OS sockets, the main difference is that they were designed to carry text and not binary data. However many commonly used protocols are text: HTTP, SMTP and Stomp come to mind.

Now if you haven’t heard of Stomp go check it out. It’s basically a lightweight publish/subscribe server. It’s not based on SOAP nor XML and it doesn’t include the kitchen sink. It’s simple and it works. If you want something more enterprisy go look at AMQP.

So Stomp turns out to be a really useful way to send messages back and forth between groups of clients. You can define end points however you wish (think queues that are the URL’s path). So how would one go about adding this message passing to their web application?

  1. First get a stomp server, I recommend stompserver because it’s ruby. It doesn’t support the complete Stomp protocol but more than enough to be useful. gem install stompserver
  2. Then you’ll need a server to proxy the web socket connections as web sockets require a special handshake. Luckily I have thrown em-websocket-proxy together for you. In this case we are going to use it to proxy Stomp but it will equally as work for HTTP or SMTP servers.gem install em-websocket-proxyem-websocket-proxy -p 8080 -r localhost -q 61613
  3. Checkout the example code from this gist (which includes a javascript stomp client) git clone git://gist.github.com/469609.git stomp-example Or (if you don’t want to use git) wget https://gist.github.com/gists/469609/download | tar -xz
  4. Visit the page stomp-example/index.html in a browser which supports WebSockets and then enter some text in the text box and press “Send through Stomp”. Viola! You have a successfully used a javascript client to a Stomp server.