SelectBlackBerry Storage & Playback API Reference for Realtime Apps

 

This SDK has been deprecated.

If you have questions about the PubNub for BlackBerry SDK, please contact us at support@pubnub.com.

The PubNub Storage and Playback Service provides realtime access to history for all messages published to PubNub. Each published message is timestamped to the nearest 10 nanoseconds, and is stored 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.

Messages can be stored for a configurable duration or forever, as controlled by the retention policy that is configured on your account. The following options are available: 1 day, 7 days, 1 month, and Forever.


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.
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 BlackBerry SDK
  1. ParameterTypeRequiredDescription
    channelStringYesSpecifies channelto return history messages from.
    countintYesSpecifies the number of historical messages to return.

    default/maximum is 100.

    callbackCallbackYesCallback methods from this callback object are called on a successful connection or error to the PubNub cloud and specifies the name of the function that the callback executes.
  2. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    reversebooleanYesSetting to true will traverse the time line in reverse starting with the oldest message first. Default is false.
    callbackCallbackYesCallback methods from this callback object are called on a successful connection or error to the PubNub cloud and specifies the name of the function that the callback executes.
  3. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    startlongYesTime token delimiting the start of time slice (exclusive) to pull messages from.
    endlongYesTime token delimiting the end of time slice (inclusive) to pull messages from.
    callbackCallbackYesCallback methods from this callback object are called on a successful connection or error to the PubNub cloud and specifies the name of the function that the callback executes.
  4. ParameterTypeRequiredDescription
    channel
    String
    Yes
    Specifies channel to return history messages from.
    count
    int
    Yes
    Specifies the number of historical messages to return. The Default is 100.
    reverse
    boolean
    Yes
    Setting to true will traverse the time line in reverse starting with the oldest message first.

    Default is false.

    callback
    Callback
    Yes
    Callback methods from this callback object are called on a successful connection or error to the PubNub cloud and specifies the name of the function that the callback executes.
  5. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    startlongYesTime token delimiting the start of time slice (exclusive) to pull messages from.
    reversebooleanYesSetting to true will traverse the time line in reverse starting with the oldest message first. Default is false.
    callbackCallbackYesCallback methods from this callback object are called on a successful connection or error to the PubNub cloud and specifies the name of the function that the callback executes.
  6. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    startlongYesTime token delimiting the start of time slice (exclusive) to pull messages from.
    countintYesSpecifies the number of historical messages to return. The Default is 100.
    callbackCallbackYesCallback methods from this callback object are called on a successful connection or error to the PubNub cloud and specifies the name of the function that the callback executes.
  7. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    startlongYesTime token delimiting the start of time slice (exclusive) to pull messages from.
    countintYesSpecifies the number of historical messages to return. The Default is 100.
    reversebooleanYesSetting to true will traverse the time line in reverse starting with the oldest message first. Default is false.
    callbackCallbackYesCallback methods from this callback object are called on a successful connection or error to the PubNub cloud and specifies the name of the function that the callback executes.
  8. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    startlongYesTime token delimiting the start of time slice (exclusive) to pull messages from.
    endlongYesTime token delimiting the end of time slice (inclusive) to pull messages from.
    countintYesSpecifies the number of historical messages to return. The Default is 100.
    callbackCallbackYesCallback methods from this callback object are called on a successful connection or error to the PubNub cloud and specifies the name of the function that the callback executes.
  9. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    includeTimetokenbooleanYesWhether event dates (time tokens) should be included in response or not.
    countintYesSpecifies the number of historical messages to return. The Default is 100.
  10. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    startlongYesTime token delimiting the start of time slice (exclusive) to pull messages from.
    endlongYesTime token delimiting the end of time slice (inclusive) to pull messages from.
    countintYesSpecifies the number of historical messages to return. The Default is 100.
    reversebooleanYesSetting to true will traverse the time line in reverse starting with the oldest message first. Default is false.
    includeTimetokenbooleanYesWhether event dates (time tokens) should be included in response or not.
Retrieve the last 100 messages on a channel:
Callback callback = new Callback() {
  public void successCallback(String channel, Object response) {
    System.out.println(response.toString());
  }
  public void errorCallback(String channel, PubnubError error) {
	System.out.println(error.toString());
  }
};
pubnub.history("history_channel", 100, true, callback);
The BlackBerry SDK returns false on fail. An array is returned on success. The history() function returns a list of up to 100 messages, the time token of the first (oldest) message and the time token of the last (newest) message in the resulting set of messages. The output below demonstrates the format for a history() response:

