Created
May 30, 2012 12:19
-
-
Save vivekdurai/2835916 to your computer and use it in GitHub Desktop.
Server Side (Django) Juggernaut Style Websockets
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
from django.contrib.sessions.models import Session | |
from django.contrib.auth import get_user | |
from django.contrib.auth.models import User | |
from juggernaut import Juggernaut | |
jug = Juggernaut() | |
def get_current_sessions(uid): | |
class Dummy(object): pass | |
django_request = Dummy() | |
s = Session.objects.all() | |
list_of_sessions=[] | |
for session in s: | |
if session.get_decoded().get('_auth_user_id') == uid: | |
list_of_sessions.append(session.session_key) | |
return list_of_sessions | |
def get_current_user(session_key): | |
class Dummy(object): pass | |
django_request = Dummy() | |
s = Session.objects.get(session_key=session_key) | |
uid = s.get_decoded().get('_auth_user_id') | |
user = User.objects.get(pk=uid) | |
if user.is_authenticated(): | |
return user | |
else: | |
return None | |
def send_a_jug(sender, instance, *args, **kwargs): | |
channels = ["connections-%s" % (session) for session in get_current_sessions(instance.user.id)] | |
jug.publish(channels, json.dumps({'message_type':'aaaa','message':'bbbb'})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure to delete session objects on logout or you will end up publishing to a huge list of sessions