History
Channel object
The Rest Channel object exposes the following public method to obtain Message history.
Methods
history
PaginatedResult<Message> history(Hash option)
Gets a paginated set of historical messages for this channel.
Parameters
| Parameter | Description | Type |
|---|---|---|
| option | an optional set of key value pairs containing the query parameters, as specified below. | Object |
options parameters
| Parameter | Description | Type |
|---|---|---|
| :start | beginning of time earliest Time or time in milliseconds since the epoch for any messages retrieved | Int or Time |
| :end | current time latest Time or time in milliseconds since the epoch for any messages retrieved | Int or Time |
| :direction | backwards :forwards or :backwards | Symbol |
| :limit | 100 maximum number of messages to retrieve per page, up to 1,000 | Integer |
Returns
On success, the returned PaginatedResult encapsulates an array of Message objects corresponding to the current page of results. PaginatedResult supports pagination using next and first methods.
Failure to retrieve the message history will raise an AblyException
Presence object
Rest Presence object exposes the following public method to obtain presence event history such as enter, update and leave events. These events are represented as PresenceMessage objects.
Methods
history
PaginatedResult<PresenceMessage> history(Hash option)
Gets a paginated set of historical presence events for this channel.
Parameters
| Parameter | Description | Type |
|---|---|---|
| option | an optional set of key value pairs containing the query parameters, as specified below. | Object |
options parameters
| Parameter | Description | Type |
|---|---|---|
| :start | beginning of time earliest Time or time in milliseconds since the epoch for any presence events retrieved | Int or Time |
| :end | current time latest Time or time in milliseconds since the epoch for any presence events retrieved | Int or Time |
| :direction | backwards :forwards or :backwards | Symbol |
| :limit | 100 maximum number of presence events to retrieve up to 1,000 | Integer |
Returns
On success, the returned PaginatedResult encapsulates an array of PresenceMessage objects corresponding to the current page of results. PaginatedResult supports pagination using next and first methods.
Failure to retrieve the presence event history will raise an AblyException
Related types
Ably::Models::Message Enum
A Message represents an individual message that is sent to or received from Ably.
name
The event name, if provided.
Type: String
data
The message payload, if provided.
Type: String, Binary (ASCII-8BIT String), Hash, Array
extras
Metadata and/or ancillary payloads, if provided. Valid payloads include push, headers (a map of strings to strings for arbitrary customer-supplied metadata), ephemeral, and privileged objects.
Type: Hash, Array
id
A Unique ID assigned by Ably to this message.
Type: String
client_id
The client ID of the publisher of this message.
Type: String
connection_id
The connection ID of the publisher of this message.
Type: String
connection_key
A connection key, which can optionally be included for a REST publish as part of the publishing on behalf of a realtime client functionality.
Type: String
timestamp
Timestamp when the message was first received by the Ably, as a Time object.
Type: Time
encoding
This will typically be empty as all messages received from Ably are automatically decoded client-side using this value. However, if the message encoding cannot be processed, this attribute will contain the remaining transformations not applied to the data payload.
Type: String
Message constructors
Message.fromEncoded
Message.fromEncoded(Object encodedMsg, ChannelOptions channelOptions?) -> Message
A static factory method to create a Message from a deserialized Message-like object encoded using Ably's wire protocol.
Parameters
| Parameter | Description | Type |
|---|---|---|
| encodedMsg | a Message-like deserialized object. | Object |
| channelOptions | an optional ChannelOptions. If you have an encrypted channel, use this to allow the library to decrypt the data. | Object |
Returns
A Message object
Message.fromEncodedArray
Message.fromEncodedArray(Object[] encodedMsgs, ChannelOptions channelOptions?) -> Message[]
A static factory method to create an array of Messages from an array of deserialized Message-like object encoded using Ably's wire protocol.
Parameters
| Parameter | Description | Type |
|---|---|---|
| encodedMsgs | an array of Message-like deserialized objects. | Array |
| channelOptions | an optional ChannelOptions. If you have an encrypted channel, use this to allow the library to decrypt the data. | Object |
Returns
An Array of Message objects
Ably::Models::PresenceMessage Enum
A PresenceMessage represents an individual presence update that is sent to or received from Ably.
Attributes
| Property | Description | Type |
|---|---|---|
| action | the event signified by a PresenceMessage. See PresenceMessage::ACTION | enum { :absent, :present, :enter, :leave, :update } |
| data | The presence update payload, if provided | String, Binary (ASCII-8BIT String), Hash, Array |
| extras | Metadata and/or ancillary payloads, if provided. The only currently valid payloads for extras are the push, ref and privileged objects. | Hash, Array |
| id | Unique ID assigned by Ably to this presence update | String |
| client_id | The client ID of the publisher of this presence update | String |
| connection_id | The connection ID of the publisher of this presence update | String |
| timestamp | Timestamp when the presence update was received by Ably. | Time |
| encoding | This will typically be empty as all presence updates received from Ably are automatically decoded client-side using this value. However, if the message encoding cannot be processed, this attribute will contain the remaining transformations not applied to the data payload | String |
PresenceMessage constructors
PresenceMessage.fromEncoded
PresenceMessage.fromEncoded(Object encodedPresMsg, ChannelOptions channelOptions?) -> PresenceMessage
A static factory method to create a PresenceMessage from a deserialized PresenceMessage-like object encoded using Ably's wire protocol.
Parameters
| Parameter | Description | Type |
|---|---|---|
| encodedPresMsg | a PresenceMessage-like deserialized object. | Object |
| channelOptions | an optional ChannelOptions. If you have an encrypted channel, use this to allow the library to decrypt the data. | Object |
Returns
A PresenceMessage object
PresenceMessage.fromEncodedArray
PresenceMessage.fromEncodedArray(Object[] encodedPresMsgs, ChannelOptions channelOptions?) -> PresenceMessage[]
A static factory method to create an array of PresenceMessages from an array of deserialized PresenceMessage-like object encoded using Ably's wire protocol.
Parameters
| Parameter | Description | Type |
|---|---|---|
| encodedPresMsgs | an array of PresenceMessage-like deserialized objects. | Array |
| channelOptions | an optional ChannelOptions. If you have an encrypted channel, use this to allow the library to decrypt the data. | Object |
Returns
An Array of PresenceMessage objects
Ably::Models::PresenceMessage::ACTION
Ably::Models::PresenceMessage::ACTION is an enum-like value representing all the Realtime Presence states & events. ACTION can be represented interchangeably as either symbols or constants.
Symbol states
1
2
3
4
5
:absent # => 0 (reserved for internal use)
:present # => 1
:enter # => 2
:leave # => 3
:update # => 4Constant states
1
2
3
4
5
PresenceMessage::ACTION.Absent # => 0 (internal use)
PresenceMessage::ACTION.Present # => 1
PresenceMessage::ACTION.Enter # => 2
PresenceMessage::ACTION.Leave # => 3
PresenceMessage::ACTION.Update # => 4Example usage
1
2
3
4
5
6
7
8
# Example with symbols
presence.on(:attached) { ... }
# Example with constants
presence.on(Ably::Models::PresenceMessage::ACTION.Enter) { ... }
# Interchangeable
Ably::Models::PresenceMessage::ACTION.Enter == :enter # => trueAbly::Models::PaginatedResult
A PaginatedResult is a type that represents a page of results for all message and presence history, stats and REST presence requests. The response from a Ably REST API paginated query is accompanied by metadata that indicates the relative queries available to the PaginatedResult object.
Attributes
| Property | Description | Type |
|---|---|---|
| items | contains the current page of results (for example an Array of Message or PresenceMessage objects for a channel history request) | Array <Message, Presence, Stats> |
Methods
first
PaginatedResult first
has_next?
Boolean has_next?
Returns true if there are more pages available by calling next and returns false if this page is the last page available.
last?
Boolean last?
Returns true if this page is the last page and returns false if there are more pages available by calling next available.
next
PaginatedResult next
Returns a new PaginatedResult loaded with the next page of results. If there are no further pages, then is returned. When using the Realtime library, the first method returns a Deferrable and yields a PaginatedResult.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# When using the REST sync library
first_page = channel.history
puts "Page 0 item 0: #{first_page.items[0].data}"
if first_page.has_next?
next_page = first_page.next
puts "Page 1 item 1: #{next_page.items[1].data}"
puts "Last page?: #{next_page.is_last?}"
end
# When using the Realtime EventMachine library
channel.history do |first_page|
puts "Page 0 item 0: #{first_page.items[0].data}"
if first_page.has_next?
first_page.next do |next_page|
puts "Page 1 item 1: #{next_page.items[1].data}"
puts "Last page?: #{next_page.is_last?}"
end
end
end