26

I'd like to enable socket-based p2p communications between two or more different node.js application servers. I'm using socket.io to handle all such communication between a given server and the web application it serves - but what I'm looking for is a way to communicate server-to-server.

I had initially assumed it would be as easy as something like this:

var io = require("socket.io");
var socket = io.connect("my remote endpoint");

However, as it turns out the server-side socket.io implementation doesn't offer a "connect" method, only a listen method.

Why is this? Why can't I treat a node application server as a client to a socket.io server running elsewhere? Is there any way that I can achieve this functionality?

Myk
  • 6,205
  • 4
  • 26
  • 33
  • 3
    Is the client library just what you're after? https://github.com/LearnBoost/socket.io-client – pimvdb Mar 13 '12 at 15:59
  • Can I plug that into a node.js app? How do I require() it? It's designed for browser-based usage, as far as I can tell. – Myk Mar 13 '12 at 16:02
  • See this s/o thread: http://stackoverflow.com/questions/6785979/socket-io-client-issues-require-socket-io-client-js-not-working – Myk Mar 13 '12 at 16:04
  • 2
    If I run one node instance as the server (`require("socket.io")`) and another one as the client (`require("socket.io-client")`), I can establish a connection - the client also supports node. – pimvdb Mar 13 '12 at 16:12
  • I don't understand. When I type require("socket.io-client") I get an error. – Myk Mar 13 '12 at 16:28
  • Oh, hmm, if I do npm install socket.io-client then I get something installed. Time to play with this. – Myk Mar 13 '12 at 16:30
  • well that's interesting, it looks like it should be fairly easy to make something like this work, if I change the socket.io source: var client = require("socket.io").client; client.connect("myURL") – Myk Mar 13 '12 at 17:00

3 Answers3

60

OK, so thanks to @pimvdb in the comments above I've got a workable solution.

Basically, the socket.io library that npm installs has a dependency on another module, called socket.io-client. In a standard socket.io installation this will be installed in node_modules/socket.io/node_modules/socket.io-client

However, it's also possible to say "npm install socket.io-client" and install it as its own first-class citizen library.

Then your usage looks like this:

var client = require("socket.io-client");
var socket = client.connect("http://myendpoint.com:3000/whatever");
socket.emit("test", "foo");

And everything works.

So, thanks man!

Zaman
  • 130
  • 2
  • 8
Myk
  • 6,205
  • 4
  • 26
  • 33
  • 13
    `var client = require("socket.io-client"); var socket = client.connect("http://localhost:3000"); socket.emit("test", "foo");` This worked for me. – nullbuilt Oct 22 '15 at 09:02
6

Just for clarification, this is an example with listeners and possibility to emit events (and without install again a module already installed)

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

client = io.connect('http://'+CONFIG.host+':'+CONFIG.port);

client.on('connect',function() {
    client.emit("test","foo");
}); 
Luca Rainone
  • 16,138
  • 2
  • 38
  • 52
  • Since i am new to sockets.What about handling this request if in client.emit("test","foo") "foo" is a stream and test is a blob,something like this client.emit("blobs",stream) to be more clear i am looking for server side script(server.js) and setting the portal to for listening help!!? – A Sahra Jan 07 '17 at 09:57
2

Before you go full speed on socket.io for server-to-server communications..... socket.io is engineered as a browser to server comm infrastructure. I'm far from certain it is the best solution for P2P server stuff. Plus, if you do server-to-server - why not just do Websockets? There are various websocket modules for node - e.g. https://github.com/einaros/ws

Mark
  • 866
  • 9
  • 18
  • 1
    I understand all of that. The situation is that I have a socket.io based web application with browser <-> node communication already established and the whole API built. I wanted to be able to spin up a local instance of node.js and connect it to an Arduino, then connect it as one more item on the existing socket.io session. Does that make sense? – Myk Mar 14 '12 at 18:32
  • Sure, sounds interesting. Good luck. – Mark Mar 14 '12 at 19:01