Client: Setup & Configuration
MeshClient
manages the WebSocket connection to a MeshServer
. It handles subscriptions, reconnects (with automatic resubscription), ping/pong heartbeat monitoring, round-trip latency tracking, and command dispatching.
Basic usage
import { } from "@mesh-kit/client";
const = new ("ws://localhost:8080");
await .();
const = await .("echo", "Hello!");
.(); // "echo: Hello!"
Configuration options
The second argument to MeshClient
is optional and lets you control reconnect behavior and liveness settings:
import { } from "@mesh-kit/client";
const = new ("ws://localhost:8080", {
: true, // default: true
: 2000, // ms between attempts (default: 2000)
: ,
: 30000, // ms to wait for pings (default: 30000)
: 1, // allowed missed pings before reconnecting
});
💡
These options should match the server’s configured pingInterval
and maxMissedPongs
values to avoid premature disconnects.
Connect / Disconnect
You must call connect()
manually to start the WebSocket connection.
import { } from "@mesh-kit/client";
const = new ("ws://localhost:8080");
await .(); // Connect
await .(); // Disconnect (and disable auto-reconnect)
Events
The client emits events for connection state:
.("message", () => { /* .. */ });
.("connect", () => {
.("Connected");
});
.("close", () => {
.("Connection closed");
});
.("disconnect", () => {
.("Disconnected");
});
.("reconnect", () => {
.("Reconnected");
});
.("reconnectfailed", () => {
.("Gave up trying to reconnect");
});
.("ping", () => {
.("Ping");
});
.("latency", () => {
.(`Latency: ${}ms`);
});
Reconnection behavior
If shouldReconnect
is enabled and a ping is missed:
- The client disconnects
- Mesh attempts to reconnect
- On success, emits
"reconnect"
- On failure (after
maxReconnectAttempts
), emits"reconnectfailed"
Connection status
Check current status via:
import { } from "@mesh-kit/client";
if (. === .ONLINE) {
.("Connected");
}
Command timeout
By default, client.command(...)
times out after 30s. You can override per-call:
const = await .("echo", "hi", 10_000); // 10s timeout
Next steps:
Last updated on