Node.js - 处理数百个用户首选项

时间:2022-08-22 14:37:34

I'm learning node.js (my web background is mainly PHP) and I'm loving it so far but I have the following question. In PHP and other similar languages, each request is a single lived execution of the script. All user preferences can be loaded, etc can be loaded and there's no issue there as once the script execution has been completed, all resources will be released.

我正在学习node.js(我的网页背景主要是PHP),到目前为止我很喜欢它,但我有以下问题。在PHP和其他类似语言中,每个请求都是脚本的单个执行执行。可以加载所有用户首选项等,并且没有问题,因为一旦脚本执行完成,所有资源都将被释放。

In node.js, especially in a long running process like a chatroom (I'm using socket.io), you will have hundreds/thousands of users being handled by one process. Assuming for instance I have a chatroom with 200 people, and I want messages to be highlighted if it comes from a participant the user has deemed a "Friend", then I will have to loop through 200 users to see if the user is a friend or not (especially if chats are to be only sent to friends and not publicly).

在node.js中,特别是在像聊天室这样的长时间运行的进程中(我使用的是socket.io),一个进程将处理数百/数千个用户。假设我有一个200人的聊天室,如果消息来自用户认为是“朋友”的参与者,我想要突出显示消息,那么我将需要遍历200个用户以查看用户是否是朋友或不(特别是如果聊天只发送给朋友而不是公开)。

Won't this be really slow, especially over time? Is there something I'm missing out on? In my small tests as the number of users as well as number of messages go up, the responsiveness of the server goes down noticeably.

这会不是很慢,特别是随着时间的推移?有什么我错过了吗?在我的小测试中,随着用户数量和消息数量的增加,服务器的响应能力明显下降。

1 个解决方案

#1


2  

If you are going to develop a complex chatroom, you have to consider design the server side code and maintain the clients information at the server side. For example, you have to map the newly connected client socket to variables at the server side, also if you want to introduce "Friend" feature you have to maintain those information at server side. So your server don't have to look up each client see if they are the correct message receivers.

如果要开发复杂的聊天室,则必须考虑设计服务器端代码并​​在服务器端维护客户端信息。例如,您必须将新连接的客户端套接字映射到服务器端的变量,如果要引入“朋友”功能,则必须在服务器端维护这些信息。因此,您的服务器不必查找每个客户端,看它们是否是正确的消息接收者。

With all those implemented, in the scenario of sending message to the public, at the server side we could first find all the "friend" sockets, then send the message highlighted as "Friend" to those sockets, then send normal text to others. For private message to Friend, it will be much easier as we only consider friends sockets.

在所有这些实现的情况下,在向公众发送消息的场景中,在服务器端,我们可以首先找到所有“朋友”套接字,然后将突出显示为“朋友”的消息发送到这些套接字,然后将普通文本发送给其他人。对于给朋友的私人消息,因为我们只考虑朋友套接字会更容易。

So you still need to reuse some of your design patterns you've used in PHP, socket.io would only maintain the long connections for you, and that is all.

所以你仍然需要重用你在PHP中使用的一些设计模式,socket.io只会为你保持长连接,就是这样。

#1


2  

If you are going to develop a complex chatroom, you have to consider design the server side code and maintain the clients information at the server side. For example, you have to map the newly connected client socket to variables at the server side, also if you want to introduce "Friend" feature you have to maintain those information at server side. So your server don't have to look up each client see if they are the correct message receivers.

如果要开发复杂的聊天室,则必须考虑设计服务器端代码并​​在服务器端维护客户端信息。例如,您必须将新连接的客户端套接字映射到服务器端的变量,如果要引入“朋友”功能,则必须在服务器端维护这些信息。因此,您的服务器不必查找每个客户端,看它们是否是正确的消息接收者。

With all those implemented, in the scenario of sending message to the public, at the server side we could first find all the "friend" sockets, then send the message highlighted as "Friend" to those sockets, then send normal text to others. For private message to Friend, it will be much easier as we only consider friends sockets.

在所有这些实现的情况下,在向公众发送消息的场景中,在服务器端,我们可以首先找到所有“朋友”套接字,然后将突出显示为“朋友”的消息发送到这些套接字,然后将普通文本发送给其他人。对于给朋友的私人消息,因为我们只考虑朋友套接字会更容易。

So you still need to reuse some of your design patterns you've used in PHP, socket.io would only maintain the long connections for you, and that is all.

所以你仍然需要重用你在PHP中使用的一些设计模式,socket.io只会为你保持长连接,就是这样。