Message Persistence API for Go 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
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Fetch historical messages from one or multiple channels. The includeMessageActions flag also allows you to fetch message actions along with the messages.
You can 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 retrieve messages between those timetokens (inclusive of theendvalue).
You can receive up to 100 messages for a single channel, or 25 messages per channel when querying multiple channels (up to 500 channels). If more messages match the time range, make iterative calls and adjust the start timetoken to page through the results.
Method(s)
To run Fetch History, you can use the following method(s) in the Go SDK:
1pn.Fetch().
2 Channels(channels).
3 Count(count).
4 Start(start).
5 End(end).
6 IncludeMeta(bool).
7 IncludeMessageType(bool).
8 IncludeUUID(bool).
9 IncludeMessageActions(bool).
10 IncludeCustomMessageType(bool).
11 QueryParam(queryParam).
12 Execute()
| Parameter | Description |
|---|---|
Channels *Type: string Default: n/a | Specifies channelsto return history messages. Maximum of 500 channels are allowed. |
CountType: int Default: 100 or 25 | Specifies the number of historical messages to return. Default and maximum value is 100 for a single channel, 25 for multiple channels, and 25 when IncludeMessageActions is true. |
StartType: int64 Default: n/a | Timetoken delimiting the start of time slice (exclusive) to pull messages from. |
EndType: int64 Default: n/a | Timetoken delimiting the end of time slice (inclusive) to pull messages from. |
IncludeMetaType: bool Default: false | Whether meta (passed when Publishing the message) should be included in response or not. |
IncludeMessageTypeType: bool Default: true | Pass true to receive the message type with each history message. |
IncludeUUIDType: bool Default: true | Pass true to receive the publisher uuid with each history message. |
IncludeMessageActionsType: bool Default: false | Whether MessageActions should be included in response or not. If true, the method is limited to one channel and 25 messages only. |
IncludeCustomMessageTypeType: bool Default: n/a | Indicates whether to retrieve messages with the custom message type. For more information, refer to Retrieving Messages. |
QueryParamType: map[string]string Default: nil | QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API. |
Truncated response
If you fetch messages with message actions, the response may be truncated when internal limits are reached. If truncated, a more property is returned with additional parameters. Make iterative calls to history, adjusting the parameters to fetch more messages.
Sample code
Reference code
Retrieve the last messages on a channel:
1
Returns
The Fetch() operation returns a FetchResponse object which contains the following field:
| Field | Type | Description |
|---|---|---|
Messages | map[string][]FetchResponseItem | Map where keys are channel names and values are arrays of message items. |
Each FetchResponseItem contains:
| Field | Type | Description |
|---|---|---|
Message | interface | Message from history. |
Timetoken | string | Timetoken of the message. |
Meta | interface | The metadata (if sent with the message). |
MessageType | int | Message type. Returns 4 for file messages. |
UUID | string | Publisher UUID of the message. |
MessageActions | map[string]PNHistoryMessageActionsTypeMap | Message actions associated with the message. Details of type PNHistoryMessageActionsTypeMap are described below. |
File | PNFileDetails | File details if the message contains a file. |
Error | error | Decryption error if message couldn't be decrypted. |
PNHistoryMessageActionsTypeMap
| Field | Type | Description |
|---|---|---|
ActionsTypeValues | map[string][]PNHistoryMessageActionTypeVal | Details of type PNHistoryMessageActionTypeVal are described below. |
PNHistoryMessageActionTypeVal
| Field | Type | Description |
|---|---|---|
UUID | string | The UUID of the publisher. |
ActionTimetoken | string | The publish timetoken of the action. |
Status response
| Field | Type | Description |
|---|---|---|
Error | error | Error information, if any |
Category | StatusCategory | Status category of the operation |
Operation | OperationType | Operation type |
StatusCode | int | HTTP status code |
TLSEnabled | bool | Whether TLS is enabled |
UUID | string | UUID of the client |
AuthKey | string | Auth key used |
Origin | string | Origin server |
OriginalResponse | string | Raw response from server |
Request | string | Request that was sent |
AffectedChannels | []string | Channels affected by this operation |
AffectedChannelGroups | []string | Channel groups affected by this operation |
Other examples
Fetch with metadata
1
Fetch with time range
1
Fetch from multiple channels
1
Delete messages from history
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Remove messages from the history of a specific channel.
Required setting
To accept delete-from-history requests, enable the Delete-From-History setting for your key in the Admin Portal and initialize the SDK with a secret key.
Method(s)
To Delete Messages from History you can use the following method(s) in the Go SDK.
1pn.DeleteMessages().
2 Channel(channel).
3 Start(start).
4 End(end).
5 QueryParam(queryParam).
6 Execute()
| Parameter | Description |
|---|---|
Channel *Type: string | Specifies channels to delete messages from. |
StartType: int64 | Timetoken delimiting the start of time slice (inclusive) to delete messages from. |
EndType: int64 | Timetoken delimiting the end of time slice (exclusive) to delete messages from. |
QueryParamType: map[string]string | QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API. |
Sample code
1
Returns
The DeleteMessages() operation returns an empty HistoryDeleteResponse struct. Check the status response to verify success.
Status response
| Field | Type | Description |
|---|---|---|
Error | error | Error information, if any |
Category | StatusCategory | Status category of the operation |
Operation | OperationType | Operation type |
StatusCode | int | HTTP status code |
TLSEnabled | bool | Whether TLS is enabled |
UUID | string | UUID of the client |
AuthKey | string | Auth key used |
Origin | string | Origin server |
OriginalResponse | string | Raw response from server |
Request | string | Request that was sent |
AffectedChannels | []string | Channels affected by this operation |
AffectedChannelGroups | []string | Channel groups affected by this operation |
Other examples
Delete specific message from history
To delete a specific message, pass the publish timetoken (received from a successful publish) in the End parameter and timetoken +/- 1 in the Start parameter. For example, if 15526611838554310 is the publish timetoken, pass 15526611838554309 in Start and 15526611838554310 in End parameters respectively as shown in the following code snippet.
1
Message counts
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Return the number of messages published on one or more channels since a given time. The count is the number of messages in history with a timetoken greater than or equal to the value passed in ChannelsTimetoken.
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 Go SDK:
1pn.MessageCounts().
2 Channels(channels).
3 ChannelsTimetoken(channelsTimetoken).
4 QueryParam(queryParam).
5 Execute()
| Parameter | Description |
|---|---|
Channels *Type: []string Default: n/a | The channels to fetch the message count |
ChannelsTimetoken *Type: []int64 Default: nil | Array of timetokens, in order of the channels list. Specify a single timetoken to apply it to all channels. Otherwise, the list of timetokens must be the same length as the list of channels, or the function returns a PNStatus with an error flag. |
QueryParamType: map[string]string Default: nil | QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API. |
Sample code
1
Returns
The MessageCounts() operation returns a MessageCountsResponse object which contains the following field:
| Field | Type | Description |
|---|---|---|
Channels | map[string]int | Channel names with message counts. Channels without messages have a count of 0. Channels with 10,000 messages or more have a count of 10000. |
Status response
| Field | Type | Description |
|---|---|---|
Error | error | Error information, if any |
Category | StatusCategory | Status category of the operation |
Operation | OperationType | Operation type |
StatusCode | int | HTTP status code |
TLSEnabled | bool | Whether TLS is enabled |
UUID | string | UUID of the client |
AuthKey | string | Auth key used |
Origin | string | Origin server |
OriginalResponse | string | Raw response from server |
Request | string | Request that was sent |
AffectedChannels | []string | Channels affected by this operation |
AffectedChannelGroups | []string | Channel groups affected by this operation |
Other examples
Retrieve count of messages using different timetokens for each channel
1
History (deprecated)
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Alternative method
This method is deprecated. Use fetch history instead.
This function fetches historical messages of a channel.
It is possible to control how messages are returned and in what order, for example you can:
- Search for messages starting on the newest end of the timeline (default behavior -
Reverse=false) - Search for messages from the oldest end of the timeline by setting
Reversetotrue. - Page through results by providing a
StartOREndtimetoken. - Retrieve a slice of the time line by providing both a
StartANDEndtimetoken. - Limit the number of messages to a specific quantity using the
Countparameter.
Start & End parameter usage clarity
If only the Start parameter is specified (without End), you will receive messages that are older than and up to that Start timetoken value. If only the End parameter is specified (without Start) you will receive messages that match that End timetoken value and newer. Specifying values for both Start and End parameters will return messages between those timetoken values (inclusive on the End value). Keep in mind that you will still receive a maximum of 100 messages even if there are more messages that meet the timetoken values. Iterative calls to history adjusting the Start timetoken is necessary to page through the full set of results if more than 100 messages meet the timetoken values.
Method(s)
To run History you can use the following method(s) in the Go SDK:
1pn.History().
2 Channel(string).
3 Reverse(bool).
4 IncludeTimetoken(bool).
5 IncludeMeta(bool).
6 Start(int64).
7 End(int64).
8 Count(int).
9 QueryParam(queryParam).
10 Execute()
| Parameter | Description |
|---|---|
ChannelType: string Default: n/a | Specifies channel to return history messages from. |
ReverseType: bool Default: false | Setting to true will traverse the time line in reverse starting with the oldest message first. |
IncludeTimetokenType: bool Default: false | Whether event dates timetokens should be included in response or not. |
IncludeMetaType: bool Default: false | Whether meta (passed when Publishing the message) should be included in response or not. |
StartType: int64 Default: n/a | Timetoken delimiting the start of time slice (exclusive) to pull messages from. |
EndType: int64 Default: n/a | Timetoken delimiting the end of time slice (inclusive) to pull messages from. |
CountType: int Default: 100 | Specifies the number of historical messages to return. |
QueryParamType: map[string]string Default: nil | QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API. |
tip
reverse parameterMessages are always returned sorted in ascending time direction from history regardless of Reverse. The Reverse direction matters when you have more than 100 (or Count, if it's set) messages in the time interval, in which case Reverse determines the end of the time interval from which it should start retrieving the messages.
Sample code
Retrieve the last 100 messages on a channel:
1
Returns
The History() operation returns a HistoryResponse object which contains the following fields:
| Field | Type | Description |
|---|---|---|
Messages | []HistoryResponseItem | Array of messages. See HistoryResponseItem for more details. |
StartTimetoken | int64 | Start timetoken of the returned messages. |
EndTimetoken | int64 | End timetoken of the returned messages. |
HistoryResponseItem
| Field | Type | Description |
|---|---|---|
Timetoken | int64 | Timetoken of the message. |
Message | interface | The message content. |
Meta | interface | The metadata (if sent when publishing the message). |
Error | error | Decryption error if message couldn't be decrypted. |
Status response
| Field | Type | Description |
|---|---|---|
Error | error | Error information, if any |
Category | StatusCategory | Status category of the operation |
Operation | OperationType | Operation type |
StatusCode | int | HTTP status code |
TLSEnabled | bool | Whether TLS is enabled |
UUID | string | UUID of the client |
AuthKey | string | Auth key used |
Origin | string | Origin server |
OriginalResponse | string | Raw response from server |
Request | string | Request that was sent |
AffectedChannels | []string | Channels affected by this operation |
AffectedChannelGroups | []string | Channel groups affected by this operation |
Other examples
Use history() to retrieve the three oldest messages
1
Use history() to retrieve messages newer than a given timetoken
1
Use history() to retrieve messages until a given timetoken
1
History paging example
1
Include timetoken in history response
1