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?
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:
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 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, declaringNPM.require()
dependencies, etc. And best of all, you can deploy to.meteor.com
without a hitch.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
For anyone searching for a short working example, see below. This example works with
socket.io@0.9.16
andsocket.io-client@0.9.16
.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:
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
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.