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

I'm not sure how to use one server-side port at the same time for multiple sockets. How can we do this in node.js. I am currently using socket.io and have one socket per port. In the event that solutions do not exist, but are possible, please also give your suggestion to achieve the same. What problems can arise when sharing a port? What other related options can be, given the situation when clients can remain inactive but consume a port on the server, since we need to maintain a socket connection for each client?

+6
source share
1 answer

Assuming your server is running on port 80, here's what's going on under it:

  • The server is listening on port 80.
  • Client1 connects to server port 80 from its port 12345
  • The server accepts the client1 connection request and assigns port 9876 to communicate with client1.
  • The server continues to listen on port 80.

So, despite the fact that you think port 80 is not consumed, it is a listener. Your computer probably has 50,000 ports for free, so no problem.

FYI: Ports cannot be shared with other processes. Only Node child processes can be separated, see how it can be: http://nodejs.org/docs/latest/api/cluster.html

+5
source

Source: https://habr.com/ru/post/911130/


All Articles