Guides
Real-time Events
Subscribe to NATS events via WebSocket. Telemetry data, alerts, module-to-module communication, and custom events.
Real-time Events
Insolitum Universe uses NATS as its event bus. Modules connect via a WebSocket gateway that bridges Socket.io to NATS subjects.
Architecture
useNats Hook
The useNats hook manages the WebSocket connection and provides subscribe/publish methods:
Hook API
| Property | Type | Description |
|---|---|---|
connected | boolean | Whether the WebSocket connection is active |
subscribe | (channel, callback) => unsubscribe | Subscribe to a NATS channel. Returns cleanup function. |
publish | (subject, data) => void | Publish an event to a NATS subject |
Available Channels
| Channel | Events | Source |
|---|---|---|
telemetry | Sensor readings (temperature, humidity, battery) | neurosense-core |
alerts | Threshold alerts (high temp, low battery, device offline) | neurosense-core |
events | General application events | Various services |
modules | Module-to-module custom events | Partner modules |
system | System health, service status | Infrastructure |
NATS Subjects
Events are organized by subject patterns:
Event Types
Telemetry Reading
Alert Event
Module Event (Custom)
Publishing Custom Events
Send events that other modules can subscribe to:
Custom events should follow the subject pattern modules.{your_module_id}.{tenant_id}.{event_type} for proper routing and tenant isolation.
Connection Management
The useNats hook automatically:
- Connects when auth tokens are available
- Authenticates with the WebSocket gateway using the Supabase access token
- Reconnects on disconnect (up to 10 attempts with 1s delay)
- Disconnects on component unmount
- Scopes subscriptions to the current tenant