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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@stevvvns/koa-wsapi

v0.0.12

Published

## What? A system for publishing type-safe RPC and pub-sub over a websocket using [Koa](https://github.com/gcanti/io-ts), [Redis](https://github.com/redis/redis) and [io-ts](https://github.com/gcanti/io-ts).

Readme

koa-wsapi

What?

A system for publishing type-safe RPC and pub-sub over a websocket using Koa, Redis and io-ts.

Why?

You want a websocket API and would like declarative checking of client parameters to simplify validation.

How?

Check the example app.

In particular, on the server, make a folder for your actions, and then use this library to attach them with the websocket interface:

import * as t from 'io-ts';

// see the io-ts docs for info about how to make more useful custom types
function isEnthusiasm(input) {
  return [1, 2, 3].includes(input);
}
export const enthusiasm = new t.Type(
  'enthusiasm',
  isEnthusiasm,
  (input, context) =>
    isEnthusiasm(input) ? t.success(input) : t.failure(input, context),
  t.identity,
);

export const arg = t.type({
  name: t.string,
  enthusiasm: enthusiasm
});

export default function hello({ name, enthusiasm }, { greeting, publicKey }) {
  const punc = { 1: '.', 2: '!', 3: '!!!' }[enthusiasm];
  return {
    message:
      `${greeting}, ${name}${punc}` +
      (publicKey
        ? ` (and ${greeting.toLowerCase()}, ${publicKey.toString('base64')}${punc})`
        : ''),
  };
}
const app = new Koa();

await attachActions({
  app,
  path: resolve('actions'),
  // you can omit this, provide an entire console-like object, or provide only the log levels you want to see
  log: { info: console.info, error: console.error },
  // by default this uses msgpack, you can use signed msgpack if it's useful to you to have unique identifiers for each client
  transport: signedMsgpack,
  // set to false to disable pub-sub, omit to use this default
  redis: 'redis://localhost:6379'
});

// put anything you need here that you want available to your actions that you can't/don't want to import separately
app.context.actionsContext = { greeting: 'Hello' };

On the client side, there are two parts, your main thread app:

const api = start({
  // this is the default
  worker: './worker.js',
  log: console,
});

// optional to cache responses or share them between tabs
api.hello.memoize();

and a shared worker:

onconnect = apiWorker({
  // should probably persist the key in localStorage or something to be any use
  transport: signedMsgpack(sign.keyPair()),
  // this is the default
  url: location.origin.replace(/^http/, 'ws'),
  log: console,
});

after which:

await api.hello({ name: 'world', enthusiasm: 2 });
// {"message":"Hello, world! (and hello, 9VEllJ9pbQeaP4dFgffFqxUnv1OVvm8CS3kFCU68ZPQ=!)"}
await api.hello({ enthusiasm: 4 });
// {"error":[
//    "Invalid value undefined supplied to : { name: string, enthusiasm: enthusiasm }/name: string",
//    "Invalid value 4 supplied to : { name: string, enthusiasm: enthusiasm }/enthusiasm: enthusiasm"
// ]}

pub-sub

Consider an action, tick.js:

import * as t from 'io-ts';

export const arg = t.type({});

let interval;
export default function tick(_, { channels, sock }) {
  if (!interval) {
    let ticks = 0;
    interval = setInterval(() => channels.pub('tick', { ticks: ++ticks }), 1000);
  }
  channels.connectSocket('tick', sock);
}

The client can now call:

const [result, unsubscribe] = api.tick.subscribe({}, console.log)

Which will log incrementing { ticks: N } every second until unsubscribe is called.