macula (macula v2.0.0)

View Source

Macula SDK — Public API for mesh applications.

This is the main entry point for applications using the Macula SDK. All functions delegate to macula_mesh_client or macula_multi_relay for relay mesh communication.

Summary

Functions

Abort the stream with an error frame.

Advertise an RPC procedure handler.

Advertise a streaming procedure (default: server_stream). LOCAL dispatch only — see advertise_stream/3,4 for the Client form.

Advertise a streaming procedure.

Advertise with explicit mode / options.

Wait for the terminal reply (client-stream / bidi).

Call a remote procedure (default 5s timeout).

Call a remote procedure with timeout.

Call a remote procedure on a specific target node (default 5s timeout).

Call a remote procedure on a specific target node with timeout.

Open a server-stream call (default mode).

Open a server-stream call with options.

Open a remote server-stream call against a mesh client, with opts.

Close the stream (both sides).

Half-close the write side; recv still drains.

Connect to a Macula relay.

Disconnect from the relay.

Ensure this node is running in distributed mode.

Get the Erlang cluster cookie.

Enable Erlang distribution over a dedicated dist relay (macula-io/macula-dist-relay).

Join the Macula relay mesh with Erlang distribution.

List all nodes connected to the relay (default 5s timeout).

List all nodes connected to the relay with options.

Subscribe to node up/down events.

Open a client-stream or bidi call.

Open a stream with explicit mode.

Publish an event to a topic (fire-and-forget).

Publish an event with options.

Receive the next chunk (blocks).

Resolve a mesh name to node identity information.

Send a binary chunk on the stream.

Send a chunk with explicit encoding.

Set the Erlang cluster cookie.

Server-side: emit the terminal reply value.

Subscribe to a topic.

Stop advertising a procedure.

Stop advertising a streaming procedure.

Unsubscribe from node up/down events.

Unsubscribe from a topic.

Types

client/0

-type client() :: pid().

procedure/0

-type procedure() :: binary().

stream/0

-type stream() :: pid().

stream_handler/0

-type stream_handler() :: fun((stream(), term()) -> any()).

stream_mode/0

-type stream_mode() :: server_stream | client_stream | bidi.

topic/0

-type topic() :: binary().

Functions

abort(Stream, Code, Message)

-spec abort(stream(), binary(), binary()) -> ok.

Abort the stream with an error frame.

advertise(Client, Procedure, Handler)

-spec advertise(client(), procedure(), fun()) -> {ok, reference()} | {error, term()}.

Advertise an RPC procedure handler.

advertise(Client, Procedure, Handler, Opts)

-spec advertise(client(), procedure(), fun(), map()) -> {ok, reference()} | {error, term()}.

Advertise with options.

await_reply(Stream)

-spec await_reply(stream()) -> {ok, term()} | {error, term()}.

Wait for the terminal reply (client-stream / bidi).

await_reply(Stream, Timeout)

-spec await_reply(stream(), timeout()) -> {ok, term()} | {error, term()}.

call(Client, Procedure, Args)

-spec call(client(), procedure(), term()) -> {ok, term()} | {error, term()}.

Call a remote procedure (default 5s timeout).

call(Client, Procedure, Args, Timeout)

-spec call(client(), procedure(), term(), pos_integer()) -> {ok, term()} | {error, term()}.

Call a remote procedure with timeout.

call_node(Client, Target, Procedure, Args)

-spec call_node(client(), binary(), procedure(), term()) -> {ok, term()} | {error, term()}.

Call a remote procedure on a specific target node (default 5s timeout).

Target can be a mesh name, site_id, or node_id (all binaries). The relay resolves the target and routes the CALL directly to that node.

call_node(Client, Target, Procedure, Args, Timeout)

-spec call_node(client(), binary(), procedure(), term(), pos_integer()) ->
                   {ok, term()} | {error, term()}.

Call a remote procedure on a specific target node with timeout.

call_stream(Procedure, Args)

-spec call_stream(procedure(), term()) -> {ok, stream()} | {error, term()}.

Open a server-stream call (default mode).

Phase 1 shortcut — dispatches in-process via the local registry. For cross-node streaming use the (Client, Procedure, Args) form.

call_stream(Procedure, Args, Opts)

-spec call_stream(procedure() | client(), procedure() | term(), term() | map()) ->
                     {ok, stream()} | {error, term()}.

Open a server-stream call with options.

Two shapes: call_stream(Procedure, Args, Opts) — LOCAL dispatch only call_stream(Client, Procedure, Args) — REMOTE via mesh client

call_stream(Client, Procedure, Args, Opts)

-spec call_stream(client(), procedure(), term(), map()) -> {ok, stream()} | {error, term()}.

Open a remote server-stream call against a mesh client, with opts.

close(Stream)

-spec close(stream()) -> ok.

Close the stream (both sides).

close_send(Stream)

-spec close_send(stream()) -> ok.

Half-close the write side; recv still drains.

