Socket.io - making symmetric connection between two servers

I'm trying to build network consisting of many servers with clients connected to them and I want to make server connections transparent for communication. By that I mean that I don't want to use client socket.io directly in the server code and I want to use io object in a symmetric way (so that the servers are connected to each other in the same way). I stumbled upon socket.io adapters and tried to write myself one.

In general this is what I want to achieve: - One io object in each server (no 'socket.io-client' library and client calls directly in sever code to prevent confusion from using two separate socket objects doing sometimes the same job) - Server messages are a bit different from the client ones (differing in fields) - Servers are authing in a different way than the clients (database auth vs session auth)

I tried to achieve this by using socket.io-adapter and writing my own one. Adapter is in general object that has client socket using 'socket.io-client', however it is obscuring that fact thanks to the adapter functionality and it receives and forwards all messages to the object it is attached to (io object from one of the servers). For now, I am able to send message using main io object from the server code (message is forwarded to adapter and sent to another server), but I am not able to intercept message from the other server (hovewer - it is correctly intercepted inside adapter code using socket.io client).

This is the code for the adapter:

var Adapter = require('socket.io-adapter');
var io = require('socket.io-client');
module.exports = adapter;

function adapter(uri, opts) {
    (...)
    var socket = io.connect('http://'+host+':'+port);

    function ServerAdapter(nsp) {
        Adapter.call(this, nsp);
        socket.emit('login', {deviceId: deviceId, isServer: true});
        socket.on('message', this.onmessage.bind(this));
    }
    ServerAdapter.prototype.__proto__ = Adapter.prototype;
    ServerAdapter.prototype.onmessage = function(message) {
        this.broadcast.apply(this,[message]);
    };
    ServerAdapter.prototype.broadcast = function(packet, opts) {
        var optsToSend = opts || {rooms:[],except:[],flags:{}};
        Adapter.prototype.broadcast.call(this,packet,optsToSend);
    };
    return ServerAdapter;
}

And this is the code inside server that is attaching the adapter:

var sio = require('socket.io');
sa = require('server-adapter.js');
(...)
io = sio.listen(httpServer);
(...)
io.adapter(sa('localhost:9090',{deviceId:'server1'}));

So my question is - how should I write the adapter in order to be able to forward intercepted message to the object adapter is attached to (usually io object inside server)?


UPDATE To be more explanatory - i would like to separate socket management for clients and for servers and use adapter for it in that way:

  • Clients use socket.io-client and connect to the listening server (normal behaviour)
  • Servers connect to each other on the following basis:
  • One server is client (it has to be obviously) and other one is the listening server
  • Servers use adapter based on socket.io-adapter which obscures additional authorization and processing of the messages inside it
  • Adaper is added to socket-io listening object so that every message from the client will be intercepted by it and every message intercepted by it will be forwarded to the object

I hope that it explains more :)