README.rst
1SockJS-tornado server
2=====================
3
4SockJS-tornado is a Python server side counterpart of `SockJS-client browser library <https://github.com/sockjs/sockjs-client>`_
5running on top of `Tornado <http://tornadoweb.org>`_ framework.
6
7Simplified echo SockJS server could look more or less like::
8
9 from tornado import web, ioloop
10 from sockjs.tornado import SockJSRouter, SockJSConnection
11
12 class EchoConnection(SockJSConnection):
13 def on_message(self, msg):
14 self.send(msg)
15
16 if __name__ == '__main__':
17 EchoRouter = SockJSRouter(EchoConnection, '/echo')
18
19 app = web.Application(EchoRouter.urls)
20 app.listen(9999)
21 ioloop.IOLoop.instance().start()
22
23(Take look at `examples <https://github.com/MrJoes/sockjs-tornado/tree/master/examples>`_ for a complete version).
24
25Subscribe to `SockJS mailing list <https://groups.google.com/forum/#!forum/sockjs>`_ for discussions and support.
26
27SockJS-tornado API
28------------------
29
30SockJS provides slightly different API than ``tornado.websocket``. Main differences are:
31
321. Depending on transport, actual client connection might or might not be there. So, there is no _self.request_ and
33 other ``tornado.web.RequestHandler`` properties.
342. Changed ``open`` callback name to ``on_open`` to be more consistent with other callbacks.
353. Instead of ``write_message``, all messages are sent using ``send`` method. Just in case, ``send`` in ``tornado.web.RequestHandler``
36 sends raw data over the connection, without encoding it.
374. There is handy ``broadcast`` function, which accepts list (or iterator) of clients and message to send.
38
39Settings
40--------
41
42You can pass various settings to the ``SockJSRouter``, in a dictionary::
43
44 MyRouter = SockJSRouter(MyConnection, '/my', dict(disabled_transports=['websocket']))
45
46Deployment
47----------
48
49sockjs-tornado properly works behind haproxy and it is recommended deployment approach.
50
51Sample configuration file can be found `here <https://raw.github.com/sockjs/sockjs-node/master/examples/haproxy.cfg>`_.
52
53If your log is full of "WARNING: Connection closed by the client", pass ``no_keep_alive`` as ``True`` to ``HTTPServer`` constructor::
54
55 HTTPServer(app, no_keep_alive=True).listen(port)
56
57or::
58
59 app.listen(port, no_keep_alive=True)
60
61