Created
June 8, 2012 18:39
-
-
Save yuvadm/2897508 to your computer and use it in GitHub Desktop.
Real-time messaging using Tornado/sock.js/Redis/Brukva
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
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