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

wsapi-js

v1.0.0

Published

An abstraction to add RESTful functionality to websockets, with PID / timestamp information, request timeout and schema.

Downloads

3

Readme

WSAPI

An abstraction to add RESTful functionality to WebSockets API, to create request responses.

This is client-side only.

Your WebSockets server should implement the logic for WSAPI, with an example given in test/server.js.

Server logic is pretty much: return the same timestamp.

Methods

Functions of WSAPI are intended to update a JSON tree:

import { WSAPI } from 'wsapi'

const ws = new WSAPI()
await ws.open('ws://localhost:9092')

// Equivalent to GET (get)
// =======================

ws.get('/object') // get a structure

// Equivalent to POST (add)
// ========================

ws.add('/object/list', { foo: 'bar' }) // add an element of an object or array

// Equivalent to PUT (set)
// =======================

ws.set('/object/list', [ { foo: 'bar' } ] ) // set an entire structure

// Equivalent to PATCH (update)
// ============================

ws.update('/object/list/42', { hello: 'world' } ) // update properties of a structure

// Equivalent to DELETE (remove)
// =============================

ws.remove('/object/list/42') // remove an element of an object or array

This functionality should be implemented on your server!

Configuration

Options can be set globally as well as on each request:


import { WSAPI, LOG_VERBOSE, LOG_SILENT, LOG_DEBUG } from 'wsapi'

// GLOBAL OPTIONS
// ==============

const ws = new WSAPI({
	timeout: 3000, // default is 3000ms, falsey argument means no response is expected,
	pid: 1, // default is 1, any integer is accepted,
	log: LOG_VERBOSE // default is LOG_DEBUG
})

ws.open('ws://localhost:9092')

// REQUEST OPTIONS
// ===============

const res = await ws.get( '/hello-world', { foo: 'bar' }, {
	timeout: 1000, // overrides global option
	pid: 3, // overrides global option
	customValue:  1234 // send custom request value
})

Schema & Timestamps

An outgoing WebSockets message will always be formatted and stringified JSON like so:

{
	"type": "set",
	"pid": 1,
	"path": "/entries/47",
	"timestamp": 1647466021863.3, // important
	"data": { "foo": "bar" }
}

Server logic must return this same object in order for requests to resolve using the timestamp:

{
	"type": "set",
	"pid": 1,
	"path": "/entries/47",
	"timestamp": 1647466021863.3, // important
	"data": "I received this, thanks!"
}

Unique Timestamps

The timestamp is used to resolve requests when returned from the WebSockets server, so will always be unique.

A floating point of 0.1 is added to the integer timestamp if it is one of multiple requests:


// If I were to make multiple requests at the same time:

GET /helloworld @ 1647466021863 // 1st
GET /helloworld @ 1647466021863 -> 1647466021863.1 // 2nd
GET /helloworld @ 1647466021863 -> 1647466021863.2 // 3rd
GET /helloworld @ 1647466021863 -> 1647466021863.3 // 4th

Development & Testing

# install basic clientside server
pnpm i --global simple-autoreload-server

# run both client and server
pnpm run client
pnpm run server