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

ws-messaging

v2.0.0

Published

A minimalistic abstraction layer for websockets.

Downloads

24

Readme

ws-messaging

NPM Version Build Status Appveyor status Coverage Status Dependency Status JavaScript Style Guide

Just a really thin abstraction layer on top of WebSocket for Node.js and Web Browsers with Promise and EventEmitter based APIs.

Features

  • Send and receive notifications (events) via an EventEmitter API.

  • Request-reply API using promises (works in both directions, without any connection blocking during processing).

  • Built-in auth via WebSocket messages exchange (no more query strings).

  • Auto reconnection is provided.

  • Binary messages support via custom encoders/decoders.

  • Reasonable client size: around 14KB minified, 5KB gziped.

Table of Contents

Background

Read this article for more background information.

Installation

npm i ws-messaging

Usage

On a server:

const Server = require('ws-messaging')

const port = 8000

function connectionHook (client, authData) {
  // check an authData
  // then assign client events handlers
  // return a promise
}

let server = new Server({port}, {connectionHook})

On a client:

const Client = require('ws-messaging/client')

const url = `ws://${HOST}:${PORT}`
const auth = { /* will be authData in connectionHook */ }

let client = new Client(url, {auth})

client.on('someEvent', (...data) => { /* do smth */ })

client.register('someMethod', (...args) => { /* do smth, return a promise */ })

client.on('connect', () => {
  /* now this client can send messages */
  client.send('myEvent', ...someData)
  /* or use request-reply (RPC) API */
  client.invoke('myMethod', ...someArgs)
    .then(result => { /* do smth */ })
    .catch(error => { /* do smth */ })
})

Essentially there are two usage patterns that are working in both directions. Fire and forget via send/on, and RPC-style via invoke/register. Unlike on, only a single handler function can be registered per a method name.

API

Server API and Client API documentation is available online.

Network format description

This section describes what data is actually passed to an encoder.

There are only two types of messages. The first one is for normal messages:

{
  name: string,
  args: Array,
  id?: number
}

An id field is present for invoke calls. The second one is for ack (replies for invoke calls) messages:

{
  id: number
  result?: Object
  error?: Object
}

Either a result or an error field is included. Note that an error is the value returned by an errorFormatter, by default String is used as an errorFormatter.

Data validation

All incoming data must be validated on a server side, including errors that are passed to a catch callback. By default only the network format itself is validated. Validation can be made by a custom decoder (useful when a decoder is already using some scheme) or via a receiveHook, or inside a handler itself (useful for registered procedures). When validation is done inside decoder/receiveHook, just throw an error or reject a promise to fail a validation and prevent handlers execution. Also note that a promise returned by invoke can be rejected locally either with Client.ConnectionError or with Client.TimeoutError.

Contribute

If you encounter a bug in this package, please submit a bug report to github repo issues.

PRs are also accepted.

License

MIT