Note that there are some explanatory texts on larger screens.

plurals
  1. PORedis and django-socketio
    text
    copied!<p>I built a chat with django-socketio and I'm trying to use redis with it.</p> <p>I have a very simple event.py:</p> <pre><code> @events.on_message(channel='^channel-') def messages(request, socket, context, message): socket.send_and_broadcast_channel(message) </code></pre> <p>The chat works perfectly.</p> <p>Now I'm trying to implement redis:</p> <pre><code> @events.on_message(channel='^channel-') def messages(request, socket, context, message): r = redis.StrictRedis() r = r.pubsub() r.subscribe('chat') s = redis.Redis() s.publish('chat', message) for m in r.listen(): data = m['data'] print data </code></pre> <p>The message is published on the redis channel, but once I call .listen(), nothing works anymore. It looks like .listen() is 'blocking' everything. I can't even escape the server using ctr+C</p> <p>Any idea on how I can solve that? Thank you!</p> <p>EDIT:</p> <p>Here is what I have now:</p> <pre><code> def listener(self): r = redis.StrictRedis() r = r.pubsub() r.subscribe('chat') for m in r.listen(): message = m['data'] socket.send_and_broadcast_channel(message) @events.on_message() def messages(request, socket, context, message): r = redis.Redis() r.publish('chat', message) </code></pre> <p>Still doesn't work.</p> <p>In order redis to work with gevent(used by django-socketio), we have to start a Greenlet when the user subscribes. So we need to add something like that:</p> <pre><code> def on_subscribe(self, *args, **kwargs): self.spawn(self.listener) </code></pre> <p>But I didn't figure how to do that with django-socketio given that we have to use an event </p> <pre><code> @event.on_connect() def connect(request, socket, context) </code></pre> <p>But I can't pass an argument 'self'. So I can't use 'self.spawn(self.listener)', and I don't know how I could do differently</p>
 

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