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

@open-ot/transport-websocket

v0.3.0

Published

WebSocket transport adapter for OpenOT. Provides low-latency, bidirectional real-time communication for collaborative applications.

Readme

@open-ot/transport-websocket

WebSocket transport adapter for OpenOT. Provides low-latency, bidirectional real-time communication for collaborative applications.

Installation

npm install @open-ot/transport-websocket ws

Overview

This package provides a TransportAdapter implementation using WebSockets for both sending and receiving operations. It's ideal for:

  • Traditional server environments (Node.js, VPS, Docker)
  • Applications requiring the lowest possible latency
  • Environments where long-lived connections are supported

Usage

Client-Side

import { WebSocketTransport } from "@open-ot/transport-websocket";
import { OTClient } from "@open-ot/client";
import { TextType } from "@open-ot/core";

const transport = new WebSocketTransport("ws://localhost:3000");

const client = new OTClient({
  type: TextType,
  initialSnapshot: "",
  initialRevision: 0,
  transport: transport,
});

// The client automatically connects and syncs

Server-Side (Node.js)

import { WebSocketServer } from "ws";
import { Server, MemoryBackend } from "@open-ot/server";
import { TextType } from "@open-ot/core";

const backend = new MemoryBackend();
const otServer = new Server(backend);
otServer.registerType(TextType);

await backend.createDocument("doc-1", "text", "");

const wss = new WebSocketServer({ port: 3000 });

wss.on("connection", (ws) => {
  ws.on("message", async (data) => {
    const msg = JSON.parse(data.toString());

    if (msg.type === "op") {
      const result = await otServer.submitOperation(
        "doc-1",
        msg.op,
        msg.revision
      );

      // Acknowledge sender
      ws.send(JSON.stringify({ type: "ack" }));

      // Broadcast to others
      const update = JSON.stringify({
        type: "op",
        op: result.op,
        revision: result.revision,
      });

      wss.clients.forEach((client) => {
        if (client !== ws && client.readyState === 1) {
          client.send(update);
        }
      });
    }
  });
});

API Reference

WebSocketTransport

Constructor

new WebSocketTransport(url: string)

Parameters:

  • url: WebSocket server URL (e.g., "ws://localhost:3000" or "wss://api.example.com")

Methods

connect(onReceive: (msg: unknown) => void): Promise<void>

Establishes the WebSocket connection and starts listening for messages.

await transport.connect((message) => {
  console.log("Received:", message);
});

Returns: Promise that resolves when the connection is established.

send(msg: unknown): Promise<void>

Sends a message to the server.

await transport.send({
  type: "op",
  op: [{ i: "Hello" }],
  revision: 0
});

Throws: Error if the transport is disconnected.

disconnect(): Promise<void>

Closes the WebSocket connection.

await transport.disconnect();

Message Protocol

Messages are JSON-encoded and follow this format:

Client → Server

{
  "type": "op",
  "op": [{ "i": "Hello" }],
  "revision": 5
}

Server → Client

{
  "type": "ack"
}
{
  "type": "op",
  "op": [{ "i": "World" }],
  "revision": 6
}

Connection Lifecycle

const transport = new WebSocketTransport("ws://localhost:3000");

// 1. Connect
await transport.connect((msg) => {
  console.log("Message:", msg);
});

// 2. Send/Receive
await transport.send({ type: "op", ... });

// 3. Disconnect (cleanup)
await transport.disconnect();

Error Handling

The transport handles connection errors and reconnection attempts:

try {
  await transport.connect(onReceive);
} catch (err) {
  console.error("Failed to connect:", err);
  // Implement retry logic
}

Production Deployment

Secure WebSockets (WSS)

Always use wss:// in production for encrypted connections:

const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const url = `${protocol}//${window.location.host}`;
const transport = new WebSocketTransport(url);

Load Balancing

WebSocket connections are stateful. Use sticky sessions or Redis Pub/Sub for multi-instance deployments:

import Redis from "ioredis";

const redis = new Redis();
const redisSub = new Redis();

redisSub.subscribe("ot:updates");

redisSub.on("message", (channel, message) => {
  wss.clients.forEach((client) => {
    if (client.readyState === 1) {
      client.send(message);
    }
  });
});

// When processing an operation
const update = JSON.stringify({ type: "op", ... });
await redis.publish("ot:updates", update);

Heartbeat / Ping-Pong

Prevent connection timeouts with periodic pings:

wss.on("connection", (ws) => {
  const interval = setInterval(() => {
    if (ws.readyState === 1) {
      ws.ping();
    }
  }, 30000);

  ws.on("close", () => {
    clearInterval(interval);
  });
});

Next.js Integration

WebSockets require a custom server in Next.js:

// server.ts
import { createServer } from "http";
import { parse } from "url";
import next from "next";
import { WebSocketServer } from "ws";

const app = next({ dev: process.env.NODE_ENV !== "production" });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = createServer((req, res) => {
    const parsedUrl = parse(req.url!, true);
    handle(req, res, parsedUrl);
  });

  const wss = new WebSocketServer({ server });
  
  // ... WebSocket logic

  server.listen(3000);
});

See the Next.js + WebSocket Integration Guide for a complete example.

Browser Compatibility

WebSockets are supported in all modern browsers. For Node.js environments, the ws package is required.

Comparison with SSE

| Feature | WebSocket | SSE | |---------|-----------|-----| | Bidirectional | ✅ Yes | ❌ No (client → server via HTTP) | | Latency | Lower | Slightly higher | | Serverless | ❌ Requires long-lived process | ✅ Works on Vercel/Lambda | | Browser Support | Excellent | Excellent | | Complexity | Moderate | Low |

Use WebSocket when:

  • You need the lowest latency
  • You have a traditional server environment
  • You need bidirectional streaming

Use SSE when:

  • Deploying to serverless platforms
  • Simplicity is preferred
  • Slightly higher latency is acceptable

See Also