# Message history (Beta) (/en/realtime-media/rtm/build/send-and-receive-messages/message-history/web)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

The Signaling message history feature allows you to store messages when publishing them to a channel. For example, if a user joins a channel midway through a conversation, you can retrieve messages that were published before the user joined. You can configure the message retention period for each project, ranging from 1 day to permanent storage.

When a message is published, it is stored using the channel name and the message timestamp. You can use this information to retrieve historical messages.

<CalloutContainer type="info">
  <CalloutDescription>
    The historical message feature is currently available for User Channels and Message Channels, but not for Stream Channels.
  </CalloutDescription>
</CalloutContainer>

## Prerequisites

Before implementing this feature, ensure that you have:

* Integrated the Signaling SDK in your project, and implemented the framework functionality from the [SDK quickstart](../../index) page.
* Enabled the message history feature by contacting [support@agora.io](mailto\:support@agora.io). You can request a storage duration of 1, 7, 30, 90, or 365 days, or opt for permanent storage.

  <CalloutContainer type="info">
    <CalloutDescription>
      Changes to storage duration only affect future messages.
    </CalloutDescription>
  </CalloutContainer>

## Implement message history

This section shows how to store and retrieve messages from history.

### Store messages

To store a message on the server, set `setStoreInHistory` to `true` in the `publish()` method. The following example shows the minimum code to store a message published to a message channel:

```javascript
try {
  const result = await rtm.publish(
    "chatroom",
    "Hello world!",
    {
      channelType: "MESSAGE",
      customType: "STRING",
      storeInHistory: true, // Overrides the default storage option
    }
  );
} catch (status) {
  console.log(status);
}
```

To store a message in the history of a user channel, use the same parameter:

```javascript
try {
  const result = await rtm.publish(
    "Tony", // User ID
    "Hello world!",
    {
      channelType: "USER",
      customType: "STRING",
      storeInHistory: true, // Overrides the default storage option
    }
  );
} catch (status) {
  console.log(status);
}
```

### Retrieve messages

The SDK provides the `getMessages()` method to retrieve up to 100 history messages at a time. You can filter messages by setting the `start` and `end` parameters to define a time range. To retrieve only the most recent messages, such as those sent between the last offline time and now, set only the `end` parameter.

The following example retrieves the latest 50 messages sent before a specific time:

```javascript
try {
  const result = await rtmClient.history.getMessages(
    "chat_room",
    "MESSAGE",
    {
      messageCount: 50,
      end: 1688978391800,
    }
  );
} catch (status) {
  const { operation, reason, errorCode } = status;
  console.log(`${operation} failed. Error code: ${errorCode}, reason: ${reason}.`);
}
```

The `result` includes a `newStart` field, which indicates whether there are unread messages in the history:

* If `newStart` is `0`, all messages have been retrieved.
* If `newStart` is not `0`, use this value as the new `start` parameter in the next `getMessages()` call to continue retrieving messages.

The following use-cases show how to specify the `start` and `end` parameters to retrieve messages in a specific range:

#### Retrieve before a start timestamp

| Parameter | Behavior                                                                                |
| --------- | --------------------------------------------------------------------------------------- |
| `start`   | Returns messages sent before the specified timestamp. The timestamp itself is excluded. |

```text
Timeline:
[Older messages] ------------ start timestamp ------------ [Newer messages]
[             ←-- n messages]
```

#### Retrieve messages up to an end timestamp

| Parameter | Behavior                                                                             |
| --------- | ------------------------------------------------------------------------------------ |
| `end`     | Returns the n most recent messages sent up to and including the specified timestamp. |

```text
Timeline:
[Older messages] ------------ end timestamp ------------ [Newer messages]
                                       [  ←-- n messages]
```

#### Retrieve messages between two timestamps

| Parameter | Behavior                                                  |
| --------- | --------------------------------------------------------- |
| `start`   | Returns messages sent before this timestamp (excluded).   |
| `end`     | Returns messages sent up to and including this timestamp. |

```text
Timeline:
[Older messages] ---- end timestamp ------------ start timestamp ---- [Newer messages]
                               [ ←-- n messages]
```

    
  
      
  
      
  
      
  
      
  
      
  
