Presence

Methods

get

Get the current presence member set for this channel. In the REST client library this method directly queries Ably's REST presence API

PaginatedResult<PresenceMessage> get(Hash options)

Gets an array of members present on the channel as PresenceMessage objects.

Parameters

NameDescription
optionsan optional set of symbol key and value pairs containing the query parameters as specified below.

options parameters

NameDescription
:client_idwhen provided, will filter array of members returned that match the provided client_id string
:connection_idwhen provided, will filter array of members returned that match the provided connection_id string

Returns

On success, the returned PaginatedResult encapsulates an array of PresenceMessage objects corresponding to the current page of members currently present on the channel. PaginatedResult supports pagination using next and first methods.

Failure to retrieve the current presence member set will raise an AblyException

Returns

On success, resultPage contains a PaginatedResult encapsulating an array of PresenceMessage objects corresponding to the current page of members currently present on the channel. PaginatedResult supports pagination using next() and first() methods.

Failure to retrieve the current presence member, the error contains an ErrorInfo object with the failure reason.

history

PaginatedResult<PresenceMessage> history(Hash options)

Gets a paginated set of historical presence message events for this channel. If the channel is configured to persist messages to disk, then the presence message event history will typically be available for 24 - 72 hours. If not, presence message events are only retained in memory by the Ably service for two minutes.

Parameters

ParameterDescriptionType
optionsan optional set of key value pairs containing query parameters, as specified in the presence history API documentation.Object

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 message history will raise an AblyException

Ably::Models::PresenceMessage

A PresenceMessage represents an individual presence update that is sent to or received from Ably.

Attributes

PropertyDescriptionType
actionthe event signified by a PresenceMessage. See PresenceMessage::ACTIONenum { :absent, :present, :enter, :leave, :update }
dataThe presence update payload, if providedString, Binary (ASCII-8BIT String), Hash, Array
extrasMetadata and/or ancillary payloads, if provided. The only currently valid payloads for extras are the push, ref and privileged objects.Hash, Array
idUnique ID assigned by Ably to this presence updateString
client_idThe client ID of the publisher of this presence updateString
connection_idThe connection ID of the publisher of this presence updateString
timestampTimestamp when the presence update was received by Ably.Time
encodingThis 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 payloadString

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

ParameterDescriptionType
encodedPresMsga PresenceMessage-like deserialized object.Object
channelOptionsan 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

ParameterDescriptionType
encodedPresMsgsan array of PresenceMessage-like deserialized objects.Array
channelOptionsan 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

Ruby

1

2

3

4

5

  :absent  # => 0 (reserved for internal use)
  :present # => 1
  :enter   # => 2
  :leave   # => 3
  :update  # => 4

Constant states

Ruby

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  # => 4

Example usage

Ruby

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 # => true

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

PropertyDescriptionType
itemscontains 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

Ruby

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