最佳性能 - 通过循环或房间发射到插座 [英] Best Performance - emit to sockets via a loop or rooms

查看:12
本文介绍了最佳性能 - 通过循环或房间发射到插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前有一个聊天应用程序,当向适当的用户(可能是 1 个或几个,取决于对话中的用户数量)发送消息时,我们会遍历所有套接字 (Socket.io 2.0.2) 连接到服务器(NodeJS) 以根据成员 ID 值获取用户拥有的套接字列表,因为每个用户都可以从多个设备连接.代码看起来像这样,以确定我们应该发送消息的用户拥有哪些套接字,

We currently have a chat app whereby when emitting messages out to the appropriate users (could be 1 or several depending how many are in the conversation) we loop through all socket (Socket.io 2.0.2) connections to the server (NodeJS) to get a list of sockets that a user has based on a member ID value as each user could be connected from multiple devices. The code looks like this in order to determine which sockets a user has that we should be sending the message,

    var sockets = Object.keys(socketList);
    var results = [];
    for (var key in sockets) {
        if (hasOwnProperty(socketList[sockets[key]].handshake.query, 'token')) {
            if (JSON.parse(socketList[sockets[key]].handshake.query.member).id === memberId) {
                results.push(socketList[sockets[key]]);
            }
        }
    }

不得不循环访问套接字连接似乎效率低下,我想知道有没有更好的方法.我的想法是为每个用户创建一个房间,大多数用户只有一个连接,但有些用户将通过多个设备连接,因此他们的房间可以有多个插座.然后我会广播到适当的房间而不是总是循环遍历所有套接字.鉴于 95% 的用户将只有一个套接字连接,我不确定这种方法是否更有效,并希望对此提供一些意见.谢谢.

Having to loop through the socket connections seems inefficient and I wonder is there a better way. My thought is to create a room for each user, most users will have only the one connection but some will be connected via multiple devices so they could have multiple sockets in their room. Then I would just broadcast to the appropriate rooms rather than always looping through all sockets. Given that 95% of users will only have the one socket connection I'm not sure if this approach is any more efficient or not and would appreciate some input on this. Thanks.

推荐答案

首先,socket.io 已经为每个用户创建了一个房间.该房间的名称为 socket.id.房间是非常轻的物体.它们基本上只包含一个对象,其中包含房间中所有插座的 id.所以,使用房间应该毫不犹豫.如果它们符合你正在做的事情的模型,那就使用它们.

First off, socket.io already creates a room for every single user. That room has the name of the socket.id. Rooms are very lightweight objects. They basically just consist of an object with all the ids of the sockets that are in the room. So, there should be no hesitancy to use rooms at all. If they fit the model of what you're doing, then use them.

至于循环自己与发射到房间,真的没有区别 - 使用使您的代码更简单的那个.当您发送到房间时,它所做的只是遍历房间中的套接字并单独发送给每个人.

As for looping yourself vs. emitting to a room, there's really no difference - use whichever makes your code simpler. When you emit to a room, all it does is loop through the sockets in the room and send to each one individually.

不得不循环访问套接字连接似乎效率低下,我想知道有没有更好的方法.

Having to loop through the socket connections seems inefficient and I wonder is there a better way.

房间的主要优点是它们是预先构建的套接字关联,因此您不必动态确定要发送到哪些套接字 - 在正确的房间中已经有一个您可以发送的套接字列表到.因此,仅发送到房间中的所有套接字可能比执行您的代码正在执行的操作更有效,因为您的代码正在动态地尝试确定要发送到哪些套接字,而不是发送到已经创建的套接字列表.这会有所作为吗?这取决于整个套接字列表的长度以及确定要发送到哪些套接字的计算成本.我的猜测是,无论哪种方式,它可能都不会产生太大影响.

The main advantage of rooms is that they are pre-built associations of sockets so you don't have to dynamically figure out which sockets you want to send to - there's already a list of sockets in the right room that you can send to. So, it would likely be a small bit more efficient to just send to all sockets in a room than to do what your code is doing because you code is dynamically trying to figure out which sockets to send to, rather than sending to an already made list. Would this make a difference? That depends upon how long the whole list of sockets is and how expensive the computation is to figure out which ones you want to send to. My guess is that it probably wouldn't make much difference either way.

向房间发送消息在实际发送部分效率并不高.每个套接字都必须单独发送消息,因此有人(您的代码或 socket.io 房间代码)将通过任一方式循环访问套接字列表.底层操作系统不包含将单个消息发送到多个套接字的功能.每个套接字都必须单独发送.

Sending a message to a room is not much more efficient on the actual sending part. Each socket has to be sent the message individually so somebody (your code or the socket.io rooms code) is going to be looping through a list of sockets either way. The underlying OS does not contain a function to send a single message to multiple sockets. Each socket has to be sent to individually.

然后我只会向适当的房间广播,而不是总是遍历所有套接字.

Then I would just broadcast to the appropriate rooms rather than always looping through all sockets.

发送到房间对您来说是一种编程便利,但 socket.io 无论如何都会在被子下循环.

Sending to a room is a programming convenience for you, but socket.io will just be looping under the covers anyway.

这篇关于最佳性能 - 通过循环或房间发射到插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