Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

server-side connection to a socket.io server

server-side connection to a socket.io server

Problem

I have a Java Server that connects with web-browsers through web-sockets, specifically socket.io, specifically this implementation: https://github.com/mrniko/netty-socketio

Now I would like to connect to the same server, not from a web-browser (through Javascript), but from a Node.JS program.

The problem is, in all examples that I found, the Node.JS program is the server, and the clients are always web-browsers.

I tried to use a code similar to the client connection code in node.js:

var io = require('socket.io');
var socket = io.connect("http://localhost:9080");

but got this error:

TypeError: Object # has no method 'connect'
Problem courtesy of: Erel Segal-Halevi

Solution

You need to use socketio-client

$ npm install socket.io-client

https://github.com/LearnBoost/socket.io-client

You can then connect to another socket server by the following:

var io = require('socket.io-client');
var socket = io.connect('http://domain.com');
socket.on('connect', function () {
  // socket connected
});
Solution courtesy of: Menztrual

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

server-side connection to a socket.io server

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×