ZenWebsocket.Examples.Docs.JsonRpcClient (ZenWebsocket v0.8.0)

Copy Markdown View Source

JSON-RPC 2.0 calls over a WebSocket connection.

Defines methods with defrpc/3 from ZenWebsocket.JsonRpc and issues them through Client.send_message/2, which correlates any message carrying an "id" with its response — no caller-side correlation bookkeeping is needed.

Summary

Functions

Makes an async JSON-RPC call without waiting for response.

Echo test method

Get account balance

Get server timestamp

Processes incoming WebSocket messages and returns JSON-RPC responses.

Functions

call_method(client, method, params \\ nil, timeout \\ 5000)

@spec call_method(
  ZenWebsocket.Client.t(),
  String.t(),
  list() | map() | nil,
  timeout()
) ::
  {:ok, term()} | {:error, term()}

Makes a synchronous JSON-RPC call.

The Client automatically handles JSON-RPC correlation - when sending a message with an "id" field, it tracks the request and returns the correlated response.

cast_method(client, method, params \\ nil)

@spec cast_method(ZenWebsocket.Client.t(), String.t(), list() | map() | nil) ::
  :ok | {:error, term()}

Makes an async JSON-RPC call without waiting for response.

echo(params \\ %{})

@spec echo(list() | map() | nil) :: {:ok, map()}

Echo test method

get_balance(params \\ %{})

@spec get_balance(list() | map() | nil) :: {:ok, map()}

Get account balance

get_server_time(params \\ %{})

@spec get_server_time(list() | map() | nil) :: {:ok, map()}

Get server timestamp

handle_message(decoded)

@spec handle_message(map() | String.t()) :: {:ok, map()} | {:error, term()} | :ignore

Processes incoming WebSocket messages and returns JSON-RPC responses.