Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage multiple tabs (but same user) in socket.io

Tags:

I'm having some problems with socket.io, and I have no idea how to solve it. I've an app with a login system with socket.io to manage user interactions. Also I've an array to manage all active websocket sessions. This array stores a pair session.id => username.

Problems begin when a user opens a new tab with the same page (or a different page that uses the websocket). This cause a duplicate username entry in the array.

{id1, username} {id2, username} 

When socket sends a message to the user, it sends only to the first session id of the array, so only one tab receives the message.

I've tried to change the socket id of the new connection to match the other, but it doesn't work.

socket.id = sessionId of the other websocket; 

I think another solution is to send the message to all the open sessions, but I think it's not the best way to do that.

Thanks.

like image 657
Miguel Avatar asked Aug 28 '12 19:08

Miguel


People also ask

How many concurrent connections can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

Is Socket.IO multithreaded?

js is not multithreaded, it means that the Socket.IO is also not multithreaded? Exactly. You can scale by running a distributed backend for the messaging, there is a redis store built in.

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 messages per second can Socket.IO handle?

Both server and client node processes use 95-100% of a CPU core each. So pure throughput looks ok. I can emit 100 messages per second to 100 local clients at 55% CPU usage on the server process.


1 Answers

It sounds like you may be referring to the socket id in your question, not the session id. If you have an express session you can use the session id, which will be the same regardless of how many tabs are open as long as they use the same browser and it's not in "private" mode. Take a look at this question for how to enable sessions in socket.io.

socket.io and session?

Update: When you're saving the session id, you'll associate it with the user's nickname and the connected sockets. Then you can iterate through each socket and send the message. Something like the following:

[     {sessionId: '12345', nickname: 'timmay!', socketIds: [1, 2, 3]},     {sessionId: '23456', nickname: 'pete', socketIds: [4, 5, 6]} ] 

When another connection is established you push the new socket id into the socketIds array, and when a user disconnects you remove the id.

Another option might be to only allow one tab per user per session. You can store a single socketId and when a new socket connects for the same user, you can disconnect the original socket and use the new one instead.

like image 102
Timothy Strimple Avatar answered Dec 18 '22 02:12

Timothy Strimple