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

1import asyncio
2from signalwire.relay import RelayClient
3
4client = RelayClient(
5 project="your-project-id",
6 token="your-api-token",
7 host="your-space.signalwire.com",
8 contexts=["default"],
9)
10
11async def main():
12 await client.connect()
13 # Connected and authenticated -- do work here
14 call = await client.dial(
15 devices=[[{"type": "phone", "params": {"to_number": "+15559876543", "from_number": "+15551234567"}}]]
16 )
17 await call.hangup()
18 await client.disconnect()
19
20asyncio.run(main())

Async context manager

1import asyncio
2from signalwire.relay import RelayClient
3
4client = RelayClient(
5 project="your-project-id",
6 token="your-api-token",
7 host="your-space.signalwire.com",
8 contexts=["default"],
9)
10
11async def main():
12 async with client:
13 call = await client.dial(
14 devices=[[{"type": "phone", "params": {"to_number": "+15559876543", "from_number": "+15551234567"}}]]
15 )
16 await call.hangup()
17
18asyncio.run(main())