Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io as a client

is there any way to run socketio as a client(not a browser, but a nodejs script)

I need to broadcast data from a server to some clients (browsers) and to another linux machine (only running nodejs to get variables, no browser)

Any ideias is welcome

Regards

like image 994
Vitor Avatar asked Jan 28 '12 02:01

Vitor


People also ask

What is socket IO client?

Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).

How does Socket.IO connect to client server?

listen(port); // Create a Socket.IO instance, passing it our server var socket = io. listen(server); // Add a connect listener socket. on('connection', function(client){ console. log('Connection to client established'); // Success!

Does Socket.IO need a server?

Socket.io, and WebSockets in general, require an http server for the initial upgrade handshake. So even if you don't supply Socket.io with an http server it will create one for you. The issue is that the second parameter in your io. listen(3000, '0.0.


3 Answers

There is a project on github which implements a socket.io client. Take a look here:

https://github.com/remy/Socket.io-node-client

var socket = new io.Socket('localhost', 8000);

socket.on('connect', function () {
  console.log('yay, connected!');
  socket.send('hi there!');
});

socket.on('message', function (msg) {
  console.log('a new message came in: ' + JSON.stringify(msg));
});

socket.connect();
like image 72
Timothy Strimple Avatar answered Sep 21 '22 20:09

Timothy Strimple


I believe you could just use socket.io-client. require that and use that in your node.js code as would in the client/browser. I also found this interesting tutorial right now => http://liamkaufman.com/blog/2012/01/28/testing-socketio-with-mocha-should-and-socketio-client/

like image 26
Alfred Avatar answered Sep 23 '22 20:09

Alfred


Just require('socket.io-client') and run $ node client.js as pointed out by Alfred. I confirm 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.
  });
});

The server:

// server.js
var io = require('socket.io')(3000);
io.on('connection', function (socket) {
  socket.on('echo', function (data, response) {
    response(data);
  });
});

Spin up the server with $ node server.js and then the client $ node client.js in another terminal and watch the magic happening:

$ node client.js
Hello universe!

It works! A very convenient way for example to test your socket.io API.

like image 42
Akseli Palén Avatar answered Sep 22 '22 20:09

Akseli Palén