What is SockJS?
Under the hood SockJS tries to use native WebSockets first. If that fails it can use a variety of browser-specific transport protocols and presents them through WebSocket-like abstractions.
SockJS is intended to work for all modern browsers and in environments which don't support WebSocket protcol, for example behind restrictive corporate proxies.
If you worked with socket.io before, you might think that it is another socket.io spin-off. Well, there are some major differences between these two libraries:
- SockJS is designed with scalability in mind. I don't want to go into details here, but it is very easy to load balance connections by just using data stored in the URL.
- SockJS follows HTML5 Websockets API as closely as possible. So, switching to native implementation should be painless. On other hand, current stable socket.io version exposes higher level (richer) API, which is not compatible with Websocket spec.
- SockJS implements at least one streaming protocol for every major browser. Since version 0.7, socket.io does not support any streaming transports - only websockets (or flashsockets fallback) and various polling transports. One can argue that streaming protocols are not worth the trouble, but they really reduce latency and server load, if client is able to use them.
- SockJS has very simple protocol and very good documentation. Also, there is excellent test suite for backend developers, so they verify their SockJS server implementation. So, SockJS enforces certain behavioral patterns and expects all server implementations confirm to the protocol test.
- There is no flashsocket fallback, because it does not work for most of the time.
If you worked with TornadIO, you will find API very similar. This is a simple echo server:
from tornado import web, ioloop from sockjs.tornado import SockJSRouter, SockJSConnection class EchoConnection(SockJSConnection): def on_message(self, msg): self.send(msg) if __name__ == '__main__': EchoRouter = SockJSRouter(EchoConnection, '/echo') app = web.Application(EchoRouter.urls) app.listen(9999) ioloop.IOLoop.instance().start()
Anyway, hope you'll like it.