Events and Subscriptions
Receive real-time notifications when nodes are discovered on the local network or when their state changes.
info
For unified updates with channel priority (SDK v2.2.2+), see Subscription Channels.
note
The userInstance refers to ESPRMUser from User Sign in.
What This Module Does ?
Register callbacks to keep your app in sync with devices:
localDiscovery— node reachable on the local network; use with Transport Order for local-first controlnodeUpdates— parameter or connectivity changes; requires notification adapter
Use Subscription Channels instead for multi-source updates (push, MQTT, custom) with channel priority.
Expected outcome: Callbacks run when nodes are discovered locally or state changes remotely.
Common Workflows
Subscribe to discovery and updates
import { ESPRMEventType } from "@espressif/rainmaker-base-sdk";
userInstance.subscribe(ESPRMEventType.localDiscovery, ({ nodeId, transportDetails }) => {
updateNodeTransport(nodeId, transportDetails);
});
userInstance.subscribe(ESPRMEventType.nodeUpdates, (updateData) => {
refreshNodeInUI(updateData);
});
Enable local transport via discovery
userInstance.subscribe(ESPRMEventType.localDiscovery, ({ nodeId, transportDetails }) => {
const node = findNodeById(nodeId);
if (node) {
node.availableTransports = node.availableTransports || {};
node.availableTransports[transportDetails.type] = transportDetails;
}
});
Pair with Transport Order.
Setup with transport priority
import { ESPRMBase, ESPRMEventType, ESPTransportMode } from "@espressif/rainmaker-base-sdk";
ESPRMBase.setTransportOrder([ESPTransportMode.local, ESPTransportMode.cloud]);
userInstance.subscribe(ESPRMEventType.localDiscovery, handleLocalDiscovery);
userInstance.subscribe(ESPRMEventType.nodeUpdates, handleNodeUpdates);
Manage subscriptions
userInstance.subscribe(ESPRMEventType.localDiscovery, [callback1, callback2]);
userInstance.unsubscribe(ESPRMEventType.localDiscovery, callback1);
userInstance.removeAllCallbacks(ESPRMEventType.localDiscovery);
Error Handling
const handleNodeUpdates = (updateData) => {
try {
updateStore(updateData);
} catch (error) {
console.error("Failed to process node update:", error);
}
};
Keep callbacks lightweight.
Advanced Concepts
Mental model
Subscribe once → get notified when things change.
Local discovery flow
Transport + discovery
Discovery fills availableTransports; transport order selects the route for setValue(). Both are required for local-first control.
Best Practices
- Subscribe before actions that expect updates
- Keep callbacks lightweight
- Remove callbacks on unmount
- Configure required adapters
- Test on-network and remote
Related Resource
- API Reference: