Question

I am using node.js socket.io-client and socket.io . I have a javascript program which makes multiple connections to the websocket in a client side . The connections are all fine but the problem I am seeing is that the client.id for all the connections are the same because of which I am not able to select a client individually to send a message . The keys are properly received on the server side .

If I run two instances of the client from two different node.js instances the problem is not there . But this is not a scalable solution for my scenario . Is there a way to distinguish the client threads ?

Client Side

    function setup(socketHost,socketPort,key,mesg,count) {
        var socket = new require('socket.io-client').connect( socketHost, {'port': socketPort});
        socket.on('connect', function() {
             var timeNow = new Date().getTime();
             socket.emit('message' , {key : key , instanceCount : count});
        });
        socket.on('disconnect', function() {
             console.log("Disconnected for port " + socketPort + " Key " + key );
         });
     }

     for (i=0; i<10; i++) {
         var key = "key"+i;
         setup('localhost',12000,""hello",i);
     }

Server Side

  socket.sockets.on('connection', function(client) {
        console.log("Joining " + message.key + " for instance count " + message.instanceCount + " with id as " + client.id );
  });
Was it helpful?

Solution

I think you might need to force a new connection.

var io_client require('socket.io-client');

options = {
  transports: ['websocket'],
  'force new connection': true
};

var client = io_client.connect(HOST, options);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top