Sharing One Port Among Multiple Node.Js Http Processes

Sharing one port among multiple node.js HTTP processes

Personally, I'd just have them all listen on dedicated ports or preferably sockets and then stick everything behind either a dedicated router script or nginx. It's the simplest approach IMO.

Node cluster have multiple processes listen to the same port

I wondered this myself awhile ago and went digging.

The child processes aren't listening to the same port. Incoming socket connections to the master process are being delegated to the child processes.

What's actually going on is deceiving here because you certainly see the server.listen() in each child process. But, inside of .listen() is some magic that knows about the fact that this process is meant to be a clustered process so instead of actually listening in the traditional sense (which would indeed cause the error you cite), they are listening for delegated sockets from their parent.

If I were designing this, I probably wouldn't have hidden this functionality inside of .listen() and caused this confusion. I would have provided a different method to be used in this circumstance that carried this special behavior.

If you want to see more about it here are some resources:

Source code for server.listen() where you can see that it calls listenInCluster() in some circumstances.

In the Net doc for server.listen(), there are references to the special handling when clustered.

The listenInCluster() source code itself where the deed is carried out here:

  // Get the master's server handle, and listen on it
cluster._getServer(server, serverQuery, listenOnMasterHandle);

Is it a good practice to start http server on one port for Cluster forks for 1-3k requests per second?

When you exceed the scale of one single node.js process, it is a good practice to scale on one server using local clustering (all on the same port) if your node.js CPU processing is indeed the bottleneck (and not some shared resource like your database server that should itself be clustered or scaled). That is the recommended scheme from the cluster module that is built-into node.js.

When you exceed that scale, it is a good practice to cluster with multiple servers using some sort of load balancer/proxy to spread the load. When you exceed that scale, you may scale geographically so that requests from different locales go to different data centers that are closer to them on the network (such as US, Europe, Africa, Asia, etc...).

There may be some scenarios (it all depends upon where the bottlenecks are) where using a high performance load balancer to spread the load to separate server processes (on the same computer) on separate ports is better than having all of the processes share one port. This would ultimately only be settled with testing.

Multiple socket.io processes share the same port?

For this I use node-http-proxy, and route the traffic to internal ports based off of the URL being requested. Below is a very stripped down example of what I am using to route requests.

var httpProxy = require('http-proxy');

var httpOptions = {
router: {
'domain1.com/foo': 'localhost:3001',
'domain1.com/bar': 'localhost:3002',
'domain2.com/baz': 'localhost:3003',
}
};

var httpServer = httpProxy.createServer(httpOptions);
httpServer.listen(80);

More details on my particular setup can be found on this question: How to use vhosts alongside node-http-proxy?

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

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

Running multiple httpServer in one Node Process or each server in own node process

Depends on the rest of the code. If both the servers have shared states it's better to start them in same process.

If there is no shared state it's better to have them in separate processes so that one's execution flow does not affect the number of requests server by the other one. Specially true if one is IO bound and the other one is cpu intensive.

Also if you are starting them in the same process why not do both the things on the same port?



Related Topics



Leave a reply



Submit