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.
| Param | Type | Description |
|---|---|---|
url | string | The 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.
| Param | Type | Description |
|---|---|---|
boolean | boolean | true to enable keepalive, false to disable. |
time | number | Interval between keepalive pings in milliseconds. |
Returns: The current Slikr instance for chaining.
Example:
ts
client.keepalive(true).keepalive(3000); // Enable with 3s intervalonRetryTimeout(fn: (retryNum?: number) => any): Slikr
Registers a callback fired when an individual retry attempt times out.
| Param | Type | Description |
|---|---|---|
fn | (retryNum?: number) => any | Callback 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.
| Param | Type | Description |
|---|---|---|
fn | Function | Callback 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.
| Param | Type | Description |
|---|---|---|
fn | (retryNum?: number) => any | Callback receiving the retry index. |
Returns: The current Slikr instance for chaining.
retryDelay(num: number): Slikr
Sets the base delay between retries in milliseconds.
| Param | Type | Description |
|---|---|---|
num | number | Retry 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.
| Param | Type | Description |
|---|---|---|
fn | (current: number) => number | Function 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.
| Param | Type | Description |
|---|---|---|
num | number | Retry 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.
| Param | Type | Description |
|---|---|---|
num | number | Maximum retry count. |
Returns: The current Slikr instance for chaining.
totalTimeout(num: number): Slikr
Sets the global timeout budget for the entire connection process.
| Param | Type | Description |
|---|---|---|
num | number | Total timeout in milliseconds. |
Returns: The current Slikr instance for chaining.
async connect(options?: { ... }): Promise>Slikr<
Connects to the specified URL with optional retry configuration.
| Param | Type | Description |
|---|---|---|
options | Object | Optional connection options. |
Options:
retry.number: Maximum retry countretry.delay.number: Base retry delayretry.delay.increaseFn: Function to increase delayretry.onRetry: Callback before retryretry.timeout.time: Per-retry timeoutretry.timeout.onTimeout: Timeout callbacktimeout.time: Total timeouttimeout.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 eventon(callback): Subscribe to all events (any)
| Param | Type | Description |
|---|---|---|
name | string | Event name to subscribe to. |
callback | Function | Handler function. |
Returns: The current Slikr instance for chaining.
Special Events:
any: Fires for every incoming eventclose: Connection closedon: Connection openedopen: Connection opened
Example:
ts
client.on("message", (data) => console.log(data));
client.on((eventName, payload) => console.log(eventName, payload)); // anylisten = this.on
Alias for on().
async send(name: string, data: any): Promise>Slikr<
Sends a named event with payload data.
| Param | Type | Description |
|---|---|---|
name | string | Event name. |
data | any | Payload 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.
| Param | Type | Description |
|---|---|---|
name | string | Event name to receive. |
timeout | number | Optional 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);