Connection
Monitor the connection lifecycle between your device and RelayX. Register listeners for status changes like connect, disconnect, and reconnect events.
Overview
The connection module provides visibility into the device's connection state. You don't need to manage reconnection yourself — the SDK handles it automatically — but you can react to status changes.
API
- JavaScript
- Python
- C++
device.connection.listeners(callback: (event) => void) → void- callback — Called whenever the connection status changes
- event — An object with a
typeproperty indicating what happened
device.connection.listeners(callback: (event) → None) → None- callback — Called whenever the connection status changes
- event — A dict with a
typekey indicating what happened
device->connection.on_status(callback: relay_status_cb_t) → relay_err_tdevice->connection.is_connected() → bool- callback — Called whenever the connection status changes. Receives a
relay_connection_status_tenum value. - is_connected() — Returns
trueif the device is currently connected - Returns
RELAY_OKon success,RELAY_ERR_DUPLICATE_LISTENERif max listeners reached (RELAY_MAX_STATUS_LISTENERS, default 4)
Status Events
- JavaScript
- Python
- C++
| Event | Description |
|---|---|
connected | Successfully connected to RelayX |
disconnected | Connection closed (only on explicit disconnect() calls) |
reconnecting | Connection lost, SDK is attempting to reconnect |
reconnected | Successfully reconnected after a drop |
auth_failed | Authentication failed — check your API key and secret |
| Event | Description |
|---|---|
connected | Successfully connected to RelayX |
disconnected | Connection closed (only on explicit disconnect() calls) |
reconnecting | Connection lost, SDK is attempting to reconnect |
reconnected | Successfully reconnected after a drop |
auth_failed | Authentication failed — check your API key and secret |
| Status | Description |
|---|---|
RELAY_STATUS_CONNECTED | Initial connection established and authenticated |
RELAY_STATUS_DISCONNECTED | Connection lost |
RELAY_STATUS_RECONNECTING | Reconnection attempts starting |
RELAY_STATUS_RECONNECTED | Successfully reconnected, offline buffer flushed |
Register a Listener
- JavaScript
- Python
- C++
device.connection.listeners((event) => {
console.log("Status:", event.type);
});
def on_status(event):
print(f"Status: {event['type']}")
device.connection.listeners(on_status)
device->connection.on_status([](relay_connection_status_t status) {
ESP_LOGI("app", "Status: %d", status);
});
Handling Each Event
- JavaScript
- Python
- C++
device.connection.listeners((event) => {
switch (event.type) {
case "connected":
console.log("Connected to RelayX");
break;
case "disconnected":
console.log("Disconnected");
break;
case "reconnecting":
console.log("Connection lost, reconnecting...");
break;
case "reconnected":
console.log("Reconnected — buffered messages flushed");
break;
case "auth_failed":
console.error("Authentication failed — check your API key");
break;
}
});
def on_status(event):
match event["type"]:
case "connected":
print("Connected to RelayX")
case "disconnected":
print("Disconnected")
case "reconnecting":
print("Connection lost, reconnecting...")
case "reconnected":
print("Reconnected — buffered messages flushed")
case "auth_failed":
print("Authentication failed — check your API key")
device.connection.listeners(on_status)
device->connection.on_status([](relay_connection_status_t status) {
switch (status) {
case RELAY_STATUS_CONNECTED:
ESP_LOGI("app", "Connected to RelayX");
break;
case RELAY_STATUS_DISCONNECTED:
ESP_LOGW("app", "Disconnected");
break;
case RELAY_STATUS_RECONNECTING:
ESP_LOGW("app", "Connection lost, reconnecting...");
break;
case RELAY_STATUS_RECONNECTED:
ESP_LOGI("app", "Reconnected — buffered messages flushed");
break;
default:
break;
}
});
Multiple Listeners
You can register multiple listeners. Each one is called independently when a status change occurs.
- JavaScript
- Python
- C++
// logging
device.connection.listeners((event) => {
console.log(`[${new Date().toISOString()}] ${event.type}`);
});
// UI updates
device.connection.listeners((event) => {
updateStatusIndicator(event.type);
});
from datetime import datetime
# logging
def log_status(event):
print(f"[{datetime.now().isoformat()}] {event['type']}")
# app logic
def handle_status(event):
update_status_indicator(event["type"])
device.connection.listeners(log_status)
device.connection.listeners(handle_status)
// logging
device->connection.on_status([](relay_connection_status_t status) {
ESP_LOGI("app", "Status changed: %d", status);
});
// app logic
device->connection.on_status([](relay_connection_status_t status) {
update_status_indicator(status);
});
The C++ SDK supports up to RELAY_MAX_STATUS_LISTENERS (default 4) concurrent status listeners.
Reconnection Behavior
When the connection drops unexpectedly, the SDK automatically handles reconnection:
- A
reconnectingevent is emitted once - The SDK keeps retrying until a connection is re-established
- All active RPC and command listeners are resubscribed
- Any telemetry or events buffered while offline are flushed in order
- A
reconnectedevent is emitted
- JavaScript
- Python
- C++
The disconnected event only fires on explicit disconnect() calls — not on unexpected connection drops. If the connection drops unexpectedly, you'll see reconnecting followed by reconnected.
The disconnected event only fires on explicit disconnect() calls — not on unexpected connection drops. If the connection drops unexpectedly, you'll see reconnecting followed by reconnected.
On unexpected connection loss, the SDK emits RELAY_STATUS_DISCONNECTED → RELAY_STATUS_RECONNECTING → RELAY_STATUS_RECONNECTED once restored.
Connect and Disconnect
Connection is managed at the device level, not through this module:
- JavaScript
- Python
- C++
// connect
await device.connect();
// disconnect
await device.disconnect();
Both methods return true if the state changed, false if already in that state.
# connect
await device.connect()
# disconnect
await device.disconnect()
Both methods return true if the state changed, false if already in that state.
// connect
device->connect();
// disconnect
device->disconnect();
Both methods return relay_err_t. You can also check connection state at any time:
if (device->connection.is_connected()) {
// device is online
}