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

# Chat WebSocket

> Stream a Retell AI chat in real time over WebSocket: get agent replies, tool calls, and node transitions, submit messages, and hand off to a human.

> [!NOTE]
>
> This is the real-time transport for an [API chat](https://docs.retellai.com/deploy/create-chat-completion.md). If you only need request and response, [Create chat completion](https://docs.retellai.com/api-references/create-chat-completion.md) sends a message and returns the agent's reply in one call. Use the WebSocket when you want the agent's reply to stream in and your UI to update as it does.

## Overview

The chat WebSocket runs a live chat session: your application submits the user's messages and receives the agent's replies, tool calls, and node transitions as JSON events, in order. Open one connection per participant in a chat you already [created](https://docs.retellai.com/api-references/create-chat.md).

Use it when a single request and response isn't enough:

* **Live chat UI** — show the agent's reply as it's generated and update the thread the moment each message is stored, instead of waiting for one completion call to return.
* **Human handoff** — when the agent escalates a chat to a person, that human connects as `transfer_target`, reads what the user sends, and replies on the same socket.
* **Multiple viewers** — up to 5 connections can follow one chat at once, so a user's browser and a supervisor can both watch.

For example, a support site opens a chat socket from the browser when the visitor starts chatting. The agent's reply streams into the thread, and when it reaches an escalation node, the site routes the conversation to a live agent who joins as `transfer_target` and takes over.

## Endpoint

> [!NOTE]
>
> WebSocket endpoint: `wss://api.retellai.com/connect-to-chat?chat_id={chat_id}&role={role}`

### Query parameters

**chat\_id** `string` *required*

Id of the chat to connect to, from [Create chat](https://docs.retellai.com/api-references/create-chat.md). The chat must belong to your workspace, be ongoing, and be an API chat — SMS chats can't be connected to.

**role** `enum\<string\>` *required*

Who you're connecting as: `user` for the person chatting with the agent, or `transfer_target` for the human a chat was escalated to.

Available options: `user`, `transfer_target`

### Authentication

Authenticate the handshake the same way you would a chat REST request: a [Retell API key](https://docs.retellai.com/accounts/manage-api-keys.md) on your server, or a [public key](https://docs.retellai.com/accounts/public-keys.md) from an allowed domain in the browser. A restricted API key needs the **Call → Edit** permission. A public key can connect as `user` only — it's visible to every visitor, so it can't vouch for the human a chat was escalated to. Connect as `transfer_target` with an API key from your backend.

### Connection limits

* **The chat must be ongoing.** Connecting to an ended chat, or an SMS chat, is rejected.
* **Up to 5 connections per chat** (as of November 2026). A sixth is rejected.
* **Up to 10,000 concurrent chats per workspace** have an open connection at once (as of November 2026). Each connection holds its place with a renewing lease, so a chat's place frees itself shortly after its last connection drops.

## Protocol

All messages are text frames whose data is a stringified JSON object. Check the `type` field to tell events apart.

### Event flow

1. Retell sends one [`init_socket`](#init-socket-event) event. It carries `last_message_timestamp`, the created time of the newest message already stored. The socket forwards only messages created after this, so list the earlier ones with [List chat messages](#listing-earlier-messages).
2. From then on, each change arrives as a message event: a [`new_message`](#new-message-event) for a user, agent, transfer target, or tool call, or a [`node_transition`](#node-transition-event) for a conversation flow move.
3. To send a message, your application sends a [`submit_message`](#submitting-messages) frame. You may send it once you've received `init_socket`; frames sent earlier are ignored.
4. When the chat is handed to a human, Retell sends a [`chat_escalated`](#chat-escalated-event) event. When the chat ends, it sends a [`chat_ended`](#chat-ended-event) event and closes the connection with code `1000`.

Message events carry an `event_timestamp`, the message's created time. Forward only events newer than the last one you handled: a message can be re-sent when a handler can't confirm its earlier broadcast reached you.

### Submitting messages

Send a `submit_message` frame to add the user's (or transfer target's) message. The agent replies unless the chat has been escalated to a human.

**type** `enum\<string\>` *required*

Available options: `submit_message`

**idempotency\_key** `string` *required*

A key you choose to identify this message, 1 to 256 characters. The message is stored once per key, so resending the same key doesn't store it twice. A message counts as delivered when it comes back as a `new_message` with this key.

**content** `string` *required*

The message text, 1 to 50,000 characters.

A message you submit while the agent is still replying interrupts the reply, so one reply answers both messages. Keep at most 10 submissions in flight on a socket at a time.

If a submission fails, Retell closes the socket. Reconnect and submit the message again from the failed one on, under the same `idempotency_key` — the key keeps it from being stored twice.

### Listing earlier messages

The socket doesn't replay a chat's history. To show messages created before you connected, call `POST /list-chat-messages/{chat_id}` up to `init_socket`'s `last_message_timestamp`, then append the events the socket sends from there. Each listed message carries the `idempotency_key` it was submitted with, the same key the socket's `new_message` events use, so a message you both list and receive is easy to match.

### Close codes

| Code | Meaning |
| - | - |
| `1000` | The chat ended. Reason `chat_ended`. |
| `1013` | The connection's lease expired. Reconnect to get your place back. |

## Events

### Init socket event

The `init_socket` event, sent once, first.

**type** `enum\<string\>` *required*

Available options: `init_socket`

**last\_message\_timestamp** `integer` *required*

Created time of the newest stored message, in milliseconds since the Unix epoch, or `0` if the chat has none. The socket sends only messages created after this.

**is\_escalated** `boolean` *required*

Whether the chat has already been handed to a human.

### New message event

A `new_message` event: a user, agent, transfer target, or tool-call message was stored. The fields depend on `role`.

**type** `enum\<string\>` *required*

Available options: `new_message`

**role** `enum\<string\>` *required*

Available options: `user`, `agent`, `transfer_target`, `tool_call_invocation`, `tool_call_result`

**event\_timestamp** `integer` *required*

The message's created time, in milliseconds since the Unix epoch.

For a `user`, `agent`, or `transfer_target` message:

**message\_id** `string` *required*

Id of the stored message.

**content** `string` *required*

The message text.

**idempotency\_key** `string`

The key the message was submitted with, so the sender can recognize its own message. Absent on messages the agent generated.

For a `tool_call_invocation` message:

**tool\_call\_id** `string`

Id of the tool call, to match with its result.

**name** `string`

Name of the tool.

**tool\_type** `string`

Kind of tool, such as `custom`, `code`, or `mcp`.

**arguments** `object | string`

Arguments passed to the tool. A stored JSON object is parsed; anything else is the stored string.

**is\_state\_transition** `boolean`

Whether the call moved the agent to another state.

For a `tool_call_result` message:

**tool\_call\_id** `string` *required*

Id of the invocation this result belongs to.

**content** `object | string`

What the tool returned. A stored JSON object is parsed; anything else is the stored string.

**successful** `boolean`

Whether the tool call succeeded.

### Node transition event

A `node_transition` event: a conversation flow agent moved to another node.

**type** `enum\<string\>` *required*

Available options: `node_transition`

**event\_timestamp** `integer` *required*

When the move happened, in milliseconds since the Unix epoch.

**former\_node\_id** `string` *required*

Id of the node the agent left.

**former\_node\_name** `string` *required*

Name of the node the agent left.

**new\_node\_id** `string` *required*

Id of the node the agent entered.

**new\_node\_name** `string` *required*

Name of the node the agent entered.

**transition\_type** `enum\<string\>` *required*

How the node was reached: `normal`, `global`, `global_go_back`, or `interrupt_go_back`.

### Chat escalated event

A `chat_escalated` event: the chat was handed to a human.

**type** `enum\<string\>` *required*

Available options: `chat_escalated`

**event\_timestamp** `integer` *required*

When the chat was escalated, in milliseconds since the Unix epoch.

**destination** `object` *required*

The kind of destination only. Where the handoff goes stays between you and Retell, since the socket can reach an end user's browser.

**properties**

**type** `string` *required*

The destination type.

### Chat ended event

A `chat_ended` event, the last one before Retell closes the connection with code `1000`.

**type** `enum\<string\>` *required*

Available options: `chat_ended`

**event\_timestamp** `integer` *required*

When the chat ended, in milliseconds since the Unix epoch.

**disconnection\_reason** `string`

Why the chat ended, when recorded.

## Sample events

```json init_socket theme={"dark"}
{
  "type": "init_socket",
  "last_message_timestamp": 1764460800000,
  "is_escalated": false
}
```

```json submit_message (you send this) theme={"dark"}
{
  "type": "submit_message",
  "idempotency_key": "4b1f8e2a-7c9d-4a1e-9f3b-2d6c8a0e5f11",
  "content": "Can I move my cleaning to Thursday?"
}
```

```json new_message theme={"dark"}
{
  "type": "new_message",
  "role": "agent",
  "message_id": "message_9f2e1c7a4b",
  "event_timestamp": 1764460803000,
  "content": "Sure — Thursday at 10:00 AM or 2:30 PM works. Which do you prefer?"
}
```

```json chat_ended theme={"dark"}
{
  "type": "chat_ended",
  "event_timestamp": 1764460860000,
  "disconnection_reason": "agent_hangup"
}
```

## FAQ

> [accordion] Does the socket send the chat's earlier messages?
>
> No. It sends `init_socket` and then only messages created after `last_message_timestamp`. List the earlier ones with `POST /list-chat-messages/{chat_id}` up to that timestamp.

> [accordion] How do I know my submitted message was stored?
>
> It comes back as a `new_message` event carrying the `idempotency_key` you submitted it with. Until then, treat it as pending.

> [accordion] A submission failed and the socket closed. What now?
>
> Reconnect and submit again from the failed message on, under the same `idempotency_key`. The key keeps the message from being stored twice.

> [accordion] Can I use this for SMS chats?
>
> No. Only API chats can be connected to. SMS chats run over Twilio — see [Enable SMS](https://docs.retellai.com/deploy/enable-sms.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.