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

ews-client

v1.0.4

Published

An abstraction layer built on top of uWebSockets.js to provide callbacks and authentication for Web Sockets

Downloads

7

Readme

enhancedWebSockets.js Client

You can view the server documentation here.

This is the client for a small abstraction layer built on top of uWebSockets.js that provides simple socket authentication alongside message callbacks.

It provides the same API as WebSocket, with some extended functionality.

You can browse the WebSocket documentation to get an understanding of how to use the client.

You are able to pass an authentication string to the server when using this client.

Example

import { WebSocketClient } from 'ews-client'; // or access it through window.WebSocketClient

// Authentication token is optional, as are the reconnection options. These
// are just the defaults set by the library.
const socket = new WebSocketClient('ws://127.0.0.1:9001'[, someAuthToken[, {
    maxReconnectInterval: 30000,
    maxReconnectAttempts: 0,
    reconnectInterval: 1000,
    timeoutInterval: 2000,
    reconnectDecay: 1.5,
    reconnect: false
}]]);

// Or use socket.onopen = event => {};
socket.on('open', event => {
    console.log(`Socket connection was opened`);
    
    socket.send('someEvent', { someData: true }, (error, data) => {
        if(error)
            console.error(`Received an error reply: ${ error }`);
        else console.log(`Received reply: ${ data }`);
    });
});

// Or use socket.onerror = event => {};
socket.on('error', event => {
    console.log(`Socket emitted an error: ${ event }`);
});

// Or use socket.onclose = event => {};
socket.on('close', event => {
    console.log(`Socket connection was closed: ${ event.reason }`);
});

// Or use socket.onmessage = (event, data) => {};
socket.on('someEvent', data => {
    console.log(`Received socket event 'someEvent', data ${ data }`);
})

Key differences to the WebSocket class

Authentication

This library supports an authentication string being sent with the initial WebSocket upgrade request, by using the sec-websocket-protocol header. By sending the authentication string in this header you prevent it from being leaked in the url.

Pass an authentication string (such as a JWT token) as the second argument and the server will perform authentication. The server can either accept unauthenticated requests or reject the connection, in which case the close event will be fired with closeEvent.code = 4000 and a message in closeEvent.reason.

Automatic reconnection

You can configure the library to automatically reconnect if the socket closes. The defaults are shown below.

{
maxReconnectInterval: 30000, // Maximum delay between reconnection attempts
maxReconnectAttempts: 0, // Maximum reconnection attempts before giving up (0 for infinite)
reconnectInterval: 1000, // Initial delay between reconnection attempts
timeoutInterval: 2000, // Maximum timeout before a connection is considered as failed
reconnectDecay: 1.5, // Rate of increase in the reconnection delay
reconnect: false // If the library should automatically reconnect or not
}

If you are happy with the defaults and just want the library to automatically reconnect for you, set { reconect: true } as the third argument passed to the constructor.

Sending events

Clients can optionally specify a callback function that allows the server to provide a response to individual events. You don't have to send a callback, nor do you have to send data. You can invoke the send function via the following:

socket.send(event[, data[, callback ]]);

If the server does reply to the message, it will be called with the signature callback(error, data).

Receiving events

The WebSocketClient class extends an event emitter, so you can either handle events using socket.onevent = () => {} where event is the event name, or use socket.on('event', (...args) => {}).

Since socket events are just directly broadcast top-level, it's possible the server could mistakenly send close, open, or error events to the client. If this somehow happens the events are instead ignored.