Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io listening on multiple ports?

I've did some searching and couldn't find any updated answers about this topic, seeing as the answers found applied only to older versions of Socket.io.

Here is my code:

let http = require('http').Server(app);
let io = require('socket.io')(http);

http.listen(7000, '::1', function () {
    // Now listening
});

io.on('connect', function (socket) {

}

Am I able to add in additional code? such as:

http.listen(8000, '::1', function () {
    // Now listening
});

io2.on('connect', function (socket) {

}

I want this single node.js instance to be listening on two different ports at once. This is so that I can handle two different types of clients appropriately, by having them connect to their appropriate socket, and then handling the two groups differently.

How would one do this?

like image 367
pizzae Avatar asked May 31 '17 06:05

pizzae


People also ask

Can one Socket listen multiple ports?

A single listening port can accept more than one connection simultaneously. There is a '64K' limit that is often cited, but that is per client per server port, and needs clarifying.

Can a process listen on multiple ports?

Yes, a single process can listen on multiple ports, just like 80 + 443 are done. Apache has different ways to the handle the requests, the so called MPM (MultiProcessingModules).

How many connections Socket.IO can handle?

As we saw in the performance section of this article, a Socket.IO server can sustain 10,000 concurrent connections.

Can a Web Server listen on multiple ports?

Overview. In web applications, the server often needs to listen on more than a single TCP port. This can give improved performance and better flexibility for the application. In this tutorial, we'll learn how to configure the Apache web server to listen on two different ports.


2 Answers

I'm not sure what app is, assuming you are using the popular express server module. Yes, it is possible to do.

What you could do is by getting the main NodeJS process to spawn 2 express servers. Each listening on a specific port. Then have 2 sockets each binding to a server.

Note: There is a major downside in this kind of design as you never know how many more customers you might possibly have in the future. You can possibly continue to duplicate the code to spawn n number of server and sockets but you'll eventually run out of ports to bind.

That is assuming your business grows extensively. 1 option is to look into namespaces within a socket. If you're happy to discuss, let me know hwne you have open a new topic on it.

Example:

Server:

global.appRoot = require('app-root-path');

var express = require('express');
var session = require('express-session');
var app = express();

var server1 = app.listen(7788, "0.0.0.0", function() {
    console.log('Server listening on port ' + server1.address().port);
});

var server2 = app.listen(5566, "0.0.0.0", function() {
    console.log('Server listening on port ' + server2.address().port);
});

var socket = require('socket.io').listen(server1);
var socket2 = require('socket.io').listen(server2);

socket.on('connect', function (socket) {
    console.log("hi socket 1");
});

socket2.on('connect', function (socket) {
    console.log("hi socket 2");
});

Client:

<!doctype html>
<html>
    <head>
        <script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.1/socket.io.js'></script>
        <script>
            var socketClient1 = io('ws://localhost:5566');
            var socketClient2 = io('ws://localhost:7788');
        </script>
    </head>
    <body>
        <ul id='messages'></ul>
    </body>
</html>

Output:

Server listening on port 7788

Server listening on port 5566

hi socket 1

hi socket 2

like image 142
Samuel Toh Avatar answered Oct 09 '22 01:10

Samuel Toh


I'm pretty sure one HTTP instance can not listen to multiple ports. You should try something like:

let http = require('http').Server(app);
let io = require('socket.io')(http);

io.on('connect', function (socket) {

});

http.listen(7000, '::1', function () {
    // Now listening
});

let http2 = require('http').Server(app);
let io2 = require('socket.io')(http2);

http2.listen(7001, '::1', function () {
    // Now listening
});

io2.on('connect', function(socket){

});

By the way, I think you can handle multiple different types of clients with a single socket by making them join different namespaces. From socket.io documentation:

https://socket.io/docs/#restricting-yourself-to-a-namespace

https://socket.io/docs/rooms-and-namespaces/#

like image 25
ardilgulez Avatar answered Oct 09 '22 01:10

ardilgulez