Requirements
- A credential: a user access token or a product API key. See API authentication.
- The credential’s role must be able to view the device. Every built-in role can.
- A Phoenix Channels client. The stream uses the Phoenix Channels protocol, and client libraries exist for most languages.
Authenticating with a product API key is on the NervesHub main branch but in no tagged release as of NervesHub 2.5.4. On 2.5.4 and earlier, use a user access token.
Connect
Open a WebSocket to/events-socket/websocket on the web host, the same host that serves the HTTP API. Pass the credential as the token query parameter.
token is refused.
Subscribe to a device
Join the topicdevice:<identifier>. Join one topic for each device you want to follow.
ok. A refused join is answered with status error and this response:
- The device does not exist, or the credential cannot see it. A product API key only sees devices in its own product. A member with a tag-scoped role only sees devices with matching tags.
- The device’s organization requires two-factor authentication and the token’s user has not set it up.
The firmware_update event
The server pushes firmware_update on the device’s topic each time the device reports on an update.
Example
This example uses thephoenix JavaScript client.
Check that it works
- Connect and join the topic for a device that is online.
- Send firmware to the device, for example with
nh device upgrade <identifier> <firmware-uuid>. - Watch for
firmware_updateevents as the device downloads and applies it.
When access changes
The socket authenticates once, when it connects. NervesHub closes what you no longer have access to:- NervesHub checks your access to the device again when your role changes, when you are removed from the organization, when the organization starts requiring two-factor authentication, and when the device’s tags change. If you can no longer view the device, the subscription is closed.
- A socket opened with a product API key is disconnected when the key is deactivated, or when the custom role it holds is edited. Reconnect to pick up the edited role.
Limits
- The stream is live only. Events sent while you are not subscribed are not replayed.
- It covers firmware update progress for one device per topic. There is no topic for a whole product or organization.

