Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 410
haijin Avatar asked Oct 01 '10 19:10

haijin


People also ask

Is it possible to have a node JS WebSocket client?

Using websocket-node To implement the WebSocket client on Node. js, you can use one of the many package libraries available. One of the simplest is 'websocket-node,' a pure JavaScript implementation of the WebSocket protocol for Node.

How do I run a node JS WebSocket?

All we need to do is call the WebSocket Object with a URI as the parameter. const webSocket = new WebSocket('ws://localhost:443/'); And then we can create an event for the WebSocket to do something when it gets data. That's it we can now receive data from the WebSocket server.

How many WebSockets can NodeJS handle?

The theoretical limit is 65k connections per IP address but the actual limit is often more like 20k, so we use multiple addresses to connect 20k to each (50 * 20k = 1 mil). I then run the web server as root by typing sudo -i followed by ulimit -n 1024000 and then node examples/WebSocket. js (in the uWebSockets.

Does Axios support WebSockets?

This guide will use Redux with Redux Toolkit for state management and Axios for API connectivity. The chat server will support both an HTTP REST API for CRUD operations and WebSockets for socket-related functions. WebSockets are mainly used for providing bilateral communication between the users and the server.


2 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

like image 113
colin moock Avatar answered Oct 22 '22 03:10

colin moock


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.

like image 6
Michael McHenry Avatar answered Oct 22 '22 04:10

Michael McHenry