7👍
What you probably want to do is add a module variable to track connections, say _connections
, like so:
_connections = {}
@namespace('/connect')
class ConnectNamespace(BaseNamespace):
and then add initialize
and disconnect
methods that use some happy identifier you can reference later:
def initialize(self, *args, **kwargs):
_connections[id(self)] = self
super(ConnectNamespace, self).initialize(*args, **kwargs)
def disconnect(self, *args, **kwargs):
del _connections[id(self)]
super(ConnectNamespace, self).disconnect(*args, **kwargs)
When you need to generate an event, you can then just look up the right connection in the _connections
variable, and fire off the event with emit
.
(Didn’t test any of this, but I’ve used a similar pattern in many other languages: don’t see any reason why this wouldn’t work in Python as well).
0👍
Other then Femi’s answer, which I think certainly works. Using Redis would probably give you a bit more flexibility and using greenlet from gevent may qualify this approach as a bit more “in the framework”, since you are already using gevent-socketio 😀
REDIS_HOST = getattr(settings, 'REDIS_HOST', '127.0.0.1')
class YourNamespace(BaseNamespace):
def _listener(self, channel_label_you_later_call_in_post_save):
pubsub = redis.StrictRedis(REDIS_HOST).pubsub()
pubsub.subscribe(chan)
while True:
for i in pubsub.listen():
self.send({'message_data': i}, json=True)
def recv_message(self, message):
if is_message_to_subscribe(message):
self.spawn(self.listener, get_your_channel_label(message))
And in your post_save, you can do
red = redis.StrictRedis(REDIS_HOST)
red.publish(channel_label, message_data)