Node.js client to server with socket.io


Socket.io is usually employed by browser apps communicating with a Node.js server application. It is however possible to create a client in Node.js if you need to call the same server application. It is also possible for the server to return values by calling a function that the client passes to it.

You’ll need to install socket.io and socket.io-client using npm as shown below. Additionally, we also use express for serving static HTTP content.

npm -g install socket.io socket.io-client express errorhandler

This is how a client connection can be established. The namespace ns is used for communicating with the server. Client emits the event call with parameter p1, and a function parameter that receives a response code and additional data. Remember to export global node_modules folder in NODE_PATH before running the script with node e.g. export NODE_PATH=/usr/local/lib/node_modules/. npm -g list | head shows the path to node_modules.

var io = require('socket.io-client');
var serverUrl = 'http://localhost:8080/ns';
var conn = io.connect(serverUrl);
 
var p1 = 'hello';
conn.emit('call', p1, function(resp, data) {
    console.log('server sent resp code ' + resp);
});

This is how a server can serve the client above.

var http = require('http');
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/'));
var errorhandler = require('errorhandler');
app.use(errorhandler()); // development only
var server = http.createServer(app);
 
var io = require('socket.io').listen(server);
var ns = io.of('/ns');
ns.on('connection', function (socket) {
    socket.on('call', function (p1, fn) {
	console.log('client sent '+p1);
        // do something useful
        fn(0, 'some data'); // success
    });
});

server.listen(8080);

Socket.io makes event-based communication really easy.

6 thoughts on “Node.js client to server with socket.io

  1. Hi,
    When I run node client.js, it gives at line ‘var app = express.createServer();’ this error: createServer() is undefined.
    Why?

      1. Thanks for your response.
        Server.js:

        var express = require(‘express’)
        , http = require(‘http’);

        var app = express();
        var server = http.createServer(app);
        var io = require(‘socket.io’).listen(server);
        server.listen(1235);

        Client.js:

        var io = require(‘socket.io-client’);
        var socket = io.connect(‘ws://192.168.1.2:1235’);

        When I run both server.js and client.js, I give ‘info – unhandled socket.io url’ error. ??

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s