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):
        self.send(msg)

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

    app = web.Application(EchoRouter.urls)
    app.listen(9999)
    ioloop.IOLoop.instance().start()

(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.

Settings

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

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

Deployment

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)

or:

app.listen(port, no_keep_alive=True)

Changelog

1.0.0

  • Major version bump. sockjs-tornado can be considered mature

  • Python 3 support

  • Tornado 3.0 support on both Python 2 and Python 3

  • Bug fixes

0.0.5

  • iOS 6 support

  • SockJS 0.3.3 client support

  • Some headers and request URI path is now passed to on_open handler

  • Added binary transport support for raw websocket transport

  • Minor multiplex sample fixes

0.0.4

  • Added support for older simplejson library versions

  • Fixed installation script

0.0.3

  • SockJS 0.3 support

  • Connection multiplexing sample

  • IP validation is now optional

0.0.2

  • SockJS 0.2 support and compatibility fixes

0.0.1

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.

Source Distribution

sockjs-tornado-1.0.0.zip (34.6 kB view details)

Uploaded Source

File details

Details for the file sockjs-tornado-1.0.0.zip.

File metadata

  • Download URL: sockjs-tornado-1.0.0.zip
  • Upload date:
  • Size: 34.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sockjs-tornado-1.0.0.zip
Algorithm Hash digest
SHA256 ee138b0a405a42cd899dd1ee42d51f25706c208c0a226260a8928e8ba2f2445d
MD5 ff03279696f3466b54fb184a8c358221
BLAKE2b-256 2ebb47646e8eedb6c3b38a2c160f2def9cece99b39c6453b8bda8c2812cf10f4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page