Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect a Node.JS server to another Node.JS server with Socket.IO?

I'm trying to use Socket.IO to send data between two Node.JS servers. I usually only use Socket.IO for Node.JS to/from a website, but I've never gone server to server. How can I do this? I really only need to know how a Node.JS server can create that connection to another Node.JS server.

Side note: If there's a better/easier way of sending data from server to server, please do let me know. Both servers are hosted at the same IP.

like image 868
APixel Visuals Avatar asked May 20 '26 13:05

APixel Visuals


1 Answers

You get the socket.io-client module for node.js and use pretty much the same code with it that you would use from a browser to connect to another socket.io server.

You can see an example of server-side usage of socket.io-client here in the socket.io doc.

var socket = require('socket.io-client')('http://localhost');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
like image 78
jfriend00 Avatar answered May 22 '26 02:05

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!