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

import { RelayClient } from '@signalwire/sdk';
const client = new RelayClient({
project: process.env.SIGNALWIRE_PROJECT_ID!,
token: process.env.SIGNALWIRE_API_TOKEN!,
contexts: ['default']
});
await client.connect();
const message = await client.sendMessage({
toNumber: '+15551234567',
fromNumber: '+15559876543',
body: 'Order confirmed',
});
const onStateChange = (event) => {
console.log(`Message ${message.messageId} -> ${message.state}`);
};
message.on(onStateChange);
await message.wait();
await client.disconnect();