Skip to content

Adapter: Realtime Channel

IRealtimeChannel is a protocol-agnostic realtime connection handle. RealtimeFactory is the function shape that creates one, swappable globally or per call.

The interfaces

TypeScript
interface IRealtimeChannel {
send(data: string | ArrayBuffer | Blob): void;
close(code?: number, reason?: string): void;
on(event: 'open' | 'message' | 'close' | 'error', fn: (data?: unknown) => void): void;
off(event: 'open' | 'message' | 'close' | 'error', fn: (data?: unknown) => void): void;
readonly readyState: 'connecting' | 'open' | 'closing' | 'closed';
}

type RealtimeFactory = (url: string, opts?: RealtimeFactoryOptions) => IRealtimeChannel;

RealtimeFactoryOptions: protocols (WebSocket sub-protocol strings), reconnect (auto-reconnect on unexpected close), baseDelayMs / maxDelayMs (back-off window, default 1000/30000), and factory (a per-call override, one connection using a different transport without changing the global default).

Rules & restrictions

  • send() should only be called when readyState === 'open', the underlying transport throws otherwise, the port doesn’t buffer or queue for you.
  • Resolution order for which factory actually runs: a per-call opts.factory first, then the player’s configured websocketFactory, then the built-in nativeWebSocketAdapter as the final fallback.

Default implementation

nativeWebSocketAdapter wraps the browser’s native WebSocket, with the reconnect/back-off behavior described above layered on top.

How to extend it

Global swap, for every realtime connection the player opens:

TypeScript
import nmplayer from '@nomercy-entertainment/nomercy-video-player'; // nomercy-music-player's factory works identically

const player = nmplayer('player');

player.setup({ websocketFactory: signalRFactory });

Per-call override, inside a plugin, for one connection using a different transport:

TypeScript
this.websocket(url, { factory: socketIoFactory });

Next steps

  • Reference: full signatures and types for the composition primitives, config, events, and errors this tour and these reference pages have been building on.