Skip to main content
Version: v4

Send a Message

Using CometChat, you can send three types of messages:

  1. Text Message is the most common and standard message type.
  2. Media Message for sending photos, videos and files.
  3. Custom Message, for sending completely custom data using JSON structures.
  4. Interactive Messages, for sending end-user interactive messages of type form, card and custom Interactive

You can also send metadata along with a text, media or custom message. Think, for example, if you'd want to share the user's location with every message, you can use the metadata field

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 the sendMessage() method and pass a TextMessage object to it.

Add Metadata

To send custom data along with a text message, you can use the setMetadata method and pass a JSON Object to it.

let metadata = {
latitude: "50.6192171633316",
longitude: "-72.68182268750002",
};

textMessage.setMetadata(metadata);

Add Tags

To add a tag to a message you can use the setTags() method of the TextMessage Class. The setTags() method accepts a list of tags.

let tags = ["starredMessage"];

textMessage.setTags(tags);

Once the text message object is ready, you need to use the sendMessage() method to send the text message to the recipient.

let receiverID = "UID";
let messageText = "Hello world!";
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);
}
);

The TextMessage class constructor takes the following parameters:

ParameterDescription
receiverIDUID of the user or GUID of the group receiving the messageRequired
messageTextThe text messageRequired
receiverTypeThe type of the receiver - CometChat.RECEIVER_TYPE.USER or CometChat.RECEIVER_TYPE.GROUPRequired

When a text message is sent successfully, the response will include a 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 the sendMediaMessage() method and pass a MediaMessage object to it.

Add Metadata

To send custom data along with a media message, you can use the setMetadata method and pass a JSON Object to it.

let metadata = {
latitude: "50.6192171633316",
longitude: "-72.68182268750002",
};

mediaMessage.setMetadata(metadata);

Add Caption(Text along with Media Message)

To send a caption with a media message, you can use the setCaption method and pass text to it.

let caption = "Random Caption";

mediaMessage.setCaption(caption);

Add Tags

To add a tag to a message you can use the setTags() method of the MediaMessage Class. The setTags() method accepts a list of tags.

let tags = ["starredMessage"];

mediaMessage.setTags(tags);

There are 2 ways you can send Media Messages using the CometChat SDK:

  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 the sendMediaMessage() 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 file Object:

<html>
<body>
<input type="file" name="img_file" id="img_file" />
<script>
var UID = "UID";
var file = document.getElementById("img_file").files[0];
var fileType = MESSAGE_TYPE.IMAGE;
var userType = CometChat.RECEIVER_TYPE.USER;
var mediaMessage = new MediaMessage(UID, file, fileType, userType);
</script>
</body>
</html>
let receiverID = "UID";
let messageType = CometChat.MESSAGE_TYPE.FILE;
let receiverType = CometChat.RECEIVER_TYPE.USER;
let mediaMessage = new CometChat.MediaMessage(
receiverID,
"INPUT FILE OBJECT",
messageType,
receiverType
);

CometChat.sendMediaMessage(mediaMessage).then(
(message) => {
console.log("Media message sent successfully", message);
},
(error) => {
console.log("Media message sending failed with error", error);
}
);

The MediaMessage class constructor takes the following parameters:

ParameterDescription
receiverIdThe UID or GUID of the recipient.Required
fileThe file object to be sentRequired
messageTypeThe 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
Required
receiverTypeThe type of the receiver to whom the message is to be sent.
1. CometChat.RECEIVER_TYPE.USER``2. CometChat.RECEIVER_TYPE.GROUP
Required
  1. 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:
let receiverID = "superhero2";
let messageType = CometChat.MESSAGE_TYPE.IMAGE;
let receiverType = CometChat.RECEIVER_TYPE.USER;
let mediaMessage = new CometChat.MediaMessage(
receiverID,
"",
messageType,
receiverType
);

let file = {
name: "mario",
extension: "png",
mimeType: "image/png",
url: "https://pngimg.com/uploads/mario/mario_PNG125.png",
};

let attachment = new CometChat.Attachment(file);

mediaMessage.setAttachment(attachment);

CometChat.sendMediaMessage(mediaMessage).then(
(mediaMessage) => {
console.log("message", mediaMessage);
},
(error) => {
console.log("error in sending message", error);
}
);

When a media message is sent successfully, the response will include a MediaMessage object which includes all information related to the sent message.

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 the sendMediaMessage() 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 files:

