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 🙏

© 2026 – Pkg Stats / Ryan Hefner

wsps

v1.0.0

Published

WSPS client for JavaScript

Readme

WSPS JavaScript client

This is a JavaScript client for WSPS server. It should work equally well in a (modern) browser or Node.js.

WSPS stands for WebSocket Pub(lisher)-Sub(scriber), and that pretty much covers what it does. The WSPS server provides a WebSocket interface for clients to subscribe to messages on different "channels", and then publishing messages on those channels.

Licensed under MIT and new BSD licenses, more details in LICENSE.md.

Supported environments

WSPS uses WebSocket (RFC 6455) and this should work on anything supporting them. Any modern browser should be supported, but there is a notable exception: Android browser.

If you are planning on supporting Android browser pre 4.4, you might have some issues. However, if you are using Cordova you can install CrossWalk which has modern WebSocket support.

For browsers this is standalone and has no dependencies other than if you want to further develop wsps-javascript itself.

The library also works on Node.js, but it uses the ws -library for WebSocket support.

For WSS (SSL-protected WebSocket) you should terminate SSL with something like Nginx.

Performance

Using the bundled latency.html -test I've managed to get about 3,000 messages/sec bounced through the WSPS server with the client, with an average latency of 0.3msec over localhost using Chrome 42 with a i7-4930k CPU running Windows 7.

Using 20 tabs on listener.html and 5 tabs on sender.html I managed to get a total throughput of 1,000 messages in and 10,000 messages out per second.

Keep in mind these are over localhost, real world performance will be different but the numbers show the overhead should be fairly low.

Usage

Example

This example assumes the WSPS server is running at localhost:52525.

The server local_settings.py should contain the following:

LISTEN_PORT = 52525

SUBSCRIBE_KEYS = {
    r"some-channel": "subscribe-key"
}

PUBLISH_KEYS = {
    r"some-channel": "publish-key"
}

AUTHORIZATION_MANAGER = 'wspsserver.auth.settings'

After that, this code should work in a browser with WSPS script loaded:

var subscribeKey = "subscribe-key";
var writeKey = "publish-key";

var wsps = WSPS.create({
    server: "ws://localhost:52525",
    onclose: function() {
        console.log("Disconnected, reconnecting...");
        wsps.connect();
    }
});

wsps.connect();

var receive = function(packet) {
    console.log(packet.data.msg);
};

var send = function() {
    wsps.send("some-channel", {msg: "Hello, WSPS!"}, writeKey);
};

wsps.listen("some-channel", receive, subscribeKey);
 
setTimeout(send, 2500);

In Node.js you only need to prepend:

var WSPS = require("wsps");

Configuration

The config object you give to WSPS.create can have the following properties:

  • server - Mandatory: The full URI for the server, e.g. ws://localhost:52525/

  • onconnect - Callback to be run when a connection is established

  • onclose - Callback to be run when the connection fails or disconnects for any reason

  • debug - Enable some logging by the library straight to console

API

Here's per-method documentation for the library.

WSPS.create(config)

Creates a new instance of a WSPS connection. You generally can only have one connection per destination address.

Arguments:

  • config - An object containing configuration as per the above section

Returns a configured instance of the WSPS client.

instance.connect()

Attempts to connect to the server, if connection fails the onclose callback is called. If connection is successful the onconnect callback is called.

instance.disconnect()

Disconnects from the server, calls the onclose callback.

instance.publish(channel, data[, key])

Publish a message to a channel.

Arguments:

  • channel - The name of the channel to subscribe to
  • data - The data to send, string, int, bool, object, pretty much whatever.
  • key - Optional: The authorization key for publishing to the channel, if required in WSPS server configuration.

Returns false if that failed due to e.g. connection not being established yet, true if the message was successfully published.

instance.subscribe(channel, callback[, key])

Subscribe to messages from a channel.

Arguments:

  • channel - The name of the channel to subscribe to
  • callback - The callback that should get the packets from the channel. You can have multiple subscribers to the same channel.
  • key - Optional: The authorization key for subscribing to the channel, if required in WSPS server configuration.

Development

Prerequisites

You'll need Node.js and npm available in the environment: https://nodejs.org/, and some modern browser.

Install development dependencies

All the dependencies can be easily installed npm and bower, but you'll need gulp available globally.

npm install -g gulp bower
npm install
bower install

Build and test

The tests expect a WSPS server running with the default settings on localhost.

To lint, concatenate, minify, run tests, and watch for changes just run gulp:

gulp