32

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.

Josh Jones
  • 498
  • 1
  • 4
  • 8

1 Answers1

67

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
Herman Junge
  • 2,759
  • 24
  • 20
  • 5
    Have a doubt, is this going to create two separate processes, or just one? – juancancela Jun 10 '14 at 21:29
  • 4
    @juancancela It will create a single process, as known node is single process, until you fork a child process explicitly everything will be from a single process. – ShrekOverflow Feb 09 '16 at 11:32
  • Cool, Can we combine multiple sockets listening and Clustering? – suresh Mar 30 '16 at 11:07
  • @suresh you would use a built-in cluster module and just listen on a single socket with all your processes. I don't think there are performance benefits in multiple sockets vs a single one. – Capaj Dec 21 '17 at 21:48