How can i set my own id to a socket on nodeJS server

5,471 views
Skip to first unread message

veeru

unread,
May 17, 2012, 7:57:35 AM5/17/12
to sock...@googlegroups.com
Hi all,
I want to set a custom name or id  for a connected socket, and i want to use this socket in future when some other socket wants to send message to this particular socket.

Kishor

unread,
May 23, 2012, 7:08:55 AM5/23/12
to sock...@googlegroups.com
This is how I have assigned my own id to socket.
 1. Sent the Id in handshake URL
 2. Assigned a new property say "Id" to the socket when it gets created in "your node installation folder/node_modules/socket.io/lib/manager.js"
     and it can be used in server.js file.
Regards ,
Kishor

Rhald Khein

unread,
May 24, 2012, 4:37:34 AM5/24/12
to sock...@googlegroups.com
Socket already comes with id. You can get the particular socket by id through "io.sockets.sockets[id]" and you can also get the id from "socket.id".

Veerabhadraiah A S

unread,
May 23, 2012, 7:14:51 AM5/23/12
to sock...@googlegroups.com
Thanks a lot kishor,
I want to know a bit more..
Can you give a code snippet to show how to send the id with handshake URL,
and how to identify the socket with it id at nodeJS server code?
Waiting for your response.:)
--
Thanks and Regards,
Veerabhadraiah A S,
Software Engineer,
Above Solutions India Pvt Ltd,
Indira Nagar,
Bangalore.



Kishor

unread,
May 30, 2012, 10:08:52 AM5/30/12
to sock...@googlegroups.com
Hi Veerabhadraiah A S ,
           Suppose this is the handshake URL , http://SERVER/socket.io/1/xhr-polling/84f7f1ea36e4cfc7e3737a583df07c57 , now add your user ID to this url like ,http://SERVER/socket.io/1/xhr-polling/84f7f1ea36e4cfc7e3737a583df07c57?userID=100. In Manager.js , split this url and extract userID and assign it to newly created socket , like socket.ID = SPLITTED userID .
          
           Now , in server.js , you can refer the ID property of the connected socket same as we refer other socket properties.
Regards ,
Kishor

veeru

unread,
Jun 12, 2012, 9:44:08 AM6/12/12
to sock...@googlegroups.com
Hi Kishore,
Am doing like

    socket.myappuserid=userId;

Now this has been successful, now i want to directly send an event to a particular socket with a myappuserid, ie i want to identify the socket uniquely based on the myappuserid. is it possible? if yes then how?

Kishor

unread,
Jun 12, 2012, 10:39:33 AM6/12/12
to sock...@googlegroups.com
Hi Veerabhadraiah A S ,
                                     This is straight  forward . You can traverse the connected clients array and see which one has myappuserid  you are looking for  and then call , socket.emit('YOUR_EVENT_NAME') .

Regards ,
Kishor

Veerabhadraiah A S

unread,
Jun 13, 2012, 1:03:42 AM6/13/12
to sock...@googlegroups.com
HI Kishore, 
Thanks for your reply. As you said we can traverse through all the clients connected, but in real time application there would be thousands of clients connected so every time we cannot go and run the loop right? Is there any better way to do it?

Kishor

unread,
Jun 13, 2012, 3:10:44 AM6/13/12
to sock...@googlegroups.com
Hi Veerabhadraiah A S ,
In that case , while adding client to the connected clients list add it with myappuserid as index and fetch it out . For example ,
'clientsConnected' is clients array , now when a new client connects add it like clientsConnected [myappuserid] = client_object . While emiting event , call clientsConnected [myappuserid].emit('YOUR_EVENT_NAME')  .

Regards ,
Kishor
Reply all
Reply to author
Forward
0 new messages