Add, remove, and fetch message reactions in real-time using the CometChat JavaScript SDK. Includes listener events and helper methods for updating UI.
AI Integration Quick Reference
let messageId = "MESSAGE_ID";// Add a reactionawait CometChat.addReaction(messageId, "😊");// Remove a reactionawait CometChat.removeReaction(messageId, "😊");// Fetch reactions for a messageconst request = new CometChat.ReactionRequestBuilder() .setMessageId(messageId).setLimit(10).build();const reactions = await request.fetchNext();// Listen for reaction eventsCometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({ onMessageReactionAdded: (reaction) => {}, onMessageReactionRemoved: (reaction) => {}}));
Reactions let users respond to messages with emoji. You can add or remove reactions, fetch all reactions on a message, listen for reaction events in real time, and update your UI when reactions change.Reactions work on text messages, media messages, and custom messages.
Call getReactedByMe() on any ReactionCount object to check whether the logged-in user has reacted with that particular emoji. Returns true if they have, false otherwise.
TypeScript
JavaScript
let reactions = message.getReactions();reactions.forEach((reaction) => {reaction.getReactedByMe(); //Return true is logged-in user reacted on that message, otherwise false})
let reactions = message.getReactions();reactions.forEach((reaction) => {reaction.getReactedByMe(); //Return true is logged-in user reacted on that message, otherwise false})
To get the full list of who reacted with what on a specific message, use ReactionRequestBuilder. You can paginate through results with fetchNext() and fetchPrevious() (max 100 per request).
Builder Method
Description
setMessageId(value)
The message ID to fetch reactions for. Required.
setReaction(value)
Filter to a specific emoji (e.g., "😊"). When set, only reactions matching this emoji are returned.
setLimit(value)
Number of reactions to fetch per request. Max 100.
let limit = 10;let messageId = 1;let reactionRequest = new CometChat.ReactionRequestBuilder().setMessageId(messageId).setLimit(limit).build();reactionRequest.fetchNext().then( (reactions) => { console.log("list fetched:", reactions); }, (error) => { console.log('list fetching failed with error:', error); }, );
The fetchNext() method returns an array of Reaction objects representing individual user reactions on the message.Fetch Previous The fetchPrevious() method fetches the previous set of reactions for the message.
TypeScript
JavaScript
let limit = 10;let messageId = 1;let reactionRequest = new CometChat.ReactionRequestBuilder().setMessageId(messageId).setLimit(limit).build();reactionRequest.fetchNext().then( messages => { console.log("list fetched:", messages); }, error => { console.log('list fetching failed with error:', error); }, );
let limit = 10;let messageId = 1;let reactionRequest = new CometChat.ReactionRequestBuilder().setMessageId(messageId).setLimit(limit).build();reactionRequest.fetchNext().then( messages => { console.log("list fetched:", messages); }, error => { console.log('list fetching failed with error:', error); }, );
let listenerID:string = "UNIQUE_LISTENER_ID";CometChat.removeMessageListener(listenerID);
let listenerID = "UNIQUE_LISTENER_ID";CometChat.removeMessageListener(listenerID);
Always remove listeners when they’re no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.
When you receive a real-time reaction event, you’ll want to update the corresponding message object so your UI reflects the change. updateMessageWithReactionInfo() does this — it takes the BaseMessage instance, the MessageReaction event data, and the action type, then returns the updated message.
TypeScript
JavaScript
message.getReactions()
message.getReactions()
The getReactions() method returns an array of ReactionCount objects, each containing the reaction emoji, the total count of users who reacted with it, and whether the logged-in user used that reaction.
To check if the logged-in user has reacted on a particular message or not, You can use the getReactedByMe() method on any ReactionCount object instance. This method will return a boolean value, true if the logged-in user has reacted on that message, otherwise false.
TypeScript
JavaScript
let reactions = message.getReactions();reactions.forEach((reaction) => {reaction.getReactedByMe(); //Return true is logged-in user reacted on that message, otherwise false})
let reactions = message.getReactions();reactions.forEach((reaction) => {reaction.getReactedByMe(); //Return true is logged-in user reacted on that message, otherwise false})
The getReactedByMe() method on a ReactionCount object returns a boolean — true if the logged-in user has reacted with that specific emoji, false otherwise.
When a user adds or removes a reaction, you will receive a real-time event. Once you receive the real time event you would want to update the message with the latest reaction information. To do so you can use the updateMessageWithReactionInfo() method.The updateMessageWithReactionInfo() method provides a seamless way to update the reactions on a message instance (BaseMessage) in real-time. This method ensures that when a reaction is added or removed from a message, the BaseMessage object’s getReactions() property reflects this change immediately.When you receive a real-time reaction event (MessageReaction), call the updateMessageWithReactionInfo() method, passing the BaseMessage instance (message), event data (MessageReaction) and reaction event action type (CometChat.REACTION_ACTION.REACTION_ADDED or CometChat.REACTION_ACTION.REACTION_REMOVED) that corresponds to the message being reacted to.
TypeScript
JavaScript
// The message to which the reaction is related (obtained from fetchPrevious/fetchNext or a listener)let message!: CometChat.BaseMessage;// The reaction event data received in real-time (obtained from onMessageReactionAdded/onMessageReactionRemoved)let messageReaction!: CometChat.MessageReaction;// The received reaction event real-time action type. Can be CometChatConstants.REACTION_ADDED or CometChatConstants.REACTION_REMOVEDlet action: CometChat.REACTION_ACTION = CometChat.REACTION_ACTION.REACTION_ADDED;let modifiedBaseMessage = CometChat.CometChatHelper.updateMessageWithReactionInfo(message,messageReaction,action);
// The message to which the reaction is relatedlet message = ...;// The reaction event data received in real-timelet messageReaction = ...;// The recieved reaction event real-time action type. Can be CometChatConstants.REACTION_ADDED or CometChatConstants.REACTION_REMOVEDlet action = CometChat.REACTION_ACTION.REACTION_ADDED;let modifiedBaseMessage = CometChat.CometChatHelper.updateMessageWithReactionInfo(baseMessage, messageReaction, action);
On success, this method returns a BaseMessage objectAfter calling this method, use modifiedBaseMessage.getReactions() to get the latest reactions and refresh your UI.