Background information on websockets and an example walkthrough building an evented web application with websockets

Plack, AnyEvent, Redis, and SockJS

I thought I would share some of what I’ve been doing with SockJS and Redis in Perl lately. This is an example application and some background information on websockets that will be part of a talk on AnyEvent and Plack/Twiggy at PDX.pm this week.

WebSockets

A websocket is a bi-directional socket connecting a client and the server. It allows for communication similar to comet or ajax, without polling overhead. A couple websocket wrapper implementations have popped up in the Node.js community: Socket.IO and SockJS.

Both implementations use alternative transports to create a cross-browser websocket, or websocket-like, connection. Cross-browser support makes use of a proper WebSocket protocol initially, and failing that, backs down to a supported transport.

SockJS vs Socket.IO

SockJS was a project started to address bloat in Socket.IO. In the relative past, SockJS has appeared to be a more consistent project, and has had more active development. In response to a competing project, Socket.IO was trimmed down to reduce bloat and given a different name: Engine.IO.

WebSocket Server

These websocket implementations require a server-side component to run. The Node SocketIO module is the server module that also distributes the client side module, and SockJS is broken into a server module and client side javascript.

WebSockets in Perl

We can use them in Perl!

  • Perl’s SockJS is the SockJS server to compliment the javascript SockJS client
  • PocketIO is the Perl Socket.IO server

But, it’s not, not painful.

Problems

  • Make sure to turn chunking off on the SockJS instance, otherwise:

    4f
    {"origins":["*:*"],"entropy":3165083279,"websocket":true,"cookie_needed":false}
    0
    

Will be your output on the websocket. This is chunked return from Twiggy, which is returned because Twiggy returns via HTTP1.1 by default.