diff --git a/README.md b/README.md
index 908ab7e..e4a47fd 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,8 @@ By popular demand, I'm building a better anonkun. It doesn't do much right now t
## Requirements
Python 3.6+
PostgreSQL 10.4+
-Python packages: `django psycopg2 channels jinja2 argon2-cffi`
+Redis 4.0.10+
+Python packages: `django psycopg2 channels channels_redis jinja2 argon2-cffi`
## Install
```
diff --git a/quest/consumers.py b/quest/consumers.py
index b28d095..cd8799c 100644
--- a/quest/consumers.py
+++ b/quest/consumers.py
@@ -4,6 +4,7 @@ Consumers available for the /quest websocket.
"""
import json
+from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
from .events import events
@@ -21,15 +22,26 @@ class QuestConsumer(WebsocketConsumer):
super().__init__(*args, **kwargs)
def connect(self):
+ self.quest_id = self.scope['url_route']['kwargs']['quest_id']
+ self.group_name = 'quest_' + str(self.quest_id)
+
+ async_to_sync(self.channel_layer.group_add)(
+ self.group_name,
+ self.channel_name
+ )
+
self.accept()
def disconnect(self, close_code):
- pass
+ async_to_sync(self.channel_layer.group_discard)(
+ self.group_name,
+ self.channel_name
+ )
def receive(self, text_data):
"""
- Parses the received data as JSON and dispatches the appropirate
- event handler.
+ Parses the data received from the WebSocket as JSON and
+ dispatches the appropriate event handler.
"""
try:
data = json.loads(text_data)
@@ -42,9 +54,22 @@ class QuestConsumer(WebsocketConsumer):
def send(self, event, data={}):
"""
- Overridden method. If a dictionary is provided, it is converted
- to JSON before sending it.
- If a string is provided, it is sent out directly.
+ Overridden method. All data provided (preferably as a dictionary)
+ is parsed into JSON before sending to other consumers in the group.
"""
data = json.dumps({'event': event, 'data': data})
+ async_to_sync(self.channel_layer.group_send)(
+ self.group_name,
+ {
+ 'type': 'dispatch_send',
+ 'message': data
+ }
+ )
+
+ def dispatch_send(self, event):
+ """
+ Receives events from other consumers in the group and relays the
+ data to the WebSocket.
+ """
+ data = event['message']
super().send(text_data=data)
diff --git a/quest/events.py b/quest/events.py
index bc84ebd..5352162 100644
--- a/quest/events.py
+++ b/quest/events.py
@@ -143,7 +143,7 @@ def text_post(socket, data):
# cleaning
post_text = bleach.clean(post_text.strip())
- post_text = text.replace("\n", "
")
+ post_text = post_text.replace("\n", "
")
# handle image
diff --git a/titivillus/settings.py b/titivillus/settings.py
index f3a0906..d369595 100644
--- a/titivillus/settings.py
+++ b/titivillus/settings.py
@@ -147,3 +147,11 @@ PASSWORD_HASHERS = [
]
ASGI_APPLICATION = 'titivillus.routing.application'
+CHANNEL_LAYERS = {
+ 'default': {
+ 'BACKEND': 'channels_redis.core.RedisChannelLayer',
+ 'CONFIG': {
+ "hosts": [('127.0.0.1', 6379)],
+ },
+ },
+}