Skip to content

Getting Started with @briklab/slikr

slikr provides a unified API for WebTransport and WebSocket connections with automatic fallback, retries, and keepalive.

What is slikr?

slikr is a real-time communication client that automatically chooses between WebTransport (for modern browsers) and WebSocket (fallback) based on browser support. It handles connection management, retries, and provides a simple event-based API.

ts
import slikr from "@briklab/slikr";

const client = slikr("wss://echo.websocket.org");

await client.connect();

client.on("message", (data) => {
  console.log("Received:", data);
});

await client.send("hello", "world");

Basic Usage

Connecting

ts
import slikr from "@briklab/slikr";

const client = slikr("wss://your-server.com/ws");

// Connect with default settings
await client.connect();

// Or with custom retry settings
await client
  .retry(3)
  .retryDelay(1000)
  .connect();

Sending Messages

ts
// Send JSON data
await client.send("chat", {
  message: "Hello everyone!",
  timestamp: Date.now()
});

// Send binary data
await client.send("file", new Uint8Array([1, 2, 3, 4]));

Receiving Messages

ts
// Listen for specific events
client.on("chat", (data) => {
  console.log("Chat message:", data.message);
});

// Listen for all events
client.on((eventName, payload) => {
  console.log(`Event ${eventName}:`, payload);
});

Waiting for Specific Messages

ts
// Wait for a response with timeout
try {
  const response = await client.receive("response", 5000);
  console.log("Got response:", response);
} catch (error) {
  console.log("Timeout waiting for response");
}

Connection Status

ts
console.log(client.status);
// Output: { url: "wss://your-server.com/ws", type: "WebSocket" }

Keepalive

slikr automatically sends keepalive pings to maintain connections:

ts
// Enable keepalive with 5 second intervals
client.keepalive(true).keepalive(5000);

Live Demo

Console
No logs yet.

Transport Selection

slikr automatically detects browser support:

  • WebTransport: Used when available (modern browsers with datagram support)
  • WebSocket: Fallback for older browsers or when WebTransport isn't supported

Features

  • Automatic Fallback: WebTransport with WebSocket fallback
  • Retry Logic: Configurable connection retries with backoff
  • Keepalive: Automatic heartbeat to maintain connections
  • Event System: Simple pub/sub messaging
  • TypeScript: Full type safety
  • Binary Support: Send/receive binary data

Next Steps

Learn more about slikr: