Quick Reference - Send a text message:
- Text Message is the most common and standard message type.
- Media Message, for sending photos, videos and files.
- Custom Message, for sending completely custom data using JSON structures.
- Interactive Message for sending end-user interactive messages of type form, card and custom interactive.
Text Message
In other words, as a sender, how do I send a text message? To send a text message to a single user or group, you need to use thesendMessage() method and pass a TextMessage object to it.
Add Metadata
To send custom data along with a text message, you can use thesetMetadata method and pass a JSON Object to it.
- JavaScript
- TypeScript
Add Tags
To add a tag to a message you can use thesetTags() method of the TextMessage Class. The setTags() method accepts a list of tags.
- JavaScript
- TypeScript
sendMessage() method to send the text message to the recipient.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
TextMessage class constructor takes the following parameters:
| Parameter | Description | Required |
|---|---|---|
| receiverID | UID of the user or GUID of the group receiving the message | YES |
| messageText | The text message | YES |
| receiverType | The type of the receiver- CometChat.RECEIVER_TYPE.USER or CometChat.RECEIVER_TYPE.GROUP | YES |
TextMessage object which includes all information related to the sent message.
Response
Response
On Success — Returns a TextMessage object:TextMessage Object:
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
id | string | Unique message identifier | "25182" |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
text | string | Message text content | "Hello world!" |
type | string | Message type | "text" |
category | string | Message category | "message" |
receiverId | string | UID of the receiver | "cometchat-uid-3" |
receiverType | string | Type of receiver | "user" |
sentAt | number | Unix timestamp when sent | 1771320772 |
updatedAt | number | Unix timestamp of last update | 1771320772 |
sender | object | Sender user details | See below ↓ |
receiver | object | Receiver user details | See below ↓ |
data | object | Additional message data | See below ↓ |
reactions | array | Message reactions | [] |
mentionedUsers | array | Users mentioned in message | [] |
mentionedMe | boolean | Whether logged-in user is mentioned | false |
metadata | object | Custom metadata | {"@injected": {"extensions": {"link-preview": {"links": []}}}} |
sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the sender | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771320632 |
hasBlockedMe | boolean | Whether sender has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked sender | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the receiver | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771320647 |
hasBlockedMe | boolean | Whether receiver has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked receiver | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
data Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
text | string | Message text | "Hello world!" |
resource | string | SDK resource identifier | "REACT_NATIVE-4_0_13-..." |
entities | object | Sender and receiver entities | See below ↓ |
metadata | object | Injected metadata from extensions | {"@injected": {"extensions": {"link-preview": {"links": []}}}} |
moderation | object | Moderation status | See below ↓ |
data.entities Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
sender | object | Sender entity wrapper | See below ↓ |
receiver | object | Receiver entity wrapper | See below ↓ |
data.entities.sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Sender user details | See below ↓ |
data.entities.sender.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771320632 |
tags | array | User tags | [] |
data.entities.receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Receiver user details | See below ↓ |
data.entities.receiver.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771320647 |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
tags | array | User tags | [] |
data.moderation Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
status | string | Moderation status | "pending" |
Set Quoted Message Id
To set a quoted message ID for a message, use thesetQuotedMessageId() method of the TextMessage class. This method accepts the ID of the message to be quoted.
- JavaScript
- TypeScript
sendMessage() method to send the text message to the recipient.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
TextMessage class constructor takes the following parameters:
| Parameter | Description | Required |
|---|---|---|
| receiverID | UID of the user or GUID of the group receiving the message | Required |
| messageText | The text message | Required |
| receiverType | The type of the receiver - CometChat.RECEIVER_TYPE.USER or CometChat.RECEIVER_TYPE.GROUP | Required |
TextMessage object which includes all information related to the sent message.
Media Message
In other words, as a sender, how do I send a media message like photos, videos & files? To send a media message to any user or group, you need to use thesendMediaMessage() method and pass a MediaMessage object to it.
Add Metadata
To send custom data along with a media message, you can use thesetMetadata method and pass a JSON Object to it.
- JavaScript
- TypeScript
Add Caption (Text along with Media Message)
To send a caption with a media message, you can use thesetCaption method and pass text to it.
- JavaScript
- TypeScript
Add Tags
To add a tag to a message you can use thesetTags() method of the MediaMessage Class. The setTags() method accepts a list of tags.
- JavaScript
- TypeScript
1. By Providing the File
You can directly share the file object while creating an object of the MediaMessage class. When the media message is sent using thesendMediaMessage() method, this file is then uploaded to CometChat servers and the URL of the file is sent in the success response of the sendMediaMessage() function.
Getting the file Object: You can use different React Native packages for sending media messages. We demonstrate how to send images using CometChat.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
MediaMessage class constructor takes the following parameters:
| Parameter | Description | Required |
|---|---|---|
| receiverId | The UID or GUID of the recipient. | YES |
| file | The file object to be sent | YES |
| messageType | The type of the message that needs to be sent which in this case can be: 1. CometChat.MESSAGE_TYPE.IMAGE 2. CometChat.MESSAGE_TYPE.VIDEO 3. CometChat.MESSAGE_TYPE.AUDIO 4. CometChat.MESSAGE_TYPE.FILE | YES |
| receiverType | The type of the receiver to whom the message is to be sent. 1. CometChat.RECEIVER_TYPE.USER 2. CometChat.RECEIVER_TYPE.GROUP | YES |
Response
Response
On Success — Returns a MediaMessage object:MediaMessage Object:
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
id | string | Unique message identifier | "25189" |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
type | string | Media type | "image" |
category | string | Message category | "message" |
receiverId | string | UID of the receiver | "cometchat-uid-3" |
receiverType | string | Type of receiver | "user" |
sentAt | number | Unix timestamp when sent | 1771323061 |
updatedAt | number | Unix timestamp of last update | 1771323061 |
sender | object | Sender user details | See below ↓ |
receiver | object | Receiver user details | See below ↓ |
data | object | Additional message data | See below ↓ |
reactions | array | Message reactions | [] |
mentionedUsers | array | Users mentioned in message | [] |
mentionedMe | boolean | Whether logged-in user is mentioned | false |
sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the sender | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771323060 |
hasBlockedMe | boolean | Whether sender has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked sender | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the receiver | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "offline" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771322968 |
hasBlockedMe | boolean | Whether receiver has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked receiver | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
data Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
type | string | Media type | "image" |
category | string | Message category | "message" |
url | string | URL to the uploaded media file | "https://data-in.cometchat.io/.../c35f9734fc20947b7456bbea68126f99.png" |
resource | string | SDK resource identifier | "REACT_NATIVE-4_0_14-..." |
attachments | array | Media attachments | See below ↓ |
entities | object | Sender and receiver entities | See below ↓ |
moderation | object | Moderation status | {"status": "pending"} |
data.attachments Array (per item):| Parameter | Type | Description | Sample Value |
|---|---|---|---|
url | string | URL to the attachment | "https://data-in.cometchat.io/.../c35f9734fc20947b7456bbea68126f99.png" |
name | string | File name | "1770616246_260562078_3832605b5c8a337ac672b0c60933d208.png" |
mimeType | string | MIME type | "image/png" |
extension | string | File extension | "png" |
size | number | File size in bytes | 2295572 |
data.entities Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
sender | object | Sender entity wrapper | See below ↓ |
receiver | object | Receiver entity wrapper | See below ↓ |
data.entities.sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Sender user details | See below ↓ |
data.entities.sender.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771323060 |
tags | array | User tags | [] |
data.entities.receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Receiver user details | See below ↓ |
data.entities.receiver.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "offline" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771322968 |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
tags | array | User tags | [] |
2. By Providing the URL of the File
The second way to send media messages using the CometChat SDK is to provide the SDK with the URL of any file that is hosted on your servers or any cloud storage. To achieve this you will have to make use of the Attachment class. For more information, you can refer to the below code snippet:- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
MediaMessage object which includes all information related to the sent message.
Response
Response
On Success — Returns a MediaMessage object:MediaMessage Object:
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
id | string | Unique message identifier | "25189" |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
type | string | Media type | "image" |
category | string | Message category | "message" |
receiverId | string | UID of the receiver | "cometchat-uid-3" |
receiverType | string | Type of receiver | "user" |
sentAt | number | Unix timestamp when sent | 1771323061 |
updatedAt | number | Unix timestamp of last update | 1771323061 |
sender | object | Sender user details | See below ↓ |
receiver | object | Receiver user details | See below ↓ |
data | object | Additional message data | See below ↓ |
reactions | array | Message reactions | [] |
mentionedUsers | array | Users mentioned in message | [] |
mentionedMe | boolean | Whether logged-in user is mentioned | false |
sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the sender | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771323060 |
hasBlockedMe | boolean | Whether sender has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked sender | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the receiver | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "offline" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771322968 |
hasBlockedMe | boolean | Whether receiver has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked receiver | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
data Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
type | string | Media type | "image" |
category | string | Message category | "message" |
url | string | URL to the media file | "https://data-in.cometchat.io/.../c35f9734fc20947b7456bbea68126f99.png" |
resource | string | SDK resource identifier | "REACT_NATIVE-4_0_14-..." |
attachments | array | Media attachments | See below ↓ |
entities | object | Sender and receiver entities | See below ↓ |
moderation | object | Moderation status | {"status": "pending"} |
data.attachments Array (per item):| Parameter | Type | Description | Sample Value |
|---|---|---|---|
url | string | URL to the attachment | "https://pngimg.com/uploads/mario/mario_PNG125.png" |
name | string | File name | "mario" |
mimeType | string | MIME type | "image/png" |
extension | string | File extension | "png" |
data.entities Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
sender | object | Sender entity wrapper | See below ↓ |
receiver | object | Receiver entity wrapper | See below ↓ |
data.entities.sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Sender user details | See below ↓ |
data.entities.sender.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771323060 |
tags | array | User tags | [] |
data.entities.receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Receiver user details | See below ↓ |
data.entities.receiver.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "offline" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771322968 |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
tags | array | User tags | [] |
Multiple Attachments in a Media Message
Starting version 3.0.9 & above the SDK supports sending multiple attachments in a single media message. As in the case of a single attachment in a media message, there are two ways you can send Media Messages using the CometChat SDK:1. By Providing an Array of Files
You can now share an array of files while creating an object of the MediaMessage class. When the media message is sent using thesendMediaMessage() method, the files are uploaded to the CometChat servers & the URL of the files is sent in the success response of the sendMediaMessage() method.
Getting the file Object: You can use different React Native packages for sending media messages. We demonstrate how to send images using CometChat.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
MediaMessage class constructor takes the following parameters:
| Parameter | Description |
|---|---|
| receiverId | The UID or GUID of the recipient. |
| files | An array of files. |
| messageType | The type of the message that needs to be sent which in this case can be: 1. CometChat.MESSAGE_TYPE.IMAGE 2. CometChat.MESSAGE_TYPE.VIDEO 3. CometChat.MESSAGE_TYPE.AUDIO 4. CometChat.MESSAGE_TYPE.FILE |
| receiverType | The type of the receiver to whom the message is to be sent. 1. CometChat.RECEIVER_TYPE.USER2. CometChat.RECEIVER_TYPE.GROUP |
2. By Providing the URL of Multiple Files
The second way to send multiple attachments in a single media message using the CometChat SDK is to provide the SDK with the URL of multiple files that are hosted on your servers or any cloud storage. To achieve this you will have to make use of the Attachment class. For more information, you can refer to the below code snippet:- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
MediaMessage object which includes all information related to the sent message.
You can use the setMetadata(), setCaption() & setTags() methods to add metadata, caption and tags respectively in exactly the same way as it is done while sending a single file or attachment in a Media Message.
Response
Response
On Success — Returns a MediaMessage object with multiple attachments:MediaMessage Object:
Example with multiple attachments:
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
id | string | Unique message identifier | "25189" |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
type | string | Media type | "image" |
category | string | Message category | "message" |
receiverId | string | UID of the receiver | "cometchat-uid-3" |
receiverType | string | Type of receiver | "user" |
sentAt | number | Unix timestamp when sent | 1771323061 |
updatedAt | number | Unix timestamp of last update | 1771323061 |
sender | object | Sender user details | See below ↓ |
receiver | object | Receiver user details | See below ↓ |
data | object | Additional message data | See below ↓ |
reactions | array | Message reactions | [] |
mentionedUsers | array | Users mentioned in message | [] |
mentionedMe | boolean | Whether logged-in user is mentioned | false |
sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the sender | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771323060 |
hasBlockedMe | boolean | Whether sender has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked sender | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the receiver | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "offline" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771322968 |
hasBlockedMe | boolean | Whether receiver has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked receiver | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
data Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
type | string | Media type | "image" |
category | string | Message category | "message" |
url | string | URL to the primary media file | "https://data-in.cometchat.io/.../c35f9734fc20947b7456bbea68126f99.png" |
resource | string | SDK resource identifier | "REACT_NATIVE-4_0_14-..." |
attachments | array | Media attachments | See below ↓ |
entities | object | Sender and receiver entities | See below ↓ |
moderation | object | Moderation status | {"status": "pending"} |
data.attachments Array (per item):| Parameter | Type | Description | Sample Value |
|---|---|---|---|
url | string | URL to the attachment | "https://pngimg.com/uploads/mario/mario_PNG125.png" |
name | string | File name | "mario" |
mimeType | string | MIME type | "image/png" |
extension | string | File extension | "png" |
- Attachment 1:
{"name": "mario", "extension": "png", "mimeType": "image/png", "url": "https://pngimg.com/uploads/mario/mario_PNG125.png"} - Attachment 2:
{"name": "jaguar", "extension": "png", "mimeType": "image/png", "url": "https://pngimg.com/uploads/jaguar/jaguar_PNG20759.png"}
data.entities Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
sender | object | Sender entity wrapper | See below ↓ |
receiver | object | Receiver entity wrapper | See below ↓ |
data.entities.sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Sender user details | See below ↓ |
data.entities.sender.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771323060 |
tags | array | User tags | [] |
data.entities.receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Receiver user details | See below ↓ |
data.entities.receiver.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "offline" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771322968 |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
tags | array | User tags | [] |
Custom Message
In other words, as a sender, how do I send a custom message like location coordinates? CometChat allows you to send custom messages which are neither text nor media messages. In order to send a custom message, you need to use thesendCustomMessage() method. The sendCustomMessage() method takes an object of the CustomMessage which can be obtained using the below constructor.
- JavaScript
- TypeScript
custom.
The parameters involved are:
| Parameter | Description |
|---|---|
receiverId | The unique ID of the user or group to which the message is to be sent |
receiverType | Type of the receiver i.e user or group |
customType | Custom message type that you need to set |
customData | The data to be passed as the message in the form of a JSON Object |
CustomMessage class to set a specific type for the custom message. This can be achieved using the setSubtype() method.
Add Tags
To add a tag to a message you can use thesetTags() method of the CustomMessage Class. The setTags() method accepts a list of tags.
- JavaScript
- TypeScript
CustomMessage class is ready you can send the custom message using the sendCustomMessage() method.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
CustomMessage class.
Response
Response
On Success — Returns a CustomMessage object:CustomMessage Object:
| Parameter | Type | Description | Sample Value |
|---|---|---|---|
id | string | Unique message identifier | "25190" |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
type | string | Custom message type | "location" |
category | string | Message category | "custom" |
receiverId | string | UID of the receiver | "cometchat-uid-3" |
receiverType | string | Type of receiver | "user" |
sentAt | number | Unix timestamp when sent | 1771323234 |
updatedAt | number | Unix timestamp of last update | 1771323234 |
customData | object | Custom payload data | See below ↓ |
sender | object | Sender user details | See below ↓ |
receiver | object | Receiver user details | See below ↓ |
data | object | Additional message data | See below ↓ |
reactions | array | Message reactions | [] |
mentionedUsers | array | Users mentioned in message | [] |
mentionedMe | boolean | Whether logged-in user is mentioned | false |
metadata | object | Custom metadata | {"@injected": {"extensions": {"link-preview": {"links": []}}}} |
customData Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
latitude | string | Latitude coordinate | "50.6192171633316" |
longitude | string | Longitude coordinate | "-72.68182268750002" |
sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the sender | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771323089 |
hasBlockedMe | boolean | Whether sender has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked sender | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier of the receiver | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "offline" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771322968 |
hasBlockedMe | boolean | Whether receiver has blocked logged-in user | false |
blockedByMe | boolean | Whether logged-in user has blocked receiver | false |
deactivatedAt | number | Deactivation timestamp (0 if active) | 0 |
tags | array | User tags | [] |
data Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
text | string | Fallback text | "Sent a custom message" |
resource | string | SDK resource identifier | "REACT_NATIVE-4_0_14-..." |
customData | object | Custom payload data | {"latitude": "50.6192171633316", "longitude": "-72.68182268750002"} |
entities | object | Sender and receiver entities | See below ↓ |
metadata | object | Injected metadata from extensions | {"@injected": {"extensions": {"link-preview": {"links": []}}}} |
moderation | object | Moderation status | {"status": "pending"} |
data.entities Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
sender | object | Sender entity wrapper | See below ↓ |
receiver | object | Receiver entity wrapper | See below ↓ |
data.entities.sender Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Sender user details | See below ↓ |
data.entities.sender.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-2" |
name | string | Display name | "George Alan" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp" |
status | string | Online status | "online" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771323089 |
tags | array | User tags | [] |
data.entities.receiver Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
entityType | string | Type of entity | "user" |
entity | object | Receiver user details | See below ↓ |
data.entities.receiver.entity Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
uid | string | Unique identifier | "cometchat-uid-3" |
name | string | Display name | "Nancy Grace" |
avatar | string | URL to avatar image | "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp" |
status | string | Online status | "offline" |
role | string | User role | "default" |
lastActiveAt | number | Unix timestamp of last activity | 1771322968 |
conversationId | string | Conversation identifier | "cometchat-uid-2_user_cometchat-uid-3" |
tags | array | User tags | [] |
Update Conversation
How can I decide whether the custom message should update the last message of a conversation? By default, a custom message will update the last message of a conversation. If you wish to not update the last message of the conversation when a custom message is sent, please useshouldUpdateConversation(value: boolean) method of the Custom Message.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
Custom Notification Body
How can I customize the notification body of a custom message? To add a custom notification body forPush, Email & SMS notification of a custom message you can use the setConversationText(text: string) method of the CustomMessage class.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
It is also possible to send interactive messages from CometChat. To learn more, see Interactive Messages.
Best Practices
Best Practices
- Use appropriate message types (
TEXT,IMAGE,VIDEO,AUDIO,FILE) for media messages - Add metadata to messages when you need to pass additional context (e.g., location, user preferences)
- Use tags to categorize messages for easier filtering and retrieval
- Set
shouldUpdateConversation(false)for background or system-level custom messages that shouldn’t appear in conversation lists - Use
setConversationText()to provide meaningful notification text for custom messages
Troubleshooting
Troubleshooting
- Message not sending: Ensure the user is logged in and CometChat is initialized
- Media upload fails: Check file size limits and ensure the file object has correct
name,type, anduriproperties - Custom message not appearing: Verify the receiver UID/GUID is correct and the receiver type matches
- Notifications not showing custom text: Make sure
setConversationText()is called before sending the message