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?
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.
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).
As we saw in the performance section of this article, a Socket.IO server can sustain 10,000 concurrent connections.
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.
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
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/#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With