title: Socket.io server to server (sending message to specific server)
body: <p>I have a specific situation. I have client (browser) which connects to server A. Server A is connected to server B and C. When client sends message to server A, I need to send message to specific server which means to one of the servers B or C, not both of them. Generally I need separated communication between servers but I have only broadcasted communication. Message from server A to another server can't be seen on all connected servers.</p>
  
  <p>How can I send messages directly to one of the servers ? Here is my code snippet for servers. I just change ports for each server and connect them.</p>
  
  <pre><code>// Load requirements
  var PORT = process.env.PORT || 8080;//just change port for other servers
  var express = require("express");
  var app = express();
  var http = require("http").Server(app);
  var io = require("socket.io")(http);
  
  app.use(express.static(__dirname + '/public'));
  
  var client = require('socket.io-client');
  
  var socket1 = client.connect('http://localhost:8081', { reconnect: true });//connection to server B
  var socket2 = client.connect('http://localhost:8082', { reconnect: true });//connection to server C
  
  // Add a connect listener
  io.on('connection', function (socket) {
      console.log('Client connected.');
  //when server receive message from client
      socket.on('message_from_browser', function (message) {
          console.log("Message from browser broadcasted: " + message.text);
          var updated_message = {
             text: message.text,
             port: PORT  
          };
  
          // send message to server B
          socket1.emit('server_message', updated_message);//send message to server B
      });
  
      // Disconnect listener
      socket.on('disconnect', function () {
          console.log('Client disconnected.');
      });
  });
  
  socket1.on('connect', function () {
      socket1.on('server_message', function (message) {
          console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
      });
      //change server name
      console.log('Connected to server B!');
  });
  
  socket2.on('connect', function () {
      socket2.on('server_message', function (message) {
          console.log('RECEIVED MESSAGE FROM ANOTHER SERVER ON PORT '+ message.port + ": " + message.text);
      });
      //change server name
      console.log('Connected to server C!');
  });
  
  http.listen(PORT, function (req, res) {
      console.log("Server Started on port: " + PORT);
  });
  </code></pre>