Skip to main content

Relay.Client

Relay.Client

Relay.Client is the basic connection to Relay, allowing you send commands to Relay and setup handlers for inbound events.

Constructor

Constructs a client object to interact with Relay.

Parameters

ParameterTypeRequiredDescription
projectstringrequiredProject ID from your SignalWire Space
tokenstringrequiredToken from your SignalWire Space

Examples

Create a Client to interact with the Relay API:

from signalwire.relay.client import Client

client = Client(project='<project-id>', token='<project-token>')

Properties

PropertyTypeDescription
connectedbooleanReturns true if the client is connected to Relay.
callingRelay.CallingReturns a Relay.Calling instance associated with the client.
taskingRelay.TaskingReturns a Relay.Tasking instance associated with the client.
messagingRelay.MessagingReturns a Relay.Messaging instance associated with the client.

Methods

connect

Activates the connection to the Relay API. The connection to Relay does not happen automatically so that you can setup handlers to events that might occur before the connection is successfully established.

Returns

None

Examples

# Make sure you have attached the listeners you need before connecting the client, or you might miss some events.
client.connect()

disconnect

Coroutine that close the Relay connection, cancel the pending tasks and stop the event loop.

Returns

awaitable - A coroutine object.

Examples

Within an async function:

import asyncio

# with a 'client' variable already created..
async def sleep_and_disconnect():
await asyncio.sleep(5)
await client.disconnect()
print('Client disconnected!')

asyncio.run(sleep_and_disconnect())

Scheduling a new asyncio task:

import asyncio

# schedule a task to disconnect the client..
asyncio.create_task(client.disconnect())

on

Attach an event handler for a specific type of event. The handler could be a coroutine or a normal function.

Parameters

ParameterTypeRequiredDescription
eventstringrequiredEvent name. Full list of events Relay.Client Events
handlerfunctionrequiredFunction to call when the event comes.

Returns

Relay.Client - The client object itself.

Examples

Subscribe to the ready event:

def do_something(client):
pass

client.on('ready', do_something)

off

Remove an event handler that were attached with .on(). If no handler parameter is passed, all listeners for that event will be removed.

Parameters

ParameterTypeRequiredDescription
eventstringrequiredEvent name. Full list of events Relay.Client Events
handlerfunctionoptionalFunction to remove.
Note: handler will be removed from the stack by reference so make sure to use the same reference in both .on() and .off() methods.

Returns

Relay.Client - The client object itself.

Examples

Subscribe to the ready event and then, remove the handler:

def do_something(client):
pass

client.on('ready', do_something)

# .. later
client.off('ready', do_something)

Events

All available events you can attach a listener on.

PropertyDescription
readyThe session has been established and all other methods can now be used.
signalwire.socket.openThe websocket is open. However, you have not yet been authenticated.
signalwire.socket.errorThe websocket gave an error.
signalwire.socket.messageThe client has received a message from the websocket.
signalwire.socket.closeThe websocket is closing.