connect(Url, Opts)

-spec connect(binary() | [binary()], map()) -> {ok, client()} | {error, term()}.

Connect to a Macula relay.

Returns a client PID (macula_mesh_client gen_server) that you pass to all other API functions. Accepts a single URL binary or a list.

disconnect(Client)

-spec disconnect(client()) -> ok.

Disconnect from the relay.

ensure_distributed()

-spec ensure_distributed() -> ok | {error, term()}.

Ensure this node is running in distributed mode.

get_cookie()

-spec get_cookie() -> atom().

Get the Erlang cluster cookie.

join_dist_relay(Opts)

-spec join_dist_relay(map()) -> ok | {error, term()}.

Enable Erlang distribution over a dedicated dist relay (macula-io/macula-dist-relay).

Different from join_mesh/1: - Connects to a dist relay (port 4434, ALPN macula-dist), NOT the pub/sub station mesh - No mesh_client, no pub/sub subscriptions — only dist traffic - Uses raw QUIC stream routing with no MessagePack overhead

Options: - url (required): <<"quic://relay.example.com:4434">>

After this returns ok, standard OTP distribution (rpc:call/4, gen_server:call/3 across nodes, pg groups, etc.) works across firewalls via the dist relay.

join_mesh(Opts)

-spec join_mesh(map()) -> ok | {error, term()}.

Join the Macula relay mesh with Erlang distribution.

After calling this, standard OTP distribution works across firewalls. Options: relays (required list), realm, identity, site, tls_verify.

list_nodes(Client)

-spec list_nodes(client()) -> {ok, map()} | {error, term()}.

List all nodes connected to the relay (default 5s timeout).

list_nodes(Client, Opts)

-spec list_nodes(client(), map()) -> {ok, map()} | {error, term()}.

List all nodes connected to the relay with options.

monitor_nodes()

-spec monitor_nodes() -> ok.

Subscribe to node up/down events.

open_stream(Procedure, Args, Opts)

-spec open_stream(procedure() | client(), procedure() | term(), term() | map()) ->
                     {ok, stream()} | {error, term()}.

Open a client-stream or bidi call.

Two shapes: open_stream(Procedure, Args, Opts) — LOCAL dispatch open_stream(Client, Procedure, Args) — REMOTE, default mode bidi

open_stream(Procedure, Args, Opts, Mode)

-spec open_stream(procedure() | client(), procedure() | term(), term() | map(), stream_mode() | map()) ->
                     {ok, stream()} | {error, term()}.

Open a stream with explicit mode.

Two shapes: open_stream(Procedure, Args, Opts, Mode) — LOCAL dispatch, explicit mode open_stream(Client, Procedure, Args, Opts) — REMOTE via mesh client

publish(Client, Topic, Data)

-spec publish(client(), topic(), term()) -> ok.

Publish an event to a topic (fire-and-forget).

publish(Client, Topic, Data, Opts)

-spec publish(client(), topic(), term(), map()) -> ok.

Publish an event with options.

recv(Stream)

-spec recv(stream()) -> {chunk, binary()} | {data, term()} | eof | {error, term()}.

Receive the next chunk (blocks).

recv(Stream, Timeout)

-spec recv(stream(), timeout()) -> {chunk, binary()} | {data, term()} | eof | {error, term()}.

resolve(Client, Name)

-spec resolve(client(), binary()) -> {ok, map()} | {error, term()}.

Resolve a mesh name to node identity information.

Returns the node's identity including name, site_id, city, endpoint, and connected_at timestamp. Works for names, site_ids, or node_ids.

send(Stream, Bin)

-spec send(stream(), binary()) -> ok | {error, term()}.

Send a binary chunk on the stream.

send(Stream, Body, Encoding)

-spec send(stream(), binary() | term(), raw | msgpack) -> ok | {error, term()}.

Send a chunk with explicit encoding.

set_cookie(Cookie)

-spec set_cookie(atom() | binary()) -> ok.

Set the Erlang cluster cookie.

set_reply(Stream, Result)

-spec set_reply(stream(), term()) -> ok.

Server-side: emit the terminal reply value.

subscribe(Client, Topic, Callback)

-spec subscribe(client(), topic(), fun((term()) -> ok) | pid()) -> {ok, reference()} | {error, term()}.

Subscribe to a topic.

The callback receives the event payload (map or binary). Returns a subscription reference for unsubscribing.

unadvertise(Client, Procedure)

-spec unadvertise(client(), procedure()) -> ok | {error, term()}.

Stop advertising a procedure.

unadvertise_stream(Procedure)

-spec unadvertise_stream(procedure()) -> ok.

Stop advertising a streaming procedure.

unmonitor_nodes()

-spec unmonitor_nodes() -> ok.

Unsubscribe from node up/down events.

unsubscribe(Client, SubRef)

-spec unsubscribe(client(), reference()) -> ok | {error, term()}.

Unsubscribe from a topic.