Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to uniquely identify a socket with Node.js

TLDR; How to identify sockets in event based programming model.

I am just starting up with node.js , in the past i have done most of my coding part in C++ and PHP sockets() so node.js is something extremely new to me.

In c++ to identify a socket we could have done something like writing a main socket say server to listen for new connections and changes, and then handling those connections accordingly.

like image 388
ShrekOverflow Avatar asked Jul 24 '11 07:07

ShrekOverflow


People also ask

How do you identify a unique socket?

If you are looking for actual sockets and not socket.io, they do exist. But as stated, Node. js and Javascript use an event-based programming model, so you create a (TCP) socket, listen on an IP:port (similar to bind), then accept connection events which pass a Javascript object representing the connection.

Are socket IDS unique?

Assuming that you're using [email protected] (which is the present version in NPM), the code that generates a new id for each connection/client can be found here. I think that you can safely assume that each socket id is going to be unique.

How do you check socket is connected or not js?

You can check the socket. connected property: var socket = io. connect(); console.

What is socket on in node js?

Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of: a Node. js server: Source | API. a Javascript client library for the browser (which can be also run from Node.


2 Answers

If you are looking for actual sockets and not socket.io, they do exist.

But as stated, Node.js and Javascript use an event-based programming model, so you create a (TCP) socket, listen on an IP:port (similar to bind), then accept connection events which pass a Javascript object representing the connection.

From this you can get the FD or another identifier, but this object is also a long-lived object that you can store an identifier on if you wish (this is what socket.io does).

var server = net.createServer();

server.on('connection', function(conn) {
  conn.id = Math.floor(Math.random() * 1000);
  conn.on('data', function(data) {
    conn.write('ID: '+conn.id);
  });
});
server.listen(3000);
like image 114
Timothy Meade Avatar answered Oct 02 '22 15:10

Timothy Meade


Timothy's approach is good, the only thing to mention - Math.random() may cause id's duplication. So the chance it will generate the same random number is really tiny, but it could happen. So I'd recommend you to use dylang's module - shortid:

var shortid = require('shortid');
var server = net.createServer();

server.on('connection', function(conn) {
  conn.id = shortid.generate();
  conn.on('data', function(data) {
    conn.write('ID: '+conn.id);
  });
});
server.listen(3000);

So in that case you can be sure that no id duplications will occur.

like image 24
ecdeveloper Avatar answered Oct 02 '22 15:10

ecdeveloper