Send messages with user mentions, retrieve mentioned users, and filter messages by mention metadata using the CometChat JavaScript SDK.
AI Integration Quick Reference
Report incorrect code
Copy
Ask AI
// Send a message with a mention (use <@uid:UID> format)const msg = new CometChat.TextMessage("receiverUID", "Hello <@uid:cometchat-uid-1>", CometChat.RECEIVER_TYPE.USER);await CometChat.sendMessage(msg);// Get mentioned users from a messageconst mentionedUsers = message.getMentionedUsers();// Fetch messages with mention tag infoconst request = new CometChat.MessagesRequestBuilder() .setUID("UID").setLimit(30).mentionsWithTagInfo(true).build();const messages = await request.fetchPrevious();
Mentions in messages enable users to refer to specific individual within a conversation. This is done by using the <@uid:UID> format, where UID represents the user’s unique identification.Mentions are a powerful tool for enhancing communication in messaging platforms. They streamline interaction by allowing users to easily engage and collaborate with particular individuals, especially in group conversations.
To send a message with a mentioned user, you must follow a specific format: <@uid:UID>. For example, to mention the user with UID cometchat-uid-1 with the message “Hello,” your text would be "Hello, <@uid:cometchat-uid-1>"
User
Group
TypeScript (User)
TypeScript (Group)
Report incorrect code
Copy
Ask AI
let receiverID = "UID";let messageText = "Hello, <@uid:cometchat-uid-1>";let receiverType = CometChat.RECEIVER_TYPE.USER;let textMessage = new CometChat.TextMessage( receiverID, messageText, receiverType);CometChat.sendMessage(textMessage).then( (message) => { console.log("Message sent successfully:", message); }, (error) => { console.log("Message sending failed with error:", error); });
Report incorrect code
Copy
Ask AI
let receiverID = "GUID";let messageText = "Hello <@uid:cometchat-uid-1>";let receiverType = CometChat.RECEIVER_TYPE.GROUP;let textMessage = new CometChat.TextMessage( receiverID, messageText, receiverType);CometChat.sendMessage(textMessage).then( (message) => { console.log("Message sent successfully:", message); }, (error) => { console.log("Message sending failed with error:", error); });
By default, the SDK will fetch all the messages irrespective of the fact that the logged-in user is mentioned or not in the message. The SDK has other optional filters such as tags and blocked relationships.
Setting
Description
mentionsWithTagInfo(boolean value)
If set to true, SDK will fetch a list of messages where users are mentioned & will also fetch the tags of the mentioned users. Default value = false.
mentionsWithBlockedInfo(boolean value)
If set to true, SDK will fetch a list of messages where users are mentioned & will also fetch their blocked relationship with the logged-in user. Default value = false.
To get a list of messages in a conversation where users are mentioned along with the blocked relationship of the mentioned users with the logged-in user.
To retrieve the list of users mentioned in the particular message, you can use the message.getMentionedUsers() method. This method will return an array containing the mentioned users, or an empty array if no users were mentioned in the message.
Report incorrect code
Copy
Ask AI
message.getMentionedUsers()
Messages containing mentions are returned as BaseMessage objects with mention-related fields populated. Access the mention data using getter methods: