What's the maximum number of rooms socket.io can handle?

前端 未结 1 2016
暗喜
暗喜 2021-02-04 02:21

I am building an app using 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 me

相关标签:
1条回答
  • 2021-02-04 03:05

    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.

    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?

    Yes, that is easily feasible.

    Will socket.io perform well like this, or is there a better way to approach this problem?

    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.

    0 讨论(0)
提交回复
热议问题