Can someone please explain how does socket.to(username).emit('eventName',{})
work? Basically I want to know how it identifies the 'username' is logged in or not.

- 45
- 7
-
Socket.io has no concept of a username at all so that part of the question that you edited in 7 hrs after posting does not really make sense. That's something your own code has to add to things, either via a login cookie or a login event over the connection or some other way. – jfriend00 Jul 03 '18 at 06:27
-
i already removed the edit – sudo_shreyas Jul 03 '18 at 06:33
2 Answers
socket.to(room).emit(...)
will emit messages to all the users that joined room
using: socket.join(room)
.
By default, each socket joins a room identified by the socket id, that's why, you can also do: socket.to(socketId)
Without knowing your logic, username
room will be empty if the user isn't logged in, and it will have the logged user if the user is online. Socket.io
doesn't really know if the user is online or not, it only knows that there is an user in that room.
io.on('connection', (socket) => {
console.log('New user connected');
const username = getUsernameSomehow(); // logged user, ip, cookie, or whatever you like
// When the user is online, it will join to
// a room named after his username
socket.join(username);
socket.on('send', (message) => {
console.log(message);
// Now you can send a message by only knowing the username
socket.to(message.target).emit('message', message.text);
});
});
If you don't join the user to username
room, your code will never work.

- 37,983
- 8
- 84
- 98
-
in fact the code is supposed to work only when the user is logged in and username is the logged in user.it is working perfectly for web app. but when i listen for this event from mobile app it is not working – sudo_shreyas Jul 03 '18 at 05:46
-
can you please tell me how cld i get username in case of mobile app i.e what shld the app return? – sudo_shreyas Jul 03 '18 at 06:02
-
You can send the user in a connection query to socket.io. https://stackoverflow.com/questions/13745519/send-custom-data-along-with-handshakedata-in-socket-io – Marcos Casagrande Jul 03 '18 at 13:31
First of all, it's not a username, it's a socket.id value or a room name that works in:
socket.to(roomname).emit(...)
The way it works is very socket that connects to your server is given a socket.id value and then that value is then added to a data structure the socket.io server keeps. Any times a socket disconnects that socket.id is removed from the data structure.
So, when you do this:
socket.to(roomname).emit(...)
socket.io looks up the roomname you pass in its internal data structure. If it's there, then it can get the socket from that data structure and can then send to it.
Because socket.io also automatically creates a room with the name of the socket.id that every socket is given, you can also do:
socket.to(socketID).emit(...)
And, using the same mechanism above, it will look for a room named with the socketID and because there is matching room for every socket that is connected, it will find that room and then send to the socket with that socketID.
socket.io itself does not have any notion of username. You would have to add that level of functionality yourself or get it from a cookie (if there's already a login cookie) when the socket.io connection is first established.

- 683,504
- 96
- 985
- 979
-
in fact the code is supposed to work only when the user is logged in and it is working perfectly for webapp. but when i listen for this event from mobile app it is not working – sudo_shreyas Jul 03 '18 at 05:44
-
@coders - Any what do you expect us to help you with on that? You don't show any code relative to that issue. A login has nothing to do with socket.io (it has no login features itself). That will be your own app code. Nothing we can do to help you without seeing the login code you're using. Add, now 7 hours after I answered your original question, you've somehow changed the question to be about a login. That's not cool on stack overflow. Accept an answer for this original question (undo your edit that changed it), then ask a new question about login. – jfriend00 Jul 03 '18 at 05:48
-
sorry for the confusion.i actually wanted to know how this method emit event for the particular user which is username in this case – sudo_shreyas Jul 03 '18 at 06:15
-
@coders - socket.io does not have ANY notion of a username so your question doesn't really make sense. Each connection has an internal ID, but that's not anything that the user knows and it's different for every new connection. It's that internal ID that can be used with `socket.to(id)`. I think I've answered what info there is about `io.to()`. Not sure what else could be added. – jfriend00 Jul 03 '18 at 06:17
-
is there any way in socket.io that we cld generate an event only for particular user? – sudo_shreyas Jul 03 '18 at 06:21
-
@coders - Yes, that can be done. `io.to(socketIdOfUser).emit(...)`. Or, your own data structure saves each incoming `socket` connection into a data structure, finds the one you want and then does `socket.emit(...)` to the socket you found in your data structure. – jfriend00 Jul 03 '18 at 06:23