Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple sockets sharing a port in node.js (via socket.io)

I am not sure how to use a single port at server side simultaneously for multiple sockets. How can we do it in node.js. I am currently using socket.io and have one socket per port. In case solutions do not exist but is possible then also please give your suggestion to achieve the same. Also what issues can be there if we share a port? What could be other related options considering the situation that clients can be idle but will consume a port on server as we need to maintain a socket connection for each client?

like image 387
singhsumit Avatar asked Mar 19 '12 17:03

singhsumit


1 Answers

Assuming your server is running on port 80, here is what happens underneath:

  1. Server listens port 80.
  2. Client1 connects to server port 80 from its port 12345
  3. Server accepts client1's connection request and assigns port 9876 to commune with client1.
  4. Server continues listening port 80.

So despite what you think, the port 80 is not consumed, it is a listener. Your computer probably has 50000 ports at free, so there is no problem.

FYI: Ports cannot be shared among other processes. Only Node's child processes can be shared, have a look at how it can be: http://nodejs.org/docs/latest/api/cluster.html

like image 85
Mustafa Avatar answered Sep 27 '22 23:09

Mustafa