RubyRuby V4 Storage & Playback API Reference for Realtime Apps

Go to Channel Groups


 

These docs are for PubNub 4.0 for Ruby which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.0 for Ruby.

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

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 Ruby V4 SDK
  1. ParameterTypeRequiredDescription
    channelsString, SymbolYesSpecify channels to return history messages from.
    countIntegerOptionalSpecifies the number of historical messages to return.

    Default/maximum is 100.

    startIntegerOptionalTime token delimiting the start of time slice (exclusive) to pull messages from.
    endIntegerOptionalTime token delimiting the end of time slice (inclusive) to pull messages from.
    reverseBooleanOptionalSetting to true will traverse the time line in reverse starting with the oldest message first.

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

    http_syncBooleanOptional

    Default false. Method will be executed asynchronously and will return future, to get it's value you can use value method. If set to true, method will return array of envelopes (even if there's only one envelope).

    For sync methods Envelope object will be returned.

    include_tokenBooleanOptional

    With include_token parameter set to true each envelope will contain timetoken specific for message that it holds.

    Default: false

    callbackLambda accepting one parameterOptional

    Callback that will be called for each envelope.

    For async methods future will be returned, to retrieve value Envelope object you have to call value method (thread will be locked until the value is returned).

Retrieve the last 100 messages on a channel:
pubnub.history(
	channel: 'history_channel',
	count: 100
) do |envelope|
	puts envelope.result[:data][:messages]
end
The Ruby V4 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:
#<Pubnub::Envelope:0x007fd384da4ad0
	@result = {
		:data => {
			:messages => ["Pub1", "Pub2", "Pub3", "Pub4", "Pub5", "Pub6", "Pub7", "Pub8", "Pub9", "Pub10"], 
			:end => 15010808292416521, 
			:start => 15010808287349573
		}
	},
	@status = { 
		:code => 200
	}
>
  1. pubnub.history(
    	channel: :history, 
    	count: 3, 
    	reverse: true, 
    	http_sync: true
    )
    #<Pubnub::Envelope:0x007fd3858e34c8
    	@result = {
    		:data => {
    			:messages => ["Pub1", "Pub2", "Pub3"], 
    			:end => 15010808288498250, 
    			:start => 15010808287349573
    		}
    	},
    	@status = {
    		:code => 200
    	}
    >
  2. pubnub.history(
    	channel: :history, 
    	start: 15010808287700000, 
    	reverse: true, 
    	http_sync: true
    )
    #<Pubnub::Envelope:0x007fd38523ced0
    	@result = {
    		:data => {
    			:messages => ["Pub1"], 
    			:end => 15010808287349573, 
    			:start => 15010808287349573
    		}
    	}
    	@status = {
    		:code => 200
    	}
    >
  3. pubnub.history(
    	channel: :history, 
    	end: 15010808287700000, 
    	http_sync: true
    )
    #<Pubnub::Envelope:0x007fd3860dbce8
    	@result = {
    		:data => {
    			:messages => ["Pub2", "Pub3", "Pub4", "Pub5", "Pub6", "Pub7", "Pub8", "Pub9", "Pub10"], 
    			:end => 15010808292416521, 
    			:start => 15010808287951883
    		}
    	}
    	@status = {
    		:code => 200
    	}
    >
  4.  
    Usage!
    You can call the method by passing 0 or a valid time token as the argument.
    pubnub.paged_history(channel: :messages, limit: 10, page: 20) do |envelope|
    	puts envelope.result[:data][:messages]
    end
  5. # ASYNC
    # Call history with include_token: true
    future_envelope = pubnub.history(channel: :demo, include_token: true)
    # Get timetoken of first retrieved message
    future_envelope.value.result[:data][:messages].first['timetoken']
    
    # SYNC
    # Call history with include_token: true
    envelope = pubnub.history(channel: :demo, include_token: true, http_sync: true)
    # Get timetoken of first retrieved message
    envelope.result[:data][:messages].first['timetoken']
    
    # Example response in result[:data][:messages]
    # [
    #   {"message"=>"Whatever", "timetoken"=>14865606002747651},
    #   {"message"=>"Message", "timetoken"=>14865606051899206}, 
    #   {"message"=>"Another", "timetoken"=>14865606101428628}
    # ]
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.

Requires Initialization with secret key.

To Delete Messages from History you can use the following method(s) in the Ruby V4 SDK.
  1. ParameterTypeRequiredDescription
    channelsString, SymbolYesChannels from which messages will be deleted.
    startString, IntegerNoTimestamp since when messages should be deleted.
    endString, IntegerNoTimestamp until when messages should be deleted.
    http_syncBooleanNo

    Default false. Method will be executed asynchronously and will return future, to get it's value you can use value method. If set to true, method will return array of envelopes (even if there's only one envelope).

    For sync methods Envelope object will be returned.

    callbackLambda accepting one parameterNo

    Callback that will be called for each envelope.

    For async methods future will be returned, to retrieve value Envelope object you have to call value method (thread will be locked until the value is returned).

pubnub.delete_messages(channel: 'my-channel', start: 1508284800, end: 1508935781, callback: check_reponse_status)
#<Pubnub::Envelope
    @status = {
        :code => 200,
        :operation => :delete,
        :category => :ack,
        :error => false,
        # [...]
    },
    # [...]
>

  1. 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. e.g. e.g. if 15526611838554310 is the publish timetoken, pass 15526611838554309 in start and 15526611838554310 in end parameters respectively as shown in the following code snippet.
    pubnub.delete_messages(channel: 'my-channel', start: 15526611838554309, end: 15526611838554310, callback: check_reponse_status)
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.
Returns the number of messages published on one or more channels since a given time. The count returned is the number of messages in history with a timetoken value greater than the passed value in the channel_timetokensparameter.
 For keys with unlimited message retention enabled, this method considers only messages published in the last 7 days.
You can use the following method(s) in the Ruby V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    channel String, SymbolYesEither array of channels, string with single channel or string with comma separated channels
    channel_timetokens ArrayYesnullArray 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 PNStatuswith an error flag.
    http_syncBooleanNo

    Default false. Method will be executed asynchronously and will return future, to get it's value you can use value method. If set to true, method will return array of envelopes (even if there's only one envelope).

    For sync methods Envelope object will be returned.

envelope = pubnub.message_counts(channel:['a', 'b', 'c', 'd'], channel_timetokens: 12123).value
    p envelope.result[:data]
 Channels without messages have a count of 0. Channels with 10,000 messages or more have a count of 10000.
Returns Concurrent::Future object if PubNub is configured with http_sync: false (default behavior) or envelope if it's set to sync mode
#<Pubnub::Envelope
    @result=
      {
       :data=>
        {
         "channels"=>{"a"=>2, "c"=>0, "b"=>0, "d"=>0}
        }
     @status=
      {
        :code=>200
      }
>
  1. envelope = pubnub.message_counts(channel:['a', 'b', 'c', 'd'], channel_timetokens: [123135129, 123135124, 12312312, 123135125]).value
        p envelope.result[:data]

Go to Mobile Push