High-level SpacetimeDB client that ties Connection, ClientCache, and Schema together.
Usage
defmodule MyApp.SpaceClient do
use Spacetimedbex.Client
def config do
%{
host: "localhost:3000",
database: "my_db",
subscriptions: ["SELECT * FROM users", "SELECT * FROM messages"]
}
end
def on_connect(identity, _conn_id, token, state) do
IO.puts("Connected with identity: #{inspect(identity)}")
{:ok, Map.put(state, :token, token)}
end
def on_insert("users", row, state) do
IO.puts("New user: #{inspect(row)}")
{:ok, state}
end
end
# Start it
{:ok, pid} = Spacetimedbex.Client.start_link(MyApp.SpaceClient, %{})
# Call a reducer
Spacetimedbex.Client.call_reducer(pid, "create_user", %{"name" => "Alice", "age" => 30})
# Query the cache
Spacetimedbex.Client.get_all(pid, "users")Callbacks
All callbacks are optional except config/0.
config()— returns connection configuration mapon_connect(identity, connection_id, token, state)— called on initial connectionon_subscribe_applied(table_name, rows, state)— called per table when subscription data arriveson_insert(table_name, row, state)— called per inserted rowon_delete(table_name, row, state)— called per deleted rowon_update(table_name, old_row, new_row, state)— called when a row with the same PK is deleted then inserted (row replacement)on_transaction(changes, state)— called with full transaction; return{:ok, state, :skip_row_callbacks}to suppress per-row callbackson_reducer_result(request_id, result, state)— called when a reducer completeson_unsubscribe_applied(query_set_id, rows, state)— called when an unsubscribe completeson_query_result(request_id, result, state)— called with one-off query resultson_disconnect(reason, state)— called on disconnection
Summary
Functions
Call a reducer with a map of arguments. Auto-encodes via schema.
Call a reducer with pre-encoded BSATN binary arguments.
Returns a specification to start this module under a supervisor.
Count rows in a cached table.
Find a row by primary key.
Get all rows from a cached table.
Execute a one-off SQL query via WebSocket.
Get the cached schema.
Start a Client GenServer.
Unsubscribe from a query set by ID. Options: :send_dropped_rows.
Types
Callbacks
@callback config() :: map()
@callback on_query_result(request_id :: non_neg_integer(), result :: term(), state()) :: {:ok, state()}
@callback on_reducer_result(request_id :: non_neg_integer(), result :: term(), state()) :: {:ok, state()}
@callback on_unsubscribe_applied( query_set_id :: non_neg_integer(), rows :: [map()], state() ) :: {:ok, state()}
Functions
Call a reducer with a map of arguments. Auto-encodes via schema.
Call a reducer with pre-encoded BSATN binary arguments.
Returns a specification to start this module under a supervisor.
See Supervisor.
Count rows in a cached table.
Find a row by primary key.
Get all rows from a cached table.
Execute a one-off SQL query via WebSocket.
Get the cached schema.
Start a Client GenServer.
Parameters
module— callback module thatuse Spacetimedbex.Clientinit_state— initial user state passed to callbacksopts— GenServer options (e.g.:name)
Unsubscribe from a query set by ID. Options: :send_dropped_rows.