Search code examples
node.jsserversocket.ioserver-to-server

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.


Solution

  • 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(){});