Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/pages/docs/chat/rooms/history.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,25 @@ The following optional parameters can be passed when retrieving previously sent

| Parameter | Description |
| --------- | ----------- |
| start | Earliest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp equal to, or greater than, this value will be returned. |
| end | Latest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp less than this value will be returned. |
| orderBy | The order in which to retrieve messages from; either `oldestFirst` or `newestFirst`. |
| limit | Maximum number of messages to be retrieved per page, up to 1,000. |
| start | Earliest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp equal to, or greater than, this value will be returned. **Defaults to no lower bound (earliest available message).** |
| end | Latest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp less than this value will be returned. **Defaults to current time.** |
| orderBy | The order in which to retrieve messages from; either `oldestFirst` or `newestFirst`. **Defaults to `newestFirst`.** |
| limit | Maximum number of messages to be retrieved per page, up to 1,000. **Defaults to 100.** |

### Understanding message ordering <a id="understanding-message-ordering"/>

The `orderBy` parameter determines both *which* messages are retrieved and their return order:

| Order | Retrieves | Returns | Use case |
| ----- | --------- | ------- | -------- |
| `newestFirst` (default) | Most recent messages | `[newest, ..., oldest]` | Standard chat UIs |
| `oldestFirst` | Oldest messages from room start | `[oldest, ..., newest]` | Archives, exports, admin tools |

**For typical chat applications:** Use `newestFirst` (default) to get recent messages, then reverse the result for chronological display.

## Retrieve messages sent prior to subscribing <a id="subscribe"/>

Users can also retrieve historical messages that were sent to a room before the point that they registered a listener by [subscribing](/docs/chat/rooms/messages#subscribe). The order of messages returned is from most recent, to oldest. This is useful for providing conversational context when a user first joins a room, or when they subsequently rejoin it later on. It also ensures that the message history they see is continuous, without any overlap of messages being returned between their subscription and their history call.
Retrieve historical messages sent before subscribing using [`historyBeforeSubscribe()`](#understanding-message-ordering). Messages are returned in `newestFirst` order. This is useful for providing conversational context when a user joins or rejoins a room, ensuring continuous message history without overlap.

<If lang="javascript,swift,kotlin">
Use the <If lang="javascript">[`historyBeforeSubscribe()`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.MessageSubscriptionResponse.html#historyBeforeSubscribe)</If><If lang="swift">[`historyBeforeSubscribe(withParams:)`](https://sdk.ably.com/builds/ably/ably-chat-swift/main/AblyChat/documentation/ablychat/messagesubscriptionresponse/historybeforesubscribe%28withparams%3A%29))</If><If lang="kotlin">[`getPreviousMessages()`](https://sdk.ably.com/builds/ably/ably-chat-kotlin/main/dokka/chat/com.ably.chat/-messages-subscription/get-previous-messages.html)</If> function returned as part of a [message subscription](/docs/chat/rooms/messages#subscribe) response to only retrieve messages that were received before the listener was subscribed to the room. This returns a paginated response, which can be queried further to retrieve the next set of messages.
Expand Down Expand Up @@ -169,10 +180,14 @@ println("End of messages")
```
</Code>

The following parameters can be passed when retrieving previously sent messages:
The following optional parameters can be passed when retrieving messages before subscribing:

| Parameter | Description |
| --------- | ----------- |
| start | Earliest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp equal to, or greater than, this value will be returned. |
| end | Latest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp less than this value will be returned. |
| limit | Maximum number of messages to be retrieved per page, up to 1,000. |
| start | Earliest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp equal to, or greater than, this value will be returned. **Defaults to no lower bound (earliest available message).** |
| end | Latest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp less than this value will be returned. **Defaults to current time.** |
| limit | Maximum number of messages to be retrieved per page, up to 1,000. **Defaults to 100.** |

<Aside>
The `orderBy` parameter is not available for `historyBeforeSubscribe()`. Messages are always returned in `newestFirst` order.
</Aside>