1

I have an application that uses several namespaces to differentiate between different kind of clients, so since the beginning I separate them in this manner (I'm using cluster and spawn 4 processes)

//server code
io.of("/TYPE_ONE").on("connection", function(socket){
    console.log("Client connected to TYPE_ONE with id:\t"+socket.id+"\t"+process.env.NODE_WORKER_ID);
});

io.of("/TYPE_TWO").on("connection", function(socket){
    console.log("Client connected to TYPE_TWO with id:\t"+socket.id+"\t"+process.env.NODE_WORKER_ID);
});

//client code

//for type one
socket = io.connect("http://mydomain.com/TYPE_ONE", socketOptions);

//different files always, only one type sent to each client

//for type two
socket = io.connect("http://mydomain.com/TYPE_TWO", socketOptions);

All of a sudden, after looking at the console, when a single client connects and I get the following output:

Client connected to TYPE_ONE with id:    1234      3
 .
 .
 .
Client connected to TYPE_TWO with it:    1234      3 
(same id and workerId as previous connection)

I'm certain that there is only one connection being made to the server, t

I'm wondering what could be causing this? Because Ive looked through my code, and simplified the methods to the stubs I just showed, and can't seem to find the issue.

Thanks for your help.

scanales
  • 602
  • 1
  • 7
  • 18

2 Answers2

0

There are no errors in your code: you have only one connection, therefore socket connects to namespaces with one socket.id. I don't know how to make many connections, maybe you should try two ports of connection, or connect/listen server multiple times.

To solve your problem i would use one connection, but think about how to sturcure your site. For example, use rooms and store in session NEW user id's to figure out in what room or namespace to put user.

"Multiple namespaces and multiple rooms can share the same (WebSocket) connection" socket.io rooms or namespacing?

If you want to differentiate clients, you can put theme in another rooms in one namespace and having their socket.id check in what room they are (or join rooms with socket.id name (.joind(socket.id)). https://github.com/LearnBoost/socket.io/wiki/Rooms

Community
  • 1
  • 1
sirjay
  • 1,767
  • 3
  • 32
  • 52
  • But a socket using `socket = io.connect("http://mydomain.com/TYPE_ONE", socketOptions);` shouldn't connect to `TYPE_TWO` namespace. That's what im trying to avoid. I have `TYPE_THREE` as well, and it doesn't join that namespace, just `TYPE_ONE` and `TYPE_TWO` – scanales Dec 07 '12 at 17:20
0

It turns out, at some point in my code, I had a io.of("/TYPE_ONE").socket(socket.id).emit(message); and that socket belonged to the namespace TYPE_TWO, so it seems whenever you send a message to a socket from a namespace to which he isn't connected, this will automatically connect it to that said namespace. Weird though.

scanales
  • 602
  • 1
  • 7
  • 18