UNPKG

@ws-kit/bun

Version:

Bun platform adapter for WS-Kit leveraging native WebSocket API with built-in pub/sub and low-latency message routing

73 lines 3.19 kB
import type { PubSubAdapter, PublishEnvelope, PublishOptions, PublishResult } from "@ws-kit/core"; import type { Server } from "bun"; /** * Bun-native Pub/Sub adapter leveraging server.publish(). * * Uses Bun's event-loop integrated broadcasting with zero-copy semantics. * All WebSocket connections that have called `ws.subscribe(topic)` will * receive messages published via this adapter. * * **Performance characteristics**: * - Zero-copy: Messages are broadcast directly without serialization overhead * - In-process: All subscribers must be in the same Bun instance * - Synchronous: Messages are delivered immediately to all subscribers * * **Scope**: Messages published to a topic are received by ALL WebSocket * connections in this Bun process that have subscribed to that topic. * For multi-process deployments (load-balanced cluster), each instance has * its own scope—use RedisPubSubAdapter for cross-process broadcasting. * * **Note**: For public API, use `bunPubSub()` factory function instead. * * @internal Use `bunPubSub(server)` factory for creating instances. */ export declare class BunPubSub implements PubSubAdapter { private server; constructor(server: Server<any>); /** * Publish a message to a topic. * * All WebSocket connections subscribed to this topic (via ws.subscribe) * will receive the message immediately. * * Note: Bun's native pub/sub is too primitive to support excludeSelf or * track subscriber counts. Both are rejected with UNSUPPORTED errors. * Capability is always "unknown" (can't enumerate subscribers). * * @param envelope - Validated message with topic, payload, type, meta * @param options - Publish options (partitionKey ignored, excludeSelf unsupported) */ publish(envelope: PublishEnvelope, options?: PublishOptions): Promise<PublishResult>; /** * Subscribe a client to a topic. * * **Note**: Bun's pub/sub is connection-based; subscriptions happen via * `ws.subscribe(topic)` on the WebSocket. This is a no-op since actual * subscription management is handled per-connection by the platform. * * @param clientId - Client identifier (unused; Bun handles per-connection) * @param topic - Topic name */ subscribe(clientId: string, topic: string): Promise<void>; /** * Unsubscribe a client from a topic. * * **Note**: Like subscribe, this is a no-op for Bun as subscriptions are * connection-based and managed via ws.unsubscribe(topic). * * @param clientId - Client identifier (unused) * @param topic - Topic name (unused) */ unsubscribe(clientId: string, topic: string): Promise<void>; /** * Get local subscribers for a topic. * * **Note**: Bun's pub/sub doesn't expose subscriber tracking. This always * returns an empty async iterable since we cannot enumerate subscribers. * The router will attempt delivery to all connections separately. * * @param topic - Topic name (unused; no subscriber tracking) */ getSubscribers(topic: string): AsyncIterable<string>; } //# sourceMappingURL=pubsub.d.ts.map