Node.js app communicating with multiple Node apps via WebSocket Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Are there any Node WebSocket Server javascript client libraries?What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?node.js: proxy websockets to other portWhich websocket library to use with Node.js?Using node http-proxy to proxy websocket connectionsWhich WebSocket library to use in Android app?Nodejs Socket.io working on desktop safari & chrome, but not iPhoneHow can I communicate between Node/Express routes and Socket.ioHow to test maximum connection Node JS websocketSocket.io produces random sockets for one session?
Deconstruction is ambiguous
Why do early math courses focus on the cross sections of a cone and not on other 3D objects?
1-probability to calculate two events in a row
Does the Mueller report show a conspiracy between Russia and the Trump Campaign?
Tannaka duality for semisimple groups
Why does 14 CFR have skipped subparts in my ASA 2019 FAR/AIM book?
An adverb for when you're not exaggerating
Why can't I install Tomboy in Ubuntu Mate 19.04?
How to write capital alpha?
Google .dev domain strangely redirects to https
How long can equipment go unused before powering up runs the risk of damage?
Semigroups with no morphisms between them
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
Dyck paths with extra diagonals from valleys (Laser construction)
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
Is multiple magic items in one inherently imbalanced?
How can I set the aperture on my DSLR when it's attached to a telescope instead of a lens?
How to identify unknown coordinate type and convert to lat/lon?
What order were files/directories output in dir?
How many morphisms from 1 to 1+1 can there be?
preposition before coffee
How does light 'choose' between wave and particle behaviour?
Significance of Cersei's obsession with elephants?
What makes a man succeed?
Node.js app communicating with multiple Node apps via WebSocket
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Are there any Node WebSocket Server javascript client libraries?What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?node.js: proxy websockets to other portWhich websocket library to use with Node.js?Using node http-proxy to proxy websocket connectionsWhich WebSocket library to use in Android app?Nodejs Socket.io working on desktop safari & chrome, but not iPhoneHow can I communicate between Node/Express routes and Socket.ioHow to test maximum connection Node JS websocketSocket.io produces random sockets for one session?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have 5 Node.js apps. Let us assume that the 5 apps that I have are called:
App A,
App B,
App C,
App D,
App E,
I want App A to connect to Apps B, C, D, E using their IP addresses. I want to use WebSocket protocol (so that B, C, D, E can reply back on certain events). Apps B, C, D, E do not know communicate among themselves, nor do they know of each others existance. Apps B, C, D, E do not know that App A exists. Apps B, C, D, E are running on separate hardware, each having its own IP address. Once again, App A is the one that initiates connection to Apps B, C, D, E via WebSocket.
I tried to use the socket.io and socket.io-client libraries to do achieve this. Since my App A needs to be the one that initiates connection I used the socket.io-client library for it, and for Apps B, C, D, E I used the socket.io library. In usual case Apps B, C, D, E would act as the clients and would use socket.io-client library to connect to App A which would be the server and would use socket.io library. In my case it is opposite, as Apps B, C, D, E do not know of App A, and App A knows their IP addresses.
The problem I have when using the socket.io-client library is how to keep track of socket IDs within App A so that I can emit to a specific socket when I need to by using the socket ID? The socket.io-client library does not seem to keep track (a list) of socket IDs which are connected, as is the case on the server side with socket.io library.
Here is an example of what I tried so far:
App A Code:
const io = require('socket.io-client');
const socketB = io('http://10.11.18.1:3000');
const socketC = io('http://10.11.19.1:3000');
const socketD = io('http://10.11.20.1:3000');
const socketE = io('http://10.11.21.1:3000');
socketB.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketC.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketD.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketE.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
App B, C, D, E Code:
const io = require('socket.io');
const socket = io();
const PORT = 80;
socket.on('connection', client =>
...
);
socket.listen(PORT);
node.js websocket socket.io
add a comment |
I have 5 Node.js apps. Let us assume that the 5 apps that I have are called:
App A,
App B,
App C,
App D,
App E,
I want App A to connect to Apps B, C, D, E using their IP addresses. I want to use WebSocket protocol (so that B, C, D, E can reply back on certain events). Apps B, C, D, E do not know communicate among themselves, nor do they know of each others existance. Apps B, C, D, E do not know that App A exists. Apps B, C, D, E are running on separate hardware, each having its own IP address. Once again, App A is the one that initiates connection to Apps B, C, D, E via WebSocket.
I tried to use the socket.io and socket.io-client libraries to do achieve this. Since my App A needs to be the one that initiates connection I used the socket.io-client library for it, and for Apps B, C, D, E I used the socket.io library. In usual case Apps B, C, D, E would act as the clients and would use socket.io-client library to connect to App A which would be the server and would use socket.io library. In my case it is opposite, as Apps B, C, D, E do not know of App A, and App A knows their IP addresses.
The problem I have when using the socket.io-client library is how to keep track of socket IDs within App A so that I can emit to a specific socket when I need to by using the socket ID? The socket.io-client library does not seem to keep track (a list) of socket IDs which are connected, as is the case on the server side with socket.io library.
Here is an example of what I tried so far:
App A Code:
const io = require('socket.io-client');
const socketB = io('http://10.11.18.1:3000');
const socketC = io('http://10.11.19.1:3000');
const socketD = io('http://10.11.20.1:3000');
const socketE = io('http://10.11.21.1:3000');
socketB.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketC.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketD.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketE.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
App B, C, D, E Code:
const io = require('socket.io');
const socket = io();
const PORT = 80;
socket.on('connection', client =>
...
);
socket.listen(PORT);
node.js websocket socket.io
Can you include a code snippet of what you've tried so far?
– Andrew Eisenberg
Mar 8 at 23:15
@AndrewEisenberg I've added a code snippet. Thank you
– user1064089
Mar 10 at 6:52
add a comment |
I have 5 Node.js apps. Let us assume that the 5 apps that I have are called:
App A,
App B,
App C,
App D,
App E,
I want App A to connect to Apps B, C, D, E using their IP addresses. I want to use WebSocket protocol (so that B, C, D, E can reply back on certain events). Apps B, C, D, E do not know communicate among themselves, nor do they know of each others existance. Apps B, C, D, E do not know that App A exists. Apps B, C, D, E are running on separate hardware, each having its own IP address. Once again, App A is the one that initiates connection to Apps B, C, D, E via WebSocket.
I tried to use the socket.io and socket.io-client libraries to do achieve this. Since my App A needs to be the one that initiates connection I used the socket.io-client library for it, and for Apps B, C, D, E I used the socket.io library. In usual case Apps B, C, D, E would act as the clients and would use socket.io-client library to connect to App A which would be the server and would use socket.io library. In my case it is opposite, as Apps B, C, D, E do not know of App A, and App A knows their IP addresses.
The problem I have when using the socket.io-client library is how to keep track of socket IDs within App A so that I can emit to a specific socket when I need to by using the socket ID? The socket.io-client library does not seem to keep track (a list) of socket IDs which are connected, as is the case on the server side with socket.io library.
Here is an example of what I tried so far:
App A Code:
const io = require('socket.io-client');
const socketB = io('http://10.11.18.1:3000');
const socketC = io('http://10.11.19.1:3000');
const socketD = io('http://10.11.20.1:3000');
const socketE = io('http://10.11.21.1:3000');
socketB.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketC.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketD.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketE.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
App B, C, D, E Code:
const io = require('socket.io');
const socket = io();
const PORT = 80;
socket.on('connection', client =>
...
);
socket.listen(PORT);
node.js websocket socket.io
I have 5 Node.js apps. Let us assume that the 5 apps that I have are called:
App A,
App B,
App C,
App D,
App E,
I want App A to connect to Apps B, C, D, E using their IP addresses. I want to use WebSocket protocol (so that B, C, D, E can reply back on certain events). Apps B, C, D, E do not know communicate among themselves, nor do they know of each others existance. Apps B, C, D, E do not know that App A exists. Apps B, C, D, E are running on separate hardware, each having its own IP address. Once again, App A is the one that initiates connection to Apps B, C, D, E via WebSocket.
I tried to use the socket.io and socket.io-client libraries to do achieve this. Since my App A needs to be the one that initiates connection I used the socket.io-client library for it, and for Apps B, C, D, E I used the socket.io library. In usual case Apps B, C, D, E would act as the clients and would use socket.io-client library to connect to App A which would be the server and would use socket.io library. In my case it is opposite, as Apps B, C, D, E do not know of App A, and App A knows their IP addresses.
The problem I have when using the socket.io-client library is how to keep track of socket IDs within App A so that I can emit to a specific socket when I need to by using the socket ID? The socket.io-client library does not seem to keep track (a list) of socket IDs which are connected, as is the case on the server side with socket.io library.
Here is an example of what I tried so far:
App A Code:
const io = require('socket.io-client');
const socketB = io('http://10.11.18.1:3000');
const socketC = io('http://10.11.19.1:3000');
const socketD = io('http://10.11.20.1:3000');
const socketE = io('http://10.11.21.1:3000');
socketB.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketC.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketD.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
socketE.on('connect', function ()
if (socket.connected)
console.log(socket.id);
);
App B, C, D, E Code:
const io = require('socket.io');
const socket = io();
const PORT = 80;
socket.on('connection', client =>
...
);
socket.listen(PORT);
node.js websocket socket.io
node.js websocket socket.io
edited Mar 10 at 6:51
user1064089
asked Mar 8 at 21:57
user1064089user1064089
21138
21138
Can you include a code snippet of what you've tried so far?
– Andrew Eisenberg
Mar 8 at 23:15
@AndrewEisenberg I've added a code snippet. Thank you
– user1064089
Mar 10 at 6:52
add a comment |
Can you include a code snippet of what you've tried so far?
– Andrew Eisenberg
Mar 8 at 23:15
@AndrewEisenberg I've added a code snippet. Thank you
– user1064089
Mar 10 at 6:52
Can you include a code snippet of what you've tried so far?
– Andrew Eisenberg
Mar 8 at 23:15
Can you include a code snippet of what you've tried so far?
– Andrew Eisenberg
Mar 8 at 23:15
@AndrewEisenberg I've added a code snippet. Thank you
– user1064089
Mar 10 at 6:52
@AndrewEisenberg I've added a code snippet. Thank you
– user1064089
Mar 10 at 6:52
add a comment |
1 Answer
1
active
oldest
votes
It looks like you will need to explicitly keep track of the servers that app A is connected to. If I understand your problem correctly, you can do something like this and map socket ids to sockets:
const io = require('socket.io-client');
const idToSocket = ;
const sockets = [
'http://10.11.18.1:3000',
'http://10.11.19.1:3000',
'http://10.11.20.1:3000',
'http://10.11.21.1:3000'
]
.map(url => io(url))
.map(socket =>
socket.on('connect', () =>
idToSocket[socket.id] = socket;
);
return socket;
);
The sockets
array is an array of all the sockets, and the idToSocket
is a hash of the socket id to the socket that it relates to.
Wow great. I had no clue I could do something like this with the sockets. Andrew would you happen to know to how many servers socket.io-client can connect to, is there a limitation? Right now I have App B, C, D, E, but later I will have hundreds of these, and maybe even a thousand. Thank you
– user1064089
Mar 11 at 14:10
1
I'm not familiar with the socket.io library, but here is a page about benchmarking the library drewww.github.io/socket.io-benchmarking. Hundreds of sockets seems eminently doable, thousands would be trickier. Any limitations you reach would likely be due to the OS and hardware rather than node.js itself. So, make sure to buy yourself a nice server, or be prepared to spend $$$ on EC2, but you'll be fine.
– Andrew Eisenberg
Mar 11 at 15:19
Great. Thank you so much for your help
– user1064089
Mar 11 at 16:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071556%2fnode-js-app-communicating-with-multiple-node-apps-via-websocket%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like you will need to explicitly keep track of the servers that app A is connected to. If I understand your problem correctly, you can do something like this and map socket ids to sockets:
const io = require('socket.io-client');
const idToSocket = ;
const sockets = [
'http://10.11.18.1:3000',
'http://10.11.19.1:3000',
'http://10.11.20.1:3000',
'http://10.11.21.1:3000'
]
.map(url => io(url))
.map(socket =>
socket.on('connect', () =>
idToSocket[socket.id] = socket;
);
return socket;
);
The sockets
array is an array of all the sockets, and the idToSocket
is a hash of the socket id to the socket that it relates to.
Wow great. I had no clue I could do something like this with the sockets. Andrew would you happen to know to how many servers socket.io-client can connect to, is there a limitation? Right now I have App B, C, D, E, but later I will have hundreds of these, and maybe even a thousand. Thank you
– user1064089
Mar 11 at 14:10
1
I'm not familiar with the socket.io library, but here is a page about benchmarking the library drewww.github.io/socket.io-benchmarking. Hundreds of sockets seems eminently doable, thousands would be trickier. Any limitations you reach would likely be due to the OS and hardware rather than node.js itself. So, make sure to buy yourself a nice server, or be prepared to spend $$$ on EC2, but you'll be fine.
– Andrew Eisenberg
Mar 11 at 15:19
Great. Thank you so much for your help
– user1064089
Mar 11 at 16:26
add a comment |
It looks like you will need to explicitly keep track of the servers that app A is connected to. If I understand your problem correctly, you can do something like this and map socket ids to sockets:
const io = require('socket.io-client');
const idToSocket = ;
const sockets = [
'http://10.11.18.1:3000',
'http://10.11.19.1:3000',
'http://10.11.20.1:3000',
'http://10.11.21.1:3000'
]
.map(url => io(url))
.map(socket =>
socket.on('connect', () =>
idToSocket[socket.id] = socket;
);
return socket;
);
The sockets
array is an array of all the sockets, and the idToSocket
is a hash of the socket id to the socket that it relates to.
Wow great. I had no clue I could do something like this with the sockets. Andrew would you happen to know to how many servers socket.io-client can connect to, is there a limitation? Right now I have App B, C, D, E, but later I will have hundreds of these, and maybe even a thousand. Thank you
– user1064089
Mar 11 at 14:10
1
I'm not familiar with the socket.io library, but here is a page about benchmarking the library drewww.github.io/socket.io-benchmarking. Hundreds of sockets seems eminently doable, thousands would be trickier. Any limitations you reach would likely be due to the OS and hardware rather than node.js itself. So, make sure to buy yourself a nice server, or be prepared to spend $$$ on EC2, but you'll be fine.
– Andrew Eisenberg
Mar 11 at 15:19
Great. Thank you so much for your help
– user1064089
Mar 11 at 16:26
add a comment |
It looks like you will need to explicitly keep track of the servers that app A is connected to. If I understand your problem correctly, you can do something like this and map socket ids to sockets:
const io = require('socket.io-client');
const idToSocket = ;
const sockets = [
'http://10.11.18.1:3000',
'http://10.11.19.1:3000',
'http://10.11.20.1:3000',
'http://10.11.21.1:3000'
]
.map(url => io(url))
.map(socket =>
socket.on('connect', () =>
idToSocket[socket.id] = socket;
);
return socket;
);
The sockets
array is an array of all the sockets, and the idToSocket
is a hash of the socket id to the socket that it relates to.
It looks like you will need to explicitly keep track of the servers that app A is connected to. If I understand your problem correctly, you can do something like this and map socket ids to sockets:
const io = require('socket.io-client');
const idToSocket = ;
const sockets = [
'http://10.11.18.1:3000',
'http://10.11.19.1:3000',
'http://10.11.20.1:3000',
'http://10.11.21.1:3000'
]
.map(url => io(url))
.map(socket =>
socket.on('connect', () =>
idToSocket[socket.id] = socket;
);
return socket;
);
The sockets
array is an array of all the sockets, and the idToSocket
is a hash of the socket id to the socket that it relates to.
answered Mar 10 at 19:28
Andrew EisenbergAndrew Eisenberg
21.7k772119
21.7k772119
Wow great. I had no clue I could do something like this with the sockets. Andrew would you happen to know to how many servers socket.io-client can connect to, is there a limitation? Right now I have App B, C, D, E, but later I will have hundreds of these, and maybe even a thousand. Thank you
– user1064089
Mar 11 at 14:10
1
I'm not familiar with the socket.io library, but here is a page about benchmarking the library drewww.github.io/socket.io-benchmarking. Hundreds of sockets seems eminently doable, thousands would be trickier. Any limitations you reach would likely be due to the OS and hardware rather than node.js itself. So, make sure to buy yourself a nice server, or be prepared to spend $$$ on EC2, but you'll be fine.
– Andrew Eisenberg
Mar 11 at 15:19
Great. Thank you so much for your help
– user1064089
Mar 11 at 16:26
add a comment |
Wow great. I had no clue I could do something like this with the sockets. Andrew would you happen to know to how many servers socket.io-client can connect to, is there a limitation? Right now I have App B, C, D, E, but later I will have hundreds of these, and maybe even a thousand. Thank you
– user1064089
Mar 11 at 14:10
1
I'm not familiar with the socket.io library, but here is a page about benchmarking the library drewww.github.io/socket.io-benchmarking. Hundreds of sockets seems eminently doable, thousands would be trickier. Any limitations you reach would likely be due to the OS and hardware rather than node.js itself. So, make sure to buy yourself a nice server, or be prepared to spend $$$ on EC2, but you'll be fine.
– Andrew Eisenberg
Mar 11 at 15:19
Great. Thank you so much for your help
– user1064089
Mar 11 at 16:26
Wow great. I had no clue I could do something like this with the sockets. Andrew would you happen to know to how many servers socket.io-client can connect to, is there a limitation? Right now I have App B, C, D, E, but later I will have hundreds of these, and maybe even a thousand. Thank you
– user1064089
Mar 11 at 14:10
Wow great. I had no clue I could do something like this with the sockets. Andrew would you happen to know to how many servers socket.io-client can connect to, is there a limitation? Right now I have App B, C, D, E, but later I will have hundreds of these, and maybe even a thousand. Thank you
– user1064089
Mar 11 at 14:10
1
1
I'm not familiar with the socket.io library, but here is a page about benchmarking the library drewww.github.io/socket.io-benchmarking. Hundreds of sockets seems eminently doable, thousands would be trickier. Any limitations you reach would likely be due to the OS and hardware rather than node.js itself. So, make sure to buy yourself a nice server, or be prepared to spend $$$ on EC2, but you'll be fine.
– Andrew Eisenberg
Mar 11 at 15:19
I'm not familiar with the socket.io library, but here is a page about benchmarking the library drewww.github.io/socket.io-benchmarking. Hundreds of sockets seems eminently doable, thousands would be trickier. Any limitations you reach would likely be due to the OS and hardware rather than node.js itself. So, make sure to buy yourself a nice server, or be prepared to spend $$$ on EC2, but you'll be fine.
– Andrew Eisenberg
Mar 11 at 15:19
Great. Thank you so much for your help
– user1064089
Mar 11 at 16:26
Great. Thank you so much for your help
– user1064089
Mar 11 at 16:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071556%2fnode-js-app-communicating-with-multiple-node-apps-via-websocket%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you include a code snippet of what you've tried so far?
– Andrew Eisenberg
Mar 8 at 23:15
@AndrewEisenberg I've added a code snippet. Thank you
– user1064089
Mar 10 at 6:52