Skip to content

Slikr

A WebTransport/WebSocket client for real-time communication with automatic fallback and keepalive support.

Constructor

constructor(url: string, t?:"WebTransport"|"WebSocket")

Creates a new Slikr client instance.

ParamTypeDescription
urlstringThe WebSocket/WebTransport URL to connect to.
t"WebSocket"|"WebTransport"Whether to use WebTransport or WebSocket. Automatically chosen if not given

Example:

ts
const client = new Slikr("wss://example.com/ws");
// or
import slikr from "@briklab/slikr";
const client = slikr("wss://example.com/ws");

Live Demo

Console
No logs yet.

Properties

status:

Returns connection metadata.

Returns: Object containing the target URL and detected transport type.

Methods

keepalive(boolean: boolean): this

keepalive(time: number): this

Configures keepalive behavior.

Overloads:

  • keepalive(boolean): Enables/disables automatic keepalive pings.
  • keepalive(time): Sets the keepalive interval in milliseconds.
ParamTypeDescription
booleanbooleantrue to enable keepalive, false to disable.
timenumberInterval between keepalive pings in milliseconds.

Returns: The current Slikr instance for chaining.

Example:

ts
client.keepalive(true).keepalive(3000); // Enable with 3s interval

onRetryTimeout(fn: (retryNum?: number) => any): Slikr

Registers a callback fired when an individual retry attempt times out.

ParamTypeDescription
fn(retryNum?: number) => anyCallback receiving the retry index.

Returns: The current Slikr instance for chaining.

onTotalTimeout(fn: Function): Slikr

Registers a callback fired when total connection timeout is reached.

ParamTypeDescription
fnFunctionCallback invoked on total timeout.

Returns: The current Slikr instance for chaining.

onRetry(fn: (retryNum?: number) => any): Slikr

Registers a callback fired before each retry delay.

ParamTypeDescription
fn(retryNum?: number) => anyCallback receiving the retry index.

Returns: The current Slikr instance for chaining.

retryDelay(num: number): Slikr

Sets the base delay between retries in milliseconds.

ParamTypeDescription
numnumberRetry delay in milliseconds.

Returns: The current Slikr instance for chaining.

retryDelayIncreaseFn(fn: (current: number) => number): Slikr

Sets a function used to compute the next retry delay.

ParamTypeDescription
fn(current: number) => numberFunction receiving current delay and returning the next delay.

Returns: The current Slikr instance for chaining.

retryTimeout(num: number): Slikr

Sets the timeout for each individual retry attempt.

ParamTypeDescription
numnumberRetry timeout in milliseconds.

Returns: The current Slikr instance for chaining.

retry(num: number): Slikr

Sets the maximum number of retries after the initial connection attempt.

ParamTypeDescription
numnumberMaximum retry count.

Returns: The current Slikr instance for chaining.

totalTimeout(num: number): Slikr

Sets the global timeout budget for the entire connection process.

ParamTypeDescription
numnumberTotal timeout in milliseconds.

Returns: The current Slikr instance for chaining.

async connect(options?: { ... }): Promise>Slikr<

Connects to the specified URL with optional retry configuration.

ParamTypeDescription
optionsObjectOptional connection options.

Options:

  • retry.number: Maximum retry count
  • retry.delay.number: Base retry delay
  • retry.delay.increaseFn: Function to increase delay
  • retry.onRetry: Callback before retry
  • retry.timeout.time: Per-retry timeout
  • retry.timeout.onTimeout: Timeout callback
  • timeout.time: Total timeout
  • timeout.onTimeout: Total timeout callback

Returns: Promise resolving to the connected Slikr instance.

Example:

ts
await client
  .retry(3)
  .retryDelay(1000)
  .connect({
    retry: { number: 5, delay: { number: 2000, increaseFn: d => d * 2 } }
  });

on(name: string, callback: Function): Slikr

on(callback: Function): Slikr

Subscribes to events.

Overloads:

  • on(name, callback): Subscribe to a named event
  • on(callback): Subscribe to all events (any)
ParamTypeDescription
namestringEvent name to subscribe to.
callbackFunctionHandler function.

Returns: The current Slikr instance for chaining.

Special Events:

  • any: Fires for every incoming event
  • close: Connection closed
  • on: Connection opened
  • open: Connection opened

Example:

ts
client.on("message", (data) => console.log(data));
client.on((eventName, payload) => console.log(eventName, payload)); // any

listen = this.on

Alias for on().

async send(name: string, data: any): Promise>Slikr<

Sends a named event with payload data.

ParamTypeDescription
namestringEvent name.
dataanyPayload data (JSON or binary).

Returns: Promise resolving to the current Slikr instance.

Example:

ts
await client.send("chat", { message: "Hello!", user: "Alice" });

async disconnect(): Promise>Slikr<

Stops keepalive and closes the connection.

Returns: Promise resolving to the current Slikr instance.

async receive(name: string, timeout?: number): Promise>any<

Waits for the next payload of a given event name.

ParamTypeDescription
namestringEvent name to receive.
timeoutnumberOptional timeout in milliseconds (0 = no timeout).

Returns: Promise resolving with the event payload.

Throws: Slikr.Error on timeout.

Example:

ts
const data = await client.receive("response", 5000);