root: add Channels Message Storage and consumer

This commit is contained in:
Jens Langhammer
2020-11-26 17:08:26 +01:00
parent 7efed56acc
commit 9c00c86e9b
5 changed files with 62 additions and 1 deletions

View File

@ -0,0 +1,20 @@
"""websocket Message consumer"""
from channels.generic.websocket import JsonWebsocketConsumer
from django.core.cache import cache
class MessageConsumer(JsonWebsocketConsumer):
"""Consumer which sends django.contrib.messages Messages over WS.
channel_name is saved into cache with user_id, and when a add_message is called"""
def connect(self):
self.accept()
cache.set(f"user_{self.scope['user'].pk}_{self.channel_name}", True)
# pylint: disable=unused-argument
def disconnect(self, close_code):
cache.delete(f"user_{self.scope['user'].pk}_{self.channel_name}")
def event_update(self, event: dict):
"""Event handler which is called by Messages Storage backend"""
self.send_json(event)