0

I'm trying to build an application that has two components. There's a public-facing component and an administrative component. Each component will be hosted on a different server, but the two will access the same database. I need to set up the administrative component to be able to send a message to the public-facing component to query the database and send the information to all the public clients.

What I can't figure out is how to set up a connection between the two components. I'm using the standard HTTP server setup provided by Socket.io.

In each server:

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {

  socket.emit('news', { hello: 'world' });

  socket.on('my other event', function (data) {
    console.log(data);
  });
});

And on each client:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

I've looked at this question but couldn't really follow the answers provided, and I think the situation is somewhat different. I just need one of the servers to be able to send a message to the other server, and still send/receive messages to/from its own set of clients.

I'm brand new to Node (and thus, Socket), so some explanation would be incredibly helpful.

Community
  • 1
  • 1
Josh Vickerson
  • 429
  • 5
  • 16
  • 1
    **[this answer](http://stackoverflow.com/questions/14118076/socket-io-server-to-server)** seems to also do the trick – ddaaggeett Mar 18 '17 at 05:17

2 Answers2

6

The easiest thing I could find to do is simply create a client connection between the servers using socket.io-client. In my situation, the admin server connects to the client server:

var client = require("socket.io-client");
var socket = client.connect("other_server_hostname");

Actions on the admin side can then send messages to the admin server, and the admin server can use this client connection to forward information to the client server.

On the client server, I created an on 'adminMessage' function and check for some other information to verify where the message came from like so:

io.sockets.on('connection', function (socket) {
  socket.on('adminMessage', function (data) {
    if(data.someIdentifyingData == "data") {
      // DO STUFF
    }
  });
});
Josh Vickerson
  • 429
  • 5
  • 16
-1

I had the same problem, but instead to use socket.io-client I decided to use a more simple approach (at least for me) using redis pub/sub, the result is pretty simple. My main problem with socket.io-client is that you'll need to know server hosts around you and connect to each one to send messages.

You can take a look at my solution here: https://github.com/alissonperez/scalable-socket-io-server

With this solution you can have how much process/servers you want (using auto-scaling solution), you just use redis as a way to forward your messages between your servers.