Class: Ably::Realtime::Models::ProtocolMessage

Inherits:
Object
  • Object
show all
Includes:
Modules::Conversions, Shared
Defined in:
lib/ably/realtime/models/protocol_message.rb

Overview

A message sent and received over the Realtime protocol. A ProtocolMessage always relates to a single channel only, but can contain multiple individual Messages or PresenceMessages. ProtocolMessages are serially numbered on a connection. See the / Ably client library developer documentation for further details on the members of a ProtocolMessage

Constant Summary collapse

ACTIONS =

Actions which are sent by the Ably Realtime API

The values correspond to the ints which the API understands.

{
  heartbeat:    0,
  ack:          1,
  nack:         2,
  connect:      3,
  connected:    4,
  disconnect:   5,
  disconnected: 6,
  close:        7,
  closed:       8,
  error:        9,
  attach:       10,
  attached:     11,
  detach:       12,
  detached:     13,
  presence:     14,
  message:      15
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared

#==, #[]

Constructor Details

#initialize(json_object) ⇒ ProtocolMessage

Returns a new instance of ProtocolMessage.



80
81
82
83
# File 'lib/ably/realtime/models/protocol_message.rb', line 80

def initialize(json_object)
  @raw_json_object = json_object
  @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
end

Instance Attribute Details

#actionInteger (readonly)

Returns Protocol Message action from list of ACTIONS.

Returns:

  • (Integer)

    Protocol Message action from list of ACTIONS



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#action_symSymbol (readonly)

Returns Protocol Message action as a symbol.

Returns:

  • (Symbol)

    Protocol Message action as a symbol



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#channelString (readonly)

Returns Channel name for messages.

Returns:

  • (String)

    Channel name for messages



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#channel_serialString (readonly)

Returns Contains a serial number for amessage on the current channel.

Returns:

  • (String)

    Contains a serial number for amessage on the current channel



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#connection_idString (readonly)

Returns Contains a string connection ID.

Returns:

  • (String)

    Contains a string connection ID



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#connection_serialBignum (readonly)

Returns Contains a serial number for a message on the current connection.

Returns:

  • (Bignum)

    Contains a serial number for a message on the current connection



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#countInteger (readonly)

Returns The count field is used for ACK and NACK actions. See message acknowledgement protocol.

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#error_infoErrorInfo (readonly)

Returns Contains error information.

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#jsonHash (readonly)

Returns Access the protocol message Hash object ruby’fied to use symbolized keys.

Returns:

  • (Hash)

    Access the protocol message Hash object ruby’fied to use symbolized keys



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#message_serialBignum (readonly)

Returns Contains a serial number for a message sent from the client to the service.

Returns:

  • (Bignum)

    Contains a serial number for a message sent from the client to the service.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#messagesMessage (readonly)

Returns A Ably::Realtime::Models::ProtocolMessage with a :message action contains one or more messages belonging to a channel.

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#presencePresenceMessage (readonly)

Returns A Ably::Realtime::Models::ProtocolMessage with a :presence action contains one or more presence updates belonging to a channel.

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

#timestampTime (readonly)

Returns An optional timestamp, applied by the service in messages sent to the client, to indicate the system time at which the message was sent (milliseconds past epoch).

Returns:

  • (Time)

    An optional timestamp, applied by the service in messages sent to the client, to indicate the system time at which the message was sent (milliseconds past epoch)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ably/realtime/models/protocol_message.rb', line 36

class ProtocolMessage
  include Shared
  include Ably::Modules::Conversions

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTIONS = {
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15
  }.freeze

  # Retrieve an action symbol by the integer value
  def self.action_sym_for(action_int)
    @actions_index_by_int ||= ACTIONS.invert.freeze
    @actions_index_by_int[action_int]
  end

  # Retrive an action integer value from a symbol and raise an exception if invalid
  def self.action!(action_sym)
    ACTIONS.fetch(action_sym)
  end

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  def self.ack_required?(for_action)
    for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
    [action!(:presence), action!(:message)].include?(for_action)
  end

  def initialize(json_object)
    @raw_json_object = json_object
    @json_object     = IdiomaticRubyWrapper(@raw_json_object.clone.freeze)
  end

  %w( action count
      channel channel_serial
      connection_id connection_serial ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def action_sym
    self.class.action_sym_for(action)
  end

  def error
    @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
  end

  def timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def message_serial
    json[:msg_serial]
  end

  def messages
    @messages ||=
      Array(json[:messages]).map do |message|
        Message.new(message, self)
      end
  end

  def presence
    @presence ||=
      Array(json[:presence]).map do |message|
        PresenceMessage.new(message, self)
      end
  end

  def json
    @json_object
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  def ack_required?
    self.class.ack_required?(action)
  end

  def to_json_object
    raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
    raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

    json.dup.tap do |json_object|
      json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
      json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end
end

Class Method Details

.ack_required?(for_action) ⇒ Boolean

Indicates this protocol message action will generate an ACK response such as :message or :presence

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/ably/realtime/models/protocol_message.rb', line 75

def self.ack_required?(for_action)
  for_action = ACTIONS.fetch(for_action) if for_action.kind_of?(Symbol)
  [action!(:presence), action!(:message)].include?(for_action)
end

.action!(action_sym) ⇒ Object

Retrive an action integer value from a symbol and raise an exception if invalid



70
71
72
# File 'lib/ably/realtime/models/protocol_message.rb', line 70

def self.action!(action_sym)
  ACTIONS.fetch(action_sym)
end

.action_sym_for(action_int) ⇒ Object

Retrieve an action symbol by the integer value



64
65
66
67
# File 'lib/ably/realtime/models/protocol_message.rb', line 64

def self.action_sym_for(action_int)
  @actions_index_by_int ||= ACTIONS.invert.freeze
  @actions_index_by_int[action_int]
end

Instance Method Details

#ack_required?Boolean

Indicates this protocol message will generate an ACK response when sent Examples of protocol messages required ACK include :message and :presence

Returns:

  • (Boolean)


129
130
131
# File 'lib/ably/realtime/models/protocol_message.rb', line 129

def ack_required?
  self.class.ack_required?(action)
end

#errorObject



97
98
99
# File 'lib/ably/realtime/models/protocol_message.rb', line 97

def error
  @error_info ||= ErrorInfo.new(json[:error]) if json[:error]
end

#to_json(*args) ⇒ Object



143
144
145
# File 'lib/ably/realtime/models/protocol_message.rb', line 143

def to_json(*args)
  to_json_object.to_json
end

#to_json_objectObject

Raises:

  • (RuntimeError)


133
134
135
136
137
138
139
140
141
# File 'lib/ably/realtime/models/protocol_message.rb', line 133

def to_json_object
  raise RuntimeError, ":action is missing, cannot generate valid JSON for ProtocolMessage" unless action_sym
  raise RuntimeError, ":msg_serial is missing, cannot generate valid JSON for ProtocolMessage" if ack_required? && !message_serial

  json.dup.tap do |json_object|
    json_object[:messages] = messages.map(&:to_json_object) unless messages.empty?
    json_object[:presence] = presence.map(&:to_json_object) unless presence.empty?
  end
end