Skip to main content
FieldValue
Key ClassesTextMessage, MediaMessage, CustomMessage
Key MethodssendMessage(), sendMediaMessage(), sendCustomMessage()
Receiver TypesCometChat.RECEIVER_TYPE.USER, CometChat.RECEIVER_TYPE.GROUP
Message TypesTEXT, IMAGE, VIDEO, AUDIO, FILE, CUSTOM
PrerequisitesSDK initialized, user logged in
CometChat supports three types of messages:
TypeMethodUse Case
TextsendMessage()Plain text messages
MediasendMediaMessage()Images, videos, audio, files
CustomsendCustomMessage()Location, polls, or any JSON data
You can also send Interactive Messages for forms, cards, and custom UI elements.

Text Message

Send a text message using CometChat.sendMessage() with a TextMessage object.
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:
ParameterDescriptionRequired
receiverIDUID of the user or GUID of the group receiving the messageYes
messageTextThe text message contentYes
receiverTypeCometChat.RECEIVER_TYPE.USER or CometChat.RECEIVER_TYPE.GROUPYes
On success, sendMessage() returns a TextMessage object.

Add Metadata

Attach custom JSON data to the message:
let metadata = {
  latitude: "50.6192171633316",
  longitude: "-72.68182268750002",
};

textMessage.setMetadata(metadata);

Add Tags

Tag messages for easy filtering later:
let tags = ["starredMessage"];

textMessage.setTags(tags);

Quote a Message

Reply to a specific message by setting its ID:
textMessage.setQuotedMessageId(10);

Media Message

Send images, videos, audio, or files using CometChat.sendMediaMessage(). There are two ways to send media messages:
  1. Upload a file — Pass a file object and CometChat uploads it automatically
  2. Send a URL — Provide a URL to media hosted on your server or cloud storage

Upload a File

Get the file from an input element and pass it to MediaMessage:
<input type="file" name="img_file" id="img_file" />
let receiverID = "UID";
let messageType = CometChat.MESSAGE_TYPE.IMAGE;
let receiverType = CometChat.RECEIVER_TYPE.USER;
let file = document.getElementById("img_file").files[0];

let mediaMessage = new CometChat.MediaMessage(
  receiverID,
  file,
  messageType,
  receiverType
);

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

Send a URL

Send media hosted on your server or cloud storage using the Attachment class:
let receiverID = "UID";
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(
  (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:
ParameterDescriptionRequired
receiverIDUID of the user or GUID of the groupYes
fileFile object to upload, or empty string if using URLYes
messageTypeCometChat.MESSAGE_TYPE.IMAGE, VIDEO, AUDIO, or FILEYes
receiverTypeCometChat.RECEIVER_TYPE.USER or GROUPYes
On success, sendMediaMessage() returns a MediaMessage object.

Add Caption

Add text along with the media:
mediaMessage.setCaption("Check out this photo!");

Add Metadata and Tags

Same as text messages:
mediaMessage.setMetadata({ location: "Paris" });
mediaMessage.setTags(["vacation"]);
mediaMessage.setQuotedMessageId(10);

Multiple Attachments

Send multiple files in a single media message.

Upload Multiple Files

let receiverID = "UID";
let messageType = CometChat.MESSAGE_TYPE.FILE;
let receiverType = CometChat.RECEIVER_TYPE.USER;
let files = document.getElementById("img_file").files; // FileList

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);
  }
);

Send Multiple URLs

let receiverID = "UID";
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(
  (message) => {
    console.log("Media message sent successfully", message);
  },
  (error) => {
    console.log("Media message sending failed with error", error);
  }
);

Custom Message

Send structured data that doesn’t fit text or media categories — like location coordinates, polls, or game moves.
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 CustomMessage class constructor takes the following parameters:
ParameterDescriptionRequired
receiverIDUID of the user or GUID of the groupYes
receiverTypeCometChat.RECEIVER_TYPE.USER or GROUPYes
customTypeYour custom type string (e.g., "location", "poll")Yes
customDataJSON object with your custom dataYes
On success, sendCustomMessage() returns a CustomMessage object.

Add Tags

customMessage.setTags(["starredMessage"]);

Quote a Message

customMessage.setQuotedMessageId(10);

Control Conversation Update

By default, custom messages update the conversation’s last message. To prevent this:
customMessage.shouldUpdateConversation(false);

Custom Notification Text

Set custom text for push, email, and SMS notifications:
customMessage.setConversationText("Shared a location");
You can also send interactive messages with forms, cards, and buttons. See Interactive Messages for details.

Next Steps

Receive Messages

Listen for incoming messages in real-time

Edit Message

Edit previously sent messages

Delete Message

Delete sent messages

Interactive Messages

Send forms, cards, and custom UI elements