assuming you setup a pyramid config with config.include('pyramd_sockjs')
followed by config.add_sockjs_route()
. the general difficulty here is to figure out how to get hold of the app's current sockjs-sessions. i'm thinking of three scenarios:
- usually you react to messages from within you own subclass of
pyramid_sockjs.session.Session
as shown in the chat example.
- broadcasting messages from within one of your views is just as easy by calling
request.get_sockjs_manager().broadcast(some_message)
- however, if you're neither inside the sockjs-messaging- nor the http-request-cycle like in your case, then you have to resort to pyramid's
registry
where all addons leave their traces.
at minute 5 in your video you would write something like this:
def listener():
r = redis.Redis()
r.subscribe(['foo'])
for msg in r.listen():
from pyramid.threadlocal import get_current_registry
get_current_registry().__sockjs_managers__[''].broadcast(msg)
to explain the above hack:
get_current_registry
is usually discouraged because it's hard to test your code. it returns the current request's or the global registry - a central core-component of pyramid.
- for
__sockjs_managers__
pyramid_sockjs should have provided a getter on the registry as it did for the request
.
- the empty string is the pyramid_sockjs' default
name
attribute (pass as kw to add_sockjs_route
to change it)
unfortunately pyramid_sockjs doesn't provide "rooms" yet so your messages will be broadcasted to all connected clients without any prior filter mechanisms. to help that you might want to subclass pyramid_sockjs.session.Session
and .SessionManager
. (please tell if you do!)