django channels 中的频道房间名称是否需要唯一?我有一个功能可以正常工作但有一些顾虑
Is it necessary to make the channel room name unique in django channels? I have a function which works fine but have some concerns
大家好,我制作了一个小型提要系统,它使用 websockets 并在有人访问该网站并登录时打开,所有新提要将实时发送并实时更新给那些订阅了特定内容的人用户。为此,我正在使用 django-channels 并且拥有一个非唯一的 room_name,以便每个登录并访问该网站的用户都可以访问它。但是对于这样的系统来说,有一个非唯一的 room_name 是一个好习惯吗?如果大量用户同时访问该网站,是否会影响性能或变得过时?
或
我应该用当前用户和包含订阅用户的 manyTomanyField 创建一个新的 table 吗?如果是这样,我该如何将所有用户添加到频道组?
这是我的代码,现在运行良好,
async def connect(self):
print('connected')
user = self.scope["user"]
self.user = user
room_name = f'sub-feeds'
self.room_name = room_name
await self.accept()
目前,我正在检查 returns True
如果用户订阅了我,否则 returns False
.
async def external_feed(self, event): # sending from outside the consumer
user = self.user
oth_user = event['user']
condition = await self.get_subs_status(user, oth_user)
if condition == True:
await self.send_json({
'newFeed': event['feeds'],
})
我实际上担心如果用户数量增加很多并且使用单独的房间与订阅用户(新数据库 table)将解决问题,这是否会中断。
请询问您是否需要更多信息。非常感谢帮助。
谢谢
如果我要实现这个,我会使用用户名(假设是唯一的)作为房间名称。实现将是这样的,
from collections import defaultdict
class LiveFeedConsumer(WebsocketConsumer):
# map which uses the subscribed user's username as a key and stores the set of all users' username's who subscribed to their feeds as value.
# Ex - users 1, 2, 3, 4 are available
# __subscriptions[1] = {2, 3} -> Means 2 and 3 have subscribed to 1's feeds
__subscriptions = defaultdict(set)
def connect(self):
# authenticate
user_to_which_to_subscribe_to = get_user_somehow()
self.scope["session"]["room_name"] = user_to_which_to_subscribe_to.username
# Accept connection
self.__subscriptions[user_to_which_to_subscribe_to.username].add(self.scope["user"].username) # To extract users and their subscriptions
def disconnect(self, message):
# Disconnection logic
self.__subscriptions[ self.scope["session"]["room_name"] ].remove(self.scope["user"].username)
def get_subs_status(self, user, other_user):
return user.username in self.__subscriptions[other_user.username]
def external_feed(self, event): # sending from outside the consumer
user = self.user
oth_user = event['user']
condition = self.get_subs_status(user, oth_user)
if condition is True:
self.send_json({
'newFeed': event['feeds'],
})
# publish to pub/sub mechanism to support multiple processes/nodes
您可以添加代码的 async/await 部分。你明白逻辑了。
这样您就可以为每个用户的直播提供单独的房间。任何用户都可以订阅多个其他用户。此外,您可以添加一个 pub/sub 侦听器来水平缩放它,添加多个 nodes/processes.
大家好,我制作了一个小型提要系统,它使用 websockets 并在有人访问该网站并登录时打开,所有新提要将实时发送并实时更新给那些订阅了特定内容的人用户。为此,我正在使用 django-channels 并且拥有一个非唯一的 room_name,以便每个登录并访问该网站的用户都可以访问它。但是对于这样的系统来说,有一个非唯一的 room_name 是一个好习惯吗?如果大量用户同时访问该网站,是否会影响性能或变得过时?
或
我应该用当前用户和包含订阅用户的 manyTomanyField 创建一个新的 table 吗?如果是这样,我该如何将所有用户添加到频道组?
这是我的代码,现在运行良好,
async def connect(self):
print('connected')
user = self.scope["user"]
self.user = user
room_name = f'sub-feeds'
self.room_name = room_name
await self.accept()
目前,我正在检查 returns True
如果用户订阅了我,否则 returns False
.
async def external_feed(self, event): # sending from outside the consumer
user = self.user
oth_user = event['user']
condition = await self.get_subs_status(user, oth_user)
if condition == True:
await self.send_json({
'newFeed': event['feeds'],
})
我实际上担心如果用户数量增加很多并且使用单独的房间与订阅用户(新数据库 table)将解决问题,这是否会中断。
请询问您是否需要更多信息。非常感谢帮助。
谢谢
如果我要实现这个,我会使用用户名(假设是唯一的)作为房间名称。实现将是这样的,
from collections import defaultdict
class LiveFeedConsumer(WebsocketConsumer):
# map which uses the subscribed user's username as a key and stores the set of all users' username's who subscribed to their feeds as value.
# Ex - users 1, 2, 3, 4 are available
# __subscriptions[1] = {2, 3} -> Means 2 and 3 have subscribed to 1's feeds
__subscriptions = defaultdict(set)
def connect(self):
# authenticate
user_to_which_to_subscribe_to = get_user_somehow()
self.scope["session"]["room_name"] = user_to_which_to_subscribe_to.username
# Accept connection
self.__subscriptions[user_to_which_to_subscribe_to.username].add(self.scope["user"].username) # To extract users and their subscriptions
def disconnect(self, message):
# Disconnection logic
self.__subscriptions[ self.scope["session"]["room_name"] ].remove(self.scope["user"].username)
def get_subs_status(self, user, other_user):
return user.username in self.__subscriptions[other_user.username]
def external_feed(self, event): # sending from outside the consumer
user = self.user
oth_user = event['user']
condition = self.get_subs_status(user, oth_user)
if condition is True:
self.send_json({
'newFeed': event['feeds'],
})
# publish to pub/sub mechanism to support multiple processes/nodes
您可以添加代码的 async/await 部分。你明白逻辑了。
这样您就可以为每个用户的直播提供单独的房间。任何用户都可以订阅多个其他用户。此外,您可以添加一个 pub/sub 侦听器来水平缩放它,添加多个 nodes/processes.