<html>
<body>
<input type="file" name="img_file" id="img_file" />
<script>
var UID = "UID";
var files = document.getElementById("img_file").files;
var fileType = MESSAGE_TYPE.IMAGE;
var userType = CometChat.RECEIVER_TYPE.USER;
var mediaMessage = new MediaMessage(UID, files, fileType, userType);
</script>
</body>
</html>
let receiverID = "UID";
let messageType = CometChat.MESSAGE_TYPE.FILE;
let receiverType = CometChat.RECEIVER_TYPE.USER;
let mediaMessage = new CometChat.MediaMessage(
receiverID,
files,
messageType,
receiverType
);

CometChat.sendMediaMessage(mediaMessage).then(
(message) => {
console.log("Media message sent successfully", message);
},
(error) => {
console.log("Media message sending failed with error", error);
}
);

The MediaMessage class constructor takes the following parameters:

ParameterDescription
receiverIdThe UID or GUID of the recipient.
filesAn array of files.
messageTypeThe 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
receiverTypeThe type of the receiver to whom the message is to be sent.
1. CometChat.RECEIVER_TYPE.USER``2. CometChat.RECEIVER_TYPE.GROUP
  1. By providing the URL of the 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:
let receiverID = "superhero2";
let messageType = CometChat.MESSAGE_TYPE.IMAGE;
let receiverType = CometChat.RECEIVER_TYPE.USER;
let mediaMessage = new CometChat.MediaMessage(
receiverID,
"",
messageType,
receiverType
);

let attachment1 = {
name: "mario",
extension: "png",
mimeType: "image/png",
url: "https://pngimg.com/uploads/mario/mario_PNG125.png",
};

let attachment2 = {
name: "jaguar",
extension: "png",
mimeType: "image/png",
url: "https://pngimg.com/uploads/jaguar/jaguar_PNG20759.png",
};

let attachments = [];
attachments.push(new CometChat.Attachment(attachment1));
attachments.push(new CometChat.Attachment(attachment2));

mediaMessage.setAttachments(attachments);

CometChat.sendMediaMessage(mediaMessage).then(
(mediaMessage) => {
console.log("message", mediaMessage);
},
(error) => {
console.log("error in sending message", error);
}
);

When a media message is sent successfully, the response will include a 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.

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 the sendCustomMessage() method.

The sendCustomMessage() method takes an object of the CustomMessage which can be obtained using the below constructor.

let customMessage = new CometChat.CustomMessage(
receiverID,
receiverType,
customType,
customData
);

The above constructor, helps you create a custom message with the message type set to whatever is passed to the constructor and the category set to custom.

The parameters involved are:

  1. receiverId - The unique ID of the user or group to which the message is to be sent.
  2. receiverType - Type of the receiver i.e user or group
  3. customType - custom message type that you need to set
  4. customData - The data to be passed as the message in the form of a JSON Object.

You can also use the subType field of the 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 the setTags() method of the CustomMessage Class. The setTags() method accepts a list of tags.

let tags = ["starredMessage"];

customMessage.setTags(tags);

Once the object of CustomMessage class is ready you can send the custom message using the sendCustomMessage() method.

let receiverID = "UID";
let customData = {
latitude: "50.6192171633316",
longitude: "-72.68182268750002",
};
let customType = "location";
let receiverType = CometChat.RECEIVER_TYPE.USER;
let customMessage = new CometChat.CustomMessage(
receiverID,
receiverType,
customType,
customData
);

CometChat.sendCustomMessage(customMessage).then(
(message) => {
console.log("custom message sent successfully", message);
},
(error) => {
console.log("custom message sending failed with error", error);
}
);

The above sample explains how custom messages can be used to share the location with a user. The same can be achieved for groups.

On success, you will receive an object of the CustomMessage class.

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 use shouldUpdateConversation(value: boolean) method of the Custom Message.

let receiverID = "UID";
let customData = {
latitude: "50.6192171633316",
longitude: "-72.68182268750002",
};
let customType = "location";
let receiverType = CometChat.RECEIVER_TYPE.USER;
let customMessage = new CometChat.CustomMessage(
receiverID,
receiverType,
customType,
customData
);

customMessage.shouldUpdateConversation(false);
CometChat.sendCustomMessage(customMessage).then(
(message) => {
console.log("custom message sent successfully", message);
},
(error) => {
console.log("custom message sending failed with error", error);
}
);

::: info It is also possible to send interactive messages from CometChat, to know more click here :::