Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 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.

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?

Thanks for your help.

like image 391
simbro Avatar asked Apr 08 '16 19:04

simbro


People also ask

How many rooms can Socket.IO handle?

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.

How many requests can Socket.IO handle?

Server was able to handle 100000 events distributed in 1000 rooms with 5 requests per second querying db.

Can Socket join multiple rooms?

Joining and leaving​ In that case, a union is performed: every socket that is at least in one of the rooms will get the event once (even if the socket is in two or more rooms). In that case, every socket in the room excluding the sender will get the event. To leave a channel you call leave in the same fashion as join .

Is Socket.IO scalable?

Socket.IO servers can be replicated and scaled horizontally, to provide fault-tolerance (ie. if one server fails, your service would still work) and serve more users (ie. by adding more server capacity).


1 Answers

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.

like image 145
jfriend00 Avatar answered Sep 19 '22 19:09

jfriend00