I am building an app using socket.io
我正在使用socket.io构建一个应用程序
I'm using socket.io's rooms feature, there are 5 "topics" a user can subscribe to. Each message broadcast in that topic has a message type, of which there are 100. A user will only receive the messages of the types they are allowed to receive, which could be between 30 and 70.
我正在使用socket.io的房间功能,用户可以订阅5个“主题”。在该主题中广播的每个消息具有消息类型,其中有100个。用户将仅接收允许他们接收的类型的消息,其可以在30到70之间。
My question: is it feasible to create a room for every topic + message type combination, which will be 5 x 100 rooms? Will socket.io perform well like this, or is there a better way to approach this problem? Would emitting individual messages to each individual socket, instead of using rooms, be better?
我的问题:为每个主题+消息类型组合创建一个房间是否可行,这将是5 x 100个房间? socket.io会像这样表现得好,还是有更好的方法来解决这个问题?将单个消息发送到每个单独的套接字而不是使用房间会更好吗?
Thanks for your help.
谢谢你的帮助。
1 个解决方案
#1
19
socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources. It's just a list of sockets that wishes to be associated with that room. Emitting to a room is nothing more than iterating through the array of sockets in the room and sending to each one.
socket.io rooms是一个轻量级的数据结构。它们只是与该房间相关联的一系列连接。您可以拥有任意数量(在正常的内存使用限制内)。没有重量级的东西会使房间的资源变得昂贵。它只是一个希望与该房间相关联的套接字列表。发送到一个房间只不过是通过房间里的插座阵列进行迭代并发送到每个房间。
A room costs only a little bit of memory to keep the array of sockets that are in each room. Other than that, there is no additional cost.
一个房间只需要一点点内存来保存每个房间的插座阵列。除此之外,没有额外费用。
Further, if your alternative is to just maintain an array of sockets for each topic anyway, then your alternative is probably not saving you much or anything.
此外,如果您的替代方案是仅为每个主题维护一组套接字,那么您的替代方案可能不会为您节省太多或任何东西。
My question: is it feasible to create a room for every topic + message type combination, which will be 5 x 100 rooms?
我的问题:为每个主题+消息类型组合创建一个房间是否可行,这将是5 x 100个房间?
Yes, that is easily feasible.
是的,这很容易实现。
Will socket.io perform well like this, or is there a better way to approach this problem?
socket.io会像这样表现得好,还是有更好的方法来解决这个问题?
There's no issue with having this many rooms. Whether it performs well or not depends entirely upon what you're doing with that many rooms. If you're reguarly sending lots of messages to lots of rooms that each have lots of sockets in them, then you'll have to benchmark if that has a performance issue or not.
拥有这么多房间没有问题。它是否表现良好完全取决于你在那么多房间里做的事情。如果你经常向很多房间发送大量的消息,每个房间都有很多套接字,那么如果性能有问题,你就必须进行基准测试。
Would emitting individual messages to each individual socket, instead of using rooms, be better?
将单个消息发送到每个单独的套接字而不是使用房间会更好吗?
There won't be an appreciable difference. A room is just a convenience tool. Emitting to a room, just has to iterate through each socket in the room and sending to it anyway - the same as you proposed doing yourself. May as well use the built-in rooms capability rather than reimplement yourself.
不会有明显的差异。房间只是一个便利工具。发送到一个房间,只需要遍历房间中的每个插座并发送到它 - 就像你建议自己做的一样。也可以使用内置的房间功能,而不是重新实现自己。
#1
19
socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources. It's just a list of sockets that wishes to be associated with that room. Emitting to a room is nothing more than iterating through the array of sockets in the room and sending to each one.
socket.io rooms是一个轻量级的数据结构。它们只是与该房间相关联的一系列连接。您可以拥有任意数量(在正常的内存使用限制内)。没有重量级的东西会使房间的资源变得昂贵。它只是一个希望与该房间相关联的套接字列表。发送到一个房间只不过是通过房间里的插座阵列进行迭代并发送到每个房间。
A room costs only a little bit of memory to keep the array of sockets that are in each room. Other than that, there is no additional cost.
一个房间只需要一点点内存来保存每个房间的插座阵列。除此之外,没有额外费用。
Further, if your alternative is to just maintain an array of sockets for each topic anyway, then your alternative is probably not saving you much or anything.
此外,如果您的替代方案是仅为每个主题维护一组套接字,那么您的替代方案可能不会为您节省太多或任何东西。
My question: is it feasible to create a room for every topic + message type combination, which will be 5 x 100 rooms?
我的问题:为每个主题+消息类型组合创建一个房间是否可行,这将是5 x 100个房间?
Yes, that is easily feasible.
是的,这很容易实现。
Will socket.io perform well like this, or is there a better way to approach this problem?
socket.io会像这样表现得好,还是有更好的方法来解决这个问题?
There's no issue with having this many rooms. Whether it performs well or not depends entirely upon what you're doing with that many rooms. If you're reguarly sending lots of messages to lots of rooms that each have lots of sockets in them, then you'll have to benchmark if that has a performance issue or not.
拥有这么多房间没有问题。它是否表现良好完全取决于你在那么多房间里做的事情。如果你经常向很多房间发送大量的消息,每个房间都有很多套接字,那么如果性能有问题,你就必须进行基准测试。
Would emitting individual messages to each individual socket, instead of using rooms, be better?
将单个消息发送到每个单独的套接字而不是使用房间会更好吗?
There won't be an appreciable difference. A room is just a convenience tool. Emitting to a room, just has to iterate through each socket in the room and sending to it anyway - the same as you proposed doing yourself. May as well use the built-in rooms capability rather than reimplement yourself.
不会有明显的差异。房间只是一个便利工具。发送到一个房间,只需要遍历房间中的每个插座并发送到它 - 就像你建议自己做的一样。也可以使用内置的房间功能,而不是重新实现自己。