sagar chapagain sagar chapagain - 1 month ago 7
Node.js Question

socket connection in node js

I have just started working on node js.I have been trying to make chat application using node js. In which a single user can logged in through multiple devices or browsers. If I am not wrong and as I understand each of the browser communicates with different port address since socket connection is defined by IP address and port address hence when the same user logs in from two different browsers server creates two different socket Id.Now I verify if the user is already connected by checking the parameter send to socket info.Then if the user is already connected or the socket is already set for the user then I create connection to change the socket id to previous socket id as .

io.on('connection', function(socket){ = k;

where k is the socket id of previously connected same user

Now when any other client emits message to the current user then Is the socket id is replaced and only one browser gets message or both of them gets message. Is the connection of server is set for both browser or a single browser. Please help me for this. I am still not clear about how socket connection establishes between client and server.
Please improve if I am doing any supposition wrongly Or how do I solve the following scenerio.
Thanks in advance


If I understand your problem correctly, let me try explain in my way. Lets say you have the following code for the server:

    var io = require('')(somePort); //same port for the client to connect, e.g. 3000
    io.on('connection', function(socket){
     // in here you should define any actions on your primary socket
     // for example:
       socket.on("hello", function(dataUserSendsWithTopicHello){// overly long variable name to show my point! :)
         // do something with hello data

     // this will go to anyone who is connected and has a handler like above for "hello"
      socket.emit("hello", "hello to all who listen to hello topic"); 

A corresponding client would be:

  var socket = io.connect('ip:port'); // e.g. localhost:3000 - same ip and port as above

  socket.on('hello', function(serverResponseToHello){
    // do something when server sends back something on hello 

 // send a hello to the server
  socket.emit("hello", "Sending hello to server");

Now if you want to reply to one user only, or a group of people to see some message all together, like in a chat, you could leverage's room/ namespace implementation ( and create one room for each client, or one room for a group of clients. For this, take a look at this answer: Using with multiple clients connecting to same server

To sum it up, there is one ip and one port. Everyone listens to this ip and port and if you open three tabs and you have some implementation like the above, all tabs will be listening to "hello". Using rooms you can group your users and isolate communication so that only the users that the communication is done between the correct users and not all.

Good luck!