Django WebSockets
I want to send the connected user list from my Comment Consumer class to ListTicket consumer. Just want to show the connected user name in each room on the main page of the application.
ListTicketConsumer class:
class ListTicketConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = 'list_ticket'
# Join room group
await self.channel_layer.group_add(
self.room_name,
self.channel_name,
)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.room_name,
self.channel_name
)
async def update_table(self, event):
comment = event['comment']
await self.send(text_data = json.dumps({
'comment' : comment
}))
async def send_userinit(self, event):
print('---user in func----')
userinit = event['userinit']
id = event['ticket_id']
print(userinit, 'user in func----')
await self.send(text_data = json.dumps({
'ticket_id' : id,
'userinit' : userinit
}))
CommentConsumer class:
class CommentConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.comment_room = self.scope['url_route']['kwargs']['comment_room']
user_list.append(self.scope["session"]["userinit"])
# Join room group
await self.channel_layer.group_add(
self.comment_room,
self.channel_name,
)
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send(
'list_ticket',
{'type': 'send_userinit', 'ticket_id' : id,'userinit': user_list}
))
print(user_list)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.comment_room,
self.channel_name
)
user_list.remove(self.scope["session"]["userinit"])
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send(
'list_ticket',
{'type': 'send_userinit', 'ticket_id' : id,'userinit': user_list}
))
print(user_list)
This is what I'm trying to do, but it's not working, if anybody knows Please Help.