Message Actions API for Ruby SDK

Use message actions to add or remove metadata on published messages. Common uses include receipts and reactions. Clients subscribe to a channel to receive message action events. Clients can also fetch past message actions from Message Persistence independently or when they fetch original messages.

Reactions

"Message Reactions" is a specific application of the Message Actions API for emoji or social reactions.

Message Actions vs. Message Reactions

Message Actions is the flexible, low-level API for adding any metadata to messages (read receipts, delivery confirmations, custom data), while Message Reactions specifically refers to using Message Actions for emoji/social reactions.

In PubNub Core and Chat SDKs, the same underlying Message Actions API is referred to as Message Reactions when used for emoji reactions - it's the same functionality, just different terminology depending on the use case.

Add message action​

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Add an action to a published message. The response includes the added action.

Method(s)​

Use this Ruby method:

1add_message_action(
2 channel: channel,
3 type: type,
4 value: value,
5 message_timetoken: message_timetoken,
6 http_sync: http_sync,
7 callback: callback
8)
required
channel required#
Type: String
Default: no default
Channel name to add the message action to.
type required#
Type: String
Default: no default
Message action type (max 15 characters).
value required#
Type: String
Default: no default
Message action value.
message_timetoken required#
Type: Integer
Default: no default
Timetoken of the target message.
http_sync#
Type: Boolean
Default: false
When true, executes synchronously and returns an array of envelopes.
callback#
Type: Lambda accepting one parameter
Default: no default
Callback invoked for each envelope (for async calls, use value to retrieve the envelope).

Sample code​

1require 'pubnub'
2
3def add_message_action(pubnub)
4 puts "Adding message action..."
5 pubnub.add_message_action(
6 channel: 'chat',
7 type: 'emotion',
8 value: 'smile',
9 message_timetoken: 16701562382648731
10 ) do |envelope|
11 if envelope.status[:error]
12 puts "Error adding message action: #{envelope.status[:data]}"
13 else
14 puts "Message action added successfully:"
15 puts "Type: #{envelope.result[:data][:type]}"
show all 39 lines

Response​

1#<Pubnub::Envelope
2 @result = {
3 :data => {
4 :type => "emotion",
5 :value => "smile",
6 :uuid => "sender-uuid",
7 :action_timetoken => 16701656660127890,
8 :message_timetoken => 16701562382648731
9 }
10 },
11 @status = {
12 :code => 200
13 }
14>

Remove message action​

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Remove a previously added action from a published message. The response is empty.

Method(s)​

Use this Ruby method:

1remove_message_action(
2 channel: channel,
3 message_timetoken: message_timetoken,
4 action_timetoken: action_timetoken,
5 http_sync: http_sync,
6 callback: callback
7)
required
channel required#
Type: String
Default: no default
Channel name to remove the message action from.
message_timetoken required#
Type: Integer
Default: no default
Timetoken of the target message.
action_timetoken required#
Type: Integer
Default: no default
Timetoken of the message action to remove.
http_sync#
Type: Boolean
Default: false
When true, executes synchronously and returns an array of envelopes.
callback#
Type: Lambda accepting one parameter
Default: no default
Callback invoked for each envelope (for async calls, use value to retrieve the envelope).

Sample code​

1pubnub.add_message_action(
2 channel: 'chat',
3 message_timetoken: 16701562382648731,
4 action_timetoken: 16701656660127890
5) do |envelope|
6 puts envelope
7end

Response​

1#<Pubnub::Envelope
2 @status = {
3 :code => 200,
4 :category => :ack,
5 :error => false,
6 }
7>

Get message actions​

Requires Message Persistence

This method requires that Message Persistence is enabled for your key in the Admin Portal.

Get a list of message actions in a channel. The response sorts actions by the action timetoken in ascending order.

How to use the start and end parameters

PubNub retrieves messages by searching backward through time (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:

  • start is the newer boundary (higher timetoken value) - search begins here, exclusive
  • end is the older boundary (lower timetoken value) - search stops here, inclusive

When you provide both parameters, start must be a higher timetoken than end.

9:00 AM10:00 AM11:00 AM11:59 AM

Results are always returned in oldest-first order.

Method(s)​

Use this Ruby method:

1get_message_actions(
2 channel: channel,
3 start: start,
4 end: end,
5 limit: limit,
6 http_sync: http_sync,
7 callback: callback
8)
required
channel required#
Type: String
Default: no default
Channel name to list message actions for.
start#
Type: Integer
Default: no default
Newer boundary of the query (exclusive). Must be a higher timetoken value than end when both are provided.
end#
Type: Integer
Default: no default
Older boundary of the query (inclusive). Must be a lower timetoken value than start when both are provided.
limit#
Type: Integer
Default: no default
Number of message actions to return.
http_sync#
Type: Boolean
Default: false
When true, executes synchronously and returns an array of envelopes.
callback#
Type: Lambda accepting one parameter
Default: no default
Callback invoked for each envelope (for async calls, use value to retrieve the envelope).

Sample code​

1pubnub.get_message_actions(
2 channel: 'chat',
3 start: 16701562382648731,
4 end: 16701562382348728
5) do |envelope|
6 puts envelope
7end

Response​

1#<Pubnub::Envelope
2 @result = {
3 :data => {
4 :message_actions => [
5 {
6 :type => "emotion_type_2",
7 :uuid => "sender-uuid-1",
8 :value => "surprised",
9 :message_timetoken => 16703307481706612,
10 :action_timetoken => 16703307649086202,
11 },
12 {
13 :type => "emotion_type_3",
14 :uuid => "sender-uuid-2",
15 :value => "lol",
show all 37 lines

Was this page useful?

Last updated on