Skip to content

Instantly share code, notes, and snippets.

@yuvadm
Created June 8, 2012 18:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuvadm/2897508 to your computer and use it in GitHub Desktop.
Save yuvadm/2897508 to your computer and use it in GitHub Desktop.
Real-time messaging using Tornado/sock.js/Redis/Brukva
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()
@vivekdurai
Copy link

why are you subscribing to a channel while initializing the connection handler? how would you add dynamic channels to a tornado instance? where is the mapping between sockjs connections and redis channels? just asking cos it doesn't seem obvious

@kiddouk
Copy link

kiddouk commented Jul 25, 2012

I'd like to raise that this code is a good example. However, the implementation raises an annoying case : Each connected socket will create a socket to redis. While this is acceptable for some low concurrency scenario, it is best to use one redis connection that subscribe to the channels while "listening".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment