on

View as MarkdownOpen in Claude

Register a listener for events on this call. The handler is called each time an event of the specified type is received. Handlers can be regular functions or async functions.

Parameters

eventType
stringRequired

The event type string to listen for (e.g., "calling.call.state", "calling.call.play"). See the Events section on the Call page for class-level events, or individual method pages for method-specific events.

handler
(event: RelayEvent) => void | Promise<void>Required

Function to invoke when the event fires. Receives a RelayEvent instance.

Returns

void

Example

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['default']
7});
8
9client.onCall(async (call) => {
10 await call.answer();
11
12 // Register a listener for play events
13 call.on('calling.call.play', (event) => console.log(`Play state: ${event.params.state}`));
14
15 // Register a listener for state changes
16 const onStateChange = (event) => {
17 console.log(`Call state changed: ${event.params.call_state}`);
18 };
19 call.on('calling.call.state', onStateChange);
20
21 const action = await call.play([{ type: 'tts', text: 'Hello!' }]);
22 await action.wait();
23});
24
25await client.run();