Retrieve historical message actions

Showing JavaScript examples.

This guide shows you how to fetch message actions for a channel from Message Persistence, sorted oldest first by the action's own timetoken.

  • Fetch every stored action for a channel.
  • Narrow the fetch to a timetoken range with start, end, and limit.
  • Page through a response that was truncated because it hit an internal limit.

Every call on this page needs an SDK instance initialized with your subscribe key. Examples use the JavaScript, Swift, Java, Kotlin, C#, Python, and Objective-C SDKs. For any other language, refer to Available SDKs.

Before retrieving message actions, confirm the prerequisites are in place.

Fetch actions for a channel​

Call the fetch method with just the channel name to get every stored action for it, oldest first.

1const response = await pubnub.getMessageActions({
2 channel: 'chats.room1',
3});
4
5response.data.forEach((action) => {
6 console.log(`${action.type}: ${action.value}`);
7});

Narrow the fetch to a time range​

Bound the fetch with start, end, and limit when you don't need the whole channel history. For example, fetch only the actions added since a client last caught up.

ParameterBehavior
startNewer boundary, exclusive. Must be a higher timetoken value than end when both are set.
endOlder boundary, inclusive. Must be a lower timetoken value than start when both are set.
limitMaximum actions to return in one call. Default and maximum is 100.
1const response = await pubnub.getMessageActions({
2 channel: 'chats.room1',
3 start: '15646822873784630',
4 end: '15645905639093361',
5 limit: 50,
6});

Page through a truncated response​

PubNub truncates the response when it hits an internal limit, even if you asked for fewer actions than exist. How a truncated response tells you there's more depends on the SDK.

C# returns a More object with the Start, End, and Limit to use for the next call:

1{
2 "MessageActions": [ /* ... */ ],
3 "More": {
4 "Start": 15610547826970050,
5 "End": 15645905639093361,
6 "Limit": 2
7 }
8}

Kotlin (and PHP) return a page object instead. Pass it straight into another call:

1pubnub.getMessageActions(channel = "chats.room1").async { result ->
2 result.onSuccess { res ->
3 res.actions?.forEach { println("${it.type}: ${it.value}") }
4 res.page?.let { nextPage ->
5 pubnub.getMessageActions(channel = "chats.room1", page = nextPage).async { /* fetch the rest */ }
6 }
7 }
8}

For any SDK, a response containing exactly limit actions means more may exist. Repeat the call using the oldest action's own timetoken as the next end to keep paging toward older history. Check your language's Message Actions API reference for the exact field name your SDK uses to report truncation.

Was this page useful?

Last updated on