RelayClient
RelayClient manages a persistent WebSocket connection to SignalWire’s Relay
service. It handles authentication, automatic reconnection with exponential
backoff, inbound event dispatch, outbound dialing, and SMS/MMS messaging.
Use it when you need imperative, event-driven control over calls rather than
the declarative AI agent approach.
The client supports two authentication modes: project ID + API token, or JWT token. Credentials can be passed directly or read from environment variables.
Properties
project
SignalWire project ID. Set via constructor or SIGNALWIRE_PROJECT_ID environment variable.
token
API token for authentication. Set via constructor or SIGNALWIRE_API_TOKEN environment variable.
jwtToken
JWT token for alternative authentication. Set via constructor or SIGNALWIRE_JWT_TOKEN environment variable.
When provided, project and token are not required.
host
Relay WebSocket endpoint. The default is the endpoint for SignalWire projects; you
do not set your space here. The constructor option wins, then SIGNALWIRE_RELAY_HOST,
then SIGNALWIRE_SPACE, then the default. RestClient reads SIGNALWIRE_SPACE as
the REST host, so when it is set for REST, set SIGNALWIRE_RELAY_HOST or pass host
to keep the default.
contexts
List of contexts to subscribe to for inbound call and message events.
maxActiveCalls
Maximum number of concurrent inbound calls the client will track. Calls
arriving beyond this limit are dropped with a log warning. Set via constructor
or RELAY_MAX_ACTIVE_CALLS environment variable. Constructor-only — not
accessible as a public attribute after initialization.
scheme
WebSocket scheme. Read-only. 'wss' (production, the default) or 'ws'
(loopback / local only). Set via the scheme constructor option or the
SIGNALWIRE_RELAY_SCHEME environment variable. Production should never use
'ws'.
relayProtocol
Server-assigned protocol string from the connect response. Read-only. Used internally for session resumption on reconnect.
Methods
Async Disposable
RelayClient implements Symbol.asyncDispose, so it can be used with the
await using statement for scoped connections. The client disconnects
automatically when the scope exits.
For environments without await using support, use try/finally with
disconnect().