Skip to main content

Connection Setup

Endpoint Configuration

The endpoint field in RealtimeClientConfig defines the broker connection parameters:

const client = new RealtimeClient({
endpoint: {
host: "broker.example.com", // Broker hostname
port: 443, // Broker port
scheme: "wss", // "wss" (secure) or "ws"
path: "/mqtt", // WebSocket path (if required by broker)
clientId: "my-client-123", // Unique client identifier
username: "user", // Auth username
password: "token", // Auth password / token
cleanSession: false, // Broker session persistence
keepAliveSeconds: 15, // Keep-alive interval
},
});

Field Reference

FieldTypeDefaultDescription
hoststringrequiredBroker hostname
portnumberrequiredBroker port
schemestring"wss"WebSocket scheme
pathstringWebSocket path appended to URL
clientIdstringUnique client identifier
usernamestringAuth username
passwordstringAuth password or token
cleanSessionbooleanfalseWhether broker should discard previous session
keepAliveSecondsnumber15MQTT keep-alive interval in seconds

Connection Lifecycle

Idle → Opening → Live → Closing → Idle

Resuming → Live (auto-reconnect)

The SDK handles reconnection automatically. When the connection drops, it transitions to Resuming and attempts to reconnect using mqtt.js's built-in reconnection mechanism. Upon reconnection, all tracked subscriptions are restored.

Connection State

Monitor the connection state using callbacks or observables:

// Callback API
client.onStateChange((state) => {
// LinkState: "Live" | "Opening" | "Closing" | "Idle" | "Resuming"
});

// RxJS API
client.state$.subscribe((state) => {
console.log(state);
});