Skip to content

Instantly share code, notes, and snippets.

@ankurkumartech
Created August 22, 2023 07:07
Show Gist options
  • Save ankurkumartech/be27a182680f0598d95bfe556e281b4e to your computer and use it in GitHub Desktop.
Save ankurkumartech/be27a182680f0598d95bfe556e281b4e to your computer and use it in GitHub Desktop.
from channels.consumer import SyncConsumer, AsyncConsumer
from channels.exceptions import StopConsumer
from asgiref.sync import async_to_sync
class MySyncConsumer(SyncConsumer):
def websocket_connect(self, event):
async_to_sync(self.channel_layer.group_add)('programmers',
self.channel_name)
self.send({
'type':'websocket.accept'
})
def websocket_receive(self, event):
async_to_sync(self.channel_layer.group_send)('programmers', {
'type':'chat.message',
'message':event['text']
})
def websocket_disconnect(self, event):
print('websocker disconnect', event)
def chat_message(self, event):
self.send({'type':'websocket.send',
'text':event['message']})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment