Just require('socket.io-client')
and run $ node client.js
as pointed out by Alfred . I confirm that this works with socket.io-client
v1.4.8. To demonstrate, see the following code:
// client.js var io = require('socket.io-client'); var socket = io('http://localhost:3000/'); socket.on('connect', function () { socket.emit('echo', {msg: 'Hello universe!'}, function (response) { console.log(response.msg); socket.disconnect(); // otherwise the node process keeps on running. }); });
Server:
// server.js var io = require('socket.io')(3000); io.on('connection', function (socket) { socket.on('echo', function (data, response) { response(data); }); });
Unscrew the server using $ node server.js
and then the $ node client.js
in another terminal and see how the magic happens:
$ node client.js Hello universe!
It works! A very convenient way, for example, is to check your socket.io API.
Akseli palΓ©n
source share