Skip to main content
Quick Reference
// Text message to user
const msg = new CometChat.TextMessage("USER_UID", "Hello!", CometChat.RECEIVER_TYPE.USER);
await CometChat.sendMessage(msg);

// Text message to group
const msg = new CometChat.TextMessage("GROUP_GUID", "Hello!", CometChat.RECEIVER_TYPE.GROUP);
await CometChat.sendMessage(msg);

// Media message
const msg = new CometChat.MediaMessage("UID", file, CometChat.MESSAGE_TYPE.IMAGE, CometChat.RECEIVER_TYPE.USER);
await CometChat.sendMediaMessage(msg);

// Custom message (e.g., location)
const msg = new CometChat.CustomMessage("UID", CometChat.RECEIVER_TYPE.USER, "location", {lat: 0, lng: 0});
await CometChat.sendCustomMessage(msg);
CometChat supports multiple message types to cover all your chat needs. For a deeper understanding of how messages are structured, see Message Structure & Hierarchy.
Available via: SDK | REST API | UI Kits
TypeMethodUse Case
TextsendMessage()Standard chat messages
MediasendMediaMessage()Images, videos, audio, files
CustomsendCustomMessage()Location, polls, custom data
InteractivesendInteractiveMessage()Forms, cards, buttons

Text Message

Send plain text messages to users or groups.
const receiverID = "UID";
const messageText = "Hello!";
const receiverType = CometChat.RECEIVER_TYPE.USER;

const textMessage = new CometChat.TextMessage(
  receiverID,
  messageText,
  receiverType
);

CometChat.sendMessage(textMessage).then(
  (message) => console.log("Message sent:", message),
  (error) => console.log("Error:", error)
);

TextMessage Parameters

ParameterTypeDescription
receiverIDstringUID of user or GUID of group
messageTextstringThe text content
receiverTypestringCometChat.RECEIVER_TYPE.USER or GROUP

Optional: Add Metadata

Attach custom data (like location) to any message:
const textMessage = new CometChat.TextMessage(receiverID, "Check this out!", receiverType);

textMessage.setMetadata({
  latitude: "50.6192171633316",
  longitude: "-72.68182268750002"
});

CometChat.sendMessage(textMessage);

Optional: Add Tags

Tag messages for filtering or categorization:
textMessage.setTags(["important", "starred"]);

Optional: Quote a Message

Reply to a specific message:
textMessage.setQuotedMessageId(previousMessageId);

Media Message

Send images, videos, audio files, and documents.

Send File from Input

Upload files directly from a file input:
<input type="file" id="fileInput" />
const file = document.getElementById("fileInput").files[0];
const receiverID = "UID";
const messageType = CometChat.MESSAGE_TYPE.IMAGE; // or VIDEO, AUDIO, FILE
const receiverType = CometChat.RECEIVER_TYPE.USER;

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

CometChat.sendMediaMessage(mediaMessage).then(
  (message) => console.log("Media sent:", message),
  (error) => console.log("Error:", error)
);

Message Types

TypeConstantFile Types
ImageCometChat.MESSAGE_TYPE.IMAGEjpg, png, gif, etc.
VideoCometChat.MESSAGE_TYPE.VIDEOmp4, mov, etc.
AudioCometChat.MESSAGE_TYPE.AUDIOmp3, wav, etc.
FileCometChat.MESSAGE_TYPE.FILEpdf, doc, etc.

Send File from URL

Send media hosted on your server or cloud storage:
const receiverID = "UID";
const messageType = CometChat.MESSAGE_TYPE.IMAGE;
const receiverType = CometChat.RECEIVER_TYPE.USER;

const mediaMessage = new CometChat.MediaMessage(
  receiverID,
  "",
  messageType,
  receiverType
);

const attachment = new CometChat.Attachment({
  name: "photo",
  extension: "png",
  mimeType: "image/png",
  url: "https://example.com/image.png"
});

mediaMessage.setAttachment(attachment);

CometChat.sendMediaMessage(mediaMessage).then(
  (message) => console.log("Media sent:", message),
  (error) => console.log("Error:", error)
);

Send Multiple Attachments

Send multiple files in a single message:
// HTML: <input type="file" id="fileInput" multiple />
const files = document.getElementById("fileInput").files;

const mediaMessage = new CometChat.MediaMessage(
  receiverID,
  files,  // Pass FileList directly
  CometChat.MESSAGE_TYPE.FILE,
  CometChat.RECEIVER_TYPE.USER
);

CometChat.sendMediaMessage(mediaMessage);

Optional: Add Caption

Add text description to media:
mediaMessage.setCaption("Check out this photo!");

Optional: Add Metadata & Tags

mediaMessage.setMetadata({ location: "Paris" });
mediaMessage.setTags(["vacation", "photos"]);

Custom Message

Send structured data like location coordinates, polls, or any custom payload.
const receiverID = "UID";
const receiverType = CometChat.RECEIVER_TYPE.USER;
const customType = "location";
const customData = {
  latitude: "50.6192171633316",
  longitude: "-72.68182268750002",
  address: "123 Main St"
};

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

CometChat.sendCustomMessage(customMessage).then(
  (message) => console.log("Custom message sent:", message),
  (error) => console.log("Error:", error)
);

CustomMessage Parameters

ParameterTypeDescription
receiverIDstringUID of user or GUID of group
receiverTypestringUSER or GROUP
customTypestringYour custom type (e.g., “location”, “poll”)
customDataobjectJSON payload with your data

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/SMS notifications:
customMessage.setConversationText("📍 Shared a location");

Common Options

These options work with all message types:

Quote a Message (Reply)

message.setQuotedMessageId(originalMessageId);

Add Metadata

message.setMetadata({
  customKey: "customValue",
  priority: "high"
});

Add Tags

message.setTags(["important", "follow-up"]);

Message Response

On success, you receive a message object with:
PropertyDescription
idUnique message ID
senderUser object of sender
receiverUser/Group object of receiver
sentAtTimestamp when sent
typeMessage type (text, image, etc.)
categoryMessage category
dataMessage content/payload
CometChat.sendMessage(textMessage).then((message) => {
  console.log("Message ID:", message.getId());
  console.log("Sent at:", message.getSentAt());
  console.log("Sender:", message.getSender().getName());
});

Next Steps