0

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){
 socket.id = 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

2 Answers 2

3

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('socket.io')(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 socket.io's room/ namespace implementation (http://socket.io/docs/rooms-and-namespaces/) and create one room for each client, or one room for a group of clients. For this, take a look at this answer: Using Socket.io 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!

Sign up to request clarification or add additional context in comments.

3 Comments

My question exactly is for different browsers or computer.what happens when user logs in from multiple computer or may be different browsers .
Different browsers or different computers (when you don't have a room/namespace implementation) will be the same as to have many tabs. The socket will be on lets say someIp:somePort e.g. 98.45.32.21:3000, that doesn't change, so, when one sends "hello" server will respond to everyone with hello (in the above implementation).
If you want the user to log in from two different browsers and see the same messages - but only his messages, then you should correlate that user with the correct room, so that when he connects from a second browser and you know it is user sagar, you will join him in the correct room.
0

I too am fairly new to Sockets (and JS), and I'm trying to make a chat app too but a little differently.

As far as i understand the message will be received only on one browser. A better design would be to let the user connect through multiple browsers/devices. You can maintain a list of all the sockets a user has connected from (if you need it).

If you need a one-to-one chat, maybe you can write some code wherein all sockets the two users connect from are joined into a single room.

socket.room = roomname
socket.join(roomname)

Then you just broadcast the message to that room. That way every sockets both users have connected to will get the message.

socket.to(socket.room).broadcast.emit("message", message);

(I am saving the room info in socket, no idea if that is a good design)

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.