2

I have socket.io application that must handle a lot of namespaces. I'm wondering what is the limit of the number of namespaces socket.io instance can handle, is there any issue if I'm creating like 10k namespaces. How can I scale to make something like scharding across multiple socket.io instances (each instance handle 1k namespance for example). Actually, I can scale just to support multiple users but all my instances must handle all namespaces.

UPDATE1:

The problem I'm trying to solve is the following, I have a multiplayer card game (like poker) and each room, where 4 players are playing the game, is modeled by socketio namespace. Since I'm expecting thousands of concurrent players, my socketio instance should handle this amount of namespaces. I've already implemented the scalability solution explained in Socket.io website. When I scale out, this node snippet I run in the new instance so it can handle all the namespaces created before.

rooms.forEach(function(room){
    var socketRoom = io.of('/room/' + room.id)
    .on('connection', function(mySocket){
        return handleTableSocket(mySocket, socketRoom, room);
    });
});

So is it a good idea to model each game room with socketio namespace? What if the number of namespaces grow up?

Cheers

elghazal-a
  • 582
  • 1
  • 8
  • 15

1 Answers1

5

A namespace in socket.io is just a Javascript object and there's a master object that maintains an index of all the existing namespace objects. There are no inherent limitations on how many you can have other than the amount of memory consumed by the Javascript objects and their instance data within your ndoe.js process. It seems that a quick test where you create the number of namespaces you need and measure their memory consumption would be a good first step.

You can scale socket.io in node.js by deploying multiple node.js processes. The general concept is discussed here on the socket.io site (Using multiple nodes). That general scheme works by using redis to manage data that wants to be shared across multiple instances. This scheme does not create a preference for a specific user on a specific instance. If you wanted to do something like that, in order to only have some namespaces in each instance, then that likely could be done with custom coding, but you'd have to explain a lot more about the problem you're trying to solve for us to have much of an idea about how to approach that.

Again, if you shared your actual problem you were trying to solve (rather than just your attempted solution), we could probably come up with a solution that was more efficient than 10k actual namespaces by using your own data structures that are more targeted to your specific problem than a generic namespace.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    @hamou92 - Is there any reason you aren't modeling each game as a `room` instead of a namespace. I believe a room is a lighter weight object in socket.io than a namespace and I think a room would still give you the communication features you would want. – jfriend00 May 23 '16 at 15:32
  • I've done some research before making choice. It seems like namespaces give more separation http://stackoverflow.com/questions/10930286/socket-io-rooms-or-namespacing – elghazal-a May 23 '16 at 20:11
  • @hamou92 - Did this answer your question? – jfriend00 May 29 '16 at 17:48
  • i think yeah. Thanks – elghazal-a May 31 '16 at 21:53