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

Link to this type

connection_status()

@type connection_status() ::
  :unregistered
  | :connecting
  | :connection_scheduled
  | :failed_registration
  | :connected

Link to this section Callbacks

Link to this callback

connect_delay()

(optional)
@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

Link to this callback

connection_status()

@callback connection_status() :: connection_status()

Send an raw binary message to the server.

Implementation provided by the __using__ macro

Link to this callback

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.

@callback device_id() :: String.t()

Device id to identify this device when communicating with the server

Link to this callback

login(credentials)

@callback login(credentials :: term()) :: :ok

Initiate a login to the server.

Implementation provided by the __using__ macro

Link to this callback

ping_frequency()

(optional)
@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

@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

Link to this callback

send_raw(message)

@callback send_raw(message :: term()) :: :ok

Send an raw binary message to the server.

Implementation provided by the __using__ macro

@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

Link to this callback

token_dir()

(optional)
@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

Link to this function

default_connect_delay()

Link to this function

default_ping_frequency()

Link to this function

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"