Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 219
Sergey V. Pereslavtsev Avatar asked Jan 26 '12 13:01

Sergey V. Pereslavtsev


People also ask

How do I connect to a Socket.IO server?

listen(port); // Create a Socket.IO instance, passing it our server var socket = io. listen(server); // Add a connect listener socket. on('connection', function(client){ console. log('Connection to client established'); // Success!

How do you check if socket IO client is connected or not?

You can check the socket. connected property: var socket = io. connect(); console.

Is Socket.IO server side?

A Socket is the fundamental class for interacting with the client. It inherits all the methods of the Node. js EventEmitter, like emit, on, once or removeListener.

Does Socket.IO require a server?

It seems that Socket.IO is always dependent on a http server, to the point that it will create one for you, like in the example above.


2 Answers

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).

like image 144
guy mograbi Avatar answered Sep 24 '22 06:09

guy mograbi


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();
    } );
} );
like image 34
Simon A. Eugster Avatar answered Sep 22 '22 06:09

Simon A. Eugster