WebsockexAdapter.Examples.Docs.JsonRpcClient (WebsockexAdapter v0.1.1)

View Source

JSON-RPC client example from documentation.

Demonstrates how to make JSON-RPC calls over WebSocket connections. The WebsockexAdapter.Client automatically handles request/response correlation for JSON-RPC messages - no manual correlation 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(WebsockexAdapter.Client.t(), String.t(), 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(WebsockexAdapter.Client.t(), String.t(), map() | nil) ::
  :ok | {:error, term()}

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

echo(params \\ %{})

Echo test method

get_balance(params \\ %{})

Get account balance

get_server_time(params \\ %{})

Get server timestamp

handle_message(message)

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

Processes incoming WebSocket messages and returns JSON-RPC responses.