# Heartbeat

### Overview

To keep a WebSocket session active, clients must periodically send a **heartbeat message** to the server. If the server does not receive a heartbeat within **one minute**, the connection is considered **inactive** and will be **terminated automatically**.

Implement a recurring heartbeat routine on the client side to ensure the connection remains open. See the example below for reference.

### Rate Limits

Each WebSocket connection is limited to **60 total messages per minute**, including **subscribe**, **unsubscribe**, and **heartbeat** messages.

If this limit is exceeded:

* The connection will be **automatically closed**.
* Repeated violations may be treated as **abuse**, and the associated **API Key** may be **suspended**.

Design your message-sending logic carefully to stay within these limits.

### **Error Code Explanation**

Please refer to the [WebSocket error code](/en-docs/getting-started/error-codes/websocket-error-codes.md) documentation for further details.

### **Subscription Address**

Please refer to the [WebSocket subscription addresses](#subscription-address).

### Heartbeat Message (Protocol Number: 10010)

```json
{
    "code": 10010,
    "trace": "423afec425004bd8a5e02e1ba5f9b2b0"
}
```

### Parameters

| Field   | Type    | Required | Description                                                                                  | Example                            |
| ------- | ------- | -------- | -------------------------------------------------------------------------------------------- | ---------------------------------- |
| `code`  | Integer | Yes      | The message type identifier. Use `10010` to indicate a heartbeat message.                    | `10010`                            |
| `trace` | String  | Yes      | A unique request ID used for tracking and debugging. Typically a random UUID or hash string. | `423afec425004bd8a5e02e1ba5f9b2b0` |

### Notes

* **Send heartbeats regularly**, typically every **30 seconds**.
* If no heartbeat is received for **more than 60 seconds**, the server will close the connection.
* The `trace` field should be **unique per message** to make it easier to trace and debug message logs.
* Keep the **aggregate send rate** (including all message types) below **60 per minute** to avoid throttling or key suspension.
* If the connection closes due to timeout, the client should **reconnect and re-subscribe** to restore the session.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.infoway.io/en-docs/websocket/heartbeat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
