Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've recently written a very similar application on a similar setup, so I do have several insights.</p> <p>The proper way of doing what you need is to have a pub-sub backend. There's only so much you can do with simple <code>ConnectionHandler</code>s. Eventually, handling class-level sets of connections starts to get ugly (not to mention buggy).</p> <p>Ideally, you'd want to use something like Redis, with async bindings to tornado (check out <a href="https://github.com/evilkost/brukva">brukva</a>). That way you don't have to mess with registering clients to specific channels - Redis has all that out of the box.</p> <p>Essentially, you have something like this:</p> <pre><code>class ConnectionHandler(SockJSConnection): def __init__(self, *args, **kwargs): super(ConnectionHandler, self).__init__(*args, **kwargs) self.client = brukva.Client() self.client.connect() self.client.subscribe('some_channel') def on_open(self, info): self.client.listen(self.on_chan_message) def on_message(self, msg): # this is a message broadcast from the client # handle it as necessary (this implementation ignores them) pass def on_chan_message(self, msg): # this is a message received from redis # send it to the client self.send(msg.body) def on_close(self): self.client.unsubscribe('text_stream') self.client.disconnect() </code></pre> <p>Note that I used <a href="https://github.com/MrJoes/sockjs-tornado">sockjs-tornado</a> which I found to be much more stable than socket.io.</p> <p>Anyway, once you have this sort of setup, sending messages from any other client (such as Django, in your case) is as easy as opening a Redis connection (<a href="https://github.com/andymccurdy/redis-py">redis-py</a> is a safe bet) and publishing a message:</p> <pre><code>import redis r = redis.Redis() r.publish('text_channel', 'oh hai!') </code></pre> <hr> <p>This answer turned out pretty long, so I went the extra mile and made a blog post out of it: <a href="http://blog.y3xz.com/blog/2012/06/08/a-modern-python-stack-for-a-real-time-web-application/">http://blog.y3xz.com/blog/2012/06/08/a-modern-python-stack-for-a-real-time-web-application/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload