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 Start, you receive messages older than that timetoken. Start is the newer boundary of the query (exclusive).
  • If you specify only End, you receive messages newer than or equal to that timetoken. End is the older boundary of the query (inclusive).
  • If you specify both Start and End, Start must be a higher timetoken than End. Results contain messages between those timetokens, excluding the message at Start and including the message at End.
How to use the start and end parameters

PubNub retrieves messages by searching backward through time (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:

  • start is the newer boundary (higher timetoken value) - search begins here, exclusive
  • end is the older boundary (lower timetoken value) - search stops here, inclusive

When you provide both parameters, start must be a higher timetoken than end.

9:00 AM10:00 AM11:00 AM11:59 AM

Results are always returned in oldest-first order.

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()
required
Channels required#
Type: string
Default: no default
Specifies channelsto return history messages. Maximum of 500 channels are allowed.
Count#
Type: 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.
Start#
Type: int64
Default: no default
Newer boundary of the query (exclusive). Retrieval begins at this timetoken and moves backward in time. Must be a higher timetoken value than End when both are provided.
End#
Type: int64
Default: no default
Older boundary of the query (inclusive). Retrieval stops at this timetoken. Must be a lower timetoken value than Start when both are provided.
IncludeMeta#
Type: bool
Default: false
Whether meta (passed when Publishing the message) should be included in response or not.
IncludeMessageType#
Type: bool
Default: true
Pass true to receive the message type with each history message.
IncludeUUID#
Type: bool
Default: true
Pass true to receive the publisher uuid with each history message.
IncludeMessageActions#
Type: 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.
IncludeCustomMessageType#
Type: bool
Default: no default
Indicates whether to retrieve messages with the custom message type.

For more information, refer to Retrieving Messages.
QueryParam#
Type: 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​

Retrieve the last messages on a channel:

1

Returns​

The Fetch() operation returns a FetchResponse object which contains the following field:

FieldTypeDescription
Messagesmap[string][]FetchResponseItemMap where keys are channel names and values are arrays of message items.

Each FetchResponseItem contains:

FieldTypeDescription
MessageinterfaceMessage from history.
TimetokenstringTimetoken of the message.
MetainterfaceThe metadata (if sent with the message).
MessageTypeintMessage type. Returns 4 for file messages.
UUIDstringPublisher UUID of the message.
MessageActionsmap[string]PNHistoryMessageActionsTypeMapMessage actions associated with the message. Details of type PNHistoryMessageActionsTypeMap are described below.
FilePNFileDetailsFile details if the message contains a file.
ErrorerrorDecryption error if message couldn't be decrypted.

PNHistoryMessageActionsTypeMap​

FieldTypeDescription
ActionsTypeValuesmap[string][]PNHistoryMessageActionTypeValDetails of type PNHistoryMessageActionTypeVal are described below.

PNHistoryMessageActionTypeVal​

FieldTypeDescription
UUIDstringThe UUID of the publisher.
ActionTimetokenstringThe publish timetoken of the action.

Status response​

FieldTypeDescription
ErrorerrorError information, if any
CategoryStatusCategoryStatus category of the operation
OperationOperationTypeOperation type
StatusCodeintHTTP status code
TLSEnabledboolWhether TLS is enabled
UUIDstringUUID of the client
AuthKeystringAuth key used
OriginstringOrigin server
OriginalResponsestringRaw response from server
RequeststringRequest that was sent
AffectedChannels[]stringChannels affected by this operation
AffectedChannelGroups[]stringChannel 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.

How to use the start and end parameters

PubNub retrieves messages by searching backward through time (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:

  • start is the newer boundary (higher timetoken value) - search begins here, exclusive
  • end is the older boundary (lower timetoken value) - search stops here, inclusive

When you provide both parameters, start must be a higher timetoken than end.

9:00 AM10:00 AM11:00 AM11:59 AM

Results are always returned in oldest-first order.

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()
required
Channel required#
Type: string
Specifies channels to delete messages from.
Start#
Type: int64
Newer boundary of the query (exclusive). Must be a higher timetoken value than End when both are provided.
End#
Type: int64
Older boundary of the query (inclusive). Must be a lower timetoken value than Start when both are provided.
QueryParam#
Type: 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​

FieldTypeDescription
ErrorerrorError information, if any
CategoryStatusCategoryStatus category of the operation
OperationOperationTypeOperation type
StatusCodeintHTTP status code
TLSEnabledboolWhether TLS is enabled
UUIDstringUUID of the client
AuthKeystringAuth key used
OriginstringOrigin server
OriginalResponsestringRaw response from server
RequeststringRequest that was sent
AffectedChannels[]stringChannels affected by this operation
AffectedChannelGroups[]stringChannel 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()
required
Channels required#
Type: []string
Default: no default
The channels to fetch the message count
ChannelsTimetoken required#
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.
QueryParam#
Type: 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:

FieldTypeDescription
Channelsmap[string]intChannel 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​

FieldTypeDescription
ErrorerrorError information, if any
CategoryStatusCategoryStatus category of the operation
OperationOperationTypeOperation type
StatusCodeintHTTP status code
TLSEnabledboolWhether TLS is enabled
UUIDstringUUID of the client
AuthKeystringAuth key used
OriginstringOrigin server
OriginalResponsestringRaw response from server
RequeststringRequest that was sent
AffectedChannels[]stringChannels affected by this operation
AffectedChannelGroups[]stringChannel 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 Reverse to true.
  • Page through results by providing a Start OR End timetoken.
  • Retrieve a slice of the time line by providing both a Start AND End timetoken.
  • Limit the number of messages to a specific quantity using the Count parameter.

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()
Channel#
Type: string
Default: no default
Specifies channel to return history messages from.
Reverse#
Type: bool
Default: false
Setting to true will traverse the time line in reverse starting with the oldest message first.
IncludeTimetoken#
Type: bool
Default: false
Whether event dates timetokens should be included in response or not.
IncludeMeta#
Type: bool
Default: false
Whether meta (passed when Publishing the message) should be included in response or not.
Start#
Type: int64
Default: no default
Newer boundary of the query (exclusive). Retrieval begins at this timetoken and moves backward in time. Must be a higher timetoken value than End when both are provided.
End#
Type: int64
Default: no default
Older boundary of the query (inclusive). Retrieval stops at this timetoken. Must be a lower timetoken value than Start when both are provided.
Count#
Type: int
Default: 100
Specifies the number of historical messages to return.
QueryParam#
Type: 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
Using the reverse parameter

Messages 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:

FieldTypeDescription
Messages[]HistoryResponseItemArray of messages. See HistoryResponseItem for more details.
StartTimetokenint64Start timetoken of the returned messages.
EndTimetokenint64End timetoken of the returned messages.

HistoryResponseItem​

FieldTypeDescription
Timetokenint64Timetoken of the message.
MessageinterfaceThe message content.
MetainterfaceThe metadata (if sent when publishing the message).
ErrorerrorDecryption error if message couldn't be decrypted.

Status response​

FieldTypeDescription
ErrorerrorError information, if any
CategoryStatusCategoryStatus category of the operation
OperationOperationTypeOperation type
StatusCodeintHTTP status code
TLSEnabledboolWhether TLS is enabled
UUIDstringUUID of the client
AuthKeystringAuth key used
OriginstringOrigin server
OriginalResponsestringRaw response from server
RequeststringRequest that was sent
AffectedChannels[]stringChannels affected by this operation
AffectedChannelGroups[]stringChannel 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

Was this page useful?

Last updated on