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

5

12

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 ?

Basic answered 1/10, 2010 at 19:51 Comment(0)
C
9

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

Cantata answered 8/11, 2011 at 18:38 Comment(0)
B
6

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.

Blurb answered 11/1, 2012 at 19:3 Comment(0)
A
4

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

Allomerism answered 19/11, 2010 at 6:4 Comment(2)
Socket.io-node-client is not a "browserless websocket" implementation, it is a shim that enables "browserless socket.io" client communications. he specifically asked for "browserless websocket". Socket.io-node-client uses the socket.io client-test WebSocket implementation, which supports the old WebSocket hixie draft 75/76 only; it does not (currently) emulate the WebSocket object now in use by modern browsers, so it is not appropriate for use as a general "browserless websocket" implementation. neither Socket.io-node-client nor socket.io's client-test WebSocket are the right choice.Cantata
I don't care about the nuances of "browserless websocket" vs "browserless socket.io", I just want a dead-simple way to communicate between two node.js processes and this seems to fit the bill.Pierian
B
0

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.

Billbillabong answered 1/10, 2010 at 19:55 Comment(4)
Yes, I have node.js server running....I just want to be able to get real-time update on my client through websocket without using browser at client side....basically, I want to have a client daemon process listening on real-time push message from my node.js server... I am using node at client side also...However, I am not sure how to make node process a deamon....node is non-blocking....while I am now asking client to be blocking(while loop on sock.onmessage).Basic
@haijin: responded to you comment in my answerBillbillabong
I found the websock python lib which worked fine for my case..Good. freeasinbeard.org/post/1008785379/…Basic
Yes, I used setInterval to print out received msg. However, after my main node.js program run through and exits, the callback no longer works. Also, periodical callback is more like a poll, not a push solution, which defeats the purpose of push based solution.Basic
K
0

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

Ketubim answered 15/10, 2012 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.