on

View as MarkdownOpen in Claude

Register an event listener for state changes on this message. The handler is called each time a messaging.state event is received for this message, allowing you to react to intermediate states before the terminal state is reached.

Parameters

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

A function that receives a RelayEvent on each state change. Both synchronous and async handlers are supported.

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
9await client.connect();
10
11const message = await client.sendMessage({
12 to: '+15551234567',
13 from: '+15559876543',
14 body: 'Order confirmed',
15});
16
17const onStateChange = (event) => {
18 console.log(`Message ${message.messageId} -> ${message.state}`);
19};
20
21message.on(onStateChange);
22await message.wait();
23
24await client.disconnect();