[
	["message1", "message2", "message3",... ],
	"Start Time Token",
	"End Time Token"
]
  1. Callback callback = new Callback() {
      public void successCallback(String channel, Object response) {
        System.out.println(response.toString());
      }
      public void errorCallback(String channel, PubnubError error) {
    	System.out.println(error.toString());
      }
    };
    pubnub.history("my_channel", 3, true, callback);
    
     [
    	["Pub1","Pub2","Pub3"],
    	13406746729185766,
    	13406746780720711
    ]
    
  2. pubnub.history("my_channel", 13847168620721752L, true, callback);
    
    Callback callback = new Callback() {
      public void successCallback(String channel, Object response) {
        System.out.println(response.toString());
      }
      public void errorCallback(String channel, PubnubError error) {
    	System.out.println(error.toString());
      }
    };
    
     [
    	["Pub3","Pub4","Pub5"],
    	13406746780720711,
    	13406746845892666
    ]
    
  3. Callback callback = new Callback() {
      public void successCallback(String channel, Object response) {
        System.out.println(response.toString());
      }
      public void errorCallback(String channel, PubnubError error) {
    	System.out.println(error.toString());
      }
    };
    pubnub.history("my_channel", -1, 13847168819178600L, 100, callback);
    
    [
    	["Pub3","Pub4","Pub5"],
    	13406746780720711,
    	13406746845892666
    ]
    
  4.  
    Usage!
    You can call the method by passing 0 or a valid time token as the argument.
    package com.pubnub.api;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.TimeUnit;
    
    public class PubnubRecursiveHistoryFetcher {
        Pubnub pubnub;
    
        PubnubRecursiveHistoryFetcher() {
            // NOTICE: for demo/demo pub/sub keys Storage & Playback is disabled,
            // so use your pub/sub keys instead
            pubnub = new Pubnub("demo", "demo", "demo", false);
        }
    
        public static void main(String[] args) {
            PubnubRecursiveHistoryFetcher fetcher = new PubnubRecursiveHistoryFetcher();
    
            // Usage examples:
            //fetcher.getAllMessages();
            //fetcher.getAllMessages(null);
            //fetcher.getAllMessages(14323168799492413L);
        }
    
        public void getAllMessages() {
            getAllMessages(null);
        }
    
        public void getAllMessages(Long startTimestamp) {
            final CountDownLatch latch = new CountDownLatch(1);
            HistoryCallback callback = new HistoryCallback(latch);
    
            if (startTimestamp == null) {
                startTimestamp = -1L;
            }
    
            pubnub.history("demo", startTimestamp, 100, callback);
    
            try {
                latch.await(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            JSONArray response = callback.getResult();
    
            JSONArray msgs;
            long start;
            long end;
    
            try {
                msgs = response.getJSONArray(0);
                start = response.getLong(1);
                end = response.getLong(2);
            } catch (JSONException e) {
                e.printStackTrace();
                return;
            }
    
            int length = msgs.length();
    
            if (length > 0) {
                System.out.println(msgs.length());
                System.out.println("start: " + start);
                System.out.println("end: " + end);
            }
    
            if (length == 100) {
                getAllMessages(start);
            }
        }
    
        private class HistoryCallback extends Callback {
            private JSONArray result;
            private CountDownLatch latch;
    
            HistoryCallback(CountDownLatch latch) {
                this.latch = latch;
            }
    
            public JSONArray getResult() {
                return result;
            }
    
            @Override
            public void successCallback(String channel, Object message) {
                result = (JSONArray) message;
    
                if (this.latch != null) {
                    this.latch.countDown();
                }
            }
    
            @Override
            public void errorCallback(String channel, PubnubError error) {
                System.out.println(error.getErrorString());
    
                if (this.latch != null) {
                    this.latch.countDown();
                }
            }
        }
    }
    
  5. Callback callback = new Callback() {
      public void successCallback(String channel, Object response) {
        System.out.println(response.toString());
      }
      public void errorCallback(String channel, PubnubError error) {
        System.out.println(error.toString());
      }
    };
    pubnub.history("my_channel", true, 3, callback);