Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using node.js to listen on 2 different ports

I'm currently using Sockets.io to communicate with clients, sending JSON and whatnot, from a port.

That's all working good, but what i'd like to do is listen simultaneously on another port to create a type of administration page for testing purposes.

For example, the page would have a button to send a certain type of JSON for all the clients connected on the other port.

If this isn't ideal, any help on other simple solutions would be great.

like image 411
Josh Jones Avatar asked Feb 26 '13 20:02

Josh Jones


People also ask

Can NodeJS listen to two ports?

You can't listen on multiple ports for a single http.

How do I run node js on different ports?

If you want to run NodeJS on different port, change 80 to your required port number (e.g 8080). Also the function defined in createServer() will be executed whenever someone sends a request to your NodeJS server. In this case, it simply returns “Hello World!” string.

How many TCP connections can NodeJS handle?

No matter what size instance we use Node always appears to max out at 1000 concurrent connections (this is NOT 1000 per second, but 1000 it can handle at 1 time).

How to listen on multiple ports on a single server?

Listening on multiple ports on a single server 1 Start server; 2 Listen for incoming connections on several ports; 3 Identify the port being connected to; More ...

How to listen to port 1 and port 2 in Linux?

1. Start server; 2. Listen for incoming connections on several ports; 3. Identify the port being connected to; a. If port 1, start a thread listening to client and outputting message type x b. If port 2, start a thread listening to client and outputting message type y thanks in advance.

Which method creates a listener on the specified port or path?

The server.listen () method creates a listener on the specified port or path. Optional. Specifies the port we want to listen to Optional. Specifies the IP address we want to listen to

What is the use of Node JS?

Node.js allows developers to use JavaScript everywhere instead of just in browsers - the two big mainstream uses as of writing are web/app servers (Node.js is very well-suited for messaging-like applications like chat servers, for example) and Internet o Is Go killing Node.js? Maybe but I think Go has already taken its share from other languages.


1 Answers

Just create another instance of http and put it to listen to the port you are interested. Let me show you an example:

var http = require('http');

http.createServer(onRequest_a).listen(9011);
http.createServer(onRequest_b).listen(9012);

function onRequest_a (req, res) {
  res.write('Response from 9011\n');
  res.end();
}

function onRequest_b (req, res) {
  res.write('Response from 9012\n');
  res.end();
}

Then, you can test it (with your browser, or curl):

$ curl http://localhost:9011
Response from 9011

$ curl http://localhost:9012
Response from 9012
like image 162
Herman Junge Avatar answered Oct 17 '22 17:10

Herman Junge