1

I am trying to implement client to client messaging in my app, using socket.io, and node.js and Android.

I searched a little, and found a lot of tutorials, explaining how to deal with targetting specific client when sending messages through socket.io socket.

Send message to specific client with socket.io and node.js

The solution is almost always the same : Creating a hashmap object, linking user info such as its username, email address (or anything unique allowing to identify it), with its socketid. Then calling io.clients[sessionID].send()

Now I have two questions :

  • This would work if only one instance of the app is running, but imagine if my app is divided in multiple instances (for large app). What if a client A, connected to instance X, wants to send message to user B, connected to instance Z. If, as seen in the example, socketids are stored directly in a simple object existing in the script, some sockets wont know about others users existing in an other instance.

  • If I am totally wrong (and I might), is this a good practice to store all user's socketids in a single variable ? If yes, would it still be okay with a 50000+ users enviromnment ? If no, should I find another solution like storing user's socketids in database ?

Community
  • 1
  • 1
Scaraux
  • 2,830
  • 1
  • 27
  • 53

1 Answers1

1

You could use a redis instance, shared between all your app instances. And you get 2 birds with one stone.

The redis would store all your socket ids in a centralized place.

overburn
  • 1,106
  • 8
  • 24
  • Nice, and would you suggest me to keep the object to store socketids ? – Scaraux May 03 '16 at 08:57
  • In the redis database. I've modified my answer a bit to clearly reflect this. – overburn May 03 '16 at 08:59
  • Well no , but that is also part of the solution. In any case, if it's distributed, you'll only work directly with a limited number of socketids per instance (but you'll have access to all of them through the redis instance, for referencing puroposes). So there shouldn't be any problem in storing all of one instances' ids in a single variable. – overburn May 03 '16 at 09:06