Reactions

The Stream Chat SDK has built-in support for user reactions. Some common examples are likes, comments, loves, etc. The reactions are also customizable, so you can use any type of reaction your application requires.

Sending a Reaction

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 is Result.Success) {
    val sentReaction: Reaction = result.value
  } else {
    // Handle Result.Failure
  }
}

// Add reaction 'like' and replace all other reactions of this user by it
channelClient.sendReaction(reaction, enforceUnique = true).enqueue { result ->
  if (result is Result.Success) {
    val sentReaction: Reaction = result.value
  } else {
    // Handle Result.Failure
  }
}

Below you can find all the parameters for sending a reaction:

nametypedescriptiondefaultoptional
message_idstringID of the message to react to
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
skip_pushbooleanDo not send a push notificationfalse
emoji_codestringAccepts an unicode which is used by the backend to display the emoji in push notification

Custom data for reactions is limited to 1KB.

Cumulative Reactions (Clap)

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).

// Android SDK
Reaction reaction = new Reaction.Builder()
        .withMessageId("message-id")
        .withType("like")
        .withScore(5)
        .build();

boolean enforceUnique = false;
channelClient.sendReaction(reaction, enforceUnique).enqueue(result -> { /* ... */ });

// Backend SDK
// Add reaction 'like' and replace all other reactions of this user by it
Reaction.send(messageId)
  .enforceUnique(true)
  .reaction(ReactionRequestObject.builder().type("like").score(5).userId(userId).build())
  .request();

Removing a Reaction

// Android SDK
String reactionType = "like";
channelClient.deleteReaction("message-id", reactionType).enqueue(result -> {
  if (result.isSuccess()) {
    Message message = result.getOrNull();
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Reaction.delete(message.getId(), reactionType)
  .userId(user.getId())
  .request());

Fetching Reactions

The message object will contain the most recent reactions of a message. But if you need to display all the reactions of a message, you can do so by fetching all the reactions.

// Android SDK

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

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

// Backend SDK
// get 3 reactions past the first 10
Reaction.list(messageId).limit(3).offset(10).request();

Querying Reactions

If you need more control on how to fetch the reactions, you can use the query reactions endpoint. This allows you to filter the reactions by their type or the author id, so that you can provide an experience similar to Slack, for example, show all users who reacted with “like”.

// Filter by type
await client.queryReactions(message.id, { type: "like" });
// Filter by user id
const id = uuidv4();
await client.queryReactions(message.id, { user_id: id });
// Paginate the results
const data = await client.queryReactions(message.id, {});
const dataPage2 = await client.queryReactions(
  message.id,
  {},
  {},
  { limit: 5, next: data.next },
);
© Getstream.io, Inc. All Rights Reserved.