npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wska

v1.0.3

Published

Keep alive client layer on top of ws WebSocket to keep connections open and/or reconnect failed connections.

Downloads

19

Readme

wska

Class: WebSocketKeepAlive

The WebSocketKeepAlive class is used as a client layer on top of the ws WebSocket class to maintain a client connection to a server. The keep alive relies on the ping/pong frames defined in RFC6455 and implemented in the ws WebSocket and ws WebSocket.Server classes.

The class extends the EventEmitter class to provide events that will inform the client application off keep alive events such as the 'open' of a new client socket.

new WebSocketKeepAlive(address[, protocols][, options][, settings])

Arguments passed to the constructor of the WebSocketKeepAlive class are the same as the ws WebSocket class with one additional 'settings' argument as optional settings for the WebSocketKeepAlive class. Note that the optional settings argument must be prefixed with the optional WebSocket arguments even if they are null. I.E.

var wska = new WebSocketKeepAlive('ws://localhost:8080', null, null, { pingMessage: 'myApp' });

settings

  • heartbeatInterval {Number} The number of milliseconds between each heartbeat transmission.
  • heartbeatTimeout {Number} The number of milliseconds before a heartbeat will timeout and fail.
  • reconnectInterval {Number} The number of milliseconds of delay before a reconnection attempt will be made.
  • pingMessage {String} A message that will be included with the ping frame sent for a heartbeat.

Events

WebSocketKeepAlive uses events to communicate with the client application. It is important to make a distinction between events that are related to the WebSocketKeepAlive instance and the events related to the WebSocket instance that is making the connection to the server.

Event: 'open'

  • ws {WebSocket} Passes the WebSocket instance when the connection opens successfully.

The 'open' event from the WebSocketKeepAlive instance is related to the 'open' event from the WebSocket instance. When a successful connection to the server is established the WebSocketKeepAlive instance will use the 'open' event to inform the client application and provide the underlying WebSocket instance that has made the connection.

Note that an 'open' event will occur each time there is a connection. If the current connection fails and WebSocketKeepAlive creates a new connection then a new 'open' event will be generated and will provide a new WebSocket instance.

// create the keep alive instance that connects to a local server
var wska = new WebSocketKeepAlive('ws://localhost:8080');

// watch for a successful connection to the server
wska.on('open', function (ws) {
  /**
   * Note how the event handlers in here are for the WebSocket connected to the server.
   * While the events outside this scope are event handlers for the keep alive instance.
   */
  // watch for messages on the new WebSocket connection
  ws.on('message', function (msg) {
    console.log('NEW MSG ', msg.toString());
  });

  // watch for a close of the new WebSocket connection
  ws.on('close', function () {
    console.log('CONNECTION CLOSED, keep alive should create a new one momentarily.');
  });
});

// watch for errors in keep alive
wska.on('error', function (err) {
  console.log('KEEP ALIVE ERROR ', err.toString());
});

Event: 'error'

  • error {Object} The error object.

Errors emitted by the keep alive instance may be related to the keep alive process or they may be related to the current WebSocket instance.

Event: 'warning'

  • warning {Object} An error type object that is at a warning level.

The keep alive instance will intercept some connection errors and attempt to re-establish the connection. These will be emitted as warnings as the keep alive process will try to recover.

Methods

After a WebSocketKeepAlive instance is created it will include a few methods for control of the connection state.

WebSocketKeepAlive.setProtocols(protocols)

Set new websocket protocols value. Used to modify the protocol headers sent when a reconnect happens, I.E. if an access token has changed since the last connection.

WebSocketKeepAlive.setOptions(options)

Set new websocket options.

WebSocketKeepAlive.getWebSocket()

While the 'open' event will provide an instance of the current WebSocket that is connected to the server it is also possible to request the WebSocket instance from the keep alive process. Note that this will be the currently active WebSocket instance but may become invalid if the WebSocketKeepAlive instance needs to create a new WebScoket to re-establish a broken connection. The 'open' event is the preferred method of acquiring the currently valid WebSocket.

WebSocketKeepAlive.setReconnect(reconnect)

  • reconnect {Boolean} The reconnection state to use in the WebSocketKeepAlive instance.

The default state of a WebSocketKeepAlive instance is to attempt reconnections if a server connection fails. This state can be changed with the setReconnect() method.

WebSocketKeepAlive.prototype.KillWebSocket()

A currently active WebSocket and the reconnection process can be terminated by calling the killWebSocket() method. This method will disable the reconnect state and close the currently open WebSocket.