Connection and CID lifecycle
Browsers, Node.js, Arduino and the CLI all connect to the server in the same way. This page covers that shared flow. How to use listen, subscribe and link in JavaScript is in JS client: subscriptions and component lifecycle.
The flow
- ready: the moment the server issues a CID and the client can send and receive. It is not the same as an open socket. Send signals after ready.
- ready comes on every connection. After a lost connection reconnects, ready arrives again. Keep apart what you do once and what you do on every connection.
After a reconnection
| What | What happens |
|---|---|
| CID | An anonymous connection's CID may change. A connection logged in with a device key keeps its registered CID. |
| Subscriptions | The server forgets a lost connection's subscriptions. Subscribe again on every connection. |
Kept values ($) | Subscribing again delivers the last value the server keeps, at once. |
| What others see | Subscribers of a lost CID receive close on <cid>@$state. The CID may later be issued to another client. |
Sending to an anonymous CID stored long ago may reach someone else. When the other side may have reconnected, find it again with a procedure such as #search. When you need a fixed peer, use a CID logged in with a device key.
Redirects and reply limit
- Redirects: a public server may send a first connection or a login to another server. This is a normal step before ready, unlike a connection lost after ready.
- Reply limit: if the server's first reply does not arrive after connecting, the client closes the connection and starts again from the first address. JavaScript waits 15 seconds (from 9.19.0), Arduino 10 seconds.
Authentication clear
When the same device key logs in elsewhere and replaces this connection, the server sends an authentication clear. The client stops and does not reconnect. Give each device its own key so that two devices never share one. Duplicate logins
Where each environment subscribes again
| Environment | On every connection | To finish |
|---|---|---|
| Browser and Node.js | Tags registered with listen() or link() are subscribed again by the library. Call subscribe() in a ready handler every time. | stop() (no more reconnecting), destroy() |
| Arduino | Subscribe and send the first state in the onReady callback, which runs on every connection. | — |
CLI io-client | Subscriptions made with subscribe commands are restored after a reconnection. | exit |
- JavaScript: JS client: subscriptions and component lifecycle
- Arduino: Get started with Arduino
- CLI: CLI client