View Source Tamnoon.Methods (Tamnoon v1.0.0-a.4)

Provides default implementations for methods you need to get your Tamnoon server working - basic state management (with tmnn_get/2 and tmnn_update/2) and basic handling of PubSub channels (via tmnn_sub/2, tmnn_unsub/2, tmnn_pub/2, tmnn_subbed_channels/2)

Using the module

Since v1.0.0-a.4, you should import Tamnoon.MethodManager in your methods modules instead of using this module directly.

Note: in the documentation of the default methods, the term 'returns' is used both to describe the return value of the function and the value of the server's response.

Summary

Functions

Returns the value of the key under the field "key" in the request as an atom. Note: this is NOT a method handler - simply a utility function.

Returns the field with the key specified under the "key" field in the request. Returns an error string if there is no such item.

Invoked every 55 seconds by the client in order to prevent idle timeouts.

Will send the request under the "action" key in the request to every client in the channel under the "channel" key.

Sets the state on the server to the state sent by the client. Invoked when the client reconnects to the server in order to prevent information loss.

Subscribes to the channel under the "key" field in the request. If there is no such channel, one will be created (with the client subscribed to it). If the client is already subscribed to the channel nothing will happen.

Returns a list of the channels the client is currently subscribed to.

Returns the current state. Automatically invoked when the connection is started.

Unsubscribes from the channel under the "key" in the request.

Updates the value of the item with the key at the "key" field on the request, setting it to the value at the "val" key. Returns a tuple with the updated field and the state, or an error string if there is no such item.

Functions

@spec get_key(map(), map()) :: atom() | nil

Returns the value of the key under the field "key" in the request as an atom. Note: this is NOT a method handler - simply a utility function.

@spec tmnn_get(map(), map()) ::
  {field :: map(), state :: map()}
  | {%{error: error :: String.t()}, state :: map()}

Returns the field with the key specified under the "key" field in the request. Returns an error string if there is no such item.

Link to this function

tmnn_keep_alive(req, state)

View Source
@spec tmnn_keep_alive(map(), map()) :: {nil, state :: map()}

Invoked every 55 seconds by the client in order to prevent idle timeouts.

@spec tmnn_pub(map(), map()) :: {%{pub: :ok}, state :: map()}

Will send the request under the "action" key in the request to every client in the channel under the "channel" key.

Link to this function

tmnn_set_state(req, state)

View Source
@spec tmnn_set_state(map(), map()) :: {%{set_state: :ok}, state :: map()}

Sets the state on the server to the state sent by the client. Invoked when the client reconnects to the server in order to prevent information loss.

@spec tmnn_sub(map(), map()) :: {%{sub: :ok}, state :: map()}

Subscribes to the channel under the "key" field in the request. If there is no such channel, one will be created (with the client subscribed to it). If the client is already subscribed to the channel nothing will happen.

Link to this function

tmnn_subbed_channels(req, state)

View Source
@spec tmnn_subbed_channels(map(), map()) ::
  {%{subbed_channels: channels :: [String.t()]}, state :: map()}

Returns a list of the channels the client is currently subscribed to.

@spec tmnn_sync(map(), map()) :: {state :: map(), state :: map()}

Returns the current state. Automatically invoked when the connection is started.

@spec tmnn_unsub(map(), map()) ::
  {%{unsub: :ok}, state :: map()}
  | {%{error: error :: String.t()}, state :: map()}

Unsubscribes from the channel under the "key" in the request.

@spec tmnn_update(map(), map()) ::
  {new_field :: map(), state :: map()}
  | {%{error: error :: String.t()}, state :: map()}

Updates the value of the item with the key at the "key" field on the request, setting it to the value at the "val" key. Returns a tuple with the updated field and the state, or an error string if there is no such item.