connect

View as MarkdownOpen in Claude

Establish a WebSocket connection to SignalWire Relay and authenticate. This method connects to wss://<host>, sends a signalwire.connect authentication request, subscribes to the configured contexts, and starts the internal receive loop.

For most use cases, prefer run() which calls connect() internally and adds automatic reconnection. Use connect() directly only when you need manual control over the event loop or are using the async context manager.

By default, only one RelayClient connection is allowed per process. Calling connect() on a second instance without disconnecting the first raises RuntimeError. Set the RELAY_MAX_CONNECTIONS environment variable to allow multiple concurrent connections.

Parameters

None.

Returns

None

Examples

Manual connect/disconnect

import asyncio
from signalwire.relay import RelayClient
client = RelayClient(
project="your-project-id",
token="your-api-token",
contexts=["default"],
)
async def main():
await client.connect()
# Connected and authenticated -- do work here
call = await client.dial(
devices=[[{"type": "phone", "params": {"to_number": "+15559876543", "from_number": "+15551234567"}}]]
)
await call.hangup()
await client.disconnect()
asyncio.run(main())

Async context manager

import asyncio
from signalwire.relay import RelayClient
client = RelayClient(
project="your-project-id",
token="your-api-token",
contexts=["default"],
)
async def main():
async with client:
call = await client.dial(
devices=[[{"type": "phone", "params": {"to_number": "+15559876543", "from_number": "+15551234567"}}]]
)
await call.hangup()
asyncio.run(main())