Reactions

Stream Chat has built-in support for user Reactions. Common examples are likes, comments, loves, etc. Reactions can be customized so that you are able to use any type of reaction your application requires.

Similar to other objects in Stream Chat, reactions allow you to add custom data to the reaction of your choice. This is helpful if you want to customize the reaction logic.

nametypedescriptiondefaultoptional
message_idstringID of the message to react to
reactionobjectReaction object
reaction.typestringType of the reaction. User could have only 1 reaction of each type per message
reaction.scoreintegerScore of the reaction for cumulative reactions (see example below)1
user_idstringUser ID for server side calls
enforce_uniquebooleanIf set to true, new reaction will replace all reactions the user has (if any) on this messagefalse

Custom data for reactions is limited to 1KB.

val channelClient = client.channel("messaging", "general")

// Add reaction 'like' with a custom field
val reaction = Reaction(
  messageId = "message-id",
  type = "like",
  score = 1,
  extraData = mutableMapOf("customField" to 1),
)
channelClient.sendReaction(reaction).enqueue { result ->
  if (result.isSuccess) {
    val sentReaction: Reaction = result.data()
  } else {
    // Handle result.error()
  }
}

// Add reaction 'like' and replace all other reactions of this user by it
channelClient.sendReaction(reaction, enforceUnique = true).enqueue { result ->
  if (result.isSuccess) {
    val sentReaction = result.data()
  } else {
    // Handle result.error()
  }
}

Removing a Reaction

channelClient.deleteReaction(
  messageId = "message-id",
  reactionType = "like",
).enqueue { result ->
  if (result.isSuccess) {
    val message = result.data()
  } else {
    // Handle result.error()
  }
}

Retrieve Reactions

Reactions are being returned as a part of the Message object. Please refer to the Message format section for more details.

Paginating Reactions

Messages returned by the APIs automatically include the 10 most recent reactions. You can also retrieve more reactions and paginate using the following logic, consider that the maximum value for the Limit parameter is 300 and for the Offset parameter is 1000:

// Get the first 10 reactions
channelClient.getReactions(
  messageId = "message-id",
  offset = 0,
  limit = 10,
).enqueue { result ->
  if (result.isSuccess) {
    val reactions: List<Reaction> = result.data()
  } else {
    // Handle result.error()
  }
}

// Get the second 10 reactions
channelClient.getReactions(
  messageId = "message-id",
  offset = 10,
  limit = 10,
).enqueue { /* ... */ }

// Get 10 reactions after particular reaction
channelClient.getReactions(
  messageId = "message-id",
  firstReactionId = "reaction-id",
  limit = 10,
).enqueue { /* ... */ }

Cumulative (Clap) Reactions

You can use the Reactions API to build something similar to Medium’s clap reactions. If you are not familiar with this, Medium allows you to clap articles more than once and shows the sum of all claps from all users.

To do this, you only need to include a score for the reaction (ie. user X clapped 25 times) and the API will return the sum of all reaction scores as well as each user’s individual scores (ie. clapped 475 times, user Y clapped 14 times).

val reaction = Reaction(messageId = "message-id", type = "clap", score = 5)
channelClient.sendReaction(reaction).enqueue { /* ... */ }

Query Reactions

This endpoint allow you to query reactions by filtering over type and user on a specific message id.

In case of this endpoint is used client side, the user need to have permission to read channel.

const id = uuidv4();
await client.queryReactions(message.id, { type: 'like' });
await client.queryReactions(message.id, { user_id: id });;

Clearly it is possible to have different call for pagination:

  • first call will fetch 25 reactions

  • the second returns other 5 reactions starting from the latest of the first call

const data =await client.queryReactions(message.id, {});
const dataPage2 = await reactionClient.queryReactions(
   message.id,
   {},
   {},
   { limit: 5, next: data.next },
  );
© Getstream.io, Inc. All Rights Reserved.