Receive a Message
Receiving messages with CometChat has two parts:
- Adding a listener to receive real-time messages when your app is running
- Calling a method to retrieve missed messages when your app was not running
Real-Time Messages
In other words, as a recipient, how do I receive messages when my app is running?
To receive real-time incoming messages, you need to register the MessageListener wherever you wish to receive the incoming messages. You can use the addMessageListener() method to do so.
- Message Listener
- Typescript
let listenerID = "UNIQUE_LISTENER_ID";
CometChat.addMessageListener(
listenerID,
new CometChat.MessageListener({
onTextMessageReceived: textMessage => {
console.log("Text message received successfully", textMessage);
},
onMediaMessageReceived: mediaMessage => {
console.log("Media message received successfully", mediaMessage);
},
onCustomMessageReceived: customMessage => {
console.log("Custom message received successfully", customMessage);
}
})
);
let listenerID: string = "UNIQUE_LISTENER_ID";
CometChat.addMessageListener(
listenerID,
new CometChat.MessageListener({
onTextMessageReceived: (textMessage: CometChat.TextMessage) => {
console.log("Text message received successfully", textMessage);
},
onMediaMessageReceived: (mediaMessage: CometChat.MediaMessage) => {
console.log("Media message received successfully", mediaMessage);
},
onCustomMessageReceived: (customMessage: CometChat.CustomMessage) => {
console.log("Custom message received successfully", customMessage);
}
})
);
Parameter | Description | |
---|---|---|
listenerId | An ID that uniquely identifies that listener. |
We recommend you remove the listener once you don't want to receive a message for particular listener. You can remove the listener in componentWillUnmount() method.
- Remove Message Listener
- Typescript
var listenerID = "UNIQUE_LISTENER_ID";
CometChat.removeMessageListener(listenerID);
let listenerID: string = "UNIQUE_LISTENER_ID";
CometChat.removeMessageListener(listenerID);
As a sender, you will not receive your own message in a real-time message event. However, if a user is logged-in using multiple devices, they will receive an event for their own message in other devices.
Missed Messages
In other words, as a recipient, how do I receive messages that I missed when my app was not running?
For most use cases, you will need to add functionality to load missed messages. If you're building an on-demand or live streaming app, you may choose to skip this and fetch message history instead.
Using the same MessagesRequest
class and the filters provided by the MessagesRequestBuilder
class, you can fetch the message that we sent to the logged-in user but were not delivered to him as he was offline. For this, you will require the Id of the last message received. You can either maintain it at your end or use the getLastDeliveredMessageId()
method provided by the CometChat class. This Id needs to be passed to the setMessageId()
method of the builder class.
Now using the fetchNext()
method, you can fetch all the messages that were sent to the user when he/she was offline.
Calling the fetchNext()
method on the same object repeatedly allows you to fetch all the offline messages for the logged in user.
For a Particular One-on-one Conversation
- One-on-One Conversation
- Typescript
let UID = "UID";
let limit = 30;
let latestId = await CometChat.getLastDeliveredMessageId();
var messagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setMessageId(latestId)
.setLimit(limit)
.build();
messagesRequest.fetchNext().then(
messages => {
console.log("Message list fetched:", messages);
}, error => {
console.log("Message fetching failed with error:", error);
}
);
let UID: string = "UID",
limit: number = 30,
latestId: number = await CometChat.getLastDeliveredMessageId(),
messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setMessageId(latestId)
.setLimit(limit)
.build();
messagesRequest.fetchNext().then(
(messages: CometChat.BaseMessage[]) => {
console.log("Message list fetched:", messages);
}, (error: CometChat.CometChatException) => {
console.log("Message fetching failed with error:", error);
}
);
For a Particular Group
- Group Conversation
- Typescript
let GUID = "GUID";
let limit = 30;
let latestId = await CometChat.getLastDeliveredMessageId();
var messagesRequest = new CometChat.MessagesRequestBuilder()
.setGUID(GUID)
.setMessageId(latestId)
.setLimit(limit)
.build();
messagesRequest.fetchNext().then(
messages => {
console.log("Message list fetched:", messages);
}, error => {
console.log("Message fetching failed with error:", error);
}
);
let GUID: string = "GUID",
limit: number = 30,
latestId: number = await CometChat.getLastDeliveredMessageId(),
messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder()
.setGUID(GUID)
.setMessageId(latestId)
.setLimit(limit)
.build();
messagesRequest.fetchNext().then(
(messages: CometChat.BaseMessage[]) => {
console.log("Message list fetched:", messages);
}, (error: CometChat.CometChatException) => {
console.log("Message fetching failed with error:", error);
}
);
Unread Messages
In other words, as a logged-in user, how do I fetch the messages I've not read?
Using the MessagesRequest
class described above, you can fetch the unread messages for the logged in user. In order to achieve this, you need to set the unread
variable in the builder to true using the setUnread()
method so that only the unread messages are fetched.
For a Particular One-on-one Conversation
- One-on-One Conversation
- Typescript
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setUnread(true)
.setLimit(limit)
.build();
messagesRequest.fetchPrevious().then(
messages => {
console.log("Message list fetched:", messages);
}, error => {
console.log("Message fetching failed with error:", error);
}
);
let UID: string = "UID",
limit: number = 30,
messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setUnread(true)
.setLimit(limit)
.build();
messagesRequest.fetchPrevious().then(
(messages: CometChat.BaseMessage[]) => {
console.log("Message list fetched:", messages);
}, (error: CometChat.CometChatException) => {
console.log("Message fetching failed with error:", error);
}
);
For a Particular Group
- Group Conversation
- Typescript
let GUID = "GUID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
.setGUID(GUID)
.setUnread(true)
.setLimit(limit)
.build();
messagesRequest.fetchPrevious().then(
messages => {
console.log("Message list fetched:", messages);
}, error => {
console.log("Message fetching failed with error:", error);
}
);
let GUID: string = "GUID",
limit: number = 30,
messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder()
.setGUID(GUID)
.setUnread(true)
.setLimit(limit)
.build();
messagesRequest.fetchPrevious().then(
(messages: CometChat.BaseMessage[]) => {
console.log("Message list fetched:", messages);
}, (error: CometChat.CometChatException) => {
console.log("Message fetching failed with error:", error);
}
);
The list of messages received is in the form of objects of BaseMessage
class. A BaseMessage can either be an object of the TextMessage
, MediaMessage
, CustomMessage
, Action
or Call
class. You can use the instanceOf
operator to check the type of object.
Message History
In other words, as a logged-in user, how do I fetch the complete message history?
For a Particular One-on-one Conversation
Using the MessagesRequest
class and the filters for the MessagesRequestBuilder
class as shown in the below code snippet, you can fetch the entire conversation between the logged in user and any particular user. For this use case, it is mandatory to set the UID parameter using the setUID()
method of the builder. This UID is the unique id of the user for which the conversation needs to be fetched.
- One-on-One Conversation
- Typescript
let UID = "UID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setLimit(limit)
.build();
messagesRequest.fetchPrevious().then(
messages => {
console.log("Message list fetched:", messages);
}, error => {
console.log("Message fetching failed with error:", error);
}
);
let UID: string = "UID",
limit: number = 30,
messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setLimit(limit)
.build();
messagesRequest.fetchPrevious().then(
(messages: CometChat.BaseMessage[]) => {
console.log("Message list fetched:", messages);
}, (error: CometChat.CometChatException) => {
console.log("Message fetching failed with error:", error);
}
);
Calling the fetchPrevious()
method on the same object repeatedly allows you to fetch the entire conversation between the logged in user and the specified user. This can be implemented with upward scrolling to display the entire conversation.
For a Particular Group
Using the MessagesRequest
class and the filters for the MessagesRequestBuilder
class as shown in the below code snippet, you can fetch the entire conversation for any group provided you have joined the group. For this use case, it is mandatory to set the GUID parameter using the setGUID()
method of the builder. This GUID is the unique identifier of the Group for which the messages are to be fetched.
- Group Conversation
- Typescript
let GUID = "GUID";
let limit = 30;
let messagesRequest = new CometChat.MessagesRequestBuilder()
.setGUID(GUID)
.setLimit(limit)
.build();
messagesRequest.fetchPrevious().then(
messages => {
console.log("Message list fetched:", messages);
}, error => {
console.log("Message fetching failed with error:", error);
}
);
let GUID: string = "GUID",
limit: number = 30,
messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder()
.setGUID(GUID)
.setLimit(limit)
.build();
messagesRequest.fetchPrevious().then(
(messages: CometChat.BaseMessage[]) => {
console.log("Message list fetched:", messages);
}, (error: CometChat.CometChatException) => {
console.log("Message fetching failed with error:", error);
}
);
Calling the fetchPrevious()
method on the same object repeatedly allows you to fetch the entire conversation for the group. This can be implemented with upward scrolling to display the entire conversation.
Search Messages
Along with the other parameters mentioned above, you can use the setSearchKeyword()
method provided by the MessagesRequestBuilder
class. This method takes a string as input. This is the string that is to be searched amongst the messages. This can be used to fetch all the messages that contain the string in a paginated way. Calling the fetchPrevious()
method on the same object repeatedly allows you to fetch all the messages with the keyword specified in all your conversations.
For a Particular One-on-one Conversation
- One-on-One Conversation
- Typescript
let UID = "UID";
let limit = 30;
let searchKeyword = "Hello";
let messagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setLimit(limit)
.setSearchKeyword(searchKeyword)
.build();
messagesRequest.fetchPrevious().then(
messages => {
console.log("Message list fetched:", messages);
}, error => {
console.log("Message fetching failed with error:", error);
}
);
let UID: string = "UID",
limit: number = 30,
searchKeyword: string = "Hello",
messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder()
.setUID(UID)
.setLimit(limit)
.setSearchKeyword(searchKeyword)
.build();
messagesRequest.fetchPrevious().then(
(messages: CometChat.BaseMessage[]) => {
console.log("Message list fetched:", messages);
}, (error: CometChat.CometChatException) => {
console.log("Message fetching failed with error:", error);
}
);
For a Particular Group
- Group Conversation
- Typescript
let GUID = "GUID";
let limit = 30;
let searchKeyword = "Hello";
let messagesRequest = new CometChat.MessagesRequestBuilder()
.setGUID(GUID)
.setLimit(limit)
.setSearchKeyword(searchKeyword)
.build();
messagesRequest.fetchPrevious().then(
messages => {
console.log("Message list fetched:", messages);
}, error => {
console.log("Message fetching failed with error:", error);
}
);
let GUID: string = "GUID",
limit: number = 30,
searchKeyword: string = "Hello",
messagesRequest: CometChat.MessagesRequest = new CometChat.MessagesRequestBuilder()
.setGUID(GUID)
.setLimit(limit)
.setSearchKeyword(searchKeyword)
.build();
messagesRequest.fetchPrevious().then(
(messages: CometChat.BaseMessage[]) => {
console.log("Message list fetched:", messages);
}, (error: CometChat.CometChatException) => {
console.log("Message fetching failed with error:", error);
}
);
Unread Message Count
In other words, as a logged-in user, how do I find out the number of unread messages that I have?
For a Particular One-on-one Conversation
In other words, how do I find out the number of unread messages I have from a particular user?
In order to get the unread message count for a particular user, you can use the getUnreadMessageCountForUser()
.
This method has the below two variants:
- User
- Typescript
CometChat.getUnreadMessageCountForUser(UID);
let UID: string = "UID";
CometChat.getUnreadMessageCountForUser(UID);
if you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
- User
- Typescript
CometChat.getUnreadMessageCountForUser(UID, hideMessagesFromBlockedUsers);
let UID: string = "UID",
hideMessagesFromBlockedUsers: boolean = true;
CometChat.getUnreadMessageCountForUser(UID, hideMessagesFromBlockedUsers);
- Get Unread Count For User
- Typescript
let UID = "UID";
CometChat.getUnreadMessageCountForUser(UID).then(
array => {
console.log("Message count fetched", array);
}, error => {
console.log("Error in getting message count", error);
}
);
let UID: string = "UID";
CometChat.getUnreadMessageCountForUser(UID).then(
(unreadMessageCount: Object) => {
console.log("Message count fetched", unreadMessageCount);
}, (error: CometChat.CometChatException) => {
console.log("Error in getting message count", error);
}
);
In the then()
callback, you will receive an array which will contain the UID
of the user as the key and the unread message count as the value.
For a Particular Group Conversation
In other words, how do I find out the number of unread messages I have in a single group?
In order to get the unread message count for a particular group, you can use the getUnreadMessageCountForGroup()
.
This method has the below two variants:
- Group
- Typescript
CometChat.getUnreadMessageCountForGroup(GUID);
let GUID: string = "GUID";
CometChat.getUnreadMessageCountForGroup(GUID);
``
if you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
- Group
- Typescript
CometChat.getUnreadMessageCountForGroup(GUID, hideMessagesFromBlockedUsers);
let GUID: string = "GUID",
hideMessagesFromBlockedUsers: boolean = true;
CometChat.getUnreadMessageCountForGroup(GUID, hideMessagesFromBlockedUsers);
- Get Unread Count For Group
- Typescript
let GUID = "GUID";
CometChat.getUnreadMessageCountForGroup(GUID).then(
array => {
console.log("Message count fetched", array);
}, error => {
console.log("Error in getting message count", error);
}
);
let GUID: string = "GUID";
CometChat.getUnreadMessageCountForGroup(GUID).then(
(unreadMessageCount: Object) => {
console.log("Message count fetched", unreadMessageCount);
}, (error: CometChat.CometChatException) => {
console.log("Error in getting message count", error);
}
);
In the then()
callback, you will receive an array which will contain the UID
of the user as the key and the unread message count as the value.
For All One-on-one & Group Conversations
In other words, how do I find out the number of unread messages for every one-on-one and group conversation?
In order to get all the unread message count combined i.e unread message counts for all the users and groups, you can use the getUnreadMessageCount()
method.
This method has two variants:
- All Conversation
- Typescript
CometChat.getUnreadMessageCount();
CometChat.getUnreadMessageCount();
if you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
- All Conversation
- Typescript
CometChat.getUnreadMessageCount(hideMessagesFromBlockedUsers);
let hideMessagesFromBlockedUsers: boolean = true;
CometChat.getUnreadMessageCount(hideMessagesFromBlockedUsers);
- Get Unread Count For All Conversation
- Typescript
CometChat.getUnreadMessageCount().then(
array => {
console.log("Message count fetched", array);
}, error => {
console.log("Error in getting message count", error);
}
);
CometChat.getUnreadMessageCount().then(
(unreadMessageCount: Object) => {
console.log("Message count fetched", unreadMessageCount);
}, (error: CometChat.CometChatException) => {
console.log("Error in getting message count", error);
}
);
It return array having two keys:
- user - The value for this key holds another array that holds the UIDs of the users and their corresponding unread message counts
- group - The value for this key holds another array that holds the GUIDs of the groups and their corresponding unread message counts
For All One-on-one Conversations
In order to fetch the unread message counts for just the users, you can use the getUnreadMessageCountForAllUsers()
method. This method just like others has two variants:
- For All User Conversation
- Typescript
CometChat.getUnreadMessageCountForAllUsers();
CometChat.getUnreadMessageCountForAllUsers();
if you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
- For All User Conversation
- Typescript
CometChat.getUnreadMessageCountForAllUsers(hideMessagesFromBlockedUsers);
let hideMessagesFromBlockedUsers: boolean = true;
CometChat.getUnreadMessageCountForAllUsers(hideMessagesFromBlockedUsers);
- Get Unread Count For All User Conversation
- Typescript
CometChat.getUnreadMessageCountForAllUsers().then(
array => {
console.log("Message count fetched", array);
}, error => {
console.log("Error in getting message count", error);
}
);
CometChat.getUnreadMessageCountForAllUsers().then(
(unreadMessageCount: Object) => {
console.log("Message count fetched", unreadMessageCount);
}, (error: CometChat.CometChatException) => {
console.log("Error in getting message count", error);
}
);
It returns a Array which will contain the UIDs of the users as the key and the unread message counts as the values.
For All Group Conversations
In order to fetch the unread message counts for just the groups, you can use the getUnreadMessageCountForAllGroups()
method. This method just like others has two variants:
- For All Group Conversation
- Typescript
CometChat.getUnreadMessageCountForAllGroups();
CometChat.getUnreadMessageCountForAllGroups();
if you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true:
- For All Group Conversation
- Typescript
CometChat.getUnreadMessageCountForAllGroups(hideMessagesFromBlockedUsers);
let hideMessagesFromBlockedUsers: boolean = true;
CometChat.getUnreadMessageCountForAllGroups(hideMessagesFromBlockedUsers);
- Get Unread Count For All Group Conversation
- Typescript
CometChat.getUnreadMessageCountForAllGroups().then(
array => {
console.log("Message count fetched", array);
},
error => {
console.log("Error in getting message count", error);
}
);
CometChat.getUnreadMessageCountForAllGroups().then(
(unreadMessageCount: Object) => {
console.log("Message count fetched", unreadMessageCount);
}, (error: CometChat.CometChatException) => {
console.log("Error in getting message count", error);
}
);
It returns a Array which will contain the GUIDs of the groups as the key and the unread message counts as the values.