Skip to content
WebSocket emulation - Python server
Branch: master
Clone or download
Serge S. Koval
Serge S. Koval Bumped the version
Latest commit bd3a99b Oct 19, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
sockjs Fixed #116 XSS issue with the HTML transport Oct 19, 2018
.gitignore Update .gitignore Feb 23, 2014
LICENSE Switched license to MIT, added some module-level comments. Dec 9, 2011
README.rst Minor formatting changes in README.rst May 4, 2012
requirements.txt Fixed setup script. Jan 16, 2012 Bumped the version Oct 19, 2018


SockJS-tornado server

SockJS-tornado is a Python server side counterpart of SockJS-client browser library running on top of Tornado framework.

Simplified echo SockJS server could look more or less like:

from tornado import web, ioloop
from sockjs.tornado import SockJSRouter, SockJSConnection

class EchoConnection(SockJSConnection):
    def on_message(self, msg):

if __name__ == '__main__':
    EchoRouter = SockJSRouter(EchoConnection, '/echo')

    app = web.Application(EchoRouter.urls)

(Take look at examples for a complete version).

Subscribe to SockJS mailing list for discussions and support.

SockJS-tornado API

SockJS provides slightly different API than tornado.websocket. Main differences are:

  1. Depending on transport, actual client connection might or might not be there. So, there is no _self.request_ and other tornado.web.RequestHandler properties.
  2. Changed open callback name to on_open to be more consistent with other callbacks.
  3. Instead of write_message, all messages are sent using send method. Just in case, send in tornado.web.RequestHandler sends raw data over the connection, without encoding it.
  4. There is handy broadcast function, which accepts list (or iterator) of clients and message to send.


You can pass various settings to the SockJSRouter, in a dictionary:

MyRouter = SockJSRouter(MyConnection, '/my', dict(disabled_transports=['websocket']))


sockjs-tornado properly works behind haproxy and it is recommended deployment approach.

Sample configuration file can be found here.

If your log is full of "WARNING: Connection closed by the client", pass no_keep_alive as True to HTTPServer constructor:

HTTPServer(app, no_keep_alive=True).listen(port)


app.listen(port, no_keep_alive=True)
You can’t perform that action at this time.