rust-client
Rust client library for Milestone Basilisk gateway and service bus.
Features
- Register a service instance, receive a generated instance ID, and establish an authenticated service bus connection in one workflow
- Subscribe/unsubscribe to topics and receive events
- Publish events with structured payloads
- Send forward requests and wait for forward responses
- Register request responders with
client.on_request(message_type, responder) - Call gateway registry APIs to register/deregister service instances
- Use
BasiliskClientas the top-level client withgatewayandbushandles
API reference
This section documents the public APIs that implement the features above.
Top-level client (BasiliskClient)
Use this when you want a single workflow that registers in the gateway and then opens an authenticated bus connection.
BasiliskClientConfig fields:
gateway_base_url: Gateway base URL (for examplehttp://127.0.0.1:3000)bus_host: TCP host for the service busbus_port: TCP port for the service busservice_id: Logical service identifierfingerprint: Service fingerprint/version markerpath_prefixes: Path prefixes advertised to the gatewayscheme: Upstream scheme (httporhttps)host: Upstream hostport: Upstream portweight: Instance load-balancing weightregistration_auth_type: Gateway auth type (for exampletoken)registration_token: Gateway registration token/secret
BasiliskClient public fields:
gateway: GatewayApiClientbus: BusClientservice_id: Stringinstance_id: String
BasiliskClient methods:
connect(config) -> anyhow::Result<BasiliskClient>- Registers first (
/registry/register) and then authenticates the bus connection.
- Registers first (
deregister() -> anyhow::Result<()>- Calls gateway deregistration for the current
service_id+instance_id.
- Calls gateway deregistration for the current
subscribe(topics) -> ClientResult<()>unsubscribe(topics) -> ClientResult<()>publish(topic, message_type, payload) -> ClientResult<i32>- Returns subscriber count from the bus
ackframe.
- Returns subscriber count from the bus
publish_event(event) -> ClientResult<i32>forward(request) -> ClientResult<ServiceBusForwardResponse>on_event(topic, handler) -> ClientResult<()>- Registers an async event callback and auto-subscribes to that topic.
on_request(message_type, responder) -> ClientResult<()>- Registers an async request responder keyed by message type.
Service bus client (BusClient)
Use this when you need lower-level direct control over the TCP bus.
BusClient methods:
connect(host, port, service_id, instance_id, token) -> ClientResult<BusClient>- First protocol frame is
connectcarrying auth credentials. - Frames are newline-delimited JSON (
\nterminated).
- First protocol frame is
subscribe(topics) -> ClientResult<()>unsubscribe(topics) -> ClientResult<()>publish(topic, message_type, payload) -> ClientResult<i32>publish_event(event) -> ClientResult<i32>forward(request) -> ClientResult<ServiceBusForwardResponse>on_event(topic, handler) -> ClientResult<()>on_request(message_type, responder) -> ClientResult<()>
Supporting bus API types:
ForwardRequesttarget_service_id,message_type,payload,timeout_ms
ServiceBusRequestevent: ServiceBusEventEnvelopereply_to() -> Option<&str>
RequestResponderrespond(message_type, payload) -> ClientResult<i32>respond_ok(payload) -> ClientResult<i32>
Gateway API client (GatewayApiClient)
Use this when you only need registry operations.
GatewayApiClient methods:
new(base_url) -> GatewayApiClientregister_instance_auto(request) -> anyhow::Result<RegistrationResponse>- Clears
request.instance.instance_idand lets the registry generate it.
- Clears
register_instance(request) -> anyhow::Result<RegistrationResponse>deregister_instance(service_id, instance_id) -> anyhow::Result<()>
Gateway payload types:
RegistrationRequestservice_id,fingerprint,path_prefixes,instance,auth
InstanceInfoinstance_id,scheme,host,port,weight
AuthInfoauth_type,token
RegistrationResponsemessage,service_id,instance_id,token
Protocol types
These mirror wire payloads used over the service bus.
ServiceBusEventEnvelopeevent_id,emitted_at_utc,service_id,instance_id,topic,message_type,correlation_id,causation_id,payload
ServiceBusForwardRequestServiceBusForwardResponseServiceBusProtocolMessage- Generic frame container for
connect,subscribe,publish,forward,event,ack, anderrorframes.
- Generic frame container for
protocol_types- String constants for protocol frame names.
Error model
ClientResult<T> = Result<T, ClientError>ClientErrorIo,Serde,Protocol { code, message },MissingField,ChannelClosed,UnexpectedMessage
All fallible bus operations return ClientResult<T> and preserve protocol/transport details.
Quick usage
use ;
use HashMap;
async
BasiliskClient owns a gateway client for registry operations and a bus client for
the TCP service bus. The top-level connect flow automatically registers the instance,
accepts the generated instance ID returned by the registry, and then opens the bus
connection using the issued token.
Logging
Call init_tracing() once during application startup to install Basilisk's
console subscriber. It reads RUST_LOG
for log filtering and defaults to INFO when the variable is absent. If your
application has already installed a tracing subscriber, do not call it.
RUST_LOG=debug
Notes on low-level clients
BusClient::connect(host, port, service_id, instance_id, token)now authenticates during theconnecthandshake (no separate authenticate command is required for normal clients).GatewayApiClient::register_instance(...)responses include bothinstance_idandtoken.GatewayApiClient::register_instance_auto(...)sends an empty instance ID so the registry generates a cryptographically strong instance identity.
End-to-end test
The integration test starts:
- a temporary Basilisk gateway instance,
- an upstream HTTP service,
- two connected Basilisk clients (
ordersandbilling).
It validates registry registration, proxy forwarding and Lua middleware header forwarding, publish/subscribe, and forward-request responder behavior.
Run: