What happens by default
- The
:nerves_hub_linkOTP application startsNervesHubLink.Supervisor. - The supervisor builds its configuration from
config :nerves_hub_link, ...and passes it through a configurator. - The socket process sets the
NervesHubLink.Disconnectedalarm and waits until it can reach the NervesHub host. - The socket connects, joins the device channel, clears the alarm and calls your client’s
connected/0callback. - When the connection drops, the socket sets the alarm again and reconnects with a backoff.
Start the connection yourself
To start the connection later, for example after your own runtime setup has finished, turn off the automatic start. This needsnerves_hub_link 2.13.0 or later.
start: :manual the application starts nothing. Add NervesHubLink.Supervisor to your own supervision tree:
config :nerves_hub_link, ... keeps working.
To pass the configuration explicitly, give the supervisor a NervesHubLink.Configurator.Config struct:
NervesHubLink.Configurator.build/0 returns the struct that the default path uses, so you can call it at the moment you choose.
The configuration to use. Defaults to the result of
NervesHubLink.Configurator.build/0.start: :auto) and manual mode use the same code path.
Build configuration at runtime
A configurator is a module that receives the configuration read from the application environment and returns the configuration to connect with. Write one when part of the configuration is only known on the device, for example:- choosing which certificate and key to use
- reading a certificate file that is stored on the device and not available at compile time
:configurator, NervesHubLink picks one:
NervesHubLink.Configurator.NervesKey, if theNervesKeymodule is loaded.NervesHubLink.Configurator.TPM, if theTPMmodule is loaded.NervesHubLink.Configurator.SharedSecretotherwise.
Disable the connection in tests
To stop NervesHubLink from starting when you run tests on your host:connect: false stops the application from starting NervesHubLink.Supervisor. It has no effect on a supervisor you add to your own tree with start: :manual. Leave the supervisor out of your children in that case.
Waiting for the network
Before its first connection attempt, NervesHubLink checks that it can reach NervesHub. It opens a TCP connection to the configured host and port, with a two second timeout. If that fails, it logs[NervesHubLink] waiting for network to become available and checks again three seconds later.
To skip the check and attempt the connection straight away:
Reconnect backoff
When the connection drops, NervesHubLink reconnects after a delay. The delay comes from a list of milliseconds. Each failed attempt moves to the next entry, and the last entry repeats. The default list starts at one second and doubles up to 60 seconds. Each delay has up to 50% added at random, so a fleet that loses its connection at the same moment does not reconnect at the same moment. A new random list is generated on each disconnect.Set a fixed list
Decide at runtime
Implement the optionalreconnect_backoff/0 callback in your client module. NervesHubLink calls it each time the socket disconnects, before it starts to reconnect. Return a list of delays in milliseconds, or nil to use the default.
MyApp.Network.metered?/0 stands for your own logic.
A client built with use NervesHubLink.Client that does not override the callback returns reconnect_after_msec from config :nerves_hub_link, socket: [...], or the default when that is not set. It reads the application environment. A reconnect_after_msec that you pass only through {NervesHubLink.Supervisor, config: ...} applies to the first connection, so implement reconnect_backoff/0 as well in that case.
Related timing options
Delays in milliseconds before rejoining a channel that closed while the socket stayed connected.
Reconnect on demand
NervesHubLink.reconnect/0 closes the socket and connects again. You can also ask a device to reconnect from your workstation with nh device reconnect <identifier>.
If NervesHub answers a connection attempt with an HTTP redirect, NervesHubLink follows it, up to two redirects in a row (nerves_hub_link 2.10.0 or later).
React to the connection state
The connected/0 callback
Implement the optional connected/0 callback in your client module to run code each time the socket connects, including reconnects. This needs nerves_hub_link 2.8.0 or later. The return value is ignored.
handle_error/1 with the reason.
Ask for the state
Alarms
NervesHubLink sets and clears alarms through Erlang’s:alarm_handler. Read them with :alarm_handler.get_alarms/0, or with a library such as Alarmist.
Support for devices that sleep is on the main branch of
nerves_hub_link and is not in a tagged release as of 2.14.0. With it, a device that tells NervesHub it is going to sleep sets NervesHubLink.Disconnected with [reason: :asleep]. See Sleeping devices.Client identification
NervesHubLink names itself and its version in theUser-Agent header of the WebSocket upgrade request and of firmware downloads, for example NHL/2.14.0. NervesHub records the library and version for each connection.
Requirements
nerves_hub_link2.14.0 or later sends the header on the WebSocket. Earlier versions sent it on firmware downloads only.- NervesHub 2.5.2 or later records it. An older server ignores the header.
Custom headers
- If the socket headers already contain a
User-Agent, for example one set by a custom configurator, NervesHubLink leaves it alone and does not add its own. NervesHubLink.Configurator.SharedSecretreplaces the socket headers with its signature headers. AUser-Agentset inconfig :nerves_hub_link, socket: [headers: ...]is dropped and NervesHubLink sends its own.- To send a different
User-Agentwith shared secret authentication, write a configurator that delegates toNervesHubLink.Configurator.SharedSecretand then adds the header to the socket headers it returns. NervesHubLink.Configurator.user_agent/0returns the library’s own token, for a configurator that sends it next to its own.
What NervesHub accepts
NervesHub reads the header as product tokens,<name>/<version>.
- The first token that names a client wins. Put your token first. You can add more after it, such as
NHL/2.14.0 Mint/1.7.1. NHLandnerves_hub_linkare both recorded asnerves_hub_link. Names are matched exactly, including case.- A client that NervesHub does not list is recorded only when its name starts with
custom#, for examplecustom#acme-link/1.4.0. It is recorded under that name with the prefix. The name after the prefix can be at most 64 letters, digits,.,_and-, and starts with a letter or digit. - The version must be SemVer with no leading
v. Otherwise NervesHub records the library and leaves the version unknown. - Each connection is recorded fresh. A device that reconnects without the header goes back to unknown.

