Skip to content

Instantly share code, notes, and snippets.

@jameskeane
Last active December 13, 2015 23:39
Show Gist options
  • Save jameskeane/4993362 to your computer and use it in GitHub Desktop.
Save jameskeane/4993362 to your computer and use it in GitHub Desktop.
Pondering Django Server-Sent Events
# Uses django-signals behind the scenes to throw the events around
# Also uses gevent.pywsgi.WSGIServer to stream the event source.
# Basic usage
from django.dispatch import Events
from django.contrib.auth.models import User, Group
# Basic support for individual users
user = User.objects.get(id=1)
user.subcribe('event:name another:event') # subscriptions are session persistent (configurable to db)?
user.unsubscribe('event:name')
user.is_online() # Is the user currently connected to an event source?
# Also supports django groups (you should use them)
admins = Group(name='admin')
admins.subscribe('event:name group:event')
admins.unsubscribe('event:name')
admins.online_users() # returns an array of User objects corresponding to currently online users
# publish will push the update to all currently connected users
# It is pretty intelligent since it is using signals, it will not try to send events
# to users not currently connected, without having to check them.
Events.publish('event:name', {})
# Also support sending messages to _all_ currently connected users
Events.broadcast('to:everyone', {'data': 'set'})
@jameskeane
Copy link
Author

Can use gevent-SocketIO for inspiration on how to intercept django requests.

@jameskeane
Copy link
Author

Phase two will probably include adding support for message persistence for tracking and replays.

Also, message persistence will allow users who have lost connections to retrieve messages they missed.

@jameskeane
Copy link
Author

Keep in mind that some sort of broker server will need to be used when scaling past a single server, otherwise events in one server will not propagate.

This and the previous point, will result in the development of mongodb, redis, riak, etc. drivers for SSE_BROKER.

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