Skip to main content

SockJS python server implementation on top of Tornado framework

Project description

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)



  • SockJS 0.3 support
  • Connection multiplexing sample
  • IP validation is now optional


  • SockJS 0.2 support and compatibility fixes


Initial release.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sockjs-tornado, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size (37.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page