FedecksClient behaviour (fedecks_client v0.1.3)
Establishes a websocket connection to the server.
Eg :
defmodule MyApp.MyClient do
use FedecksClient
def device_id do
{:ok, name} = :inet.hostname()
to_string(name)
end
def connection_url do
Application.fetch_env!(:my_app, :fedecks_server_path)
end
end
Include in your supervision tree.
eg
defmodule MyApp.Application do
children = [
MyApp.MyClient
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
Initial authentication to the server with FedecksClient.login/1
. Subsequent
connections will authenticate with a token provided by the server, persisting between reboots,
until the token expires or otherwise becomes invalid.
Link to this section Summary
Callbacks
How long to wait before and between connection attempts. Bear in mind that it my take some time for a network connection to be established if using Nerves Networking and WiFi. Frequent restarts could propagate and take down the application.
Send an raw binary message to the server.
Server websocket URL. Must start with "ws://" or "wss://".
Device id to identify this device when communicating with the server
Initiate a login to the server.
How often to ping the server to maintain the connection. Bear in mind that the server will drop the connection after 1 minute of inactivity.
Send an encoded message to the server. Note that the server will use safe decoding so it is best to avoid atoms.
Send an raw binary message to the server.
Subscribe to Fedecks events. Messages are sent in the form {ModuleName, message}
.
See module doc for messages
Filesystem directory to store the Fedecks Token. Optional and defaults to FedecksClient.default_token_dir/0
("root/fedecks" on a Nerves installation)
Functions
The default directory for Fedecks tokens. Value is depenendend on the mix environment and target and assumes you are using this for Nerves.
Link to this section Types
connection_status()
@type connection_status() ::
:unregistered
| :connecting
| :connection_scheduled
| :failed_registration
| :connected
Link to this section Callbacks
@callback connect_delay() :: pos_integer()
How long to wait before and between connection attempts. Bear in mind that it my take some time for a network connection to be established if using Nerves Networking and WiFi. Frequent restarts could propagate and take down the application.
Optional and defaults to 10 seconds
connection_status()
@callback connection_status() :: connection_status()
Send an raw binary message to the server.
Implementation provided by the __using__
macro
connection_url()
@callback connection_url() :: String.t()
Server websocket URL. Must start with "ws://" or "wss://".
Remeber to append "/websocket" if using a Phoenix (ie Fedecks) websocket.
device_id()
@callback device_id() :: String.t()
Device id to identify this device when communicating with the server
login(credentials)
@callback login(credentials :: term()) :: :ok
Initiate a login to the server.
Implementation provided by the __using__
macro
@callback ping_frequency() :: pos_integer()
How often to ping the server to maintain the connection. Bear in mind that the server will drop the connection after 1 minute of inactivity.
Optional and defaults to 19 seconds
send(message)
@callback send(message :: term()) :: :ok
Send an encoded message to the server. Note that the server will use safe decoding so it is best to avoid atoms.
Implementation provided by the __using__
macro
send_raw(message)
@callback send_raw(message :: term()) :: :ok
Send an raw binary message to the server.
Implementation provided by the __using__
macro
subscribe()
@callback subscribe() :: :ok
Subscribe to Fedecks events. Messages are sent in the form {ModuleName, message}
.
See module doc for messages
Implementation provided by the __using__
macro
@callback token_dir() :: String.t()
Filesystem directory to store the Fedecks Token. Optional and defaults to FedecksClient.default_token_dir/0
("root/fedecks" on a Nerves installation)
Link to this section Functions
default_connect_delay()
default_ping_frequency()
default_token_dir(mix_env \\ :dev, mix_target \\ :host)
The default directory for Fedecks tokens. Value is depenendend on the mix environment and target and assumes you are using this for Nerves.
- If the env is
:test
or target is:host
then it is the module name under the system temp directory. - Otherwise "/root/fedecks"