Sergey V. Pereslavtsev
Sergey V. Pereslavtsev

Reputation: 661

Socket.io: Connect from one server to another

I'm trying to make a nodejs(socket.io) server to communicate with another one. So the client emits an event to the 'hub' server and this server emits an event to some second server for processing the action.

I tried to do:

var io_client = require( 'socket.io-client' );

and then,

io_client.connect( "second_server_host" ); 

it seems to work for connection but you can't do anything with this:

debug - set close timeout for client 15988842591410188424
info  - socket error Error: write ECONNABORTED
 at errnoException (net.js:642:11)
 at Socket._write (net.js:459:18)
 at Socket.write (net.js:446:15)

I guess I'm doing it wrong and missing something obvious.

Any suggestions?

Upvotes: 16

Views: 37385

Answers (6)

Jon Cursi
Jon Cursi

Reputation: 3371

For anyone looking to do this on a MeteorJS app, I created a new Meteor package joncursi:socket-io-client to solve this problem. Please see https://atmospherejs.com/joncursi/socket-io-client for more detail and example usage. Since I've bundled the NPM binaries into a package for you, you don't have to worry about installing NPM packages, declaring NPM.require() dependencies, etc. And best of all, you can deploy to .meteor.com without a hitch.

Upvotes: 1

Simon A. Eugster
Simon A. Eugster

Reputation: 4264

For anyone searching for a short working example, see below. This example works with [email protected] and [email protected].

var port = 3011;

var server = require( 'http' ).createServer( ).listen( port, function () {
    console.log( "Express server listening on port " + port );
} );

var io = require( 'socket.io' ).listen( server ).set( "log level", 0 );

io.sockets.on( "connection", function ( socket ) {
    console.log( 'Server: Incoming connection.' );
    socket.on( "echo", function ( msg, callback ) {
        callback( msg );
    } );
} );


var ioc = require( 'socket.io-client' );
var client = ioc.connect( "http://localhost:" + port );

client.once( "connect", function () {
    console.log( 'Client: Connected to port ' + port );

    client.emit( "echo", "Hello World", function ( message ) {
        console.log( 'Echo received: ', message );
        client.disconnect();
        server.close();
    } );
} );

Upvotes: 7

guy mograbi
guy mograbi

Reputation: 28598

Just came across this question, and another just like it with a much better answer.

https://stackoverflow.com/a/14118102/1068746

You can do server to server. The "client" code remains the same as if it was on the browser. Amazing isn't it?

I just tried it myself, and it works fine..

I ran 2 servers - using the same exact code - once on port 3000 as server, and another on port 3001 as client. The code looks like this:

  , io = require('socket.io')
  , ioClient = require('socket.io-client')

   .... 

   if ( app.get('port') == 3000 ){

    io.listen(server).sockets.on('connection', function (socket) {
      socket.on('my other event', function (data) {
        console.log(data);
      });
    });
}else{
    function emitMessage( socket ){
        socket.emit('my other event', { my: 'data' });
        setTimeout(function(){emitMessage(socket)}, 1000);
    }
    var socket = ioClient.connect("http://localhost:3000");
    emitMessage(socket);
}

And if you see on the server side a "{my:data}" print every second, everything works great. Just make sure to run the client (port 3001) after the server (port 3000).

Upvotes: 10

Cody
Cody

Reputation: 10015

The native Node TCP module is probably what you want - I wanted to do what you're trying to do but it seems that the fact that WebSockets are strictly many-browser to server, or browser to many-server.

You can weave a tcp strategy into your websocket logic.

Using tcp:

var net = require('net');
var tcp = net.connect({port: 3000, host: 'localhost'});
tcp.on('connect', function(){
  var buffer = new Buffer(16).fill(0);
  buffer.write('some stuff');
  tcp.write(buffer);
});

tcp.on('data', function(data){console.log('data is:', data)});
tcp.on('end', cb);
tcp.on('error', cb);

I would use a bridge pattern with this:

https://www.google.com/search?q=javascript+bridge+pattern&aq=f&oq=javascript+bridge+pattern&aqs=chrome.0.57.6617&sourceid=chrome&ie=UTF-8

OR, use the Node Module https://npmjs.org/package/ws-tcp-bridge

I also heard that using redis can be quite helpful - Socket.io uses this as a fallback.

Hope this helps...

Cheers

Upvotes: 0

Dragunov
Dragunov

Reputation: 985

For Server to Server or App to App communication, I think you should look into Redis Pub-sub. Its capable of very good speeds, and can handle the entire message queuing architecture of a big app.

Here is a slightly complex but quite understandable example for using Redis Pub Sub: Redis Pub Sub Example

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409136

ECONNABORTED means that the connection have been closed by "the other side".

For example, lets say we have two programs, A and B. Program A connects to program B, and they start to send data back and forth. Program B closes the connection for some reason. After the connection was closed program A tries to write to program B, but since the connection is closed program A will get the error ECONNABORTED.

One of your programs have closed the connection, and the other doesn't know about it and tries to write to the socket, resulting in an error.

Upvotes: 0

Related Questions