Skip to main content
Quick Reference for AI Agents & Developers
// Send transient message to user
const msg = new CometChat.TransientMessage("UID", CometChat.RECEIVER_TYPE.USER, { LIVE_REACTION: "heart" });
CometChat.sendTransientMessage(msg);

// Listen for transient messages
CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({
  onTransientMessageReceived: (msg) => console.log("Transient:", msg)
}));
Transient messages are messages that are sent in real-time only and are not saved or tracked anywhere. The receiver of the message will only receive the message if he is online and these messages cannot be retrieved later.
Available via: SDK | REST API | UI Kits

Send a Transient Message

You can use the sendTransientMessage() method to send a transient message to a user or in a group. The receiver will receive this information in the onTransientMessageReceived() method of the MessageListener class. In order to send the transient message, you need to use the TransientMessage class.
let receiverId = "UID";
let receiverType = CometChat.RECEIVER_TYPE.USER;
let data = { "LIVE_REACTION": "heart" };

let transientMessage = new CometChat.TransientMessage(receiverId, receiverType, data);
CometChat.sendTransientMessage(transientMessage);

Real-time Transient Messages

In other words, as a recipient, how do I know when someone sends a transient message?
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.
You will receive the transient message in the onTransientMessageReceived() method of the registered MessageListener class.
let listenerId = "UNIQUE_LITENER_ID";

CometChat.addMessageListener(
listenerId,
new CometChat.MessageListener({
  onTransientMessageReceived: transientMessage => {
    console.log('transient message received', transientMessage);
  },
})
);   
The TransientMessage class consists of the below parameters:
ParameterInformation
senderAn object of the User class holding all the information. related to the sender of the transient message.
receiverIdUnique Id of the receiver. This can be the Id of the group or the user the transient message is sent to.
receiverTypeThe type of the receiver - CometChat.RECEIVER_TYPE.USER or CometChat.RECEIVER_TYPE.GROUP
dataA JSONObject to provide data.

Next Steps