Message Persistence API for Swift Native SDK
Message Persistence gives you real-time access to the history of messages published to PubNub. Each message is timestamped to the nearest 10 nanoseconds and stored across multiple availability zones in several geographic locations. You can encrypt stored messages with AES-256 so they are not readable on PubNub’s network. For details, see Message Persistence.
You control how long messages are stored through your account’s retention policy. Options include: 1 day, 7 days, 30 days, 3 months, 6 months, 1 year, or Unlimited.
You can retrieve the following:
- Messages
- Message reactions
- Files (using the File Sharing API)
Fetch history
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
Fetch historical messages from one or more channels. Use includeMessageActions to include message actions.
It's possible to control how messages are returned and in what order.
- If you specify only the
startparameter (withoutend), you receive messages older than thestarttimetoken. - If you specify only the
endparameter (withoutstart), you receive messages from thatendtimetoken and newer. - If you specify both
startandend, you receive messages between those timetokens (inclusive ofend).
This function returns up to 100 messages on a single channel, or 25 per channel on up to 500 channels. To page, iteratively update the start timetoken.
Method(s)
Use the following method(s) in the Swift SDK:
1func fetchMessageHistory(
2 for channels: [String],
3 includeActions actions: Bool = false,
4 includeMeta: Bool = false,
5 includeUUID: Bool = true,
6 includeMessageType: Bool = true,
7 includeCustomMessageType: Bool = false,
8 page: PubNubBoundedPage? = PubNubBoundedPageBase(),
9 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
10 completion: ((Result<(messagesByChannel: [String: [PubNubMessage]], next: PubNubBoundedPage?), Error>) -> Void)?
11)
| Parameter | Description |
|---|---|
for *Type: [String] Default: n/a | The list of channels to fetch history messages from. Maximum of 500 channels are allowed. |
includeActionsType: Bool Default: false | If true any Message Actions will be included in the response. When set to true the method is limited to retrieving history from a single channel. |
includeMetaType: Bool Default: false | If true the meta properties of messages will be included in the response. |
includeUUIDType: Bool Default: true | If true the user ID of the sender will be included in the response. |
includeMessageTypeType: Bool Default: true | Indicates whether to retrieve messages with PubNub message type. For more information, refer to Retrieving Messages. |
includeCustomMessageTypeType: Bool Default: false | Indicates whether to retrieve messages with the custom message type. For more information, refer to Retrieving Messages. |
pageType: PubNubBoundedPage? Default: PubNubBoundedPageBase() | The paging object used for pagination. It allows you to specify a range of messages to retrieve based on specific time bounds. Set limit to control the number of results per page. Maximum value is 100 for a single channel, 25 for multiple channels, and 25 when includeActions is true. |
customDefault: PubNub.RequestConfiguration() | An object that allows for per-request customization of PubNub configuration or network session. For more information, refer to the Request Configuration section. |
completionType: ((Result<(messagesByChannel: [String: [PubNubMessage]], next: PubNubBoundedPage?), Error>) -> Void)?Default: nil | The async Result of the method call. |
Truncated response
If truncated, a next property is returned with additional parameters. Make iterative calls adjusting parameters.
Completion handler result
Success
A dictionary of channels mapped to their message lists, and the next page cursor when available.
1public protocol PubNubMessage {
2
3 /// The message sent on the channel
4 var payload: JSONCodable { get set }
5
6 /// Message reactions associated with this message
7 var actions: [PubNubMessageAction] { get set }
8
9 /// Message sender identifier
10 var publisher: String? { get set }
11
12 /// The channel for which the message belongs
13 var channel: String { get }
14
15 /// The channel group or wildcard subscription match (if exists)
show all 41 lines1public protocol PubNubBoundedPage {
2
3 /// The start value for the next set of remote data
4 var start: Timetoken? { get }
5
6 /// The bounded end value that will be eventually fetched to
7 var end: Timetoken? { get }
8
9 /// The previous limiting value (if any)
10 var limit: Int? { get }
11}
Failure
An error describing the failure.
Sample code
Reference code
Retrieve the last message on a channel:
1
Other examples
Retrieve messages newer or equal than a given timetoken
1
Retrieve messages older than a specific timetoken
1
Retrieve the last 10 messages on channelSwift, otherChannel, and myChannel
1
Retrieve messages and their metadata
1
Retrieve messages and their message action data
1
Delete messages from history
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
Removes the messages from the history of a specific channel.
Required setting
Enable Delete-From-History in key settings and initialize with a secret key.
Method(s)
To Delete Messages from History you can use the following method(s) in the Swift SDK.
1func deleteMessageHistory(
2 from channel: String,
3 start: Timetoken? = nil,
4 end: Timetoken? = nil,
5 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
6 completion: ((Result<Void, Error>) -> Void)?
7)
| Parameter | Description |
|---|---|
from *Type: String Default: n/a | The channel to delete the messages from. |
startType: timetoken?Default: nil | timetoken delimiting the start of time slice (inclusive) to delete messages from. |
endType: timetoken?Default: nil | timetoken delimiting the end of time slice (exclusive) to delete messages from. |
customDefault: PubNub.RequestConfiguration() | An object that allows for per-request customization of PubNub configuration or network session. For more information, refer to the Request Configuration section. |
completionType: ((Result<Void, Error>) -> Void)?Default: nil | The async Result of the method call |
Completion handler result
Success
A Void indicating a success.
Failure
An Error describing the failure.
Sample code
1
Other examples
Delete specific message from history
1
Message counts
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal.
Return the number of messages published since the given time; the count is messages with timetoken ≥ the provided value.
Unlimited message retention
For keys with unlimited message retention enabled, this method considers only messages published in the last 30 days.
Method(s)
You can use the following method(s) in the Swift SDK:
1func messageCounts(
2 channels: [String: Timetoken],
3 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
4 completion: ((Result<[String: Int], Error>) -> Void)?
5)
| Parameter | Description |
|---|---|
channels *Type: [String: timetoken]Default: n/a | The map of channels and the timetoken to get the message count for. |
customDefault: PubNub.RequestConfiguration() | An object that allows for per-request customization of PubNub configuration or network session. For more information, refer to the Request Configuration section. |
completionType: ((Result<[String: Int], Error>) -> Void)?Default: nil | The async Result of the method call. |
1func messageCounts(
2 channels: [String],
3 timetoken: Timetoken = 1,
4 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
5 completion: ((Result<[String: Int], Error>) -> Void)?
6)
| Parameter | Description |
|---|---|
channels *Type: [String] Default: n/a | The list of channels to get message counts for. |
timetokenType: timetokenDefault: 1 | The timetoken for all channels in the list to get message counts for. |
customDefault: PubNub.RequestConfiguration() | An object that allows for per-request customization of PubNub configuration or network session. For more information, refer to the Request Configuration section. |
completionType: ((Result<[String: Int], Error>) -> Void)?Default: nil | The async Result of the method call. |
Completion handler result
Success
A Dictionary of channels mapped to their respective message count.
Failure
An Error describing the failure.
Sample code
1
Other examples
Retrieve message counts for multiple channels with the same timetoken 15526611838554310
1
Retrieve message counts for multiple channels with different timetokens
1