***

title: on
slug: /reference/typescript/relay/message/on
description: Register an event listener for state changes on this message.
max-toc-depth: 3
---------------------

For a complete index of all SignalWire documentation pages, fetch https://signalwire.com/docs/llms.txt

[relayevent]: /docs/server-sdks/reference/typescript/relay/events

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**

<ParamField path="handler" type="(event: RelayEvent) => void | Promise<void>" required={true} toc={true}>
  A function that receives a [`RelayEvent`][relayevent]
  on each state change. Both synchronous and async handlers are supported.
</ParamField>

## **Returns**

`void`

## **Example**

```typescript {21}
import { RelayClient } from '@signalwire/sdk';

const client = new RelayClient({
  project: process.env.SIGNALWIRE_PROJECT_ID!,
  token: process.env.SIGNALWIRE_TOKEN!,
  contexts: ['default']
});

await client.connect();

const message = await client.sendMessage({
  to: '+15551234567',
  from: '+15559876543',
  body: 'Order confirmed',
});

const onStateChange = (event) => {
  console.log(`Message ${message.messageId} -> ${message.state}`);
};

message.on(onStateChange);
await message.wait();

await client.disconnect();
```