Node.jsNode.jsPhoneGapReact V4WebJavaScriptNode.JS V4 Storage & Playback API Reference for Realtime Apps

Go to Channel Groups


Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
This function fetches historical messages of a channel.
PubNub Storage/Playback Service provides real-time access to an unlimited history for all messages published to PubNub. Stored messages are replicated across multiple availability zones in several geographical data center locations. Stored messages can be encrypted with AES-256 message encryption ensuring that they are not readable while stored on PubNub's network.
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 time token.
  • Retrieve a slice of the time line by providing both a start AND end time token.
  • Limit the number of messages to a specific quantity using the count parameter.
 
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.
To run History you can use the following method(s) in the NodeJS V4 SDK
  1. ParameterTypeRequiredDefaultsDescription
    Operation ArgumentsHashYesA hash of arguments.
    channel StringYesSpecifies channel to return history messages from.
    reverse Boolean Optionalfalse

    Setting to true will traverse the time line in reverse starting with the oldest message first.


    If both start and end arguments are provided, reverse is ignored and messages are returned starting with the newest message.

    count Number Optional100Specifies the number of historical messages to return.

    Default/Maximum is 100.
    stringifiedTimeToken Boolean OptionalfalseIf stringifiedTimeToken is specified as true, the SDK will return timetoken values as a strings instead of integers. Usage of setting is encouraged in javascript environments which perform round-up/down on large integers.
    start String OptionalTime token delimiting the start of time slice (exclusive) to pull messages from.
    end String OptionalTime token delimiting the end of time slice (inclusive) to pull messages from.
    callbackFunctionOptionalExecutes on a successful/unsuccessful history.
Retrieve the last 100 messages on a channel:
pubnub.history(
    {
        channel: 'history_channel',
        reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
        count: 100, // how many items to fetch
        stringifiedTimeToken: true, // false is the default
        start: '123123123123', // start time token to fetch
        end: '123123123133' // end timetoken to fetch
    },
    function (status, response) {
        // handle status, response
    }
);
// Example of Status
{
    error: false,
    operation: "PNHistoryOperation",
    statusCode: 200
}

