***

title: onMessage
slug: /reference/typescript/relay/client/on-message
description: Register the inbound message handler.
max-toc-depth: 3
---------------------

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

[message]: /docs/server-sdks/reference/typescript/relay/message

Register the inbound SMS/MMS message handler. The provided function is called for
each `messaging.receive` event on the subscribed contexts. The function receives a
[`Message`][message] object with message
properties and state tracking.

Only one message handler can be active at a time. Calling `onMessage()` again
replaces the previous handler.

## **Parameters**

<ParamField path="handler" type="MessageHandler" required={true} toc={true}>
  An async function that receives a [`Message`][message] object. Signature:
  `(message: Message) => void | Promise<void>`.
</ParamField>

## **Returns**

`void`

## **Example**

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

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

client.onMessage(async (message) => {
  console.log(`SMS from ${message.fromNumber}: ${message.body}`);
});

await client.run();
```