How unique is socket.id?

I am creating an application where I need a unique identifier for each connection for the entire duration of the application, and I am wondering if socket.id works for this. For instance. if user 1 disconnects from the application, user 2, which connects later, should not have the same socket.id as user 1, even if user 1 is no longer connected. Socket IDs are unique for the entire time socket.io is listening on the server?

+6
source share
2 answers

Assuming you are using socket.io@0.9.x (this is the real version in NPM), the code that generates a new identifier for each connection / client can be found here .

I think you can safely assume that each socket identifier will be unique.

+4
source

Looking at the socket.io code, it seems like the user id uniquely identifies the socket client. See, for example, Socket.connect code:

 Socket.prototype.onconnect = function(){ debug('socket connected - writing packet'); this.join(this.id); this.packet({ type: parser.CONNECT }); this.nsp.connected[this.id] = this; }; 

On the last line, the identifier is used in a hash that tracks connected sockets. Since you need your identifiers to be unique, each identifier is unique if the server has not been restarted.

+2
source

All Articles