Skip to content

Commit e40e0aa

Browse files
committed
Added auto loading of events.py modules in INSTALLED_APPS, ala django.contrib.admin.autodiscover(), for ensuring event handlers are always regisrered. Closes stephenmcd#16.
1 parent 5c6288d commit e40e0aa

File tree

3 files changed

+24
-50
lines changed

3 files changed

+24
-50
lines changed

README.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ Events
188188
The ``django_socketio.events`` module provides a handful of events
189189
that can be subscribed to, very much like connecting receiver
190190
functions to Django signals. Each of these events are raised
191-
throughout the relevant stages of a Socket.IO request.
191+
throughout the relevant stages of a Socket.IO request. These events
192+
represent the main approach for implementing your socket handling
193+
logic when using django-socketio.
192194

193195
Events are subscribed to by applying each event as a decorator
194196
to your event handler functions::
@@ -199,6 +201,14 @@ to your event handler functions::
199201
def my_message_handler(request, socket, context, message):
200202
...
201203

204+
Where should these event handlers live in your Django project? They
205+
can go anywhere, so long as they're imported by Django at startup
206+
time. To ensure that your event handlers are always loaded, you can
207+
put them into a module called ``events.py`` in one of your apps listed
208+
in Django's ``INSTALLED_APPS`` setting. django-socketio looks for these
209+
modules, and will always import them to ensure your event handlers are
210+
loaded.
211+
202212
Each event handler takes at least three arguments: the current Django
203213
``request``, the Socket.IO ``socket`` the event occurred for, and a
204214
``context``, which is simply a dictionary that can be used to persist

django_socketio/example_project/chat/views.py

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,11 @@
11

22
from django.contrib.auth.decorators import user_passes_test
33
from django.shortcuts import get_object_or_404, render, redirect
4-
from django.utils.html import strip_tags
5-
from django_socketio import events, broadcast, broadcast_channel, NoSocket
4+
from django_socketio import broadcast, broadcast_channel
65

7-
from chat.models import ChatRoom, ChatUser
6+
from chat.models import ChatRoom
87

98

10-
@events.on_subscribe
11-
def message(request, socket, context, channel):
12-
print "subscribed to " + channel
13-
14-
@events.on_message(channel="^room-")
15-
def message(request, socket, context, message):
16-
"""
17-
Event handler for a room receiving a message. First validates a
18-
joining user's name and sends them the list of users.
19-
"""
20-
room = get_object_or_404(ChatRoom, id=message["room"])
21-
if message["action"] == "start":
22-
user, created = room.users.get_or_create(name=strip_tags(message["name"]))
23-
if not created:
24-
socket.send({"action": "in-use"})
25-
else:
26-
context["user"] = user
27-
users = [u.name for u in room.users.exclude(id=user.id)]
28-
socket.send({"action": "started", "users": users})
29-
user.session = socket.session.session_id
30-
user.save()
31-
joined = {"action": "join", "name": user.name, "id": user.id}
32-
socket.send_and_broadcast_channel(joined)
33-
else:
34-
try:
35-
user = context["user"]
36-
except KeyError:
37-
return
38-
if message["action"] == "message":
39-
message["message"] = strip_tags(message["message"])
40-
message["name"] = user.name
41-
socket.send_and_broadcast_channel(message)
42-
43-
@events.on_finish(channel="^room-")
44-
def finish(request, socket, context):
45-
"""
46-
Event handler for a socket session ending in a room. Broadcast
47-
the user leaving and delete them from the DB.
48-
"""
49-
try:
50-
user = context["user"]
51-
except KeyError:
52-
return
53-
socket.broadcast_channel({"action": "leave", "name": user.name, "id": user.id})
54-
user.delete()
55-
569
def rooms(request, template="rooms.html"):
5710
"""
5811
Homepage - lists all rooms.

django_socketio/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11

22
from django.conf.urls.defaults import patterns, url
3+
from django.conf import settings
4+
from django.utils.importlib import import_module
5+
6+
7+
# Try and import an ``events`` module in each installed app,
8+
# to ensure all event handlers are connected.
9+
for app in settings.INSTALLED_APPS:
10+
try:
11+
import_module("%s.events" % app)
12+
except ImportError:
13+
pass
314

415

516
urlpatterns = patterns("django_socketio.views",

0 commit comments

Comments
 (0)