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

@a-type/dialogue

v1.1.1

Published

A small—opinionated—robust—convenient WebSocket wrapper.

Readme

Dialogue

A small—opinionated—robust—convenient WebSocket wrapper.

Features

  • Automatic reconnection with exponential backoff
    • Reconnects on error
    • Reconnects based on heartbeat to detect broken pipes
  • Buffers outgoing messages until connection is ready
  • Parses and validates incoming and outgoing messages to your schema
  • Easily subscribe to specific message types with typechecking: on('chat', (chatMessage) => {})
  • All subscribers return unsubcribers
  • once()
  • Send a "request" message which awaits a response
  • Preprocess outgoing messages

Get started

Configure your Connection with client and server message validators. These validators will automatically apply appropriate types to the Connection's methods.

import { Connection } from '@a-type/dialogue';
import { z } from 'zod';

// define validators for your server and client message protocol
// (you don't have to use Zod - anything that takes in a raw
// JSON message and returns a typed one)

const clientMessageSchema = z.union([
	z.object({ type: z.literal('ping') }),
	z.object({ type: z.literal('chat'), message: z.string() }),
	z.object({ type: z.literal('ack-this'), id: z.string() }),
]);
type ClientMessage = z.infer<typeof clientMessageSchema>;
const serverMessageSchema = z.union([
	z.object({ type: z.literal('pong') }),
	z.object({ type: z.literal('new-chat'), message: z.string() }),
	z.object({ type: z.literal('ack'), ackedId: z.string() }),
]);

// setup a Connection

const connection = new Connection({
	// required: which key indicates the message type?
	messageTypeKey: 'type',
	parseClientMessage: (m) => clientMessageSchema.parse(m),
	parseServerMessage: (m) => serverMessageSchema.parse(m),
	// optional. omit to open connection manually
	openImmediately: true,
	// configure your websocket connection
	websocket: {
		getUrl: async () => {
			// you can fetch a token here, or whatever
			const token = await getToken();
			return `wss://your.app/socket?token=${token}`;
		},
	},
	// optional: setup response handling for request()
	// what indicates a message is meant as a response for another?
	// remember... delivery isn't guaranteed... request() is just
	// a convenience.
	getIsResponse: (clientMsg, serverMsg) => {
		if (clientMsg.type === 'ack-this' && serverMsg.type === 'ack') {
			return serverMsg.ackedId === clientMsg.id;
		}
		return false;
	},
	// optional: apply pre-processing to all outgoing messages, like adding
	// a timestamp or generating an ID. you must manually specify a type
	// for the parameter, which becomes the type used for validating send()
	// and request() automatically.
	preprocessClientMessage: (
		input: OmitMessageProperty<ClientMessage, 'id'>,
	) => {
		if (input.type === 'ack-this') {
			return {
				...input,
				id: Math.random().toString(),
			};
		}
		return input;
	},
	// optional: configure heartbeat
	heartbeat: {
		getPing: () => `{"type":"ping"}`, // this is the default.
		interval: 5000,
		pongTimeout: 2000,
	},
});

Using a Connection

Connecting and disconnecting

connection.open();

connection.close();

If your getUrl configured function throws an error, it will not attempt a reconnect. It's assumed you had a non-retryable failure, like authentication failure. If you have logic like token fetching in getUrl you want to retry, you must handle that yourself.

Sending and subscribing to messages

// send a message. parameter is typechecked and runtime validated.
connection.send({ type: 'chat', message: 'hello world' });

// subscribe to any incoming message with '*'. "message" is typed
// as any server message. cleanup removes the listener.
const cleanup = connection.on('*', (message) => {});

// subscribe to a particular message type. "message" is narrowed
// to that type.
connection.on('ack', (message) => {});

// listen for one matching message, then clean up automatically
connection.once('new-chat', (message) => {});

// send a message and await a response. "response" is decided
// by the 'getIsResponse' configuration passed to the constructor.
// by default any incoming message is considered a response.
const response = await connection.request(
	{
		type: 'ack-this',
		id: 'foo',
	},
	{
		timeout: 5000, // the default. throws if hit.
	},
);

Advanced

Supply an override for WebSocket with config.websocket.environment.WebSocket.

Configure whether the socket should auto-reconnect on clean server close (code 1000) with config.websocket.reconnectOnServerClose.

Configure whether an incoming message is treated as a pong for heartbeats with config.heartbeat.isPong.

Turn off default logging with config.logger: false or pass it your own object which implements the ILogger interface.

When using the default logging, show debug messages by setting DEBUG to dialogue or true in localStorage.

To tune the reconnection backoff settings, see options on config.websocket: initialReconnectDelay, maxReconnectDelay, reconnectDelayFactor, maxReconnectAttempts.

Invalid server messages are ignored by default. To handle them, pass config.onInvalidServerMessage. It receives the raw string message, and the error from either JSON.parse or your server message validator.