// Example of Response
{
    endTimeToken: "14867650866860159",
    messages: [
        {
            timetoken: "14867650866860159",
            entry: "[User 636] hello World"
        },
        {...},
        {...},
        {...}
    ],
    startTimeToken: "14867650866860159"
}
  1. pubnub.history(
        {
            channel: 'my_channel',
            reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
            count: 3, // how many items to fetch
            stringifiedTimeToken: true // false is the default
        },
        function (status, response) {
            // handle status, response
        }
    );
  2. pubnub.history(
        {
            channel: 'my_channel',
            reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
            stringifiedTimeToken: true, // false is the default
            start: '13406746780720711' // start time token to fetch
        },
        function (status, response) {
            // handle status, response
        }
    );
  3. pubnub.history(
        {
            channel: 'my_channel',
            stringifiedTimeToken: true, // false is the default
            end: '13406746780720711' // start time token to fetch
        },
        function (status, response) {
            // handle status, response
        }
    );
  4.  
    Usage!
    You can call the method by passing 0 or a valid time token as the argument.
    getAllMessages = function(timetoken) {
        pubnub.history(
            {
                channel: 'history_test',
                stringifiedTimeToken: true, // false is the default
                start: timetoken // start time token to fetch
            },
            function (status, response) {
                var msgs = response.messages;
                var start = response.startTimeToken;
                var end = response.endTimeToken;
                // if msgs were retrieved, do something useful with them
                if (msgs != undefined && msgs.length > 0) {
                    console.log(msgs.length);
                    console.log("start : " + start);
                    console.log("end : " + end);
                }
                // if 100 msgs were retrieved, there might be more; call history again
                if (msgs.length == 100)
                    getAllMessages(start);
            }
        );
    }
    
    
    //Usage examples:
    //getAllMessages();
    //getAllMessages(null);
  5. pubnub.history({
        channel: 'history_channel',
        reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
        count: 100, // how many items to fetch
        stringifiedTimeToken: true, // false is the default
        start: '123123123123', // start time token to fetch
        end: '123123123133' // end timetoken to fetch
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error) 
    });
  6. pubnub.history({
        channel: 'my_channel',
        reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
        count: 3, // how many items to fetch
        stringifiedTimeToken: true // false is the default
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error) 
    });
  7. pubnub.history({
        channel: 'my_channel',
        reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
        stringifiedTimeToken: true, // false is the default
        start: '13406746780720711' // start time token to fetch
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error) 
    });
  8. pubnub.history({
        channel: 'my_channel',
        stringifiedTimeToken: true, // false is the default
        end: '13406746780720711' // start time token to fetch
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error) 
    });
  9. getAllMessages = function(timetoken) {
        pubnub.history({
            channel: 'history_test',
            stringifiedTimeToken: true, // false is the default
            start: timetoken // start time token to fetch
        }).then((response) => { 
            console.log(response) 
        }).catch((error) => { 
            console.log(error) 
        });
    }
    
    
    //Usage examples:
    //getAllMessages();
    //getAllMessages(null);
Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
This function fetches historical messages of a channel.
PubNub Storage/Playback Service provides real-time access to an unlimited history for all messages published to PubNub. Stored messages are replicated across multiple availability zones in several geographical data center locations. Stored messages can be encrypted with AES-256 message encryption ensuring that they are not readable while stored on PubNub's network.
It is possible to control how messages are returned and in what order, for example you can:
  • Page through results by providing a start OR end time token.
  • Retrieve a slice of the time line by providing both a start AND end time token.
  • Limit the number of messages to a specific quantity using the count parameter.
 
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 25 messages even if there are more messages that meet the timetoken values.
To run Batch History you can use the following method(s) in the NodeJS V4 SDK
  1. ParameterTypeRequiredDefaultsDescription
    Operation ArgumentsHashYesA hash of arguments.
    channels ArrayYesSpecifies channels to return history messages from.
    count Number Optional25Specifies the number of historical messages to return per channel.

    Default/Maximum is 25.
    start String OptionalTime token delimiting the start of time slice (exclusive) to pull messages from.
    end String OptionalTime token delimiting the end of time slice (inclusive) to pull messages from.
    callbackFunctionOptionalExecutes on a successful/unsuccessful history.
Retrieve the last 25 messages on a channel:
// assuming pubnub is an initialized instance
// start, end, count are optional
pubnub.fetchMessages(
    { 
        channels: ['ch1', 'ch2', 'ch3'], 
        start: "123",
        end: 1234,
        count: 30 
    }, 
    (status, response) => {
        // handle response
    }
);
 
The batch history is limited to 500 channels and only the last 25 messages per channel.
//Example of status
{
    error: false,
    operation: 'PNFetchMessagesOperation',
    statusCode: 200
}

//Example of response
{ 
    channels:
    { 
        ch1: [{
            channel: 'ch1',
            subscription: null,
            timetoken: '15031815106014017',
            message: 'hello'
        }],
        ch2: [{
            channel: 'ch2',
            subscription: null,
            timetoken: '15031815106014020',
            message: 'hello'
        }],
        ch3: [{
            channel: 'ch3',
            subscription: null,
            timetoken: '15031815106014024',
            message: 'hello'
        }]
    } 
}
Requires Storage & Playback add-on XRequires that the Storage & Playback add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
Removes the messages from the history of a specific channel.
 
There is a setting to accept delete from history requests for a key, which you must enable by checking the Enable Delete-From-History checkbox in the key settings for your key in the Administration Portal.
To Delete Messages from History you can use the following method(s) in the NodeJS V4 SDK.
  1. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel messages to be deleted from history.
    startStringOptionalTime token delimiting the start of time slice (exclusive) to delete messages from.
    endStringOptionalTime token delimiting the end of time slice (inclusive) to delete messages from.
    callbackFunctionOptionalExecutes on a successful/unsuccessful deleteMessage.
pubnub.deleteMessages(
    {
        channel: 'ch1',
        start: '15088506076921021',
        end: '15088532035597390'
    },
    (result) => {
        console.log(result);
    }
);
{
    error: false,
    operation: 'PNDeleteMessagesOperation',
    statusCode: 200
}

Go to Mobile Push