Dependency-free browser client for DarkWS servers: typed requests, broadcast subscriptions, reconnects with backoff, heartbeats, opt-in retries, and automatic session restoration. ESM with TypeScript declarations.
npm install darkws
import DarkWs, { ErrorResponse } from "darkws";
const client = new DarkWs({
secure: location.protocol === "https:",
path: "/ws",
authenticationToken: () => auth.accessToken(),
}).connect();
const user = await client.request<User>("user:get", { id: "42" });
const unsubscribe = client.onAction<User>("user:updated", updated => render(updated));
try {
await client.request("user:delete", { id: "42" });
} catch (error) {
if (error instanceof ErrorResponse) console.warn(error.message); // the server's error code
}
unsubscribe();
client.dispose();
For an application-wide client created on first use, see DarkWs.lazy().
Documentation
- Browser client: requests, timeouts, retries, events, authentication, lifecycle, options
- Protocol and client lifecycle contract
- Документация на русском