Home » Nodejs » Is there a browserless websocket client for Node.js that does not need to use a browser?

Is there a browserless websocket client for Node.js that does not need to use a browser?

Posted by: admin November 29, 2017 Leave a comment

Questions:

Socket.IO, etc all require the using of browser on the client side….just wondering, how can we have browserless websocket client for node.js ?

Answers:

Current Recommendation

Use WebSocket-Node with my wrapper code (see below). As of this writing, no other public project that i know of supports the new hybi specification, so if you want to emulate current browser releases, you’ll need WebSocket-Node. If you want to emulate older browsers, such as mobile Safari on iOS 4.2, you’ll also need one of the other libraries listed below, but you’ll have to manage “WebSocket” object name collisions yourself.

A list of public WebSocket client implementations for node.js follows.

Socket.IO

The socket.io client-test WebSocket implementation does hixie draft 75/76, but as of this writing, not hybi 7+.

https://github.com/LearnBoost/socket.io/blob/master/support/node-websocket-client/lib/websocket.js

i’m asking if they intend to update to hybi 7+:
http://groups.google.com/group/socket_io/browse_thread/thread/d27320502109d0be

Node-Websocket-Client

Peter Griess’s “node-websocket-client” does hixie draft 75/76, but as of this writing, not hybi 7+.

https://github.com/pgriess/node-websocket-client/blob/master/lib/websocket.js

WebSocket-Node

Brian McKelvey’s WebSocket-Node has a client implementation for hybi 7-17 (protocol version 7-13), but the implementation does not provide a browser-style WebSocket object.

https://github.com/Worlize/WebSocket-Node

Here is the wrapper code I use to emulate the browser-style WebSocket object:

/**
 * Wrapper for Worlize WebSocketNode to emulate the browser WebSocket object.
 */
var WebSocketClient = require('./WorlizeWebSocketNode/lib/websocket').client;

exports.WebSocket = function (uri) {
  var self = this;
  this.connection = null;
  this.socket = new WebSocketClient();
  this.socket.on('connect', function (connection) {
    self.connection = connection;

    connection.on('error', function (error) {
      self.onerror();
    });

    connection.on('close', function () {
      self.onclose();
    });

    connection.on('message', function (message) {
      if (message.type === 'utf8') {
        self.onmessage({data:message.utf8Data});
      }
    });

    self.onopen();
  });
  this.socket.connect(uri);
}

exports.WebSocket.prototype.send = function (data) {
  this.connection.sendUTF(data);
}

SockJS

Just for reference, Marek Majkowski’s SockJS does not include a node client. SockJS’s client library is simply a browser dom wrapper.

https://github.com/sockjs/sockjs-client

Questions:
Answers:

Having just gone through this, I have to recommend:
https://github.com/Worlize/WebSocket-Node
Due to it’s excellent documentation.

https://github.com/einaros/ws comes a close second.

Both are active and being kept up to date at this time.

Questions:
Answers:

Remy Sharp (@rem) wrote a Socket.io-client implementation that works on the server. I think this is what you’re looking for: https://github.com/remy/Socket.io-node-client

Questions:
Answers:

A Node.js server is in no way bound to a web browser as a client. Any program can use whatever socket library is provided by its supporting libraries to make a call to a Node.js server.

EDIT

Responding to your comment: don’t forget that Node.js is Javascript! If you want to execute code periodically — in much the same way that a daemon process might — you can use setInterval to run a callback every n milliseconds. You should be able to do it right there in your node program.

Questions:
Answers:

Right now (in Oct 2012) the recommended way to do it is using a the socket.io-client library, which is available at https://github.com/LearnBoost/socket.io-client