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?
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!
You can check the socket. connected property: var socket = io. connect(); console.
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.
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.
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).
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();
} );
} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With