***

title: on
slug: /reference/python/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/python/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="Callable[[RelayEvent], None]" required={true} toc={true}>
  A function or coroutine that receives a [`RelayEvent`][relayevent]
  on each state change. Both synchronous and async handlers are supported.
</ParamField>

## **Returns**

`None`

## **Example**

```python {22}
import asyncio
from signalwire.relay import RelayClient

client = RelayClient(
    project="your-project-id",
    token="your-api-token",
    host="your-space.signalwire.com",
    contexts=["default"],
)

async def track_state():
    async with client:
        message = await client.send_message(
            to_number="+15551234567",
            from_number="+15559876543",
            body="Order confirmed",
        )

        def on_state_change(event):
            print(f"Message {message.message_id} -> {message.state}")

        message.on(on_state_change)
        await message.wait()

asyncio.run(track_state())
```