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
start
parameter (withoutend
), you receive messages older than thestart
timetoken. - If you specify only the
end
parameter (withoutstart
), you receive messages from thatend
timetoken and newer. - If you specify both
start
andend
, 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:
func fetchMessageHistory(
for channels: [String],
includeActions actions: Bool = false,
includeMeta: Bool = false,
includeUUID: Bool = true,
includeMessageType: Bool = true,
includeCustomMessageType: Bool = false,
page: PubNubBoundedPage? = PubNubBoundedPageBase(),
custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
completion: ((Result<(messagesByChannel: [String: [PubNubMessage]], next: PubNubBoundedPage?), Error>) -> Void)?
)
Parameter | Description |
---|---|
for *Type: [String] Default: n/a | The list of channels to fetch history messages from. Maximum of 500 channels are allowed. |
includeActions Type: 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. |
includeMeta Type: Bool Default: false | If true the meta properties of messages will be included in the response. |
includeUUID Type: Bool Default: true | If true the user ID of the sender will be included in the response. |
includeMessageType Type: Bool Default: true | Indicates whether to retrieve messages with PubNub message type. For more information, refer to Retrieving Messages. |
includeCustomMessageType Type: Bool Default: false | Indicates whether to retrieve messages with the custom message type. For more information, refer to Retrieving Messages. |
page Type: 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 . |
custom Default: 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. |
completion Type: ((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.
public protocol PubNubMessage {
/// The message sent on the channel
var payload: JSONCodable { get set }
/// Message reactions associated with this message
var actions: [PubNubMessageAction] { get set }
/// Message sender identifier
var publisher: String? { get set }
/// The channel for which the message belongs
var channel: String { get }
/// The channel group or wildcard subscription match (if exists)
show all 41 linespublic protocol PubNubBoundedPage {
/// The start value for the next set of remote data
var start: Timetoken? { get }
/// The bounded end value that will be eventually fetched to
var end: Timetoken? { get }
/// The previous limiting value (if any)
var limit: Int? { get }
}
Failure
An error describing the failure.
Sample code
Reference code
Retrieve the last message on a channel:
Other examples
Retrieve messages newer or equal than a given timetoken
Retrieve messages older than a specific timetoken
Retrieve the last 10 messages on channelSwift, otherChannel, and myChannel
Retrieve messages and their metadata
Retrieve messages and their message action data
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.
func deleteMessageHistory(
from channel: String,
start: Timetoken? = nil,
end: Timetoken? = nil,
custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
completion: ((Result<Void, Error>) -> Void)?
)
Parameter | Description |
---|---|
from *Type: String Default: n/a | The channel to delete the messages from. |
start Type: timetoken ?Default: nil | timetoken delimiting the start of time slice (inclusive) to delete messages from. |
end Type: timetoken ?Default: nil | timetoken delimiting the end of time slice (exclusive) to delete messages from. |
custom Default: 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. |
completion Type: ((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
Other examples
Delete specific message from history
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:
func messageCounts(
channels: [String: Timetoken],
custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
completion: ((Result<[String: Int], Error>) -> Void)?
)
Parameter | Description |
---|---|
channels *Type: [String: timetoken ]Default: n/a | The map of channels and the timetoken to get the message count for. |
custom Default: 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. |
completion Type: ((Result<[String: Int], Error>) -> Void)? Default: nil | The async Result of the method call. |
func messageCounts(
channels: [String],
timetoken: Timetoken = 1,
custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
completion: ((Result<[String: Int], Error>) -> Void)?
)
Parameter | Description |
---|---|
channels *Type: [String] Default: n/a | The list of channels to get message counts for. |
timetoken Type: timetoken Default: 1 | The timetoken for all channels in the list to get message counts for. |
custom Default: 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. |
completion Type: ((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
Other examples
Retrieve message counts for multiple channels with the same timetoken
15526611838554310
Retrieve message counts for multiple channels with different timetokens