connect / notification /consumers.py
thejagstudio's picture
Add notification app with initial models, views, and admin configuration; remove unused image files
d5374bf
raw
history blame
960 Bytes
import json
from channels.generic.websocket import AsyncWebsocketConsumer
class NotificationConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.group_name = 'Yuvak'
await self.channel_layer.group_add(
self.group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
await self.channel_layer.group_discard(
self.group_name,
self.channel_name
)
async def receive(self, text_data):
data = json.loads(text_data)
message = data['data']
await self.channel_layer.group_send(
self.group_name,
{
'type': 'send_notification',
'message': message
}
)
async def send_notification(self, event):
message = event['message']
await self.send(text_data=json.dumps({'message': message}))