Skip to main content
Android
iOS
macOS
Web
Windows
Flutter
Linux C++
Unity

Message history (Beta)

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.

info

The historical message feature is currently available for User Channels and Message Channels, but not for Stream Channels.

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 page.

  • Enabled the message history feature by contacting support@agora.io. You can request a storage duration of 1, 7, 30, 90, or 365 days, or opt for permanent storage.

    info

    Changes to storage duration only affect future messages.

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:


_17
PublishOptions options = new PublishOptions();
_17
options.setCustomType("custom type");
_17
options.setStoreInHistory(true);
_17
_17
String channelName = "chatRoom";
_17
String message = "Hello world!";
_17
rtm.publish(channelName, message, options, new ResultCallback<Void>() {
_17
@Override
_17
public void onSuccess(Void responseInfo) {
_17
log(CALLBACK, "send message success");
_17
}
_17
_17
@Override
_17
public void onFailure(ErrorInfo errorInfo) {
_17
log(ERROR, errorInfo.toString());
_17
}
_17
});

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


_18
PublishOptions options = new PublishOptions();
_18
options.setChannelType(RtmChannelType.USER);
_18
options.setCustomType("custom type");
_18
options.setStoreInHistory(true);
_18
_18
String userId = "Tony";
_18
String message = "Hello world!";
_18
rtm.publish(userId, message, options, new ResultCallback<Void>() {
_18
@Override
_18
public void onSuccess(Void responseInfo) {
_18
log(CALLBACK, "send message success");
_18
}
_18
_18
@Override
_18
public void onFailure(ErrorInfo errorInfo) {
_18
log(ERROR, errorInfo.toString());
_18
}
_18
});

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:


_19
GetHistoryMessagesOptions options = new GetHistoryMessagesOptions();
_19
options.setMessageCount(50);
_19
options.setEnd(1688978391800);
_19
_19
String channelName = "chatRoom";
_19
rtm.getHistory().getMessages(channelName, RtmChannelType.MESSAGE, options, new ResultCallback<GetMessagesResult>() {
_19
@Override
_19
public void onSuccess(GetMessagesResult result) {
_19
log(CALLBACK, "get history messages! new start: " + result.getNewStart());
_19
for (HistoryMessage message : result.getMessageList()) {
_19
log(INFO, message.toString());
_19
}
_19
}
_19
_19
@Override
_19
public void onFailure(ErrorInfo errorInfo) {
_19
log(ERROR, errorInfo.toString());
_19
}
_19
});

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

ParameterBehavior
startReturns messages sent before the specified timestamp. The timestamp itself is excluded.

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

Retrieve messages up to an end timestamp

ParameterBehavior
endReturns the n most recent messages sent up to and including the specified timestamp.

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

Retrieve messages between two timestamps

ParameterBehavior
startReturns messages sent before this timestamp (excluded).
endReturns messages sent up to and including this timestamp.

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

Signaling