> ## Documentation Index
> Fetch the complete documentation index at: https://developers.telnyx.com/llms.txt
> Use this file to discover all available pages before exploring further.

# onState()

> **onState**(`listener`): () => `void`

Subscribe to state updates; returns an unsubscribe function.

**Parameters**

| Parameter | Type |
| - | - |
| `listener` | `StateListener`\<`TState`> |

**Returns**

() => `void`

## The Local Copy

The client keeps a local copy of the agent's state. Every connection, reconnects included, starts the copy from a snapshot. Each [`setState`](/docs/agent-sdk/interface/setState) on the agent then arrives as an RFC 7396 merge patch. Each [`replaceState`](/docs/agent-sdk/interface/replaceState) arrives as a new snapshot.

A listener receives the whole copy after every change. A listener added after the first snapshot is called at once with the current copy.
