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:

add_message_action(
channel: channel,
type: type,
value: value,
message_timetoken: message_timetoken,
http_sync: http_sync,
callback: callback
)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name to add the message action to.
type *
Type: String
Default:
n/a
Message action type (max 15 characters).
value *
Type: String
Default:
n/a
Message action value.
message_timetoken *
Type: Integer
Default:
n/a
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:
n/a
Callback invoked for each envelope (for async calls, use value to retrieve the envelope).

Sample code

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
require 'pubnub'

def add_message_action(pubnub)
puts "Adding message action..."
pubnub.add_message_action(
channel: 'chat',
type: 'emotion',
value: 'smile',
message_timetoken: 16701562382648731
) do |envelope|
if envelope.status[:error]
puts "Error adding message action: #{envelope.status[:data]}"
else
puts "Message action added successfully:"
puts "Type: #{envelope.result[:data][:type]}"
show all 39 lines

Response

#<Pubnub::Envelope
@result = {
:data => {
:type => "emotion",
:value => "smile",
:uuid => "sender-uuid",
:action_timetoken => 16701656660127890,
:message_timetoken => 16701562382648731
}
},
@status = {
:code => 200
}
>

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:

remove_message_action(
channel: channel,
message_timetoken: message_timetoken,
action_timetoken: action_timetoken,
http_sync: http_sync,
callback: callback
)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name to remove the message action from.
message_timetoken *
Type: Integer
Default:
n/a
Timetoken of the target message.
action_timetoken *
Type: Integer
Default:
n/a
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:
n/a
Callback invoked for each envelope (for async calls, use value to retrieve the envelope).

Sample code

pubnub.add_message_action(
channel: 'chat',
message_timetoken: 16701562382648731,
action_timetoken: 16701656660127890
) do |envelope|
puts envelope
end

Response

#<Pubnub::Envelope
@status = {
:code => 200,
:category => :ack,
:error => false,
}
>

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.

Method(s)

Use this Ruby method:

get_message_actions(
channel: channel,
start: start,
end: end,
limit: limit,
http_sync: http_sync,
callback: callback
)
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name to list message actions for.
start
Type: Integer
Default:
n/a
Message action timetoken for the start of the range (exclusive).
end
Type: Integer
Default:
n/a
Message action timetoken for the end of the range (inclusive).
limit
Type: Integer
Default:
n/a
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:
n/a
Callback invoked for each envelope (for async calls, use value to retrieve the envelope).

Sample code

pubnub.get_message_actions(
channel: 'chat',
start: 16701562382648731,
end: 16701562382348728
) do |envelope|
puts envelope
end

Response

#<Pubnub::Envelope
@result = {
:data => {
:message_actions => [
{
:type => "emotion_type_2",
:uuid => "sender-uuid-1",
:value => "surprised",
:message_timetoken => 16703307481706612,
:action_timetoken => 16703307649086202,
},
{
:type => "emotion_type_3",
:uuid => "sender-uuid-2",
:value => "lol",
show all 37 lines
Last updated on