Skip to content

About

@coderbuzz/velox-ws-wire-server

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

Repository files navigation

Velox Wire Server: @coderbuzz/velox-ws-wire-server

Server-side Wire Protocol handler for @coderbuzz/velox. Mount binary WebSocket handling on any route. AI agents: see AI_KNOWLEDGE.md for expert context.

npm version npm downloads MIT License GitHub Stars CI Codecov

Wire Server provides wireProtocol(), a Velox middleware that handles the binary WebSocket Wire Protocol transparently. Authentication, heartbeat, pub/sub, request-response correlation are all managed internally. Your handler receives only domain messages.


Client counterpart: use with @coderbuzz/velox-ws-wire-client for end-to-end binary WebSocket communication, or any client implementing the Wire Protocol via @coderbuzz/velox-ws-wire.


Features

  • Binary protocol: automatic encode/decode of Wire frames
  • Auth: token-based authentication with upgrade or post-connect
  • Heartbeat: automatic ping/pong handling
  • Pub/Sub: SUBSCRIBE/UNSUBSCRIBE/PUBLISH frames map to peer.subscribe(), peer.unsubscribe(), peer.publish()
  • Request-response: a REQUEST frame reaches message as its JSON string payload, and the first peer.send() in that call goes back as a RESPONSE frame with the same correlation ID
  • Velox integration: mounts as a standard app.use() middleware

Installation

npm install @coderbuzz/velox @coderbuzz/velox-ws-wire @coderbuzz/velox-ws-wire-server

Both @coderbuzz/velox and @coderbuzz/velox-ws-wire are required as peer dependencies.


Quick Start

import { AppServer } from "@coderbuzz/velox";
import { wireProtocol } from "@coderbuzz/velox-ws-wire-server";

const app = new AppServer({ port: 3000 });

app.use("/ws", wireProtocol({
  open(peer) { console.log("connected"); },
  message(peer, msg) { peer.send(`echo: ${msg}`); },
  close(peer, code, reason) { console.log("disconnected"); },
}));

await app.run();

With Authentication

app.use("/ws", wireProtocol<{ userId: string }>({
  authenticate: async (token) => {
    try {
      const payload = await verifyJwt(token, "secret"); // throws on an invalid token
      return { userId: String(payload.sub) };
    } catch {
      return null; // null sends AUTH_FAIL and closes with 4001
    }
  },
  open(peer) { console.log("authed:", peer.data.userId); },
  message(peer, msg) {},
}));

API

wireProtocol(config): App

Returns a Velox App instance. Mount with app.use(). Callbacks and WebSocket options go in the same config object (WireDefinition<T>).

Handler callbacks:

Callback Signature Description
upgrade (req) => T | Response | Promise<...> Extract data from upgrade request; return a Response to reject
open (peer) => void Connection opened (after auth if configured)
message (peer, data) => void Required. Domain message received
close (peer, code, reason) => void Connection closed
ping / pong (peer, data) => void WebSocket control-frame ping/pong (not Wire PING frames)
error (peer, error) => void Error occurred
authenticate (token) => T | null | Promise<T | null> Enables auth: AUTH frame or ?token= query param

Options (all but tokenParam passed through to Velox WebSocket; defaults are Velox's):

Option Type Default Description
maxPayloadLength number 16777216 Max message size (bytes)
backpressureLimit number 16777216 Max buffered send bytes
pingInterval number 30 Server ping interval, seconds
pongTimeout number 10 Wait for pong, seconds
idleTimeout number 120 Idle disconnect seconds
perMessageDeflate boolean false Enable compression
tokenParam string 'token' Query param name (only with authenticate)

Peer Methods

peer is Velox's WsPeer<T>; wireProtocol adds no methods.

Method Description
peer.send(data) Send message (inside a REQUEST, the first call is sent as the RESPONSE)
peer.subscribe(topic) / peer.unsubscribe(topic) Join / leave topic
peer.publish(topic, data) Publish to topic subscribers, excluding this peer
peer.isSubscribed(topic) Check topic membership
peer.close(code?, reason?) Close connection
peer.data Data from upgrade or authenticate

License

MIT © 2026 Indra Gunawan

About

@coderbuzz/velox-ws-wire-server

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages