Retrieve historical message actions
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, andlimit. - 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.
- JavaScript
- Swift
- Java
- Kotlin
- C#
- Python
- Objective-C
1const response = await pubnub.getMessageActions({
2 channel: 'chats.room1',
3});
4
5response.data.forEach((action) => {
6 console.log(`${action.type}: ${action.value}`);
7});
1pubnub.fetchMessageActions(channel: "chats.room1") { result in
2 switch result {
3 case let .success((actions, _)):
4 actions.forEach { print("\($0.actionType): \($0.actionValue)") }
5 case let .failure(error):
6 print("failed: \(error.localizedDescription)")
7 }
8}
1pubnub.getMessageActions()
2 .channel("chats.room1")
3 .async(result -> result.onSuccess(res -> {
4 for (PNMessageAction action : res.getActions()) {
5 System.out.println(action.getType() + ": " + action.getValue());
6 }
7 }));
1pubnub.getMessageActions(channel = "chats.room1").async { result ->
2 result.onSuccess { res ->
3 res.actions?.forEach { println("${it.type}: ${it.value}") }
4 }
5}
1pubnub.GetMessageActions()
2 .Channel("chats.room1")
3 .Execute(new PNGetMessageActionsResultExt((result, status) => {
4 if (result != null) {
5 foreach (var action in result.MessageActions) {
6 Console.WriteLine(action.Action.Type + ": " + action.Action.Value);
7 }
8 }
9 }));
1def get_actions_callback(result, status):
2 if status.is_error():
3 print(status.error_data.information)
4 else:
5 for action in result.actions:
6 print(f"{action.type}: {action.value}")
7
8pubnub.get_message_actions() \
9 .channel("chats.room1") \
10 .pn_async(get_actions_callback)
1PNFetchMessageActionsRequest *request =
2 [PNFetchMessageActionsRequest requestWithChannel:@"chats.room1"];
3
4[self.client fetchMessageActionsWithRequest:request
5 completion:^(PNFetchMessageActionsResult *result, PNErrorStatus *status) {
6 if (!status.isError) {
7 for (PNMessageAction *action in result.data.actions) {
8 NSLog(@"%@: %@", action.type, action.value);
9 }
10 }
11}];
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.
| Parameter | Behavior |
|---|---|
start | Newer boundary, exclusive. Must be a higher timetoken value than end when both are set. |
end | Older boundary, inclusive. Must be a lower timetoken value than start when both are set. |
limit | Maximum actions to return in one call. Default and maximum is 100. |
- JavaScript
- Java
- C#
- Python
1const response = await pubnub.getMessageActions({
2 channel: 'chats.room1',
3 start: '15646822873784630',
4 end: '15645905639093361',
5 limit: 50,
6});
1pubnub.getMessageActions()
2 .channel("chats.room1")
3 .start(15646822873784630L)
4 .end(15645905639093361L)
5 .limit(50)
6 .async(result -> { /* check result */ });
1pubnub.GetMessageActions()
2 .Channel("chats.room1")
3 .Start(15646822873784630)
4 .End(15645905639093361)
5 .Limit(50)
6 .Execute(new PNGetMessageActionsResultExt((result, status) => { /* check result */ }));
1pubnub.get_message_actions() \
2 .channel("chats.room1") \
3 .start("15646822873784630") \
4 .end("15645905639093361") \
5 .limit(50) \
6 .pn_async(get_actions_callback)
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.
Related tasks
- Message Actions in PubNub. The type/value model, the event model, and the Message Persistence requirement.
- Add message actions. Attach the actions this call fetches.
- Receive message actions. Handle an action live instead of fetching it after the fact.
- Remove message actions. Delete an action once you've found its
actionTimetokenhere. - Retrieve message history. Fetch the messages these actions are attached to.