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

@sandtreader/collab

v0.1.3

Published

Model-agnostic real-time collaboration: entity-document protocol, sequenced WebSocket relay, client, and React session UI

Readme

@sandtreader/collab

Model-agnostic real-time collaboration for browser apps. Extracted from SynFlo and generalised so that any app whose document is a set of entities with properties can share it.

  • Entity document. A flat map of { kind, props } by ID. No graph, no schema: the app decides what the kinds and props mean.
  • Sequenced relay. A stateless WebSocket server that stamps every change with a per-session sequence number and echoes it to everyone, including the sender. One total order per session, so concurrent edits converge.
  • Framework-free client. Join handshake, snapshots, reconnect with backoff, presence, and the optimistic-edit logic. Talks to your model through a three-method adapter.
  • React session UI. Drop-in panel for display name, join by ID, share link and QR code, member list; plus remote cursors.

Install

pnpm add @sandtreader/collab

The server entry needs ws. The React entry needs react, @mui/material, @mui/icons-material, @emotion/react, @emotion/styled, qrcode.react and bad-words. All are optional peer dependencies: install only what you use.

Entries

| Import | Contents | |---|---| | @sandtreader/collab/protocol | Types, applyChange and friends. No dependencies. | | @sandtreader/collab/client | CollabClient. No dependencies. | | @sandtreader/collab/server | createRelay. Needs ws. | | @sandtreader/collab/react | SessionPanel, RemoteCursors, useCollab, identity and session-ID helpers. |

The protocol

type Change =
  | { type: "entity_added";   id: string; kind: string; props: Props }
  | { type: "entity_removed"; id: string }
  | { type: "props_changed";  id: string; props: Props;        // shallow merge; null deletes
      gestureStart?: boolean; gestureEnd?: boolean; transient?: boolean }
  | { type: "batch";          changes: Change[] }               // one unit, e.g. an undo group
  | { type: "state_replaced"; state: DocState };                // "open file" while connected

Convergence rules, implemented once in applyChange and relied on by every client:

  • entity_added on an existing ID replaces the entity.
  • props_changed or entity_removed on an unknown ID is ignored.
  • props_changed is a shallow merge; a null value deletes the key.
  • batch applies in order. state_replaced discards everything.

Presence (cursor in model coordinates, selection as entity IDs) is broadcast but never sequenced or stored.

Using the client

Your app keeps one place where every mutation goes, local or remote. A local mutation applies itself, then calls client.send(change). Remote changes come back through the adapter, wrapped in suppress() so they don't echo.

import { CollabClient } from "@sandtreader/collab/client";
import { applyChange, type DocState } from "@sandtreader/collab/protocol";

let doc: DocState = { entities: {} };

const client = new CollabClient({
  app: "myapp",
  userId, displayName,
  document: {
    getSnapshot: () => doc,
    applySnapshot: (s) => { doc = s; render(); },
    applyChange: (c) => { doc = applyChange(doc, c); render(); },
  },
});

function setWidth(id: string, width: number) {
  const change = { type: "props_changed", id, props: { width } } as const;
  doc = applyChange(doc, change);
  render();
  client.send(change);          // no-op while suppressed or disconnected
}

client.connect("abc-defg-hij", "wss://relay.example.com");

The first member's local document becomes the session state. Later joiners receive a snapshot from an existing member, with any changes that arrived during the handshake replayed in order.

Throttle continuous edits yourself (drags at ~50 ms, presence at ~100 ms) and flush the final value with gestureEnd: true.

How conflicts resolve

Local edits are applied optimistically and tracked until the server echoes them. While a local edit to (entity, prop) is pending, a remote edit to the same pair is ignored; when the echo arrives, the server order is authoritative and every client has the same value. Independent props never conflict.

Running the relay

PORT=2901 npx collab-relay

or in code:

import { createRelay } from "@sandtreader/collab/server";
const relay = createRelay({ port: 2901, log: console.log });

The client connects to <serverUrl>?app=<app>. Sessions are keyed by app and session ID, so one relay serves several apps without cross-joining. The query parameter survives any reverse-proxy path mapping; the relay also accepts the app as the last path segment.

The relay keeps no document state. Phase 2 (planned) has the server apply changes itself using the same applyChange, answer snapshots directly, and persist sessions.

React

import { SessionPanel, RemoteCursors, useCollab, autoJoinFromUrl,
         getOrCreateUserId, getOrCreateDisplayName } from "@sandtreader/collab/react";

const config = { serverUrl: "wss://relay.example.com", sessionUrlPrefix: "https://myapp.example/s/",
                 storageKeyPrefix: "myapp" };

<SessionPanel client={client} config={config} />

const { presence } = useCollab(client);
<RemoteCursors presence={presence} toScreen={viewport.modelToScreen} />

Development

pnpm install
pnpm test          # protocol unit tests, client tests on a fake socket, end-to-end against the real relay
pnpm build