diff --git a/docs.json b/docs.json index 0eacee627..b4eb397e3 100644 --- a/docs.json +++ b/docs.json @@ -44,7 +44,9 @@ { "group": "Docs MCP", "hidden": true, - "pages": ["mcp-server"] + "pages": [ + "mcp-server" + ] } ] }, @@ -96,7 +98,9 @@ { "dropdown": "Kubernetes", "icon": "/images/icons/kubernetes.svg", - "pages": ["on-premise-deployment/kubernetes/overview"] + "pages": [ + "on-premise-deployment/kubernetes/overview" + ] } ] } @@ -107,7 +111,9 @@ "tabs": [ { "tab": "Chat & Calling", - "pages": ["chat-call"] + "pages": [ + "chat-call" + ] }, { "tab": "Platform", @@ -220,11 +226,15 @@ }, { "group": "User-Roles", - "pages": ["fundamentals/user-roles-and-permissions"] + "pages": [ + "fundamentals/user-roles-and-permissions" + ] }, { "group": "Guides", - "pages": ["fundamentals/user-auth"] + "pages": [ + "fundamentals/user-auth" + ] } ] }, @@ -279,7 +289,10 @@ { "dropdown": "Wix", "icon": "/images/icons/wix.svg", - "pages": ["/widget/wix/overview", "/widget/wix/integration"] + "pages": [ + "/widget/wix/overview", + "/widget/wix/integration" + ] }, { "dropdown": "Webflow", @@ -1017,9 +1030,9 @@ { "group": "Migration Guide", "pages": [ - "ui-kit/react-native/upgrading-from-v4" + "ui-kit/react-native/upgrading-from-v4" ] - }, + }, "ui-kit/react-native/troubleshooting", "ui-kit/react-native/link/sample", "ui-kit/react-native/link/changelog" @@ -1305,7 +1318,10 @@ }, { "group": "Reference", - "pages": ["ui-kit/ios/methods", "ui-kit/ios/events"] + "pages": [ + "ui-kit/ios/methods", + "ui-kit/ios/events" + ] }, { "group": "Advanced", @@ -2552,33 +2568,15 @@ { "group": " ", "pages": [ - { - "group": "Overview", - "pages": [ - "sdk/javascript/overview", - "sdk/javascript/key-concepts", - "sdk/javascript/message-structure-and-hierarchy", - "sdk/javascript/rate-limits" - ] - }, - { - "group": "Setup", - "pages": ["sdk/javascript/setup-sdk"] - }, - { - "group": "Authentication", - "pages": [ - "sdk/javascript/authentication-overview", - "sdk/javascript/login-listener" - ] - }, + "sdk/javascript/overview", + "sdk/javascript/setup-sdk", + "sdk/javascript/authentication-overview", { "group": "Messaging", "pages": [ - "sdk/javascript/messaging-overview", "sdk/javascript/send-message", "sdk/javascript/receive-message", - "sdk/javascript/additional-message-filtering", + "sdk/javascript/message-filtering", "sdk/javascript/retrieve-conversations", "sdk/javascript/threaded-messages", "sdk/javascript/edit-message", @@ -2619,10 +2617,7 @@ "sdk/javascript/block-users" ] }, - { - "group": "User Presence", - "pages": ["sdk/javascript/user-presence"] - }, + "sdk/javascript/user-presence", { "group": "Groups", "pages": [ @@ -2645,27 +2640,27 @@ { "group": "Resources", "pages": [ - "sdk/javascript/resources-overview", + "sdk/javascript/key-concepts", + "sdk/javascript/message-structure-and-hierarchy", "sdk/javascript/all-real-time-listeners", - "sdk/javascript/upgrading-from-v3" - ] - }, - { - "group": "Advanced", - "pages": [ - "sdk/javascript/advanced-overview", + "sdk/javascript/rate-limits", "sdk/javascript/connection-status", "sdk/javascript/managing-web-sockets-connections-manually" ] }, { - "group": "UI Kits", + "group": "Reference", "pages": [ - "sdk/javascript/react-overview", - "sdk/javascript/vue-overview", - "sdk/javascript/angular-overview" + "sdk/reference/messages", + "sdk/reference/entities", + "sdk/reference/auxiliary", + "sdk/reference/calls" ] - }, + }, + "sdk/javascript/best-practices", + "sdk/javascript/error-codes", + "sdk/javascript/troubleshooting", + "sdk/javascript/upgrading-from-v3", "sdk/javascript/extensions-overview", "sdk/javascript/ai-user-copilot-overview", "sdk/javascript/ai-chatbots-overview", @@ -3594,23 +3589,9 @@ { "group": " ", "pages": [ - { - "group": "Overview", - "pages": [ - "sdk/android/overview", - "sdk/android/key-concepts", - "sdk/android/message-structure-and-hierarchy", - "sdk/android/rate-limits" - ] - }, + "sdk/android/overview", "sdk/android/setup", - { - "group": "Authentication", - "pages": [ - "sdk/android/authentication-overview", - "sdk/android/login-listeners" - ] - }, + "sdk/android/authentication-overview", { "group": "Messaging", "pages": [ @@ -3679,25 +3660,33 @@ { "group": "Resources", "pages": [ - "sdk/android/resources-overview", + "sdk/android/key-concepts", + "sdk/android/message-structure-and-hierarchy", "sdk/android/real-time-listeners", - "sdk/android/upgrading-from-v3-guide" + "sdk/android/rate-limits", + "sdk/android/connection-status", + "sdk/android/connection-behaviour" ] }, { - "group": "Advanced", + "group": "Reference", "pages": [ - "sdk/android/advanced-overview", - "sdk/android/connection-status", - "sdk/android/publishing-app-on-playstore", - "sdk/android/connection-behaviour" + "sdk/reference/messages", + "sdk/reference/entities", + "sdk/reference/auxiliary", + "sdk/reference/calls" ] }, + "sdk/android/login-listeners", + "sdk/android/upgrading-from-v3-guide", + "sdk/android/publishing-app-on-playstore", + "sdk/android/best-practices", + "sdk/android/error-codes", + "sdk/android/troubleshooting", "sdk/android/extensions-overview", "sdk/android/ai-user-copilot-overview", "sdk/android/ai-chatbots-overview", "sdk/android/webhooks-overview", - "sdk/android/android-overview", "sdk/android/changelog" ] } @@ -4746,7 +4735,9 @@ }, { "group": "Users", - "pages": ["rest-api/data-import-apis/users/import-users"] + "pages": [ + "rest-api/data-import-apis/users/import-users" + ] }, { "group": "Groups", @@ -5266,7 +5257,9 @@ "tabs": [ { "tab": "AI Agents", - "pages": ["ai-agents"] + "pages": [ + "ai-agents" + ] }, { "tab": "Agent Builder", @@ -5329,11 +5322,15 @@ "/ai-agents/crew-ai-tools", { "group": "Guides", - "pages": ["/ai-agents/crew-ai-knowledge-agent"] + "pages": [ + "/ai-agents/crew-ai-knowledge-agent" + ] }, { "group": "Tutorials", - "pages": ["/ai-agents/crew-ai-product-hunt-agent"] + "pages": [ + "/ai-agents/crew-ai-product-hunt-agent" + ] } ] }, @@ -5346,11 +5343,15 @@ "/ai-agents/agno-tools", { "group": "Guides", - "pages": ["/ai-agents/agno-knowledge-agent"] + "pages": [ + "/ai-agents/agno-knowledge-agent" + ] }, { "group": "Tutorials", - "pages": ["/ai-agents/agno-product-hunt-agent"] + "pages": [ + "/ai-agents/agno-product-hunt-agent" + ] } ] }, @@ -5363,11 +5364,15 @@ "/ai-agents/vercel-tools", { "group": "Guides", - "pages": ["/ai-agents/vercel-knowledge-agent"] + "pages": [ + "/ai-agents/vercel-knowledge-agent" + ] }, { "group": "Tutorials", - "pages": ["/ai-agents/vercel-product-hunt-agent"] + "pages": [ + "/ai-agents/vercel-product-hunt-agent" + ] } ] }, @@ -5378,11 +5383,15 @@ "/ai-agents/langgraph", { "group": "Guides", - "pages": ["/ai-agents/langgraph-knowledge-agent"] + "pages": [ + "/ai-agents/langgraph-knowledge-agent" + ] }, { "group": "Tutorials", - "pages": ["/ai-agents/langgraph-product-hunt-agent"] + "pages": [ + "/ai-agents/langgraph-product-hunt-agent" + ] } ] }, @@ -5395,11 +5404,15 @@ "/ai-agents/ag2-tools", { "group": "Guides", - "pages": ["/ai-agents/ag2-knowledge-agent"] + "pages": [ + "/ai-agents/ag2-knowledge-agent" + ] }, { "group": "Tutorials", - "pages": ["/ai-agents/ag2-product-hunt-agent"] + "pages": [ + "/ai-agents/ag2-product-hunt-agent" + ] } ] }, @@ -5412,7 +5425,9 @@ "/ai-agents/ag-ui-tools", { "group": "Guides", - "pages": ["/ai-agents/cometchat-ag-ui-byoa"] + "pages": [ + "/ai-agents/cometchat-ag-ui-byoa" + ] }, { "group": "Implementation", @@ -5428,12 +5443,16 @@ { "tab": "Widget Builder", "tab-id": "ai-agent-chat-builder", - "pages": ["/ai-agents/chat-widget"] + "pages": [ + "/ai-agents/chat-widget" + ] }, { "tab": "Custom Bots", "hidden": true, - "pages": ["/ai-chatbots/custom-bots"] + "pages": [ + "/ai-chatbots/custom-bots" + ] }, { "tab": "AI Bots (Legacy)", @@ -5631,7 +5650,9 @@ "tabs": [ { "tab": "Notifications", - "pages": ["notifications"] + "pages": [ + "notifications" + ] }, { "tab": "Push", @@ -5663,7 +5684,9 @@ }, { "group": " ", - "pages": ["notifications/push-notifications-extension-legacy"] + "pages": [ + "notifications/push-notifications-extension-legacy" + ] } ] }, @@ -5677,7 +5700,9 @@ "notifications/email-custom-providers", { "group": " ", - "pages": ["notifications/email-notifications-extension-legacy"] + "pages": [ + "notifications/email-notifications-extension-legacy" + ] } ] }, @@ -5691,7 +5716,9 @@ "notifications/sms-custom-providers", { "group": " ", - "pages": ["notifications/sms-notifications-extension-legacy"] + "pages": [ + "notifications/sms-notifications-extension-legacy" + ] } ] } @@ -5702,7 +5729,9 @@ "tabs": [ { "tab": "Insights", - "pages": ["insights"] + "pages": [ + "insights" + ] } ] } @@ -6447,7 +6476,7 @@ }, { "source": "/notifications/react-native-push-notifications", - "destination": "/notifications/react-native-push-notifications-android" + "destination": "/notifications/react-native-push-notifications-android" }, { "source": "/sdk/ionic/overview", diff --git a/sdk/android/additional-message-filtering.mdx b/sdk/android/additional-message-filtering.mdx index a215f75c7..fd86f1ec4 100644 --- a/sdk/android/additional-message-filtering.mdx +++ b/sdk/android/additional-message-filtering.mdx @@ -1,35 +1,75 @@ --- title: "Additional Message Filtering" +sidebarTitle: "Message Filtering" +description: "Filter messages by category, type, timestamp, tags, and other parameters using MessagesRequest in the CometChat Android SDK." --- + +```kotlin +// Build filtered message request +val messagesRequest = MessagesRequestBuilder() + .setUID("user_uid") // User conversation + .setLimit(50) // Max 100 per request + .setCategories(listOf("message")) // Filter by category + .setTypes(listOf("text", "image")) // Filter by type + .setUnread(true) // Only unread messages + .hideDeletedMessages(true) // Exclude deleted + .hideReplies(true) // Exclude threaded messages + .build() -The `MessagesRequest` class as you must be familiar with helps you to fetch messages based on the various parameters provided to it. This document will help you understand better the various options that are available using the `MessagesRequest` class. - -The `MessagesRequest` class is designed using the `Builder design pattern`. In order to obtain an object of the `MessagesRequest` class, you will have to make use of the `MessagesRequestBuilder` class in the `MessagesRequest` class. - -The `MessagesRequestBuilder` class allows you to set various parameters to the `MessagesRequest` class based on which the messages are fetched. - -Steps to generate an object of the MessagesRequest class: - -1. Create an object of the `MessagesRequestBuilder` class. -2. Set all the parameters you wish to set. -3. Call the `build()` method of the `MessagesRequestBuilder` class to get an object of the `MessagesRequest` class. - -Once you have an object of the `MessagesRequest` class, you can call either the `fetchNext()` method or the `fetchPrevious()` method using the object. - -1. fetchNext() - Calling this method will return the messages after the specified parameters. -2. fetchPrevious() - Calling this method will give you messages before the specified parameters. - -Since messages are obtained in a paginated manner, a `maximum of 100` messages can be pulled in a single iteration. Calling the `fetchPrevious()`/`fetchNext()` method on the same `MessagesRequest` object will get you the next set of messages. - -Now that you are clear how to use the `MessagesRequest` class, below are the various options available: +// Fetch messages +messagesRequest.fetchNext(callback) +messagesRequest.fetchPrevious(callback) +``` + + +The `MessagesRequest` class fetches messages based on various parameters using the Builder design pattern. + +To fetch messages: +1. Create a `MessagesRequestBuilder` object +2. Set your desired parameters +3. Call `build()` to get a `MessagesRequest` object +4. Call `fetchNext()` or `fetchPrevious()` to retrieve messages + +| Method | Description | +| --- | --- | +| `fetchNext()` | Returns messages after the specified parameters | +| `fetchPrevious()` | Returns messages before the specified parameters | + +Messages are paginated with a maximum of 100 per request. Call `fetchPrevious()`/`fetchNext()` repeatedly on the same object to get subsequent pages. + +## Filter Reference + +| Method | Description | Default | +| --- | --- | --- | +| `setUID(uid)` | Fetch messages for a one-on-one conversation | — | +| `setGUID(guid)` | Fetch messages for a group conversation | — | +| `setLimit(n)` | Number of messages per request (max 100) | — | +| `setMessageId(id)` | Fetch messages before/after a message ID | — | +| `setTimestamp(ts)` | Fetch messages before/after a Unix timestamp | — | +| `setUnread(bool)` | Fetch only unread messages | `false` | +| `setCategories(list)` | Filter by message categories | all | +| `setTypes(list)` | Filter by message types | all | +| `setParentMessageId(id)` | Fetch messages in a specific thread | — | +| `hideReplies(bool)` | Exclude threaded messages | `false` | +| `hideDeletedMessages(bool)` | Exclude deleted messages | `false` | +| `hideQuotedMessages(bool)` | Exclude quoted messages | `false` | +| `hideMessagesFromBlockedUsers(bool)` | Exclude messages from blocked users | `false` | +| `setUpdatedAfter(ts)` | Fetch messages updated after a timestamp | — | +| `updatesOnly(bool)` | Only updated messages (use with `setUpdatedAfter`) | `false` | +| `setTags(list)` | Filter by message tags | — | +| `withTags(bool)` | Include tag data in response | `false` | +| `hasLinks(bool)` | Only messages with links *(Advanced Search)* | `false` | +| `hasAttachments(bool)` | Only messages with attachments *(Advanced Search)* | `false` | +| `hasReactions(bool)` | Only messages with reactions *(Advanced Search)* | `false` | +| `hasMentions(bool)` | Only messages with mentions *(Advanced Search)* | `false` | +| `setMentionedUIDs(list)` | Only messages mentioning specific users *(Advanced Search)* | — | +| `setAttachmentTypes(list)` | Only messages with specific attachment types *(Advanced Search)* | — | ## Number of messages fetched -*In other words, how do I set the number of messages fetched in a single iteration* - -To achieve this, you can use the `setLimit()` method. This method takes an integer value as the input and informs the SDK to fetch the specified number of messages in one iteration. The maximum number of messages that can be fetched in one go is `100`. +Set the number of messages to fetch per request using `setLimit()`. Maximum is 100. @@ -84,9 +124,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages for a user conversation -*In other words, how do I fetch messages between me and any user* - -This can be achieved using the `setUID()` method. This method takes the UID of the user with whom the conversation is to be fetched. When a valid UID is passed, the SDK will return all the messages that are a part of the conversation between the logged-in user and the UID that has been specified. +Use `setUID()` to fetch messages between the logged-in user and a specific user. @@ -113,11 +151,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages for a group conversation -*In other words, how do I fetch messages for any group conversation* - -You can achieve this using the `setGUID()` method. This method takes the GUID of a group for which the conversations are to be fetched. Passing a valid GUID to this method will return all the messages that are a part of the group conversation. Please note that the logged-in user must be a member of the group to fetch the messages for that group. - -MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder() .setGUID("cometchat-guid-1") .setLimit(50) .build(); +Use `setGUID()` to fetch messages from a group. The logged-in user must be a member of the group. @@ -144,9 +178,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages before/after a message -*In other words, how do I fetch messages before or after a particular message* - -This can be achieved using the `setMessageId()` method. This method takes the message-id as input and provides messages only after/before the message-id based on if the fetchNext() or fetchPrevious() method is triggered. +Use `setMessageId()` to fetch messages before or after a specific message ID. Use `fetchNext()` to get messages after, or `fetchPrevious()` to get messages before. @@ -203,13 +235,11 @@ val messagesRequest = MessagesRequestBuilder() -This method can be used along with `setUID()` or `setGUID()` methods to fetch messages after/before any specific message-id for a particular user/group conversation. +This method can be combined with `setUID()` or `setGUID()` to fetch messages around a specific message in a conversation. ## Messages before/after a given time -*In other words, how do I fetch messages before or after a particular date or time* - -You can easily achieve this using the `setTimestamp()` method. This method takes the Unix timestamp as input and provides messages only after/before the timestamp based on if fetchNext() or fetchPrevious() method is triggered. +Use `setTimestamp()` with a Unix timestamp to fetch messages before or after a specific time. @@ -266,13 +296,11 @@ val messagesRequest = MessagesRequestBuilder() -This method can be used along with `setUID()` or `setGUID()` methods to fetch messages after/before any specific date or time for a particular user/group conversation. +This method can be combined with `setUID()` or `setGUID()` to fetch messages around a specific time in a conversation. ## Unread messages -*In other words, how do I fetch unread messages* - -This can easily be achieved using setting the unread flag to true. For this, you need to use the `setUnread()` method. This method takes a boolean value as input. If the value is set to true, the SDK will return just the unread messages. +Use `setUnread(true)` to fetch only unread messages. @@ -333,9 +361,7 @@ This method along with `setGUID()` or `setUID()` can be used to fetch unread mes ## Exclude messages from blocked users -*In other words, how do I fetch messages excluding the messages from the users I have blocked* - -This can be easily achieved using the`hideMessagesFromBlockedUsers()` method. This method accepts a boolean value which determines if the messages from users blocked by the logged-in user need to be a part if the fetched messages. If the value is set to true, the messages will be hidden and won't be a part of the messages fetched. The default value is false i.e if this method is not used, the messages from blocked users will be included in the fetched messages. +Use `hideMessagesFromBlockedUsers(true)` to exclude messages from users you've blocked. Default is `false`. @@ -392,13 +418,11 @@ val messagesRequest = MessagesRequestBuilder() -This method can be used to hide the messages by users blocked by logged in user in groups that both the members are a part of. +This also works in group conversations where both users are members. ## Updated and received messages -*In other words, how do I fetch messages that have been received or updated after a particular date or time* - -This method accepts a Unix timestamp value and will return all the messages that have been updated and the ones that have been sent/received after the specified time. The messages updated could mean the messages that have been marked as read/delivered or if the messages are edited or deleted. +Use `setUpdatedAfter()` with a Unix timestamp to fetch messages that were sent or updated after a specific time. Updated messages include those marked as read/delivered, edited, or deleted. @@ -455,13 +479,11 @@ val messagesRequest = MessagesRequestBuilder() -This can be useful in finding the messages that have been received or updated after a certain time. Can prove very useful if you are saving the messages locally and would like to know the messages that have been updated or received after the last message available in your local databases. +Useful for syncing messages with a local database — fetch only what's changed since your last sync. ## Updated messages only -*In other words, how do I fetch messages that have been updated after a particular date or time* - -This can be achieved easily by setting the updatesOnly parameter to true. To do so, you can use the updatesOnly() method. This method takes a boolean input and can be used with the `setUpdatedAfter()` method to get jus the updated messages and not the messages sent/received after the specified time. This method cannot be used independently and always needs to be used with the `setUpdatedAfter()` method. +Use `updatesOnly(true)` with `setUpdatedAfter()` to fetch only updated messages (not newly received ones). This method must be used together with `setUpdatedAfter()`. @@ -524,11 +546,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages for multiple categories -*In other words, how do I fetch messages belonging to multiple categories* - -We recommend before trying this, you refer to the [Message structure and hierarchy guide](/sdk/android/message-structure-and-hierarchy) to get familiar with the various categories of messages. - -For this, you will have to use the `setCategories()` method. This method accepts a list of categories. This tells the SDK to fetch messages only belonging to these categories. +Use `setCategories()` with a list of category names to filter by message category. See [Message structure and hierarchy](/sdk/android/message-structure-and-hierarchy) for available categories. @@ -597,15 +615,11 @@ val messagesRequest = MessagesRequestBuilder() -The above snippet will help you get only the messages belonging to the `message` and `custom` category. This can also be used to disable certain categories of messages like `call` and `action`. This along with `setGUID()` and `setUID()` can help display only the messages you wish to display avoiding the other category of messages. +The above snippet fetches only messages in the `message` and `custom` categories. Use this to exclude categories like `call` and `action`. ## Messages for multiple types -*In other words, how do I fetch messages belonging to multiple types* - -We recommend before trying this, you refer to the [Message structure and hierarchy guide](/sdk/android/message-structure-and-hierarchy) to get familiar with the various types of messages. - -This can be easily achieved using the `setTypes()` method. This method accepts a list of types. This tells the SDK to fetch messages only belonging to these types. +Use `setTypes()` with a list of type names to filter by message type. See [Message structure and hierarchy](/sdk/android/message-structure-and-hierarchy) for available types. @@ -694,13 +708,11 @@ val messagesRequest = MessagesRequestBuilder() -Using the above code snippet, you can fetch all the media messages. This along with setUID() or setGUID() can be used to fetch media messages for any particular conversation. This can be useful in many other scenarios as well. +The above snippet fetches all media messages (image, video, audio, file). ## Messages for a specific thread -*In other words, how do I fetch messages that are a part of a thread and not directly a user/group conversations* - -This can be done using the `setParentMessageId()` method. This method needs to be used when you have implemented threaded conversations in your app. This method will return the messages only belonging to the thread with the specified parent Id. +Use `setParentMessageId()` to fetch messages belonging to a specific thread. @@ -760,9 +772,7 @@ The above code snippet returns the messages that belong to the thread with paren ## Hide threaded messages in user/group conversations -*In other words, how do I exclude threaded messages from the normal user/group conversations* - -In order to do this, you can use the `hideReplies()` method. This method is also related to threaded conversations. This method takes boolean as input. This boolean when set to true will make sure that the messages that belong to threads are not fetched. If set to false, which is also the default value, the messages belong to the threads will also be fetched along with other messages. +Use `hideReplies(true)` to exclude threaded messages from the main conversation. Default is `false`. @@ -821,9 +831,7 @@ val messagesRequest = MessagesRequestBuilder() ## Hide deleted messages in user/group conversations -*In other words, how do I exclude deleted messages a user/group conversations* - -In order to do this, you can use the hideDeletedMessages() method. This method takes boolean as input. This boolean when set to true will make sure that the deleted messages are not fetched. If set to false, which is also the default value, the deleted messages will also be fetched along with other messages. +Use `hideDeletedMessages(true)` to exclude deleted messages. Default is `false`. @@ -882,9 +890,7 @@ val messagesRequest = MessagesRequestBuilder() ## Hide quoted messages in user/group conversations -*In other words, how do I exclude quoted messages in a user/group conversations* - -In order to do this, you can use the `hideQuotedMessages()` method. This method takes boolean as input. This boolean when set to true will make sure that the quoted messages are not fetched. If set to false, which is also the default value, the quoted messages will also be fetched along with other messages. +Use `hideQuotedMessages(true)` to exclude quoted messages. Default is `false`. @@ -943,9 +949,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages by tags -*In other words, how do I fetch messages belonging to specific tags* - -In order to do this, you can use the `setTags()` method. This method accepts a list of tags. This tells the SDK to fetch messages only belonging to these tags. +Use `setTags()` with a list of tag names to fetch only messages with those tags. @@ -982,9 +986,13 @@ val messagesRequest = MessagesRequestBuilder() ## Messages with tags -*In other words, how do I fetch messages with the tags information* +Use `withTags(true)` to include tag information in the response. Default is `false`. -In order to do this, you can use the `withTags()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages along with the tags information. When set to `false` , the SDK will not fetch tags information associated with messages. The default value for this parameter is `false` . +When `withTags(true)` is set, each message's `tags` field will be populated. Access tags using `getTags()`. + +| Additional Field | Getter | Return Type | Description | +| --- | --- | --- | --- | +| tags | `getTags()` | `List` | Tags associated with the message | @@ -1017,9 +1025,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages with links -In other words, as a logged-in user, how do I fetch messages that contains links? - -In order to do this, you can use the `hasLinks()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages which have links in the text. The default value for this parameter is `false`. +Use `hasLinks(true)` to fetch only messages containing links. Default is `false`. @@ -1058,9 +1064,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages with attachments -In other words, as a logged-in user, how do I fetch messages that contains attachments? - -In order to do this, you can use the `hasAttachments()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages which have attachments (image, audio, video or file). The default value for this parameter is `false`. +Use `hasAttachments(true)` to fetch only messages with attachments (image, audio, video, or file). Default is `false`. @@ -1099,9 +1103,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages with reactions -In other words, as a logged-in user, how do I fetch messages that contains reactions? - -In order to do this, you can use the `hasReactions()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages which have reactions. The default value for this parameter is `false`. +Use `hasReactions(true)` to fetch only messages that have reactions. Default is `false`. @@ -1140,9 +1142,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages with mentions -In other words, as a logged-in user, how do I fetch messages that contains mentions? - -In order to do this, you can use the `hasMentions()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages which have mentions. The default value for this parameter is `false`. +Use `hasMentions(true)` to fetch only messages that contain mentions. Default is `false`. @@ -1181,9 +1181,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages with particular user mentions -In other words, as a logged-in user, how do I fetch messages that mentions specific users? - -In order to do this, you can use the `setMentionedUIDs()` method. This method accepts an array of UIDs as input. When set, the SDK will fetch messages which have the mentions of the UIDs passed. +Use `setMentionedUIDs()` with a list of UIDs to fetch only messages that mention those specific users. @@ -1226,9 +1224,7 @@ val messagesRequest = MessagesRequestBuilder() ## Messages with specific attachment types -In other words, as a logged-in user, how do I fetch messages that contain specific types of attachments? - -In order to do this, you can use the `setAttachmentTypes()` method. This method accepts an array of `CometChat.AttachmentType` ENUM values as input. When provided, the SDK will fetch only those messages that include attachments of the specified types (such as image, audio, video, or file). +Use `setAttachmentTypes()` with a list of `AttachmentType` enum values to fetch only messages with specific attachment types. @@ -1270,3 +1266,43 @@ val UID = "cometchat-uid-1" + + +## Best Practices + + + + Set a reasonable limit (20-50 messages) using `setLimit()` to balance performance and user experience. Fetch more messages as users scroll using `fetchNext()` or `fetchPrevious()`. + + + Combine multiple filters to create specific views (e.g., unread media messages, messages with links, messages from specific time ranges). This reduces client-side filtering. + + + Store filtered message results in local storage to provide offline access and reduce API calls. Refresh the cache when new messages arrive via listeners. + + + Use `setUpdatedAfter()` with `updatesOnly(true)` to efficiently sync messages that have been edited, deleted, or marked as read/delivered since your last fetch. + + + For advanced filtering (links, attachments, reactions, mentions), ensure "Conversation & Advanced Search" is enabled in your CometChat Dashboard under Chats → Settings → General Configuration. + + + +--- + +## Next Steps + + + + Handle incoming messages with message listeners + + + Understand message categories and types + + + Implement threaded conversations with parent messages + + + Fetch conversation list with unread counts + + diff --git a/sdk/android/advanced-overview.mdx b/sdk/android/advanced-overview.mdx deleted file mode 100644 index 18501a4d5..000000000 --- a/sdk/android/advanced-overview.mdx +++ /dev/null @@ -1,6 +0,0 @@ ---- -title: "Advanced" -sidebarTitle: "Overview" ---- - - diff --git a/sdk/android/ai-agents.mdx b/sdk/android/ai-agents.mdx index 853da43a7..bc2197df2 100644 --- a/sdk/android/ai-agents.mdx +++ b/sdk/android/ai-agents.mdx @@ -1,13 +1,33 @@ --- title: "AI Agents" +sidebarTitle: "AI Agents" +description: "Integrate AI-powered agents for intelligent, automated interactions in the Android SDK" --- -# AI Agents Overview + + +```kotlin +// Listen for real-time AI events +CometChat.addAIAssistantListener("LISTENER_ID", object : CometChat.AIAssistantListener() { + override fun onAIAssistantEventReceived(event: AIAssistantBaseEvent) { + // Handle run start, tool calls, text streaming, run finished + } +}) + +// Listen for persisted agentic messages +CometChat.addMessageListener("LISTENER_ID", object : CometChat.MessageListener() { + override fun onAIAssistantMessageReceived(message: AIAssistantMessage) { } + override fun onAIToolResultReceived(message: AIToolResultMessage) { } + override fun onAIToolArgumentsReceived(message: AIToolArgumentMessage) { } +}) +``` + AI Agents enable intelligent, automated interactions within your application. They can process user messages, trigger tools, and respond with contextually relevant information. For a broader introduction, see the [AI Agents section](/ai-agents). -> **Note:** -> Currently, an Agent only responds to **Text Messages**. + +Currently, an Agent only responds to **Text Messages**. + ## Agent Run Lifecycle and Message Flow @@ -130,4 +150,338 @@ These events are received via the **`MessageListener`** after the run completes. ``` - \ No newline at end of file + + + +## Agentic Message Payload Structures + + + +The `AIAssistantMessage` object contains the AI assistant's response: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | long | Unique message identifier | +| `muid` | String | Developer-defined message ID | +| `sender` | [User](#user-object-ai) | User who sent the message | +| `receiver` | AppEntity | Message receiver ([User](#user-object-ai) or Group) | +| `receiverUid` | String | Receiver's unique identifier | +| `type` | String | Message type. Value: `"assistant"` | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `category` | String | Message category. Value: `"agentic"` | +| `sentAt` | long | Unix timestamp when sent | +| `deliveredAt` | long | Unix timestamp when delivered | +| `readAt` | long | Unix timestamp when read | +| `metadata` | JSONObject | Custom message metadata | +| `readByMeAt` | long | When logged-in user read message | +| `deliveredToMeAt` | long | When delivered to logged-in user | +| `deletedAt` | long | Unix timestamp when deleted (0 if not deleted) | +| `editedAt` | long | Unix timestamp when edited (0 if not edited) | +| `deletedBy` | String | UID of user who deleted (null if not deleted) | +| `editedBy` | String | UID of user who edited (null if not edited) | +| `updatedAt` | long | Unix timestamp of last update | +| `conversationId` | String | Associated conversation ID | +| `runId` | long | AI run identifier | +| `threadId` | String | AI thread identifier | +| `text` | String | AI response text | +| `tags` | Array\ | Message tags | + +**Sample AIAssistantMessage Object:** + +```json +{ + "id": 12345, + "muid": "msg_abc123", + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default" + }, + "receiver": { + "uid": "user_456", + "name": "Jane Smith" + }, + "receiverUid": "user_456", + "type": "assistant", + "receiverType": "user", + "category": "agentic", + "sentAt": 1699900000, + "deliveredAt": 1699900001, + "readAt": 1699900002, + "metadata": {"priority": "high"}, + "readByMeAt": 1699900002, + "deliveredToMeAt": 1699900001, + "deletedAt": 0, + "editedAt": 0, + "deletedBy": null, + "editedBy": null, + "updatedAt": 1699900000, + "conversationId": "user_123_user_456", + "runId": 98765, + "threadId": "thread_abc", + "text": "Here's the answer...", + "tags": ["ai-response"] +} +``` + + + + + +The `AIToolArgumentMessage` object contains the arguments passed to a tool: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | long | Unique message identifier | +| `muid` | String | Developer-defined message ID | +| `sender` | [User](#user-object-ai) | User who sent the message | +| `receiver` | AppEntity | Message receiver ([User](#user-object-ai) or Group) | +| `receiverUid` | String | Receiver's unique identifier | +| `type` | String | Message type. Value: `"tool_arguments"` | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `category` | String | Message category. Value: `"agentic"` | +| `sentAt` | long | Unix timestamp when sent | +| `deliveredAt` | long | Unix timestamp when delivered | +| `readAt` | long | Unix timestamp when read | +| `metadata` | JSONObject | Custom message metadata | +| `readByMeAt` | long | When logged-in user read message | +| `deliveredToMeAt` | long | When delivered to logged-in user | +| `deletedAt` | long | Unix timestamp when deleted (0 if not deleted) | +| `editedAt` | long | Unix timestamp when edited (0 if not edited) | +| `deletedBy` | String | UID of user who deleted (null if not deleted) | +| `editedBy` | String | UID of user who edited (null if not edited) | +| `updatedAt` | long | Unix timestamp of last update | +| `conversationId` | String | Associated conversation ID | +| `runId` | long | AI run identifier | +| `threadId` | String | AI thread identifier | +| `toolCalls` | Array\<[AIToolCall](#aitoolcall-object)\> | List of tool calls | +| `tags` | Array\ | Message tags | + +**Sample AIToolArgumentMessage Object:** + +```json +{ + "id": 12346, + "muid": "msg_tool_arg_123", + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default" + }, + "receiver": { + "uid": "user_456", + "name": "Jane Smith" + }, + "receiverUid": "user_456", + "type": "tool_arguments", + "receiverType": "user", + "category": "agentic", + "sentAt": 1699900000, + "deliveredAt": 1699900001, + "readAt": 1699900002, + "metadata": {"priority": "high"}, + "readByMeAt": 1699900002, + "deliveredToMeAt": 1699900001, + "deletedAt": 0, + "editedAt": 0, + "deletedBy": null, + "editedBy": null, + "updatedAt": 1699900000, + "conversationId": "user_123_user_456", + "runId": 98765, + "threadId": "thread_abc", + "toolCalls": [ + { + "id": "call_abc123", + "name": "search_flights", + "arguments": {"origin": "NYC", "destination": "LA"} + } + ], + "tags": ["tool-arguments"] +} +``` + + + + + +The `AIToolResultMessage` object contains the result from a tool execution: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | long | Unique message identifier | +| `muid` | String | Developer-defined message ID | +| `sender` | [User](#user-object-ai) | User who sent the message | +| `receiver` | AppEntity | Message receiver ([User](#user-object-ai) or Group) | +| `receiverUid` | String | Receiver's unique identifier | +| `type` | String | Message type. Value: `"tool_result"` | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `category` | String | Message category. Value: `"agentic"` | +| `sentAt` | long | Unix timestamp when sent | +| `deliveredAt` | long | Unix timestamp when delivered | +| `readAt` | long | Unix timestamp when read | +| `metadata` | JSONObject | Custom message metadata | +| `readByMeAt` | long | When logged-in user read message | +| `deliveredToMeAt` | long | When delivered to logged-in user | +| `deletedAt` | long | Unix timestamp when deleted (0 if not deleted) | +| `editedAt` | long | Unix timestamp when edited (0 if not edited) | +| `deletedBy` | String | UID of user who deleted (null if not deleted) | +| `editedBy` | String | UID of user who edited (null if not edited) | +| `updatedAt` | long | Unix timestamp of last update | +| `conversationId` | String | Associated conversation ID | +| `runId` | long | AI run identifier | +| `threadId` | String | AI thread identifier | +| `text` | String | Tool result text | +| `toolCallId` | String | ID of the tool call that produced this result | +| `tags` | Array\ | Message tags | + +**Sample AIToolResultMessage Object:** + +```json +{ + "id": 12347, + "muid": "msg_tool_result_123", + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default" + }, + "receiver": { + "uid": "user_456", + "name": "Jane Smith" + }, + "receiverUid": "user_456", + "type": "tool_result", + "receiverType": "user", + "category": "agentic", + "sentAt": 1699900000, + "deliveredAt": 1699900001, + "readAt": 1699900002, + "metadata": {"priority": "high"}, + "readByMeAt": 1699900002, + "deliveredToMeAt": 1699900001, + "deletedAt": 0, + "editedAt": 0, + "deletedBy": null, + "editedBy": null, + "updatedAt": 1699900000, + "conversationId": "user_123_user_456", + "runId": 98765, + "threadId": "thread_abc", + "text": "Flight found: NYC to LA...", + "toolCallId": "call_abc123", + "tags": ["tool-result"] +} +``` + + + + + +The `AIToolCall` object represents a single tool invocation: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | String | Unique identifier for the tool call | +| `name` | String | Name of the tool being called | +| `arguments` | JSONObject | Arguments passed to the tool | + +**Sample AIToolCall Object:** + +```json +{ + "id": "call_abc123", + "name": "search_flights", + "arguments": { + "origin": "NYC", + "destination": "LA", + "date": "2024-01-15" + } +} +``` + + + + + +The nested `User` object in `sender` contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + +**Sample User Object:** + +```json +{ + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "link": "https://example.com/profile/user_123", + "role": "default", + "metadata": {"department": "engineering"}, + "status": "online", + "statusMessage": "Available", + "lastActiveAt": 1699900000, + "hasBlockedMe": false, + "blockedByMe": false, + "tags": ["premium"], + "deactivatedAt": 0 +} +``` + + + +## Best Practices + + + + Remove AI assistant and message listeners in `onDestroy()` or `onPause()` to prevent memory leaks. Use unique listener IDs to manage multiple listeners effectively. + + + Use `Text Message Content` events to render AI responses incrementally as they arrive. This provides a better user experience than waiting for the complete response. + + + Show users when the agent is invoking tools (e.g., "Searching for flights...") using `Tool Call Start` and `Tool Call End` events. This provides transparency about agent actions. + + + Persist `AIAssistantMessage`, `AIToolResultMessage`, and `AIToolArgumentMessage` objects locally to display conversation history when users return to the chat. + + + +--- + +## Next Steps + + + + Learn about AI agent capabilities and configuration + + + Send text messages to AI agents to trigger runs + + + Handle incoming messages including AI responses + + + Configure and manage AI agents in the CometChat Dashboard + + diff --git a/sdk/android/ai-chatbots-overview.mdx b/sdk/android/ai-chatbots-overview.mdx index 453c5e30f..d77b05e36 100644 --- a/sdk/android/ai-chatbots-overview.mdx +++ b/sdk/android/ai-chatbots-overview.mdx @@ -1,4 +1,10 @@ --- -title: "AI" +title: "AI Chatbots" +sidebarTitle: "AI Chatbots" +description: "Implement custom AI chatbots for automated conversations in your Android application" url: "/ai-chatbots/overview" ---- \ No newline at end of file +--- + +AI Chatbots enable automated conversations within your application. They can process user messages and respond with contextually relevant information. For a broader introduction, see the [AI Chatbots section](/ai-chatbots). + +For message payload structures (AIAssistantMessage, AIToolArgumentMessage, AIToolResultMessage), see the [AI Agents](/sdk/android/ai-agents#agentic-message-payload-structures) page. diff --git a/sdk/android/ai-moderation.mdx b/sdk/android/ai-moderation.mdx index 3c3a0214c..070158769 100644 --- a/sdk/android/ai-moderation.mdx +++ b/sdk/android/ai-moderation.mdx @@ -1,10 +1,33 @@ --- title: "AI Moderation" +sidebarTitle: "AI Moderation" +description: "Automatically review messages for inappropriate content using AI moderation in the Android SDK" --- -## Overview + -AI Moderation in the CometChat SDK helps ensure that your chat application remains safe and compliant by automatically reviewing messages for inappropriate content. This feature leverages AI to moderate messages in real-time, reducing manual intervention and improving user experience. +```kotlin +// Send message (automatically moderated) +val textMessage = TextMessage(receiverUID, "Hello!", CometChatConstants.RECEIVER_TYPE_USER) +CometChat.sendMessage(textMessage, object : CometChat.CallbackListener() { + override fun onSuccess(message: TextMessage) { + if (message.moderationStatus == ModerationStatus.PENDING) { + // Show pending indicator + } + } + override fun onError(e: CometChatException) { } +}) + +// Listen for moderation results +CometChat.addMessageListener("LISTENER_ID", object : CometChat.MessageListener() { + override fun onMessageModerated(message: BaseMessage) { + // Handle APPROVED or DISAPPROVED status + } +}) +``` + + +AI Moderation automatically reviews messages for inappropriate content, reducing manual intervention. Messages are sent with a `PENDING` status and updated to `APPROVED` or `DISAPPROVED` after processing. For a broader understanding of moderation features, configuring rules, and managing flagged messages, see the [Moderation Overview](/moderation/overview). @@ -234,4 +257,35 @@ When a message is disapproved, handle it appropriately in your UI: ## Next Steps -After implementing AI Moderation, consider adding a reporting feature to allow users to flag messages they find inappropriate. For more details, see the [Flag Message](/sdk/android/flag-message) documentation. + + + + Configure moderation rules and manage flagged messages + + + Allow users to report inappropriate messages + + + Send text, image, and video messages with automatic moderation + + + Enable and configure AI moderation in the CometChat Dashboard + + + +## Best Practices + + + + Display a visual indicator (e.g., clock icon, "Pending review" label) for messages with `PENDING` moderation status. This informs users that their message is being reviewed. + + + When a message is disapproved, show a user-friendly message explaining why it was blocked. Avoid technical jargon and provide guidance on acceptable content. + + + Set up moderation rules in the [CometChat Dashboard](https://app.cometchat.com) under Moderation > Rules. Define what content should be flagged or blocked based on your app's policies. + + + Test moderation with various content types (text, images, videos) in a development environment to ensure rules work as expected before deploying to production. + + diff --git a/sdk/android/ai-user-copilot-overview.mdx b/sdk/android/ai-user-copilot-overview.mdx index 3e798a3fb..20da34677 100644 --- a/sdk/android/ai-user-copilot-overview.mdx +++ b/sdk/android/ai-user-copilot-overview.mdx @@ -1,4 +1,6 @@ --- -title: "AI" +title: "AI User Copilot" +sidebarTitle: "AI User Copilot" +description: "Enhance user experience with AI-powered assistance and suggestions in the Android SDK" url: "/fundamentals/ai-user-copilot/overview" --- \ No newline at end of file diff --git a/sdk/android/android-overview.mdx b/sdk/android/android-overview.mdx deleted file mode 100644 index c85f2beb4..000000000 --- a/sdk/android/android-overview.mdx +++ /dev/null @@ -1,4 +0,0 @@ ---- -title: "Java UI Kit" -url: "/ui-kit/android/overview" ---- \ No newline at end of file diff --git a/sdk/android/authentication-overview.mdx b/sdk/android/authentication-overview.mdx index 7e3f45b6b..c98607794 100644 --- a/sdk/android/authentication-overview.mdx +++ b/sdk/android/authentication-overview.mdx @@ -1,37 +1,91 @@ --- title: "Authentication" -sidebarTitle: "Overview" +sidebarTitle: "Authentication" +description: "Log users into CometChat using Auth Key for development or Auth Token for production" --- + +```kotlin +// Check if user is already logged in +val loggedInUser = CometChat.getLoggedInUser() -### Create User +// Login with Auth Key (development only) +CometChat.login("UID", "AUTH_KEY", object : CometChat.CallbackListener() { + override fun onSuccess(user: User?) { } + override fun onError(e: CometChatException?) { } +}) -Before you log in a user, you must add the user to CometChat. +// Login with Auth Token (production) +CometChat.login("AUTH_TOKEN", object : CometChat.CallbackListener() { + override fun onSuccess(user: User?) { } + override fun onError(e: CometChatException?) { } +}) -1. **For proof of concept/MVPs**: Create the user using the [CometChat Dashboard](https://app.cometchat.com). -2. **For production apps**: Use the CometChat [Create User API](https://api-explorer.cometchat.com/reference/creates-user) to create the user when your user signs up in your app. +// Logout +CometChat.logout(object : CometChat.CallbackListener() { + override fun onSuccess(p0: String?) { } + override fun onError(p0: CometChatException?) { } +}) +``` - -Sample Users +**Required Credentials:** App ID (from init), Auth Key (dev) or Auth Token (prod) +**Get from:** [CometChat Dashboard](https://app.cometchat.com) → Your App → API & Auth Keys + + +After [initializing](/sdk/android/setup) the SDK, the next step is to authenticate your user. CometChat provides two login methods — Auth Key for quick development, and Auth Token for production — both accessed through the `login()` method. + +## Before You Log In + +### Create a User + +A user must exist in CometChat before they can log in. -We have setup 5 users for testing having UIDs: `cometchat-uid-1`, `cometchat-uid-2`, `cometchat-uid-3`, `cometchat-uid-4` and `cometchat-uid-5`. +- **During development:** Create users from the [CometChat Dashboard](https://app.cometchat.com). Five test users are already available with UIDs `cometchat-uid-1` through `cometchat-uid-5`. +- **In production:** Call the [Create User REST API](https://api-explorer.cometchat.com/reference/creates-user) when a user signs up in your app. + +We have set up 5 users for testing with UIDs: `cometchat-uid-1`, `cometchat-uid-2`, `cometchat-uid-3`, `cometchat-uid-4`, and `cometchat-uid-5`. -Once initialization is successful, you will need to log the user into CometChat using the `login()` method. +### Check for an Existing Session -We recommend you call the CometChat `login()` method once your user logs into your app. The `login()` method needs to be called only once. +The SDK persists the logged-in user's session locally. Before calling `login()`, always check whether a session already exists — this avoids unnecessary login calls. - + + +```java +if (CometChat.getLoggedInUser() != null) { + // User is already logged in — proceed to your app +} +``` + + +```kotlin +if (CometChat.getLoggedInUser() != null) { + // User is already logged in — proceed to your app +} +``` + + -The CometChat SDK maintains the session of the logged-in user within the SDK. Thus you do not need to call the login method for every session. You can use the CometChat.getLoggedInUser() method to check if there is any existing session in the SDK. This method should return the details of the logged-in user. If this method returns null, it implies there is no session present within the SDK and you need to log the user into ComeChat. +If `getLoggedInUser()` returns `null`, no active session exists and you need to call `login()`. + +`CometChat.init()` must be called before any other SDK method. Calling `login()`, `sendMessage()`, or registering listeners before `init()` will fail. + + + +The CometChat SDK maintains the session of the logged-in user within the SDK. You do not need to call the login method for every session. Use `CometChat.getLoggedInUser()` to check for an existing session first. ## Login using Auth Key -This straightforward authentication method is ideal for proof-of-concept (POC) development or during the early stages of application development. For production environments, however, we strongly recommend using an [AuthToken](#login-using-auth-token) instead of an Auth Key to ensure enhanced security. +This straightforward authentication method is ideal for proof-of-concept (POC) development or during the early stages of application development. For production environments, however, we strongly recommend using an [Auth Token](#login-using-auth-token) instead of an Auth Key to ensure enhanced security. + + +**Auth Key** is for development/testing only. In production, generate **Auth Tokens** on your server using the REST API and pass them to the client. Never expose Auth Keys in production client code. + @@ -86,7 +140,7 @@ After the user logs in, their information is returned in the `User` object. ## Login using Auth Token -This advanced authentication procedure does not use the Auth Key directly in your client code thus ensuring safety. +This advanced authentication procedure does not use the Auth Key directly in your client code, ensuring better security. 1. [Create a User](https://api-explorer.cometchat.com/reference/creates-user) via the CometChat API when the user signs up in your app. 2. [Create an Auth Token](https://api-explorer.cometchat.com/reference/create-authtoken) via the CometChat API for the new user every time the user logs in to your app. @@ -175,3 +229,105 @@ CometChat.logout(object : CometChat.CallbackListener() { + +--- + +## Login Listener + +You can listen for login and logout events in real time using `LoginListener`. This is useful for updating UI state or triggering side effects when the auth state changes. + +| Callback | Description | +| --- | --- | +| `loginSuccess(User user)` | User logged in successfully. Provides the `User` object. | +| `loginFailure(CometChatException e)` | Login failed. Provides the exception with the reason. | +| `logoutSuccess()` | User logged out successfully. | +| `logoutFailure(CometChatException e)` | Logout failed. Provides the exception with the reason. | + +### Add a Listener + + + +```java +CometChat.addLoginListener("UNIQUE_ID", new CometChat.LoginListener() { + @Override + public void loginSuccess(User user) { + Log.d("LoginListener", "loginSuccess " + user.toString()); + } + + @Override + public void loginFailure(CometChatException e) { + Log.d("LoginListener", "loginFailure " + e.getMessage()); + } + + @Override + public void logoutSuccess() { + Log.d("LoginListener", "logoutSuccess"); + } + + @Override + public void logoutFailure(CometChatException e) { + Log.d("LoginListener", "logoutFailure " + e.getMessage()); + } +}); +``` + + +```kotlin +CometChat.addLoginListener("UNIQUE_ID", object : LoginListener() { + override fun loginSuccess(user: User) { + Log.d("LoginListener", "loginSuccess $user") + } + + override fun loginFailure(e: CometChatException) { + Log.d("LoginListener", "loginFailure " + e.message) + } + + override fun logoutSuccess() { + Log.d("LoginListener", "logoutSuccess") + } + + override fun logoutFailure(e: CometChatException) { + Log.d("LoginListener", "logoutFailure " + e.message) + } +}) +``` + + + +### Remove a Listener + + + +```java +CometChat.removeLoginListener("UNIQUE_ID"); +``` + + +```kotlin +CometChat.removeLoginListener("UNIQUE_ID") +``` + + + + +Always remove login listeners when they're no longer needed (e.g., in `onDestroy()`). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +--- + +## Next Steps + + + + Start sending text, media, and custom messages to users and groups + + + Retrieve and manage users in your application + + + Monitor the SDK connection state in real time + + + Understand core CometChat concepts and terminology + + diff --git a/sdk/android/best-practices.mdx b/sdk/android/best-practices.mdx new file mode 100644 index 000000000..50aa2d1aa --- /dev/null +++ b/sdk/android/best-practices.mdx @@ -0,0 +1,178 @@ +--- +title: "Best Practices" +sidebarTitle: "Best Practices" +description: "Recommended patterns and practices for building with the CometChat Android SDK." +--- + +Follow these best practices to build reliable, performant, and secure applications with the CometChat Android SDK. Organized by topic — jump to what's relevant for your current work. + +## Initialization & Authentication + +| Practice | Description | +|----------|-------------| +| Initialize once at app startup | Call `CometChat.init()` in your `Application` class `onCreate()`. It only needs to be called once per session. | +| Store credentials in `local.properties` | Keep App ID, Region, and Auth Key out of source control. Read them via `BuildConfig` fields set in `build.gradle`. | +| Check for existing sessions | Before calling `login()`, use `CometChat.getLoggedInUser()` to check if a session already exists. | +| Use Auth Tokens in production | Auth Keys are for development only. Generate Auth Tokens server-side using the [REST API](https://api-explorer.cometchat.com/reference/create-authtoken). | +| Handle token expiry | Implement a mechanism to detect login failures due to expired tokens. Use the [Login Listener](/sdk/android/authentication-overview#login-listener) to detect session changes. | +| Logout on sign-out | Always call `CometChat.logout()` when your user signs out to clear the SDK session and stop real-time events. | + +## Activity & Fragment Lifecycle + +| Practice | Description | +|----------|-------------| +| Register listeners in `onResume()` | Re-register message and call listeners when the Activity or Fragment becomes visible. | +| Remove listeners in `onPause()` | Remove listeners in `onPause()` to avoid processing events while the screen is not visible. | +| Clean up in `onDestroy()` | Remove all remaining listeners and cancel pending requests in `onDestroy()` to prevent memory leaks. | +| Use `ViewModel` for SDK state | Hold SDK data (messages, users, groups) in a `ViewModel` so it survives configuration changes like screen rotation. | +| Avoid SDK calls in `onCreate()` before `init()` | Ensure `CometChat.init()` has completed (in your `Application` class) before making any SDK calls in an Activity. | + +## Listeners + +| Practice | Description | +|----------|-------------| +| Use unique listener IDs | Use descriptive IDs like `"MESSAGE_LISTENER_CHAT_SCREEN"` to avoid accidental overwrites. | +| Register after login, remove on cleanup | Register listeners after `login()` succeeds. Remove them in `onPause()` or `onDestroy()` to prevent memory leaks. | +| Keep callbacks lightweight | Avoid heavy processing inside listener callbacks. Post updates to your `ViewModel` or `LiveData`. | +| Use specific listeners | Only register the listener types you need. Don't register a `GroupListener` if your screen only handles messages. | + +## Pagination & Caching + +| Practice | Description | +|----------|-------------| +| Use reasonable limits | Set `setLimit()` to 30–50 for users, messages, and group members. | +| Reuse request objects | Call `fetchNext()`/`fetchPrevious()` on the same request instance. Creating a new object resets the cursor. | +| Cache frequently accessed data | Store user and group objects in your `ViewModel` or a local Room database to reduce API calls. | + +## Rate Limits + +| Practice | Description | +|----------|-------------| +| Batch operations | Space out bulk operations using a queue or throttle mechanism. | +| Monitor rate limit headers | Check `X-Rate-Limit-Remaining` in REST API responses to slow down before hitting limits. | +| Distinguish operation types | Core operations (login, create/delete user) share a 10,000/min limit. Standard operations have 20,000/min. Avoid frequent login/logout cycles. | + +## Messaging + +| Practice | Description | +|----------|-------------| +| Use appropriate message types | Choose text, media, or custom messages based on your content. | +| Add metadata for context | Use `setMetadata()` to attach location, device info, or other contextual data. | +| Handle errors gracefully | Always implement `onError()` callbacks to handle network issues or invalid parameters. | +| Validate file types | Before sending media messages, verify the file type matches the message type. | +| Hide deleted/blocked content | Use `hideDeletedMessages(true)` and `hideMessagesFromBlockedUsers(true)` for cleaner lists. | + +## Threaded Messages + +| Practice | Description | +|----------|-------------| +| Track active thread ID | Store the current thread's `parentMessageId` to filter incoming messages. | +| Use `hideReplies(true)` | Exclude thread replies from the main conversation to avoid clutter. | +| Show reply count | Display the number of replies on parent messages to indicate thread activity. | + +## Reactions & Mentions + +| Practice | Description | +|----------|-------------| +| Update UI optimistically | Show reactions immediately, then sync with the server response. | +| Use correct mention format | Always use `<@uid:UID>` format for mentions in message text. | +| Highlight mentions in UI | Parse message text and style mentions differently using `SpannableString`. | + +## Typing Indicators + +| Practice | Description | +|----------|-------------| +| Debounce typing events | Don't call `startTyping()` on every keystroke — debounce to ~300ms intervals using a `Handler` or `debounce` operator. | +| Auto-stop typing | Call `endTyping()` after 3–5 seconds of inactivity or when the user sends a message. | + +## Delivery & Read Receipts + +| Practice | Description | +|----------|-------------| +| Mark as delivered on fetch | Call `markAsDelivered()` when messages are fetched and displayed. | +| Mark as read on view | Call `markAsRead()` when the user actually views or scrolls to a message. | +| Batch receipts | Mark the last message in a batch — all previous messages are automatically marked. | + +## Groups + +| Practice | Description | +|----------|-------------| +| Use meaningful GUIDs | Choose descriptive, unique GUIDs (e.g., `"project-alpha-team"`). | +| Set group type carefully | Group type cannot be changed after creation. Choose between `PUBLIC`, `PASSWORD`, and `PRIVATE`. | +| Add members at creation | Use `createGroupWithMembers()` to add initial members in a single API call. | +| Check `hasJoined` before joining | Avoid unnecessary API calls by checking the group's `hasJoined` property first. | +| Transfer ownership before leaving | Owners must transfer ownership to another member before they can leave. | +| Use `joinedOnly(true)` | Filter to joined groups when building sidebars or group lists. | + +## Group Members + +| Practice | Description | +|----------|-------------| +| Batch member additions | Add multiple members in a single `addMembersToGroup()` call. | +| Set appropriate scopes | Assign `PARTICIPANT` by default. Only use `ADMIN` or `MODERATOR` when needed. | +| Handle partial failures | Check each entry in the response for `"success"` or an error message. | +| Use scope constants | Use `CometChatConstants.SCOPE_ADMIN` instead of raw strings. | +| Kick vs. Ban | Use kick when the user can rejoin. Use ban for permanent removal until unbanned. | + +## Calling + +| Practice | Description | +|----------|-------------| +| Initialize Calls SDK after Chat SDK | Always initialize Chat SDK (`CometChat.init()`) before Calls SDK (`CometChatCalls.init()`). | +| Store session ID immediately | Save the session ID from `initiateCall()` response — you'll need it for accept, reject, and cancel. | +| Handle all call states | Implement handlers for all listener events (accepted, rejected, cancelled, busy, ended). | +| Generate tokens just-in-time | Generate call tokens immediately before starting a session rather than caching them. | +| Clean up on session end | Always call `CometChatCalls.endSession()` in both `onCallEnded` and `onCallEndButtonPressed` callbacks. | +| Request permissions before calling | Check and request `CAMERA` and `RECORD_AUDIO` permissions at runtime before initiating a call. | +| Inform users about recording | Always notify participants when recording starts — this is often a legal requirement. | +| Limit presenters to 5 | Additional users should join as viewers. | + +## Permissions + +| Practice | Description | +|----------|-------------| +| Request permissions at the right time | Request `CAMERA`, `RECORD_AUDIO`, and `READ_EXTERNAL_STORAGE` permissions contextually, not at app launch. | +| Handle permission denial gracefully | Show a rationale dialog if the user denies a permission, and disable the relevant feature rather than crashing. | +| Use `ActivityResultContracts` | Use the modern `registerForActivityResult` API instead of the deprecated `onRequestPermissionsResult`. | + +## Connection & WebSocket + +| Practice | Description | +|----------|-------------| +| Register connection listener early | Add the listener right after `CometChat.init()` succeeds. | +| Show connection status in UI | Display a banner when disconnected so users know messages may be delayed. | +| Queue actions during disconnection | Queue user actions and retry once `onConnected` fires. | +| Don't poll `getConnectionStatus()` | Use the listener-based approach instead. | +| Reconnect on app foreground | Call `CometChat.connect()` in `onResume()` if you disconnect in the background. | + +## AI Features + +| Practice | Description | +|----------|-------------| +| Register both listeners for AI Agents | Use `AIAssistantListener` for streaming events and `MessageListener` for persisted messages. | +| Handle streaming progressively | Render the assistant's reply token-by-token using `Text Message Content` events. | +| Show pending state for moderation | Display a visual indicator when `getModerationStatus()` returns `PENDING`. | +| Handle disapproved messages gracefully | Show a placeholder or notification so the sender understands what happened. | +| Track pending messages | Maintain a local map of pending message IDs to update UI when moderation results arrive. | + +## Upgrading from V3 + +| Practice | Description | +|----------|-------------| +| Follow the setup guide first | Complete the v4 [setup instructions](/sdk/android/setup) before changing dependencies. | +| Update Gradle dependency | Replace the v3 artifact with `com.cometchat:chat-sdk-android:4.x.x` in your `build.gradle`. | +| Test incrementally | Test each feature area (messaging, calling, groups) individually after updating. | +| Remove old packages | Remove the v3 dependency from `build.gradle` and sync to avoid conflicts. | + +--- + +## Next Steps + + + + Common issues and solutions + + + Installation and initialization guide + + diff --git a/sdk/android/block-users.mdx b/sdk/android/block-users.mdx index d48752d66..328e2e2c9 100644 --- a/sdk/android/block-users.mdx +++ b/sdk/android/block-users.mdx @@ -1,14 +1,80 @@ --- title: "Block Users" +sidebarTitle: "Block Users" +description: "Block and unblock users to control communication in your Android app" --- + + + +```kotlin +// Block users +val uids = listOf("UID1", "UID2", "UID3") +CometChat.blockUsers(uids, object : CometChat.CallbackListener>() { + override fun onSuccess(resultMap: HashMap) { + // resultMap contains UID -> "success"/"fail" + } + override fun onError(e: CometChatException) { } +}) -## Block Users +// Unblock users +CometChat.unblockUsers(uids, object : CometChat.CallbackListener>() { + override fun onSuccess(resultMap: HashMap) { } + override fun onError(e: CometChatException) { } +}) -*In other words, as a logged-in user, how do I block a user from sending me messages?* +// Get blocked users list +val request = BlockedUsersRequestBuilder() + .setLimit(30) + .setDirection(BlockedUsersRequest.DIRECTION_BLOCKED_BY_ME) + .build() +request.fetchNext(object : CallbackListener>() { + override fun onSuccess(users: List) { } + override fun onError(e: CometChatException) { } +}) +``` + + +```java +// Block users +List uids = Arrays.asList("UID1", "UID2", "UID3"); +CometChat.blockUsers(uids, new CometChat.CallbackListener>() { + @Override + public void onSuccess(HashMap resultMap) { + // resultMap contains UID -> "success"/"fail" + } + @Override + public void onError(CometChatException e) { } +}); -You can block users using the `blockUsers()` method. Once any user is blocked, all the communication to and from the respective user will be completely blocked. You can block multiple users in a single operation. The `blockUsers()` method takes a `List` as a parameter which holds the list of `UIDs` to be blocked. +// Unblock users +CometChat.unblockUsers(uids, new CometChat.CallbackListener>() { + @Override + public void onSuccess(HashMap resultMap) { } + @Override + public void onError(CometChatException e) { } +}); + +// Get blocked users list +BlockedUsersRequest request = new BlockedUsersRequest.BlockedUsersRequestBuilder() + .setLimit(30) + .setDirection(BlockedUsersRequest.DIRECTION_BLOCKED_BY_ME) + .build(); +request.fetchNext(new CometChat.CallbackListener>() { + @Override + public void onSuccess(List users) { } + @Override + public void onError(CometChatException e) { } +}); +``` + + + + +## Block Users + +Use `blockUsers()` to block one or more users. All communication to and from blocked users is stopped. Pass a `List` of UIDs to block. @@ -54,13 +120,11 @@ override fun onError(e: CometChatException) { -In the `onSuccess()` callback, you receive a HashMap which contains `UIDs` as the keys and `success` or `fail` as the value based on if the block operation for the `UID` was successful or not. +In the `onSuccess()` callback, you receive a HashMap which contains `UIDs` as the keys and `success` or `fail` as the value based on whether the block operation for the `UID` was successful or not. ## Unblock Users -*In other words, as a logged-in user, how do I unblock a user I previously blocked?* - -You can unblock the already blocked users using the `unblockUsers()` method. You can unblock multiple users in a single operation. The `unblockUsers()` method takes a `List` as a parameter which holds the list of `UIDs` to be unblocked. +Use `unblockUsers()` to unblock previously blocked users. Pass a `List` of UIDs to unblock. @@ -106,19 +170,17 @@ override fun onError(e: CometChatException) { -In the `onSuccess()` callback, you receive a HashMap which contains `UIDs` as the keys and `success` or `fail` as the value based on if the unblock operation for the `UID` was successful or not. +In the `onSuccess()` callback, you receive a HashMap which contains `UIDs` as the keys and `success` or `fail` as the value based on whether the unblock operation for the `UID` was successful or not. -## Get list of blocked users - -*In other words, as a logged-in user, how do I get a list of all users I've blocked?* +--- -In order to fetch the list of blocked users, you can use the `BlockedUsersRequest` class. To use this class i.e to create an object of the `BlockedUsersRequest class`, you need to use the `BlockedUsersRequestBuilder` class. The `BlockedUsersRequestBuilder` class allows you to set the parameters based on which the blocked users are to be fetched. +## Get List of Blocked Users -The `BlockedUsersRequestBuilder` class allows you to set the below parameters: +Use `BlockedUsersRequestBuilder` to fetch blocked users with optional filters. ### Set Limit -This method sets the limit i.e. the number of blocked users that should be fetched in a single iteration. +Set the number of blocked users to fetch per request. @@ -143,7 +205,7 @@ val blockedUsersRequest = BlockedUsersRequestBuilder() ### Set Search Keyword -This method allows you to set the search string based on which the blocked users are to be fetched. +Filter blocked users by a search string. @@ -168,9 +230,9 @@ val blockedUsersRequest = BlockedUsersRequestBuilder() ### Set Direction -* CometChat.BlockedUsersRequest.DIRECTION.BLOCKED\_BY\_ME - This will ensure that the list of blocked users only contains the users blocked by the logged in user. -* CometChat.BlockedUsersRequest.DIRECTION.HAS\_BLOCKED\_ME - This will ensure that the list of blocked users only contains the users that have blocked the logged in user. -* CometChat.BlockedUsersRequest.DIRECTION.BOTH - This will make sure the list of users includes both the above cases. This is the default value for the direction variable if it is not set. +* `CometChat.BlockedUsersRequest.DIRECTION.BLOCKED_BY_ME` - Ensures that the list of blocked users only contains users blocked by the logged-in user. +* `CometChat.BlockedUsersRequest.DIRECTION.HAS_BLOCKED_ME` - Ensures that the list of blocked users only contains users that have blocked the logged-in user. +* `CometChat.BlockedUsersRequest.DIRECTION.BOTH` - Ensures the list of users includes both of the above cases. This is the default value for the direction variable if it is not set. @@ -193,9 +255,9 @@ val blockedUsersRequest = BlockedUsersRequestBuilder() -Finally, once all the parameters are set in the builder class, you need to call the `build()` method to get the object of the `BlockedUsersRequest` class. +### Fetch Blocked Users -Once you have the object of the `BlockedUsersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `User` objects containing n number of blocked users where N is the limit set in the builder class. +After configuring the builder, call `build()` then `fetchNext()` to retrieve blocked users. @@ -239,3 +301,36 @@ override fun onError(e: CometChatException) { + +## Best Practices + + + + When blocking multiple users, use a single `blockUsers()` call with a list of UIDs instead of multiple individual calls. This reduces network overhead and improves performance. + + + The `onSuccess()` callback returns a HashMap with individual success/fail status for each UID. Always check the result map to handle cases where some users were blocked successfully while others failed. + + + After blocking a user, update your UI to reflect the change immediately. Remove blocked users from conversation lists and prevent message sending to blocked users on the client side for better UX. + + + +--- + +## Next Steps + + + + Fetch and filter user lists in your app + + + Learn about user objects and properties + + + Send messages to users and groups + + + Track user online/offline status + + diff --git a/sdk/android/call-logs.mdx b/sdk/android/call-logs.mdx index ef02868d2..d94021fd6 100644 --- a/sdk/android/call-logs.mdx +++ b/sdk/android/call-logs.mdx @@ -1,20 +1,35 @@ --- title: "Call Logs" +sidebarTitle: "Call Logs" +description: "Retrieve and display call history for users and groups using the Android SDK" --- + +```kotlin +// Build call log request +val callLogRequest = CallLogRequest.CallLogRequestBuilder() + .setAuthToken(CometChat.getUserAuthToken()) + .setLimit(50) + .setCallCategory(CometChatCallsConstants.CALL_CATEGORY_CALL) + .build() -## Overview - -CometChat's Android Call SDK provides a comprehensive way to integrate call logs into your application, enhancing your user experience by allowing users to effortlessly keep track of their communication history. Call logs provide crucial information such as call duration, participants, and more. +// Fetch call logs +callLogRequest.fetchNext(object : CometChatCalls.CallbackListener>() { + override fun onSuccess(callLogs: List) { /* Display logs */ } + override fun onError(e: CometChatException) { } +}) -This feature not only allows users to review their past interactions but it also serves as an effective tool to revisit important conversation details. With the flexibility of fetching call logs, filtering them according to specific parameters, and obtaining detailed information of individual calls, application developers can use this feature to build a more robust and interactive communication framework. +// Get specific call details +CometChatCalls.getCallDetails(sessionID, callback) +``` + -In the following sections, we will guide you through the process of working with call logs, offering a deeper insight into how to optimally use this feature in your Android application. +Call logs let you retrieve and display call history — who called whom, when, how long, and whether it was recorded. Use `CallLogRequestBuilder` to fetch and filter logs, and `getCallDetails()` to get details for a specific session. -## Fetching Call Logs +## Fetch Call Logs -To fetch all call logs in your Android application, you should use the `CallLogRequestBuilder`, This builder allows you to customize the call logs fetching process according to your needs. +To fetch call logs in your Android application, use the `CallLogRequestBuilder`. This builder allows you to customize the call log fetching process according to your needs. ```java CallLogRequest callLogRequest = new CallLogRequest.CallLogRequestBuilder() @@ -40,8 +55,31 @@ CallLogRequest callLogRequest = new CallLogRequest.CallLogRequestBuilder() ### Fetch Next -The**`fetchNext()`**method retrieves the next set of call logs. +The `fetchNext()` method retrieves the next set of call logs. + + + +```kotlin +val callLogRequest = CallLogRequest.CallLogRequestBuilder() + .setAuthToken(CometChat.getUserAuthToken()) + .setLimit(50) + .setCallCategory(CometChatCallsConstants.CALL_CATEGORY_CALL) + .build() + +callLogRequest.fetchNext(object : CometChatCalls.CallbackListener>() { + override fun onSuccess(callLogs: List) { + // Handle the fetched call logs + } + + override fun onError(e: CometChatException) { + // Handle the error + } +}) +``` + + + ```java CallLogRequest callLogRequest = new CallLogRequest.CallLogRequestBuilder() .setAuthToken(CometChat.getUserAuthToken()) @@ -62,10 +100,37 @@ callLogRequest.fetchNext(new CometChatCalls.CallbackListener>() { }); ``` + + + + ### Fetch Previous -The**`fetchPrevious()`**method retrieves the previous set of call logs. +The `fetchPrevious()` method retrieves the previous set of call logs. + + + +```kotlin +val callLogRequest = CallLogRequest.CallLogRequestBuilder() + .setAuthToken(CometChat.getUserAuthToken()) + .setLimit(50) + .setCallCategory(CometChatCallsConstants.CALL_CATEGORY_CALL) + .build() + +callLogRequest.fetchPrevious(object : CometChatCalls.CallbackListener>() { + override fun onSuccess(callLogs: List) { + // Handle the fetched call logs + } + override fun onError(e: CometChatException) { + // Handle the error + } +}) +``` + + + + ```java CallLogRequest callLogRequest = new CallLogRequest.CallLogRequestBuilder() .setAuthToken(CometChat.getUserAuthToken()) @@ -86,10 +151,32 @@ callLogRequest.fetchPrevious(new CometChatCalls.CallbackListener>( }); ``` + + + + ## Get Call Details -To retrieve the specific details of a call, use the**`getCallDetails()`**method. This method requires the Auth token of the logged-in user and the session ID along with a callback listener. +To retrieve the specific details of a call, use the `getCallDetails()` method. This method requires the session ID and a callback listener. + + +```kotlin +val sessionID = "SESSION_ID" +CometChatCalls.getCallDetails(sessionID, object : CometChatCalls.CallbackListener>() { + override fun onSuccess(callLogs: List) { + // Handle the fetched call details + } + + override fun onError(e: CometChatException) { + // Handle the error + } +}) +``` + + + + ```java String sessionID = "SESSION_ID"; CometChatCalls.getCallDetails(sessionID, new CometChatCalls.CallbackListener>() { @@ -105,4 +192,160 @@ CometChatCalls.getCallDetails(sessionID, new CometChatCalls.CallbackListener + + + +Returns a list of `CallLog` objects for the specified session. + +--- + +## Best Practices + + + + Use `setLimit()` to fetch call logs in manageable batches (e.g., 20-50 logs per request). This improves performance and reduces memory usage, especially for users with extensive call histories. + + + Use filters like `setCallStatus()`, `setCallType()`, and `setCallDirection()` to show relevant call logs to users. For example, show only missed calls in a "Missed Calls" tab. + + + Store fetched call logs in local storage (Room database or SharedPreferences) to provide offline access and reduce API calls. Refresh the cache periodically or when the app comes to the foreground. + + + Show call duration, status (missed, rejected, completed), and timestamps in your UI to help users quickly understand their call history. + + + +## Call Payload Structure + + + +The `Call` object extends `BaseMessage` and contains all call-related information: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | long | Unique identifier of the call message | +| `muid` | String | Developer-defined message ID | +| `sender` | [User](#user-object-call-logs) | User who sent the call message | +| `receiver` | AppEntity | Call receiver ([User](#user-object-call-logs) or [Group](#group-object-call-logs)) | +| `receiverUid` | String | Receiver's unique identifier | +| `type` | String | Call type. Values: `"audio"`, `"video"` | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `category` | String | Message category. Value: `"call"` | +| `sentAt` | long | Unix timestamp when call was sent | +| `deliveredAt` | long | Unix timestamp when delivered | +| `readAt` | long | Unix timestamp when read | +| `metadata` | JSONObject | Custom call metadata | +| `conversationId` | String | Associated conversation identifier | +| `sessionId` | String | Unique call session identifier | +| `callStatus` | String | Status of the call. Values: `"initiated"`, `"ongoing"`, `"ended"`, `"cancelled"`, `"busy"`, `"rejected"`, `"unanswered"` | +| `action` | String | Action performed on call. Values: `"initiated"`, `"ongoing"`, `"ended"` | +| `rawData` | String | Raw JSON data of the call | +| `initiatedAt` | long | Unix timestamp when call was initiated | +| `joinedAt` | long | Unix timestamp when call was joined | +| `callInitiator` | [User](#user-object-call-logs) | User who initiated the call | +| `callReceiver` | AppEntity | User or Group receiving the call | + +**Sample Call Object:** + +```json +{ + "id": 12345, + "muid": "call_abc123", + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online" + }, + "receiver": { + "uid": "user_456", + "name": "Jane Smith" + }, + "receiverUid": "user_456", + "type": "video", + "receiverType": "user", + "category": "call", + "sentAt": 1699900000, + "deliveredAt": 1699900001, + "readAt": 1699900002, + "metadata": {}, + "conversationId": "user_123_user_456", + "sessionId": "v1.us.1234567890", + "callStatus": "ended", + "action": "ended", + "rawData": "{}", + "initiatedAt": 1699900000, + "joinedAt": 1699900005, + "callInitiator": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png" + }, + "callReceiver": { + "uid": "user_456", + "name": "Jane Smith", + "avatar": "https://example.com/avatar2.png" + } +} +``` + + + + + +The nested `User` object (used in `sender`, `callInitiator`, `callReceiver`) contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + + + + + +When `receiverType` is `"group"`, the receiver/callReceiver contains a Group object: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `guid` | String | Unique identifier of the group | +| `name` | String | Display name of the group | +| `type` | String | Group type. Values: `"public"`, `"private"`, `"password"` | +| `icon` | String | URL to group icon | +| `description` | String | Group description | +| `owner` | String | UID of group owner | +| `membersCount` | int | Number of group members | + + + +--- + +## Next Steps + + + + Implement complete calling workflow with incoming/outgoing call UI + + + Start and manage call sessions with video and audio controls + + + Record calls and access recordings from the Dashboard + + + Access call logs via REST API for server-side integration + + diff --git a/sdk/android/calling-overview.mdx b/sdk/android/calling-overview.mdx index 19ef1c0f2..d8ccf2b1d 100644 --- a/sdk/android/calling-overview.mdx +++ b/sdk/android/calling-overview.mdx @@ -1,11 +1,21 @@ --- title: "Calling" sidebarTitle: "Overview" +description: "Implement voice and video calling in your Android application with CometChat" --- -## Overview + -CometChat provides voice and video calling capabilities for your Android application. This guide helps you choose the right implementation approach based on your use case. +Choose your calling implementation: +- **Default Calling** → [default-calling](/sdk/android/default-calling) - Complete ringing flow with UI +- **Direct Calling** → [direct-calling](/sdk/android/direct-calling) - Direct session management +- **Standalone Calling** → [standalone-calling](/sdk/android/standalone-calling) - Calls SDK without Chat SDK +- **Calling Setup** → [calling-setup](/sdk/android/calling-setup) - Install and initialize Calls SDK +- **Call Logs** → [call-logs](/sdk/android/call-logs) - Retrieve call history +- **Recording** → [recording](/sdk/android/recording) - Record call sessions + + +CometChat provides three ways to add voice and video calling to your Android app. Which one you pick depends on how much of the call flow you want CometChat to handle vs. building yourself. ## Prerequisites @@ -20,9 +30,7 @@ dependencies { For detailed setup instructions, see the [Calls SDK Setup](/sdk/android/calling-setup) guide. -## Choose Your Implementation - -CometChat offers three approaches to implement calling: +## Choose Your Approach @@ -36,40 +44,6 @@ CometChat offers three approaches to implement calling: -### Ringing - -Use this when you need a complete calling experience with: -- Incoming and outgoing call UI -- Call accept/reject/cancel functionality -- Call notifications via push notifications -- Integration with CometChat messaging - -**Flow:** Initiate call → Receiver gets notified → Accept/Reject → Start session - -[Get started with Ringing →](/sdk/android/default-calling) - -### Call Session - -Use this when you need to: -- Start a call session after the Ringing flow completes -- Implement custom call initiation logic with your own UI -- Join participants to a shared session using a session ID - -**Flow:** Generate token → Start session → Manage call → End session - -[Get started with Call Session →](/sdk/android/direct-calling) - -### Standalone Calling - -Use this when you want: -- Calling functionality without the Chat SDK -- Your own user authentication system -- Minimal SDK footprint - -**Flow:** Get auth token via REST API → Generate call token → Start session - -[Get started with Standalone Calling →](/sdk/android/standalone-calling) - ## Features @@ -89,3 +63,138 @@ Use this when you want: Configure automatic call termination when participants are inactive. + +--- + +## Call Payload Structure + + + +The `Call` object extends `BaseMessage` and contains all call-related information: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | long | Unique identifier of the call message | +| `muid` | String | Developer-defined message ID | +| `sender` | [User](#user-object-call) | User who sent the call message | +| `receiver` | AppEntity | Call receiver ([User](#user-object-call) or [Group](#group-object-call)) | +| `receiverUid` | String | Receiver's unique identifier | +| `type` | String | Call type. Values: `"audio"`, `"video"` | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `category` | String | Message category. Value: `"call"` | +| `sentAt` | long | Unix timestamp when call was sent | +| `deliveredAt` | long | Unix timestamp when delivered | +| `readAt` | long | Unix timestamp when read | +| `metadata` | JSONObject | Custom call metadata | +| `conversationId` | String | Associated conversation identifier | +| `sessionId` | String | Unique call session identifier | +| `callStatus` | String | Status of the call. Values: `"initiated"`, `"ongoing"`, `"ended"`, `"cancelled"`, `"busy"`, `"rejected"`, `"unanswered"` | +| `action` | String | Action performed on call. Values: `"initiated"`, `"ongoing"`, `"ended"` | +| `rawData` | String | Raw JSON data of the call | +| `initiatedAt` | long | Unix timestamp when call was initiated | +| `joinedAt` | long | Unix timestamp when call was joined | +| `callInitiator` | [User](#user-object-call) | User who initiated the call | +| `callReceiver` | AppEntity | User or Group receiving the call | + +**Sample Call Object:** + +```json +{ + "id": 12345, + "muid": "call_abc123", + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online" + }, + "receiver": { + "uid": "user_456", + "name": "Jane Smith" + }, + "receiverUid": "user_456", + "type": "video", + "receiverType": "user", + "category": "call", + "sentAt": 1699900000, + "deliveredAt": 1699900001, + "readAt": 1699900002, + "metadata": {}, + "conversationId": "user_123_user_456", + "sessionId": "v1.us.1234567890", + "callStatus": "ended", + "action": "ended", + "rawData": "{}", + "initiatedAt": 1699900000, + "joinedAt": 1699900005, + "callInitiator": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png" + }, + "callReceiver": { + "uid": "user_456", + "name": "Jane Smith", + "avatar": "https://example.com/avatar2.png" + } +} +``` + + + + + +The nested `User` object (used in `sender`, `callInitiator`, `callReceiver`) contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + + + + + +When `receiverType` is `"group"`, the receiver/callReceiver contains a Group object: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `guid` | String | Unique identifier of the group | +| `name` | String | Display name of the group | +| `type` | String | Group type. Values: `"public"`, `"private"`, `"password"` | +| `icon` | String | URL to group icon | +| `description` | String | Group description | +| `owner` | String | UID of group owner | +| `membersCount` | int | Number of group members | + + + +--- + +## Next Steps + + + + Install and initialize the CometChat Calls SDK + + + Implement complete calling flow with ringing UI + + + Start call sessions directly without ringing flow + + + Use Calls SDK without Chat SDK dependency + + diff --git a/sdk/android/calling-setup.mdx b/sdk/android/calling-setup.mdx index d7fbb4469..6ace23b53 100644 --- a/sdk/android/calling-setup.mdx +++ b/sdk/android/calling-setup.mdx @@ -1,23 +1,41 @@ --- title: "Setup" +sidebarTitle: "Setup" +description: "Install and initialize the CometChat Calls SDK for Android applications" --- + +```kotlin +// Add dependency to build.gradle +implementation 'com.cometchat:calls-sdk-android:4.3.3' -## Get your Application Keys +// Initialize (run once at app start) +val callAppSettings = CallAppSettingBuilder() + .setAppId("APP_ID") + .setRegion("REGION") + .build() + +CometChatCalls.init(context, callAppSettings, object : CometChatCalls.CallbackListener() { + override fun onSuccess(s: String?) { } + override fun onError(e: CometChatException) { } +}) +``` -[Signup for CometChat](https://app.cometchat.com/signup) and then: +**Required Credentials:** App ID, Region +**Get from:** [CometChat Dashboard](https://app.cometchat.com) → Your App → API & Auth Keys + -1. Create a new app -2. Head over to the API Keys section and note the Auth Key, App ID & Region - -Minimum Requirement -* Android API Level 21 -* Android API level 24 (in case you are using the calls SDKS) -* Androidx Compatibility +## Prerequisites + +Get your credentials from the [CometChat Dashboard](https://app.cometchat.com): +- App ID +- Region + +Minimum requirements: Android API Level 21 (API Level 24 with Calls SDK), AndroidX ## Add the CometChatCalls Dependency @@ -58,11 +76,19 @@ dependencies { ## Initialize CometChatCalls -The `init()` method initialises the settings required for CometChatCalls. The`init()`method takes the below parameters: +Call `CometChatCalls.init()` on app startup, after the Chat SDK has initialized (if you're using it). The method takes a `CallAppSettings` object built with `CallAppSettingBuilder`. + +### CallAppSettings Options -context - Your activity context callAppSettings - An object of the CallAppSettings class can be created using the CallAppSettingBuilder class. The appId and region field is mandatory and can be set using the `setAppId()` and `setRegion()` method. The `CallAppSettings` class allows you to configure three settings: +| Method | Description | +| --- | --- | +| `setAppId(appId)` | Your CometChat App ID. Required. | +| `setRegion(region)` | The region where your app was created. Required. | +| `setHost(host)` | Custom client URL for dedicated deployments. Optional. | -App ID: CometChat app ID. Region: The region where you app was created. Host(host: string): This method takes the client URL as input and uses this client URL instead of the default client URL. This can be used in case of dedicated deployment of CometChat. We suggest you call the `init()` method on activity onCreate() method. + +`CometChatCalls.init()` must be called before any other Calls SDK method. + @@ -126,3 +152,62 @@ CometChatCalls.init( | ----------------- | -------------------------------------- | | `context` | Android context for your application | | `callAppSettings` | An object of the CallAppSettings class | + +--- + +## Best Practices + + + + Call `CometChatCalls.init()` in your Application class or main activity's `onCreate()` method to ensure the SDK is ready before any calling features are accessed. + + + Double-check your App ID and Region values from the CometChat Dashboard before deploying to production. Incorrect values will cause initialization failures. + + + Always implement the `onError()` callback to catch initialization failures. Common issues include network connectivity problems or incorrect credentials. + + + +## Troubleshooting + + + + **Symptom:** `CometChatCalls.init()` fails with "Invalid App ID" or "Invalid Region" error. + + **Cause:** Incorrect App ID or Region value in initialization code. + + **Solution:** Verify your App ID and Region in the [CometChat Dashboard](https://app.cometchat.com) under API & Auth Keys. Ensure the region is exactly "us" or "eu". + + + **Symptom:** Gradle sync fails with "Could not resolve com.cometchat:calls-sdk-android" error. + + **Cause:** Maven repository URL not added to project-level build.gradle. + + **Solution:** Add the CometChat maven repository URL to the `allprojects` section in your project-level build.gradle file as shown in the setup instructions. + + + **Symptom:** Build fails with "Requires API level 24" error. + + **Cause:** Your app's minSdkVersion is below the required API level 24 for calls SDK. + + **Solution:** Update your app-level build.gradle to set `minSdkVersion 24` or higher. + + + +## Next Steps + + + + Implement one-on-one and group calling with default UI + + + Use Calls SDK without Chat SDK for calling-only apps + + + Initiate calls directly without user interaction + + + Customize the call UI appearance and layout + + diff --git a/sdk/android/changelog.mdx b/sdk/android/changelog.mdx index 2512bedbb..6a6ff2d48 100644 --- a/sdk/android/changelog.mdx +++ b/sdk/android/changelog.mdx @@ -1,4 +1,6 @@ --- title: "Changelog" +sidebarTitle: "Changelog" +description: "Release notes and version history for the CometChat Android SDK" url: "https://github.com/cometchat/chat-sdk-android/releases" --- \ No newline at end of file diff --git a/sdk/android/connection-behaviour.mdx b/sdk/android/connection-behaviour.mdx index 02a960181..b8701bb86 100644 --- a/sdk/android/connection-behaviour.mdx +++ b/sdk/android/connection-behaviour.mdx @@ -1,28 +1,43 @@ --- title: "Connection Behaviour" +sidebarTitle: "Connection Behaviour" +description: "Understand and configure WebSocket connection management in the CometChat Android SDK" --- + +```kotlin +// Auto mode (default) - SDK manages connection automatically +val appSettings = AppSettings.AppSettingsBuilder() + .setRegion("REGION") + .autoEstablishSocketConnection(true) // default + .build() + +// Manual mode - You control connection +val appSettings = AppSettings.AppSettingsBuilder() + .setRegion("REGION") + .autoEstablishSocketConnection(false) + .build() + +// Manual connection management +CometChat.connect(callback) // Establish connection +CometChat.disconnect(callback) // Break connection +CometChat.ping(callback) // Keep alive (call within 30 seconds) +``` -## Default SDK behaviour on login - -When the login method of the SDK is called, the SDK performs the below operations: +**Default:** Auto mode - SDK handles connection automatically +**Manual mode:** Requires explicit connect/disconnect calls + -1. Logs the user into the SDK -2. Saves the details of the logged in user locally. -3. Creates a web-socket connection for the logged in user. -This makes sure that the logged in user starts receiving real-time messages sent to him or any groups that he is a part of as soon as he logs in. -When the app is reopened, and the init() method is called, the web-socket connection to the server is established automatically. +## Default SDK Behaviour on Login -This is the default behaviour of the CometChat SDKs. However, if you wish to take control of the web-socket connection i.e if you wish to connect and disconnect to the web-socket server manually, you can refer to the Managing Web-socket Connection section. +On login, the SDK logs the user in, saves their details locally, and creates a WebSocket connection. When the app is reopened and `init()` is called, the WebSocket reconnects automatically. ## Auto Mode -CometChat SDK default connection behaviour is auto mode. Auto mode, the SDK automatically establishes and maintains the WebSocket connection. You do not need to explicitly call any methods to do this. - -To enable auto mode, you need to set the autoEstablishSocketConnection() method of AppSettings builder class to true. If you do not set this, the SDK will automatically apply the auto mode as the default behaviour for the WebSocket connection. +The default mode. The SDK automatically establishes and maintains the WebSocket connection. Set `autoEstablishSocketConnection(true)` (or omit it — auto mode is the default). @@ -39,13 +54,9 @@ If the app is in the foreground and there is no internet connection, the SDK wil ## Manual Mode -In manual mode, you have to explicitly establish and disconnect the WebSocket connection. To do this, you need to set the `autoEstablishSocketConnection()` method to false and then call the `CometChat.connect()` method to establish the connection and the `CometChat.disconnect()` method to disconnect the connection. - -By default, if manual mode is activated, the SDK will disconnect the WebSocket connection after 30 seconds if the app goes into the background. This means that the WebSocket connection will remain alive for 30 seconds after the app goes into the background, but it will be disconnected after that time if no pings are received. - -To keep the WebSocket connection alive even if your app goes in the background, you need to call the `CometChat.ping()` method from your app within 30 seconds. This method sends a ping message to the CometChat server, which tells the server that the app is still active. +Set `autoEstablishSocketConnection(false)` to take control of the WebSocket connection. Call `CometChat.connect()` to establish and `CometChat.disconnect()` to break it. -If you do not call the `CometChat.ping()` method within 30 seconds, the SDK will disconnect the WebSocket connection. This means that you will lose any messages that are sent to your app while it is in the background. +By default in manual mode, the SDK disconnects after 30 seconds in the background if no pings are received. Call `CometChat.ping()` within 30 seconds to keep the connection alive. @@ -56,13 +67,9 @@ If you do not call the `CometChat.ping()` method within 30 seconds, the SDK will | App in foreground | Call `CometChat.connect()` to create the WebSocket connection | | App in background | Disconnect the WebSocket connection if no ping is received within 30 seconds after the app goes in the background | -## Managing Manually - -The CometChat SDK also allows you to modify the above default behaviour of the SDK and take the control of the web-socket connection into your own hands. In order to achieve this, you need to follow the below steps: - ## Enable Manual Mode -While calling the init() function on the app startup, you need to inform the SDK that you will be managing the web socket connect. You can do so by using the `autoEstablishSocketConnection()` method provided by the `AppSettingsBuilder` class. This method takes a boolean value as an input. If set to true , the SDK will manage the web-socket connection internally based on the default behaviour mentioned above. If set to `false` , the web socket connection can will not be managed by the SDK and you will have to handle it manually. You can refer to the below code snippet for the same: +Set `autoEstablishSocketConnection(false)` during `init()` to take control of the WebSocket connection: @@ -92,36 +99,34 @@ CometChat.init(this, appId, appSettings, new CometChat.CallbackListener( ```kotlin -String appId = "YOUR_APP_ID"; -String region = "us"; +val appId = "YOUR_APP_ID" +val region = "us" -AppSettings appSettings = new AppSettings.AppSettingsBuilder() +val appSettings = AppSettings.AppSettingsBuilder() .setRegion(region) - .autoEstablishSocketConnection(false) //set it as false for manual mode - .build(); + .autoEstablishSocketConnection(false) // set to false for manual mode + .build() -CometChat.init(this, appId, appSettings, new CometChat.CallbackListener() { - @Override - public void onSuccess(String s) { - Log.d(TAG, "Init successful!"); +CometChat.init(this, appId, appSettings, object : CometChat.CallbackListener() { + override fun onSuccess(s: String) { + Log.d(TAG, "Init successful!") } - @Override - public void onError(CometChatException e) { - Log.d(TAG, "Error occurred : " + e.getMessage()); + override fun onError(e: CometChatException) { + Log.d(TAG, "Error occurred : " + e.message) } -}); +}) ``` -You can manage the connection to the web-socket server using the connect() , disconnect() and ping() methods provided by the SDK. +You can manage the connection to the WebSocket server using the `connect()`, `disconnect()`, and `ping()` methods provided by the SDK. -## Connect to the web-socket server +## Connect to the WebSocket Server -You need to use the `connect()` method provided by the `CometChat` class to establish the connection to the web-socket server. Please make sure that the user is logged in to the SDK before calling this method. You can use the CometChat.getLoggedInUser() method to check this. Once the connection is established, you will start receiving all the real-time events for the logged in user +Call `connect()` to establish the connection. Ensure the user is logged in first (`CometChat.getLoggedInUser()`). @@ -158,9 +163,9 @@ CometChat.connect(object : CallbackListener() { -## Disconnect from the web-socket server +## Disconnect from the WebSocket Server -You can use the `disconnect()` method provided by the `CometChat` class to break the established connection. Once the connection is broken, you will stop receiving all the real-time events for the logged in user. +Call `disconnect()` to break the connection. Real-time events stop until reconnected. @@ -197,15 +202,9 @@ CometChat.disconnect(object : CallbackListener() { -## Maintain long-standing background connection +## Maintain Long-Standing Background Connection - - -To ensure that the WebSocket connection is always alive, you can create a service or background service that calls the `CometChat.ping()` method in a loop. This will ensure that the ping message is sent to the server every 30 seconds, even if the app is not in the foreground. - - - -You can maintain a long-standing background connection event when your app is in the background, call the `CometChat.ping()` method within 30 seconds of your app entering the background or after the previous ping() call. +Call `CometChat.ping()` within 30 seconds of the app entering the background to keep the connection alive. @@ -242,4 +241,78 @@ CometChat.ping(object : CallbackListener() { -Reconnection If manual mode is enabled and the app is in the foreground, the SDK will automatically reconnect the WebSocket if the internet connection is lost. However, if the app is in the background and the WebSocket is disconnected or you called `CometChat.disconnect()`, then you will need to call the `CometChat.connect()` method to create a new WebSocket connection. + +In manual mode with the app in the foreground, the SDK auto-reconnects if the internet drops. If the app is in the background and the connection was disconnected, call `CometChat.connect()` to reconnect. + + +--- + +## Best Practices + + + + Auto mode is recommended for most applications as it handles connection management automatically. Only use manual mode if you have specific requirements for background connectivity or custom connection logic. + + + If using manual mode with background connectivity, implement a foreground service or WorkManager to call `CometChat.ping()` every 30 seconds. This ensures the connection stays alive when the app is backgrounded. + + + Implement connection listeners to track connection state changes and update your UI accordingly. Show connection status to users so they know when messages can be sent/received. + + + Thoroughly test your app's behavior when moving between foreground and background states. Verify messages are received correctly and the connection is managed as expected. + + + Maintaining a persistent background connection consumes battery. Balance real-time message delivery needs with battery efficiency. Consider using push notifications for background message delivery instead. + + + +## Troubleshooting + + + + **Symptom:** `CometChat.connect()` fails or times out in manual mode. + + **Cause:** User may not be logged in, or network connectivity issues. + + **Solution:** Verify the user is logged in using `CometChat.getLoggedInUser()` before calling `connect()`. Check network connectivity and ensure the SDK is initialized. Review error messages in the callback for specific issues. + + + **Symptom:** Real-time messages are not received when the app is in the background. + + **Cause:** In auto mode, the WebSocket disconnects immediately when the app is backgrounded. In manual mode, the connection times out after 30 seconds without a ping. + + **Solution:** For auto mode, this is expected behavior - use push notifications for background message delivery. For manual mode, implement a background service that calls `CometChat.ping()` every 30 seconds to keep the connection alive. + + + **Symptom:** WebSocket connection drops frequently even when the app is in the foreground. + + **Cause:** Network instability, server issues, or incorrect SDK configuration. + + **Solution:** The SDK automatically handles reconnection in auto mode. Check your network stability. Ensure you're not calling `disconnect()` unintentionally. Review SDK logs for specific error messages. + + + **Symptom:** `CometChat.ping()` doesn't keep the connection alive in manual mode. + + **Cause:** Ping is not being called frequently enough (must be within 30 seconds), or the connection was already disconnected. + + **Solution:** Ensure `ping()` is called at least every 30 seconds. Verify the connection is established before calling `ping()`. Implement a reliable background service or timer to call `ping()` consistently. + + + +## Next Steps + + + + Configure SDK initialization settings + + + Monitor SDK connection status changes + + + Handle user authentication events + + + Receive real-time messages and events + + diff --git a/sdk/android/connection-status.mdx b/sdk/android/connection-status.mdx index c56576884..e333d3a6a 100644 --- a/sdk/android/connection-status.mdx +++ b/sdk/android/connection-status.mdx @@ -1,23 +1,38 @@ --- title: "Connection Status" +sidebarTitle: "Connection Status" +description: "Monitor real-time connection status to CometChat web-socket servers in the Android SDK" --- + +```kotlin +// Add connection listener +CometChat.addConnectionListener("LISTENER_ID", object : ConnectionListener { + override fun onConnected() { /* Connection established */ } + override fun onConnecting() { /* Attempting to connect */ } + override fun onDisconnected() { /* Connection lost */ } + override fun onFeatureThrottled() { /* Features throttled */ } + override fun onError(e: CometChatException) { /* Connection error */ } +}) + +// Get current connection status +val status = CometChat.getConnectionStatus() +// Returns: WS_STATE_CONNECTED, WS_STATE_CONNECTING, WS_STATE_DISCONNECTED, or WS_STATE_FEATURE_THROTTLED + +// Remove listener +CometChat.removeConnectionListener("LISTENER_ID") +``` + -CometChat SDK provides you with a mechanism to get real-time status of the connection to CometChat web-socket servers. To achieve this you need to use the `ConnectionListener` class provided by the CometChat SDK - -Connection Status provides you with the below 3 methods to get the status of the connection to CometChat web-socket servers: - -| Delegate Method | Information | -| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------ | -| onConnecting | This method is triggered when CometChat SDK is trying to establish a connection to the web-socket server. | -| onConnected | This method is called when CometChat SDK has successfully established a connection and now is connected. | -| onDisconnected | This method is called when the CometChat SDK gets disconnected due to any issue while maintaining the connection like network fluctuations, etc. | -| onFeatureThrottled | CometChat automatically toggles off certain features to prevent performance loss for end-users under various circumstances | - -Once the connection is broken, the disconnected callback is triggered, the SDK automatically tries to establish the connection again, thus going into the connecting state and triggering the `connecting` method. Once the attempt to connect is successful, the `connected` method is triggered thus letting the developer know that the connection is established and is active. +Use `ConnectionListener` to monitor real-time WebSocket connection state. The SDK automatically attempts to reconnect when disconnected. -In order to use the ConnectionListeners, you need to add the ConnectionListeners using the `addConnectionListener` method provided by the SDK. You can add multiple listeners as shown below. Just make sure you add listeners with unique IDs. +| Callback | Description | +| --- | --- | +| `onConnecting` | SDK is attempting to establish a WebSocket connection | +| `onConnected` | Connection successfully established | +| `onDisconnected` | Connection lost (network fluctuation, etc.) | +| `onFeatureThrottled` | CometChat toggled off certain features to prevent performance loss | @@ -81,7 +96,9 @@ CometChat.addConnectionListener("UNIQUE_LISTENER_ID", object : ConnectionListene -You can also get the current connection status by using `getConnectionStatus` property provided by CometChat SDK +## Get Current Status + +Use `getConnectionStatus()` to check the current connection state at any time: @@ -100,18 +117,54 @@ val connectionStatus = CometChat.getConnectionStatus() -The above method will return either of the below 3 values: - -1. `CometChatConstants.WS_STATE_CONNECTED` (connected); - -2. `CometChatConstants.WS_STATE_CONNECTING`(connecting) +Returns one of: -3. `CometChatConstants.WS_STATE_DISCONNECTED`(disconnected) - -4. `CometChatConstants.WS_STATE_FEATURE_THROTTLED`(featureThrottled) +| Value | Description | +| --- | --- | +| `CometChatConstants.WS_STATE_CONNECTED` | Active connection | +| `CometChatConstants.WS_STATE_CONNECTING` | Attempting to connect | +| `CometChatConstants.WS_STATE_DISCONNECTED` | No connection | +| `CometChatConstants.WS_STATE_FEATURE_THROTTLED` | Feature throttled | Know more about CometChat SDK connection behaviour [click here](/sdk/android/connection-behaviour) + + +## Best Practices + + + + Display connection status indicators in your UI (e.g., "Connecting...", "Connected", "Offline") to inform users about network issues. Update the UI based on connection listener callbacks. + + + The SDK automatically attempts to reconnect when disconnected. Show a reconnecting indicator and avoid blocking the UI. Don't manually call `login()` again during reconnection. + + + Queue user actions (sending messages, making calls) when disconnected and execute them once the connection is restored. This provides a better user experience during network fluctuations. + + + Remove connection listeners in `onDestroy()` to prevent memory leaks. Use unique listener IDs to manage multiple listeners across different activities. + + + +--- + +## Next Steps + + + + Learn about SDK connection behavior and reconnection logic + + + Initialize the SDK and establish connection + + + Track user online/offline status + + + Register listeners for messages, users, groups, and calls + + diff --git a/sdk/android/create-group.mdx b/sdk/android/create-group.mdx index eecf3e26f..2fe780ca8 100644 --- a/sdk/android/create-group.mdx +++ b/sdk/android/create-group.mdx @@ -1,27 +1,35 @@ --- -title: "Create A Group" +title: "Create Group" +sidebarTitle: "Create Group" +description: "Create public, private, and password-protected groups using the Android SDK" --- + +```kotlin +// Create a public group +val group = Group("GUID", "Group Name", CometChatConstants.GROUP_TYPE_PUBLIC, "") +CometChat.createGroup(group, object : CometChat.CallbackListener() { + override fun onSuccess(group: Group) { } + override fun onError(e: CometChatException) { } +}) -## Create a Group - -*In other words, as a logged-in user, how do I create a public, private or password-protected group?* - -You can create a group using `createGroup()` method. This method takes a `Group` object as input. - -To create an object of `Group` class, you can use either of the below two constructors: - -1. `new Group(String GUID, String name, String groupType, String password)` -2. `new Group(String GUID, String name, String groupType, String password, String icon, String description)` - -The `groupType` needs to be either of the below 3 values: +// Create with members +val groupMembers = listOf( + GroupMember("uid1", CometChatConstants.SCOPE_ADMIN), + GroupMember("uid2", CometChatConstants.SCOPE_PARTICIPANT) +) +CometChat.createGroupWithMembers(group, groupMembers, emptyList(), callback) +``` + -1.`CometChatConstants.GROUP_TYPE_PUBLIC` (public) +## Create a Group -2.`CometChatConstants.GROUP_TYPE_PASSWORD` (password) +Use `createGroup()` with a `Group` object. The `groupType` must be one of: -3.`CometChatConstants.GROUP_TYPE_PRIVATE` (private) +1. `CometChatConstants.GROUP_TYPE_PUBLIC` +2. `CometChatConstants.GROUP_TYPE_PASSWORD` +3. `CometChatConstants.GROUP_TYPE_PRIVATE` @@ -76,30 +84,30 @@ The `createGroup()` method takes the following parameters: | --------- | ---------------------------- | | `group` | An instance of `Group` class | -After the successful creation of the group, you will receive an instance of `Group` class which contains all the information about the particular group. +After the successful creation of the group, you will receive an instance of the `Group` class which contains all the information about the particular group. -GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. +GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation, and other special characters are not allowed. ## Add members while creating a group -You can create a group and add members at the same time using the `createGroupWithMembers()` method. This method takes the `Group` Object, Array of `Group Member` Object to be added & Array of `UIDs` to be banned. +You can create a group and add members at the same time using the `createGroupWithMembers()` method. This method takes the `Group` object, an array of `GroupMember` objects to be added, and an array of `UIDs` to be banned. -To create an object of `Group` class, you can use either of the below two constructors: +To create an object of the `Group` class, you can use either of the following constructors: 1. `new Group(String GUID, String name, String groupType, String password)` 2. `new Group(String GUID, String name, String groupType, String password, String icon, String description)` -The `groupType` needs to be either of the below 3 values: +The `groupType` needs to be one of the following values: 1. `CometChat.GROUP_TYPE.PUBLIC` 2. `CometChat.GROUP_TYPE.PASSWORD` 3. `CometChat.GROUP_TYPE.PRIVATE` -To create an object of `Group Member` class, you can use the below constructor: +To create an object of the `GroupMember` class, you can use the following constructor: * `new GroupMember(String UID, String scope)` @@ -164,10 +172,10 @@ object : CreateGroupWithMembersListener() { -The `onSuccess()` block of this method will provide you with 2 set of info: +The `onSuccess()` block of this method provides you with 2 sets of information: -1. Group : The group object containing the information about the group that is created. -2. HashMap\ : A Hashmap that contains the key as the uid of the user that was supposed to be added and the value as success or error message. +1. `Group`: The group object containing information about the group that was created. +2. `HashMap`: A HashMap that contains the UID of the user that was supposed to be added as the key and `success` or an error message as the value. ## Group Class @@ -188,3 +196,108 @@ The `onSuccess()` block of this method will provide you with 2 set of info: | scope | Yes | Scope of the logged in user. Can be: 1. Admin 2. Moderator 3. Participant | | membersCount | No | The number of members in the groups | | tags | Yes | A list of tags to identify specific groups. | + +## Group Payload Structure + + + +The `Group` object returned by SDK methods contains the following fields: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `guid` | String | Unique identifier of the group | +| `name` | String | Display name of the group | +| `type` | String | Group type. Values: `"public"`, `"private"`, `"password"` | +| `password` | String | Password for protected groups (null for public/private groups) | +| `icon` | String | URL to group icon image | +| `description` | String | Description of the group | +| `owner` | String | UID of the group owner | +| `metadata` | JSONObject | Custom data set by developer. Can contain any key-value pairs | +| `createdAt` | long | Unix timestamp when group was created | +| `updatedAt` | long | Unix timestamp of last group update | +| `hasJoined` | boolean | Whether the logged-in user has joined this group | +| `joinedAt` | long | Unix timestamp when logged-in user joined the group | +| `scope` | String | Logged-in user's scope in group. Values: `"admin"`, `"moderator"`, `"participant"` | +| `membersCount` | int | Total number of members in the group | +| `tags` | Array\ | List of tags for group identification and filtering | +| `isBannedFromGroup` | boolean | Whether the logged-in user is banned from this group | + +**Sample Group Object:** + +```json +{ + "guid": "group_123", + "name": "Developers", + "type": "public", + "password": null, + "icon": "https://example.com/icon.png", + "description": "A group for developers", + "owner": "user_123", + "metadata": { + "category": "tech", + "isVerified": true + }, + "createdAt": 1699800000, + "updatedAt": 1699900000, + "hasJoined": true, + "joinedAt": 1699850000, + "scope": "admin", + "membersCount": 25, + "tags": ["official", "support"], + "isBannedFromGroup": false +} +``` + + + +## Best Practices + + + + Choose meaningful GUIDs and names that help identify groups. GUIDs should be alphanumeric with underscores/hyphens only - avoid spaces and special characters. + + + Use PUBLIC for open communities, PRIVATE for invite-only groups, and PASSWORD for semi-private groups. Choose based on your privacy requirements. + + + Use the metadata field to store custom group properties like categories, tags, or business-specific data without creating custom group types. + + + +## Troubleshooting + + + + **Symptom:** `createGroup()` fails with "Invalid GUID" error. + + **Cause:** GUID contains spaces, punctuation, or special characters other than underscore and hyphen. + + **Solution:** Use only alphanumeric characters, underscores, and hyphens in GUIDs. Example: `"my_group_123"` or `"group-abc-456"`. + + + **Symptom:** `createGroup()` fails with "Group already exists" error. + + **Cause:** A group with the same GUID already exists in your app. + + **Solution:** Use unique GUIDs for each group. Consider using UUIDs or timestamp-based identifiers to ensure uniqueness. + + + +--- + +## Next Steps + + + + Join existing groups to participate in conversations + + + Add members to your created groups + + + Modify group details and settings + + + Start sending messages in your group + + diff --git a/sdk/android/default-calling.mdx b/sdk/android/default-calling.mdx index 654dc8448..8f63dd515 100644 --- a/sdk/android/default-calling.mdx +++ b/sdk/android/default-calling.mdx @@ -1,10 +1,31 @@ --- title: "Ringing" +sidebarTitle: "Ringing" +description: "Implement complete calling workflow with ringing, call acceptance, rejection, and cancellation in the Android SDK" --- -## Overview + -This section explains how to implement a complete calling workflow with ringing functionality, including incoming/outgoing call UI, call acceptance, rejection, and cancellation. Previously known as **Default Calling**. +```kotlin +// Initiate call +val call = Call("UID", CometChatConstants.RECEIVER_TYPE_USER, CometChatConstants.CALL_TYPE_VIDEO) +CometChat.initiateCall(call, object : CometChat.CallbackListener() { + override fun onSuccess(call: Call) { /* Show outgoing UI */ } + override fun onError(e: CometChatException) { } +}) + +// Accept call +CometChat.acceptCall(sessionID, object : CometChat.CallbackListener() { + override fun onSuccess(call: Call) { /* Start session */ } + override fun onError(e: CometChatException) { } +}) + +// Reject/Cancel call +CometChat.rejectCall(sessionID, CometChatConstants.CALL_STATUS_REJECTED, callback) +``` + + +This page covers the complete calling workflow with ringing — incoming/outgoing call UI, accept/reject/cancel, and starting the call session. Previously known as **Default Calling**. After the call is accepted, you need to start the call session. See the [Call Session](/sdk/android/direct-calling#start-call-session) guide for details on starting and managing the actual call. @@ -203,6 +224,10 @@ CometChat.removeCallListener(listenerId) + +Always remove call listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + ### Events | Event | Description | @@ -561,3 +586,66 @@ CometChat.rejectCall(sessionID, status, object : CometChat.CallbackListener + + +## Best Practices + + + + Remove call listeners in `onDestroy()` or `onPause()` to prevent memory leaks and duplicate callbacks. Use unique listener IDs to manage multiple listeners effectively. + + + Implement all call listener callbacks (`onIncomingCallReceived`, `onOutgoingCallAccepted`, `onOutgoingCallRejected`, `onIncomingCallCancelled`) to provide a complete user experience and handle edge cases. + + + Store the `sessionId` from the `Call` object after initiating a call. You'll need it for accepting, rejecting, canceling, and ending the call. + + + Display different UI for incoming calls (accept/reject buttons), outgoing calls (cancel button), and active calls (end call button) to match user expectations. + + + +## Troubleshooting + + + + **Symptom:** `initiateCall()` fails with "User not logged in" or "Invalid receiver" error. + + **Cause:** User not logged in or invalid receiver UID/GUID. + + **Solution:** Ensure `CometChat.login()` is called successfully before initiating calls. Verify the receiver UID or GUID exists using `CometChat.getUser()` or `CometChat.getGroup()`. + + + **Symptom:** Call listener callbacks are not triggered when calls are received or accepted. + + **Cause:** Listener not registered or removed prematurely. + + **Solution:** Register the call listener in `onCreate()` or `onResume()` and ensure it's not removed until `onDestroy()` or `onPause()`. Verify the listener ID is unique. + + + **Symptom:** After accepting a call, the call session doesn't start or shows a black screen. + + **Cause:** Missing token generation or incorrect call settings. + + **Solution:** Ensure you call `generateToken()` and `startSession()` in the `acceptCall()` success callback. Verify the video container layout exists in your XML. See [Call Session](/sdk/android/direct-calling#start-call-session) for details. + + + +--- + +## Next Steps + + + + Start and manage call sessions with video and audio controls + + + Retrieve and display call history for users and groups + + + Record calls and access recordings from the Dashboard + + + Configure calling dependencies and initialization + + diff --git a/sdk/android/delete-conversation.mdx b/sdk/android/delete-conversation.mdx index 01b3924e8..2796008d4 100644 --- a/sdk/android/delete-conversation.mdx +++ b/sdk/android/delete-conversation.mdx @@ -1,12 +1,32 @@ --- -title: "Delete A Conversation" +title: "Delete Conversation" +sidebarTitle: "Delete Conversation" +description: "Delete user or group conversations using the CometChat Android SDK." --- + +```kotlin +// Delete user conversation +CometChat.deleteConversation("UID", CometChatConstants.RECEIVER_TYPE_USER, + object : CallbackListener() { + override fun onSuccess(message: String) { } + override fun onError(e: CometChatException) { } + }) + +// Delete group conversation +CometChat.deleteConversation("GUID", CometChatConstants.RECEIVER_TYPE_GROUP, callback) +``` + +**Note:** This deletes the conversation only for the logged-in user. To delete for all users, use the [REST API](https://api-explorer.cometchat.com/reference/deletes-conversation). + -In case you want to delete a conversation, you can use the `deleteConversation()` method. +Use `deleteConversation()` to delete a conversation for the logged-in user. -This method takes two parameters. The unique id (UID/GUID) of the conversation to be deleted & the type (user/group) of conversation to be deleted. +| Parameter | Description | Required | +| ---------------- | --------------------------------------------------------------------------------- | -------- | +| conversationWith | `UID` of the user or `GUID` of the group whose conversation you want to delete. | YES | +| conversationType | The type of conversation you want to delete . It can be either `user` or `group`. | YES | @@ -75,11 +95,66 @@ CometChat.deleteConversation(GUID, CometChatConstants.RECEIVER_TYPE_GROUP, objec -This method deletes the conversation only for the logged-in user. To delete a conversation for all the users of the conversation, please refer to our REST API documentation [here](https://api-explorer.cometchat.com/reference/deletes-conversation). +This deletes the conversation only for the logged-in user. To delete for all participants, use the [REST API](https://api-explorer.cometchat.com/reference/deletes-conversation). + +## Best Practices + + + + Prompt users to confirm before deleting conversations, especially for important chats, as this action removes the conversation from their list. + + + SDK deletion only removes the conversation for the logged-in user. Use REST API if you need to delete conversations for all participants. + + + Consider offering an "archive" feature using conversation tags instead of deletion, allowing users to hide conversations without losing them permanently. + + + Remove the conversation from your UI immediately after successful deletion to provide instant feedback to users. + + + +## Troubleshooting + + + + **Symptom:** `deleteConversation()` fails with "Conversation not found" error. + + **Cause:** The conversation doesn't exist or has already been deleted. + + **Solution:** Verify the UID/GUID is correct and the conversation exists by fetching it first using [retrieve-conversations](/sdk/android/retrieve-conversations). + + + **Symptom:** Delete fails with "Invalid receiver type" error. + + **Cause:** Receiver type doesn't match the conversation type. + + **Solution:** Use `RECEIVER_TYPE_USER` for user conversations and `RECEIVER_TYPE_GROUP` for group conversations. Check the conversation type before deleting. + + + **Symptom:** Deleted conversation reappears when new messages arrive. + + **Cause:** Deleting a conversation only removes it from the list. New messages create a new conversation entry. + + **Solution:** This is expected behavior. If you want to prevent messages, consider [blocking the user](/sdk/android/block-users) or leaving the group instead. + + -The `deleteConversation()` method takes the following parameters: +--- -| Parameter | Description | Required | -| ---------------- | --------------------------------------------------------------------------------- | -------- | -| conversationWith | `UID` of the user or `GUID` of the group whose conversation you want to delete. | YES | -| conversationType | The type of conversation you want to delete . It can be either `user` or `group`. | YES | +## Next Steps + + + + Fetch list of conversations + + + Block users to prevent messages + + + Leave group conversations + + + Delete individual messages + + diff --git a/sdk/android/delete-group.mdx b/sdk/android/delete-group.mdx index 36d3eb938..22c4e40f7 100644 --- a/sdk/android/delete-group.mdx +++ b/sdk/android/delete-group.mdx @@ -1,12 +1,25 @@ --- -title: "Delete A Group" +title: "Delete Group" +sidebarTitle: "Delete Group" +description: "Permanently delete groups as an admin using the Android SDK" --- + +```kotlin +// Delete a group (admin only) +CometChat.deleteGroup("GUID", object : CometChat.CallbackListener() { + override fun onSuccess(message: String) { } + override fun onError(e: CometChatException) { } +}) +``` + +**Note:** Only group admins can delete groups. This action is permanent and cannot be undone. + ## Delete Group -To delete a group you need to use the `deleteGroup()` method. The user must be an **Admin** of the group they are trying to delete. +Use `deleteGroup()` to permanently delete a group. Only group **Admins** can delete groups. @@ -49,6 +62,59 @@ CometChat.deleteGroup(GUID,object :CometChat.CallbackListener(){ The `deleteGroup()` method takes the following parameters: -| Parameter | Description | -| --------- | ---------------------------------------------- | -| `GUID` | The GUID of the group you would like to delete | +| Parameter | Description | +| --------- | ---------------------------------------- | +| `GUID` | The GUID of the group you want to delete | + + +## Best Practices + + + + Always prompt users to confirm deletion with a clear warning that this action is permanent and will remove all group messages and data. + + + Check the user's scope before showing delete options. Only admins can delete groups. + + + Consider sending a notification or message to group members before deleting the group to give them time to save important information. + + + +## Troubleshooting + + + + **Symptom:** `deleteGroup()` fails with "Insufficient permissions" error. + + **Cause:** User is not an admin of the group. + + **Solution:** Only group admins can delete groups. Check the user's scope using `group.getScope()` before attempting deletion. + + + **Symptom:** `deleteGroup()` fails with "Group not found" error. + + **Cause:** The group GUID is incorrect or the group has already been deleted. + + **Solution:** Verify the GUID is correct and the group still exists by fetching it first using [retrieve-groups](/sdk/android/retrieve-groups). + + + +--- + +## Next Steps + + + + Leave groups without deleting them + + + Modify group details instead of deleting + + + Transfer admin rights to another member + + + Create a new group + + diff --git a/sdk/android/delete-message.mdx b/sdk/android/delete-message.mdx index 082a8ba12..8a195aa2a 100644 --- a/sdk/android/delete-message.mdx +++ b/sdk/android/delete-message.mdx @@ -1,19 +1,37 @@ --- -title: "Delete A Message" +title: "Delete Message" +sidebarTitle: "Delete Message" +description: "Delete messages using the CometChat Android SDK." --- + +```kotlin +// Delete a message by ID +val messageId = 1234 + +CometChat.deleteMessage(messageId, object : CometChat.CallbackListener() { + override fun onSuccess(message: BaseMessage) { + // message.deletedAt contains deletion timestamp + } + override fun onError(e: CometChatException) { } +}) -While [deleting a message](/sdk/android/delete-message#delete-a-message) is straightforward, receiving events for deleted messages with CometChat has two parts: +// Listen for delete events +CometChat.addMessageListener("LISTENER_ID", object : CometChat.MessageListener() { + override fun onMessageDeleted(message: BaseMessage?) { } +}) +``` + -1. Adding a listener to receive [real-time message deletes](/sdk/android/delete-message#real-time-message-delete-events) when your app is running. -2. Calling a method to retrieve [missed message delete events](/sdk/android/delete-message#missed-message-delete-events)-me when your app was not running. +Deleting a message is straightforward. Receiving delete events has two parts: -## Delete a Message +1. Adding a listener for [real-time deletes](#real-time-message-delete-events) when your app is running +2. Fetching [missed deletes](#missed-message-delete-events) when your app was offline -*In other words, as a sender, how do I delete a message?* +## Delete a Message -In case you have to delete a message, you can use the `deleteMessage()` method. This method takes the message ID of the message to be deleted. +Use `deleteMessage()` with the message ID. @@ -54,6 +72,11 @@ CometChat.deleteMessage(messageId,object : CometChat.CallbackListener +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| deletedAt | `getDeletedAt()` | `long` | Timestamp when the message was deleted | +| deletedBy | `getDeletedBy()` | `String` | UID of the user who deleted the message | + Once the message is deleted, In the `onSuccess()` callback, you get an object of the `BaseMessage` class, with the `deletedAt` field set with the timestamp of the time the message was deleted. Also, the `deletedBy` field is set. These two fields can be used to identify if the message is deleted while iterating through a list of messages. By default, CometChat allows certain roles to delete a message. @@ -67,9 +90,7 @@ By default, CometChat allows certain roles to delete a message. ## Real-time Message Delete Events -*In other words, as a recipient, how do I know when someone deletes a message when my app is running?* - -In order to receive real-time events for a message being deleted, you need to override the `onMessageDeleted()` method of the `MessageListener` class. +Use `onMessageDeleted` in `MessageListener` to receive real-time delete events. @@ -97,11 +118,22 @@ CometChat.addMessageListener(listenerID, object : CometChat.MessageListener() { -## Missed Message Delete Events +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| deletedAt | `getDeletedAt()` | `long` | Timestamp when the message was deleted | +| deletedBy | `getDeletedBy()` | `String` | UID of the user who deleted the message | + + +Always remove listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. -*In other words, as a recipient, how do I know if someone deleted a message when my app was not running?* +```kotlin +CometChat.removeMessageListener("LISTENER_ID") +``` + -When you retrieve the list of previous messages, for the messages that were deleted, the `deletedAt` and the `deletedBy` fields will be set. Also, for example, if the total number of messages for a conversation are 100, and the message with message ID 50 was deleted. Now the message with ID 50 will have the `deletedAt` and the `deletedBy` fields set whenever it is pulled from the history. Also, the 101st message will be an `Action` message informing you that the message with ID 50 has been deleted. +## Missed Message Delete Events + +When fetching message history, deleted messages have `deletedAt` and `deletedBy` fields set. Additionally, an `Action` message is created when a message is deleted. For the message deleted event, in the `Action` object received, the following fields can help you get the relevant information- @@ -115,3 +147,64 @@ For the message deleted event, in the `Action` object received, the following fi In order to delete a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent. + +--- + +## Best Practices + + + + Check user roles before allowing deletions in your UI. Only message senders, group admins, and group moderators can delete messages to prevent unauthorized deletions. + + + Use the `deletedAt` and `deletedBy` fields to identify deleted messages when displaying message history. Consider showing a placeholder like "This message was deleted" instead of removing the message entirely. + + + When a message is deleted, the message object remains in history with `deletedAt` set. This preserves conversation flow and allows users to see that a message existed but was removed. + + + +## Troubleshooting + + + + **Symptom:** `deleteMessage()` fails with "Permission denied" error. + + **Cause:** User attempting to delete a message they didn't send, or lacking admin/moderator privileges in a group. + + **Solution:** Verify the logged-in user is either the message sender or has admin/moderator role in the group. Use `message.getSender().getUid()` to check message ownership before allowing deletion. + + + **Symptom:** `deleteMessage()` fails with "Message not found" error. + + **Cause:** The message ID provided doesn't exist or the message was already deleted. + + **Solution:** Verify the message ID is correct and the message exists. Check if `message.getDeletedAt()` is already set, indicating the message was previously deleted. + + + **Symptom:** Deleted messages continue to show in the message list. + + **Cause:** UI not properly handling the `onMessageDeleted()` callback or not checking the `deletedAt` field when rendering messages. + + **Solution:** Implement the `onMessageDeleted()` listener to update your UI in real-time. When fetching message history, check if `message.getDeletedAt()` is set and render accordingly. + + + +--- + +## Next Steps + + + + Modify text and custom messages after sending + + + Send text, media, and custom messages + + + Handle real-time message events with listeners + + + Understand message types and properties + + diff --git a/sdk/android/delivery-read-receipts.mdx b/sdk/android/delivery-read-receipts.mdx index 93f59de93..b5386abcc 100644 --- a/sdk/android/delivery-read-receipts.mdx +++ b/sdk/android/delivery-read-receipts.mdx @@ -1,14 +1,31 @@ --- title: "Delivery & Read Receipts" +sidebarTitle: "Delivery & Read Receipts" +description: "Mark messages as delivered, read, or unread and receive real-time receipt events using the CometChat Android SDK." --- + +```kotlin +// Mark messages as delivered +CometChat.markAsDelivered(message.id, message.receiverUid, + CometChatConstants.RECEIVER_TYPE_USER, message.sender.uid) + +// Mark messages as read +CometChat.markAsRead(message.id, message.sender.uid, + CometChatConstants.RECEIVER_TYPE_USER, message.sender.uid) + +// Listen for receipt events +CometChat.addMessageListener("LISTENER_ID", object : MessageListener() { + override fun onMessagesDelivered(receipt: MessageReceipt) { } + override fun onMessagesRead(receipt: MessageReceipt) { } +}) +``` + ## Mark Messages as Delivered -*In other words, as a recipient, how do I inform the sender that I've received a message?* - -You can mark the messages for a particular conversation as read using the `markAsDelivered()` method. This method takes the below parameters as input: +You can mark the messages for a particular conversation as read using the `markAsDelivered()` method. This method takes the following parameters as input: | Parameter | Information | | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -126,19 +143,19 @@ CometChat.markAsDelivered(message.id, receiverUID, CometChatConstants.RECEIVER_T -Another option the CometChat SDK provides is to pass the entire message object to the markAsDelivered() method. +Another option the CometChat SDK provides is to pass the entire message object to the `markAsDelivered()` method. ```java -CometChat.markAsRead(baseMessage) +CometChat.markAsDelivered(baseMessage) ``` ```kotlin -CometChat.markAsRead(baseMessage) +CometChat.markAsDelivered(baseMessage) ``` @@ -190,9 +207,7 @@ Starting v3, the messages will not be marked delivered internally by the SDK. Yo ## Mark Messages as Read -*In other words, as a recipient, how do I inform the sender I've read a message?* - -You can mark the messages for a particular conversation as read using the `markAsRead()` method. This method takes the below parameters as input: +You can mark the messages for a particular conversation as read using the `markAsRead()` method. This method takes the following parameters as input: | Parameter | Information | | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -380,8 +395,6 @@ The Mark as Unread feature allows users to designate specific messages or conver This feature is valuable for users who want to revisit and respond to important messages or conversations later, ensuring they don't forget or overlook them. -In other words, how I can mark a message as unread? - You can mark the messages for a particular conversation as unread using the `markAsUnread()` method. This method takes the below parameters as input: | Parameter | Information | @@ -466,10 +479,15 @@ CometChat.markAsUnread(message, object : CometChat.CallbackListener() { ## Receive Delivery & Read Receipts -*In other words, as a recipient, how do I know when a message I sent has been delivered or read by someone?* - ### Real-time events +| Callback | Description | +| --- | --- | +| `onMessagesDelivered` | Message delivered to a user | +| `onMessagesRead` | Message read by a user | +| `onMessagesDeliveredToAll` | Group message delivered to all members | +| `onMessagesReadByAll` | Group message read by all members | + 1. `onMessagesDelivered()` - This event is triggered when a message is delivered to a user. 2. `onMessagesRead()` - This event is triggered when a message is read by a user. 3. `onMessagesDeliveredToAll()` - This event is triggered when a group message is delivered to all members of the group. This event is only for Group conversations. @@ -602,3 +620,136 @@ The following features will be available only if the **Enhanced Messaging Status * `markAsUnread` method. + + +Always remove message listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## MessageReceipt Payload Structure + + + +The `MessageReceipt` object contains information about message delivery and read status: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `messageId` | long | ID of the message | +| `sender` | [User](#user-object-receipts) | User who sent the receipt | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `receiverId` | String | ID of the receiver | +| `timestamp` | long | Unix timestamp of the receipt | +| `receiptType` | String | Type of receipt. Values: `"delivered"`, `"read"` | +| `deliveredAt` | long | Unix timestamp when message was delivered | +| `readAt` | long | Unix timestamp when message was read | +| `messageSender` | String | UID of the message sender | + +**Sample MessageReceipt Object:** + +```json +{ + "messageId": 12345, + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default" + }, + "receiverType": "user", + "receiverId": "user_456", + "timestamp": 1699900000, + "receiptType": "read", + "deliveredAt": 1699900001, + "readAt": 1699900002, + "messageSender": "user_123" +} +``` + + + + + +The nested `User` object in `sender` contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + + + +## Best Practices + + + + Call `markAsDelivered()` when fetching messages and when receiving real-time messages to ensure senders know their messages were received. + + + Call `markAsRead()` only when messages are actually visible to the user (e.g., when chat window is open and focused) for accurate read status. + + + Use `markAsDelivered(message)` and `markAsRead(message)` overloads for simpler code when you have the message object available. + + + Listen for receipt events to update your UI with delivery/read indicators (checkmarks) in real-time. + + + Enable "Enhanced Messaging Status" in Dashboard for group message receipts and advanced features like `markAsUnread`. + + + +## Troubleshooting + + + + **Symptom:** `markAsDelivered()` or `markAsRead()` fails silently. + + **Cause:** User not logged in, or incorrect parameters provided. + + **Solution:** Verify user is logged in using `CometChat.getLoggedInUser()`. Use the callback version of methods to catch errors. + + + **Symptom:** `onMessagesDeliveredToAll` or `onMessagesReadByAll` not triggered. + + **Cause:** "Enhanced Messaging Status" feature not enabled in Dashboard. + + **Solution:** Enable the feature in [CometChat Dashboard](https://app.cometchat.com) under your app settings. + + + **Symptom:** Receipt listener methods not being called. + + **Cause:** Message listener not registered or removed prematurely. + + **Solution:** Ensure `addMessageListener()` is called before messages are sent/received. Don't remove listeners while still needed. + + + +--- + +## Next Steps + + + + Show when users are typing + + + Handle incoming messages with listeners + + + Fetch conversation list with unread counts + + + Learn more about event listeners + + diff --git a/sdk/android/direct-calling.mdx b/sdk/android/direct-calling.mdx index 1c5523339..fdeb94521 100644 --- a/sdk/android/direct-calling.mdx +++ b/sdk/android/direct-calling.mdx @@ -1,18 +1,35 @@ --- title: "Call Session" +sidebarTitle: "Call Session" +description: "Start and manage call sessions with video and audio controls in the Android SDK" --- -## Overview + -This section demonstrates how to start a call session in an Android application. Previously known as **Direct Calling**. +```kotlin +// Generate token +val sessionId = "SESSION_ID" +val userAuthToken = CometChat.getUserAuthToken() +CometChatCalls.generateToken(sessionId, userAuthToken, object : CometChatCalls.CallbackListener() { + override fun onSuccess(token: GenerateToken) { /* Start session */ } + override fun onError(e: CometChatException) { } +}) -Before you begin, we strongly recommend you read the [calling setup guide](/sdk/android/calling-setup). +// Start session +val callSettings = CometChatCalls.CallSettingsBuilder(context, videoContainer) + .setDefaultLayoutEnable(true) + .setIsAudioOnly(false) + .build() +CometChatCalls.startSession(token, callSettings, callback) - +// End session +CometChatCalls.endSession() +``` + -If you want to implement a complete calling experience with ringing functionality (incoming/outgoing call UI), follow the [Ringing](/sdk/android/default-calling) guide first. Once the call is accepted, return here to start the call session. +A call session is the active media connection between participants — camera, microphone, and the call UI. Whether you arrive here from the [Ringing flow](/sdk/android/default-calling), your own custom UI, or [Standalone Calling](/sdk/android/standalone-calling), this page covers how to manage the session itself. Previously known as **Direct Calling**. - +Before you begin, complete the [Calls SDK Setup](/sdk/android/calling-setup). ## Generate Call Token @@ -298,7 +315,9 @@ CometChatCalls.addCallsEventListeners(listenerId, object: CometChatCallsEventsLi - + +Always remove call event listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + ### Events @@ -538,7 +557,6 @@ CometChatCalls.setAudioMode(audioMode) ### Enter PIP Mode -### Enter PIP Mode Enters Picture-in-Picture mode, rendering the call view in a small floating window. This allows users to multitask while staying on the call. Ensure your app has PIP support enabled in the manifest. @@ -663,3 +681,76 @@ CometChatCalls.endSession() + + +## Best Practices + + + + Remove call event listeners in `onDestroy()` or `onPause()` to prevent memory leaks. Use unique listener IDs to manage multiple listeners across different activities or fragments. + + + Ensure the `RelativeLayout` for the video container exists in your XML layout before calling `startSession()`. The container must be visible and properly sized to display video streams. + + + Always implement error handling for `generateToken()`. Token generation can fail due to network issues or invalid session IDs. Inform users and provide retry options. + + + Set `setIsAudioOnly(true)` for audio-only calls to save bandwidth and improve performance. Use `startWithAudioMuted()` or `startWithVideoMuted()` when appropriate for user privacy. + + + Always call `endSession()` when leaving the call screen to release camera, microphone, and network resources. For ringing flow, call `CometChat.endCall()` first to notify participants. + + + +## Troubleshooting + + + + **Symptom:** `generateToken()` fails with "Invalid session ID" or "Unauthorized" error. + + **Cause:** Invalid session ID or user not logged in. + + **Solution:** Verify the session ID is correct (from `Call` object in ringing flow or generated randomly for standalone). Ensure `CometChat.login()` was called successfully and `getUserAuthToken()` returns a valid token. + + + **Symptom:** Call starts but shows a black screen instead of video. + + **Cause:** Video container not properly configured or camera permissions denied. + + **Solution:** Verify the `RelativeLayout` exists in your XML and is visible. Check that camera permissions are granted in `AndroidManifest.xml` and at runtime. Ensure `setIsAudioOnly(false)` is set for video calls. + + + **Symptom:** `startSession()` fails or doesn't trigger any callbacks. + + **Cause:** Invalid call token or missing calling setup. + + **Solution:** Ensure you've completed the [Calling Setup](/sdk/android/calling-setup) including adding the Calls SDK dependency. Verify the `GenerateToken` object is valid and not expired. Check that the activity context is valid. + + + **Symptom:** Video works but no audio is transmitted or received. + + **Cause:** Microphone permissions denied or audio muted. + + **Solution:** Check that microphone permissions are granted. Verify `startWithAudioMuted(false)` is set. Test audio output device using `setAudioMode()` to switch between speaker and earpiece. + + + +--- + +## Next Steps + + + + Implement complete calling workflow with incoming/outgoing call UI + + + Retrieve and display call history for users and groups + + + Record calls and access recordings from the Dashboard + + + Customize video tile layouts and appearance + + diff --git a/sdk/android/edit-message.mdx b/sdk/android/edit-message.mdx index 9385d0b71..f843e6b6e 100644 --- a/sdk/android/edit-message.mdx +++ b/sdk/android/edit-message.mdx @@ -1,19 +1,35 @@ --- -title: "Edit A Message" +title: "Edit Message" +sidebarTitle: "Edit Message" +description: "Edit text and custom messages using the CometChat Android SDK." --- + +```kotlin +// Edit a text message +val updatedMessage = TextMessage( + message.receiverUid, + "Updated text", + message.receiverType +) +updatedMessage.id = message.id -While [editing a message](/sdk/android/edit-message#edit-a-message) is straightforward, receiving events for edited messages with CometChat has two parts: +CometChat.editMessage(updatedMessage, object: CometChat.CallbackListener() { + override fun onSuccess(message: BaseMessage) { } + override fun onError(e: CometChatException) { } +}) -1. Adding a listener to receive [real-time message edit events](/sdk/android/edit-message#real-time-message-edit-events) when your app is running -2. Calling a method to retrieve [missed message edit events](/sdk/android/edit-message#missed-message-edit-events) when your app was not running +// Listen for edit events +CometChat.addMessageListener("LISTENER_ID", object : CometChat.MessageListener() { + override fun onMessageEdited(message: BaseMessage) { } +}) +``` + ## Edit a Message -*In other words, as a sender, how do I edit a message?* - -In order to edit a message, you can use the `editMessage()` method. This method takes an object of the `BaseMessage` class. At the moment, you are only allowed to edit `TextMessage` and `CustomMessage`. Thus, the `BaseMessage` object must either be a Text or a Custom Message. +Use `editMessage()` with a `TextMessage` or `CustomMessage` object. Set the message ID using `setId()`. ### Add/Update Tags @@ -40,9 +56,7 @@ textMessage.setTags(tags) -\` - -Once the message object is ready, you can use the `editMessage()` method and pass the message object to it. +Once the message object is ready, call `editMessage()`. @@ -89,6 +103,11 @@ CometChat.editMessage(updatedMessage, object: CometChat.CallbackListener @@ -130,11 +147,22 @@ CometChat.addMessageListener(listenerID, object : CometChat.MessageListener() { -## Missed Message Edit Events + +Always remove listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. -*In other words, as a recipient, how do I know when someone edited their message when my app was not running?* +```kotlin +CometChat.removeMessageListener("LISTENER_ID") +``` + + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| editedAt | `getEditedAt()` | `long` | Timestamp when the message was edited | +| editedBy | `getEditedBy()` | `String` | UID of the user who edited the message | -When you retrieve the list of previous messages, for the message that was edited, the `editedAt` and the `editedBy` fields will be set. Also, for example, if the total number of messages for a conversation is 100, and the message with message ID 50 was edited. Now the message with ID 50 will have the `editedAt` and the `editedBy` fields set whenever it is pulled from the history. Also, the 101st message will be an `Action` message informing you that the message with ID 50 has been edited.. +## Missed Message Edit Events + +When fetching message history, edited messages have `editedAt` and `editedBy` fields set. Additionally, an `Action` message is created when a message is edited. For the message edited event, in the `Action` object received, the following fields can help you get the relevant information- @@ -148,3 +176,64 @@ For the message edited event, in the `Action` object received, the following fie In order to edit a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent. + +--- + +## Best Practices + + + + Always set the message ID from the original message using `setId()` before calling `editMessage()`. Without the correct ID, the edit operation will fail. + + + Tags set during editing completely replace the original tags. If you want to preserve existing tags, retrieve them first and merge with new tags before calling `setTags()`. + + + Check user roles before allowing edits in your UI. Only message senders, group owners, and group moderators can edit messages to prevent unauthorized modifications. + + + +## Troubleshooting + + + + **Symptom:** `editMessage()` fails with "Permission denied" error. + + **Cause:** User attempting to edit a message they didn't send, or lacking admin/moderator privileges in a group. + + **Solution:** Verify the logged-in user is either the message sender or has admin/moderator role in the group. Use `message.getSender().getUid()` to check message ownership. + + + **Symptom:** `editMessage()` fails with "Invalid message ID" error. + + **Cause:** The message ID was not set on the updated message object before calling `editMessage()`. + + **Solution:** Always call `updatedMessage.setId(originalMessage.getId())` before editing. The ID is required to identify which message to update. + + + **Symptom:** Attempting to edit a media message fails. + + **Cause:** CometChat only supports editing `TextMessage` and `CustomMessage` types. Media messages cannot be edited. + + **Solution:** Check the message type before allowing edits. Only enable edit functionality for text and custom messages in your UI. + + + +--- + +## Next Steps + + + + Remove messages from conversations + + + Send text, media, and custom messages + + + Handle real-time message events with listeners + + + Understand message types and properties + + diff --git a/sdk/android/error-codes.mdx b/sdk/android/error-codes.mdx new file mode 100644 index 000000000..ba925eb37 --- /dev/null +++ b/sdk/android/error-codes.mdx @@ -0,0 +1,274 @@ +--- +title: "Error Codes" +sidebarTitle: "Error Codes" +description: "Complete reference for CometChat Android SDK error codes, including client-side validation errors, server-side API errors, and how to handle them." +--- + + + + + +```kotlin +// All errors are CometChatException objects +CometChat.login(uid, authKey, object : CometChat.CallbackListener() { + override fun onSuccess(user: User?) { } + override fun onError(e: CometChatException?) { + Log.e(TAG, "Code: ${e?.code}") // e.g., "AUTH_ERR_AUTH_TOKEN_NOT_FOUND" + Log.e(TAG, "Message: ${e?.message}") // Human-readable description + Log.e(TAG, "Details: ${e?.details}") // Additional context (if available) + } +}) +``` + + +```java +CometChat.login(uid, authKey, new CometChat.CallbackListener() { + @Override + public void onSuccess(User user) { } + + @Override + public void onError(CometChatException e) { + Log.e(TAG, "Code: " + e.getCode()); // e.g., "AUTH_ERR_AUTH_TOKEN_NOT_FOUND" + Log.e(TAG, "Message: " + e.getMessage()); // Human-readable description + Log.e(TAG, "Details: " + e.getDetails()); // Additional context (if available) + } +}); +``` + + + +**Error categories:** Initialization, Login, Calling, Messages, Groups, Users, Conversations, Receipts, AI, Extensions + + +Every error returned by the CometChat Android SDK is a `CometChatException` object. Access its properties via getters in Java or properties in Kotlin: + +| Property | Type | Description | +|----------|------|-------------| +| `code` | `String` | Machine-readable error code | +| `message` | `String` | Human-readable description | +| `details` | `String` | Additional context or troubleshooting info | + + + +```kotlin +CometChat.login(authToken, object : CometChat.CallbackListener() { + override fun onSuccess(user: User?) { + // proceed + } + override fun onError(e: CometChatException?) { + when (e?.code) { + "AUTH_ERR_AUTH_TOKEN_NOT_FOUND" -> { + // Token is invalid or expired — prompt re-login + } + "MISSING_PARAMETERS" -> { + // A required parameter was not provided + } + else -> { + Log.e(TAG, "Unexpected error: ${e?.message}") + } + } + } +}) +``` + + +```java +CometChat.login(authToken, new CometChat.CallbackListener() { + @Override + public void onSuccess(User user) { + // proceed + } + + @Override + public void onError(CometChatException e) { + switch (e.getCode()) { + case "AUTH_ERR_AUTH_TOKEN_NOT_FOUND": + // Token is invalid or expired — prompt re-login + break; + case "MISSING_PARAMETERS": + // A required parameter was not provided + break; + default: + Log.e(TAG, "Unexpected error: " + e.getMessage()); + } + } +}); +``` + + + +## Initialization Errors + +| Code | Message | +|------|---------| +| `MISSING_PARAMETERS` | AppID cannot be empty. Please specify a valid appID. | + +## Login & Authentication Errors + +| Code | Message | +|------|---------| +| `COMETCHAT_INITIALIZATION_NOT_DONE` | Please initialize CometChat before using the login method. | +| `USER_NOT_AUTHORISED` | The authToken of the user is not authorised. Please verify again. | +| `AUTH_ERR_AUTH_TOKEN_NOT_FOUND` | The auth token does not exist. Please make sure you are logged in and have a valid auth token. | +| `LOGIN_IN_PROGRESS` | Please wait until the previous login request ends. | +| `WS_CONNECTION_FAIL` | WebSocket connection failed. | +| `WS_CONNECTION_FALLBACK_FAIL` | WebSocket connection fallback failed. | +| `WS_AUTH_FAIL` | WebSocket username/password not correct. | +| `NO_INTERNET_CONNECTION` | You do not have an internet connection. | +| `USER_NOT_LOGGED_IN` | Please log in to CometChat before calling this method. | + +## Calling Errors + +| Code | Message | +|------|---------| +| `CALL_ALREADY_INITIATED` | There is already a call in progress. | +| `CALL_IN_PROGRESS` | There is already a call in progress. | +| `NOT_INITIALIZED` | Please call CometChat.init() before calling any other methods. | +| `NOT_LOGGED_IN` | Please login before starting a call. | +| `SESSION_ID_REQUIRED` | Please make sure you are passing a correct session ID. | +| `CALL_SETTINGS_REQUIRED` | Please make sure you are passing the call settings object. | +| `JWT_NOT_FOUND` | There was an issue while fetching JWT from API. | + +## Message Errors + +| Code | Message | +|------|---------| +| `INVALID_RECEIVER_TYPE` | Receiver type can be `user` or `group`. | +| `REQUEST_IN_PROGRESS` | Request in progress. | +| `NOT_ENOUGH_PARAMETERS` | Timestamp, MessageId, or updatedAfter is required to use fetchNext(). | +| `INVALID_REASON_ID` | Invalid reasonId provided. | + +## User Errors + +| Code | Message | +|------|---------| +| `INVALID_STATUS_VALUE` | The status parameter accepts only `online` or `offline`. | +| `INVALID_DIRECTION_VALUE` | The direction parameter accepts only `both`, `blockedByMe`, or `hasBlockedMe`. | +| `EMPTY_USERS_LIST` | The users list needs to have at least one UID. | + +## Group Errors + +| Code | Message | +|------|---------| +| `NOT_A_GROUP` | Please use the Group class to construct a new group. | +| `INVALID_SCOPE_VALUE` | Scope can be `admin`, `moderator`, or `participant`. | +| `INVALID_GROUP_TYPE` | Group type can be `public`, `private`, `protected`, or `password`. | +| `ERR_EMPTY_GROUP_PASS` | Password is mandatory to join a group. | + +## Conversation Errors + +| Code | Message | +|------|---------| +| `INVALID_CONVERSATION_TYPE` | Conversation type can be `user` or `group`. | +| `CONVERSATION_NOT_FOUND` | Conversation not found. Check the value of conversationWith and conversationType. | + +## Receipt Errors + +| Code | Message | +|------|---------| +| `MISSING_PARAMETERS` | Expected 4 parameters, received 3. | +| `NO_WEBSOCKET_CONNECTION` | Connection to WebSocket server is broken. Please retry after some time. | +| `RECEIPTS_TEMPORARILY_BLOCKED` | Due to high load, receipts have been blocked for your app. | +| `UNKNOWN_ERROR_OCCURRED` | Unknown error occurred while marking a message as read. | + +## AI Feature Errors + +| Code | Message | +|------|---------| +| `NO_CONVERSATION_STARTER` | Unable to get conversation starter for this conversation. | +| `NO_SMART_REPLY` | Unable to get smart reply for this conversation. | +| `NO_CONVERSATION_SUMMARY` | Unable to get summary of the conversation. | +| `EMPTY_RESPONSE` | Unable to get a suggestion. | +| `ERROR_INVALID_AI_FEATURE` | The provided AI Feature cannot be null or empty. | + +## Extension Errors + +| Code | Message | +|------|---------| +| `ERROR_INVALID_EXTENSION` | The provided extension cannot be null or empty. | +| `ERROR_EXTENSION_NOT_FOUND` | The provided extension could not be found. | + +## Feature Restriction Errors + +| Code | Message | +|------|---------| +| `ERROR_INVALID_FEATURE` | The provided feature cannot be null or empty. | +| `ERROR_FEATURE_NOT_FOUND` | The provided feature could not be found. | + +## Network & API Errors + +| Code | Message | +|------|---------| +| `FAILED_TO_FETCH` | There is an unknown issue with the API request. Check your internet connection. | +| `TOO_MANY_REQUEST` | Too many requests. Wait before sending the next request. | +| `ERR_TOO_MANY_REQUESTS` | Rate limiting. See [Rate Limits](/sdk/android/rate-limits). | + +## Validation Errors + +These errors use dynamic codes based on the parameter name (e.g., `INVALID_UID`, `UID_IS_COMPULSORY`): + +| Pattern | Message | +|---------|---------| +| `INVALID_{param}` | The parameter should be a string / number / boolean / object / array. | +| `{param}_IS_COMPULSORY` | The parameter cannot be blank. Please provide a valid value. | +| `{param}_NOT_PROVIDED` | Please provide the required parameter. | +| `ERROR_{param}_EXCEEDED` | Limit exceeded max limit. | +| `INVALID_SEARCH_KEYWORD` | Invalid search keyword. Please provide a valid search keyword. | +| `MISSING_KEY` | The key is missing from the object. | + +## Prosody (WebSocket Server) Errors + +| Code | Message | +|------|---------| +| `ERROR_INVALID_SESSIONID` | The provided sessionId cannot be null or empty. | +| `ERROR_INVALID_TYPE` | The provided type cannot be null or empty. | +| `ERROR_INVALID_GROUPLIST` | Grouplist cannot be null or empty. | + +## General Errors + +| Code | Message | +|------|---------| +| `ERROR_IO_EXCEPTION` | I/O exception occurred. | +| `ERROR_JSON_EXCEPTION` | JSON parsing exception. | +| `ERROR_PASSWORD_MISSING` | Password is mandatory for a password group. | +| `ERROR_LIMIT_EXCEEDED` | Limit exceeded max limit. | +| `ERROR_INVALID_GUID` | Please provide a valid GUID. | +| `ERR_SETTINGS_HASH_OUTDATED` | Settings hash is outdated. | +| `ERR_NO_AUTH` | No authentication credentials found. | + +## Server-Side API Errors + +For REST API error codes (returned by the CometChat backend), see the [Error Guide](/articles/error-guide). Common server-side errors you may encounter in SDK responses: + +| Code | Description | +|------|-------------| +| `AUTH_ERR_EMPTY_APPID` | Empty App ID in headers | +| `AUTH_ERR_INVALID_APPID` | Invalid App ID or does not exist in region | +| `ERR_UID_NOT_FOUND` | User does not exist or is soft deleted | +| `ERR_GUID_NOT_FOUND` | Group does not exist | +| `ERR_NOT_A_MEMBER` | User is not a member of the group | +| `ERR_ALREADY_JOINED` | User has already joined the group | +| `ERR_MESSAGE_ID_NOT_FOUND` | Message does not exist | +| `ERR_PLAN_RESTRICTION` | Feature not available with current plan | +| `ERR_TOO_MANY_REQUESTS` | Rate limit exceeded | + +See the full list in the [Error Guide](/articles/error-guide). + +--- + +## Next Steps + + + + Common issues and solutions + + + Understand and handle rate limits + + + Complete server-side error code reference + + + Recommended patterns for error handling + + diff --git a/sdk/android/extensions-overview.mdx b/sdk/android/extensions-overview.mdx index 29c1774bd..b9b79ce04 100644 --- a/sdk/android/extensions-overview.mdx +++ b/sdk/android/extensions-overview.mdx @@ -1,4 +1,43 @@ --- title: "Extensions" +sidebarTitle: "Extensions" +description: "Enhance your Android app with CometChat extensions for moderation, translation, and more" url: "/fundamentals/extensions-overview" ---- \ No newline at end of file +--- + + + +CometChat Extensions add powerful features to your chat application: +- **Content Moderation** - Filter inappropriate content automatically +- **Message Translation** - Translate messages in real-time +- **Sentiment Analysis** - Analyze message sentiment +- **Profanity Filter** - Block offensive language +- **Image Moderation** - Scan images for inappropriate content +- **Thumbnail Generation** - Auto-generate image thumbnails +- **Link Preview** - Display rich previews for URLs + +**Learn more:** [Extensions Overview](/fundamentals/extensions-overview) + + +Extensions are server-side features that enhance your CometChat implementation without requiring additional client-side code. They are configured through the [CometChat Dashboard](https://app.cometchat.com) and work automatically once enabled. + +For detailed information about available extensions, configuration options, and implementation guides, visit the [Extensions Overview](/fundamentals/extensions-overview) in the Fundamentals section. + +--- + +## Next Steps + + + + Explore all available CometChat extensions + + + Implement AI-powered content moderation + + + Configure extensions in your dashboard + + + Set up server-side event notifications + + \ No newline at end of file diff --git a/sdk/android/flag-message.mdx b/sdk/android/flag-message.mdx index 0843d2902..18e84ef0f 100644 --- a/sdk/android/flag-message.mdx +++ b/sdk/android/flag-message.mdx @@ -1,7 +1,32 @@ --- title: "Flag Message" +sidebarTitle: "Flag Message" +description: "Report inappropriate messages to moderators using the Android SDK flagging system" --- + + +```kotlin +// Get available flag reasons +CometChat.getFlagReasons(object : CometChat.CallbackListener>() { + override fun onSuccess(reasons: MutableList?) { + // Display reasons to user + } + override fun onError(e: CometChatException) { } +}) + +// Flag a message +val flagDetail = FlagDetail().apply { + reasonId = "spam" + remark = "Promotional content" +} +CometChat.flagMessage(messageId, flagDetail, object : CometChat.CallbackListener() { + override fun onSuccess(response: String?) { } + override fun onError(e: CometChatException?) { } +}) +``` + + ## Overview Flagging messages allows users to report inappropriate content to moderators or administrators. When a message is flagged, it appears in the [CometChat Dashboard](https://app.cometchat.com) under **Moderation > Flagged Messages** for review. @@ -225,3 +250,65 @@ Here's a complete implementation showing how to build a report message flow: ``` + +## Best Practices + + + + Fetch flag reasons once during app initialization and cache them locally. This reduces API calls and provides instant access when users need to report messages. + + + Display flag reasons in a user-friendly dialog or bottom sheet. Include the optional remark field to allow users to provide additional context about why they're reporting the message. + + + Show a confirmation dialog before submitting the flag to prevent accidental reports. Explain to users that flagged messages will be reviewed by moderators. + + + If a user tries to flag the same message multiple times, handle the error gracefully and inform them the message has already been reported. + + + +## Troubleshooting + + + + **Symptom:** `getFlagReasons()` or `flagMessage()` fails with "Moderation is not enabled" error. + + **Cause:** Moderation feature is not enabled for your app in the CometChat Dashboard. + + **Solution:** Enable moderation in the [CometChat Dashboard](https://app.cometchat.com) under **Moderation > Settings**. Configure flag reasons under **Advanced Settings**. + + + **Symptom:** `flagMessage()` fails with "Invalid reason ID" error. + + **Cause:** The reasonId provided doesn't match any configured flag reasons. + + **Solution:** Always use reason IDs from the `getFlagReasons()` response. Verify the reason ID exists in your Dashboard configuration. + + + **Symptom:** `flagMessage()` fails with "Message already flagged" error. + + **Cause:** The user has already flagged this message previously. + + **Solution:** Inform the user that they've already reported this message. Consider disabling the flag button for previously flagged messages. + + + +--- + +## Next Steps + + + + Review and manage flagged messages in the Dashboard + + + Automatically detect and filter inappropriate content + + + Allow users to block other users from contacting them + + + Delete messages from conversations + + diff --git a/sdk/android/group-add-members.mdx b/sdk/android/group-add-members.mdx index a135b4615..244c4a5e4 100644 --- a/sdk/android/group-add-members.mdx +++ b/sdk/android/group-add-members.mdx @@ -1,17 +1,31 @@ --- -title: "Add Members To A Group" +title: "Add Members" +sidebarTitle: "Add Members" +description: "Add members to groups with specific roles using the Android SDK" --- + +```kotlin +// Add members to a group +val members = listOf( + GroupMember("uid1", CometChatConstants.SCOPE_MODERATOR), + GroupMember("uid2", CometChatConstants.SCOPE_PARTICIPANT) +) + +CometChat.addMembersToGroup("GUID", members, null, + object : CallbackListener>() { + override fun onSuccess(result: HashMap) { } + override fun onError(e: CometChatException) { } + }) +``` -## Add Members to Group +**Note:** Only admins and moderators can add members. The result HashMap contains UID as key and "success" or error message as value. + -You can add members to the group using the `addMembersToGroup()` method. This method takes the below parameters: +## Add Members to Group -1. `GUID` - GUID of the group users are to be added to. -2. `List members` - This is a list of `GroupMember` objects. In order to add members, you need to create an object of the `GroupMember` class. The `UID` and the scope of the `GroupMember` are mandatory. -3. `List bannedMembers` - This is the list of `UIDs` that need to be banned from the group. This can be set to `null` if there are no members to be banned. -4. Callback +Use `addMembersToGroup()` with the group GUID, a list of `GroupMember` objects, and an optional list of UIDs to ban. @@ -60,18 +74,12 @@ In the `onSuccess()` method of the `CallbackListener`, you will receive a HashMa ## Real-Time Group Member Added Events -*In other words, as a member of a group, how do I know when someone is added to the group when my app is running?* +When a member is added to a group, existing members receive a real-time event in `onMemberAddedToGroup()` of the `GroupListener` class. - -When a group member is added by another member, this event is triggered. When a user joins a group on their own, the joined event is triggered. - +When a member is added by another user, `onMemberAddedToGroup()` fires. When a user joins on their own, `onGroupMemberJoined()` fires instead. -To receive real-time events whenever a new member is added to a group, you need to implement the `onMemberAddedToGroup()` methods of the `GroupListener` class. - -* `onMemberAddedToGroup()` - This method is triggered when any user is added to the group so that the logged-in user is informed of the other members added to the group. - ```java @@ -100,13 +108,126 @@ CometChat.addGroupListener("UNIQUE_ID", object : CometChat.GroupListener() { ## Member Added to Group event in Message History -*In other words, as a member of a group, how do I know when someone is added to the group when my app is not running?* - -When you retrieve the list of previous messages if a member has been added to any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. - -For the group member added event, in the `Action` object received, the following fields can help you get the relevant information- +When fetching message history, if a member was added to a group the logged-in user is part of, the list will contain an `Action` message with these fields: 1. `action` - `added` 2. `actionOn` - User object containing the details of the user who was added to the group 3. `actionBy` - User object containing the details of the user who added the member to the group 4. `actionFor` - Group object containing the details of the group to which the member was added + + +Always remove group listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## GroupMember Payload Structure + + + +The `GroupMember` object extends `User` and contains all User fields plus group-specific fields: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | +| `scope` | String | Member's scope in the group. Values: `"admin"`, `"moderator"`, `"participant"` | +| `joinedAt` | long | Unix timestamp when member joined the group | + +**Sample GroupMember Object:** + +```json +{ + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "link": "https://example.com/profile", + "role": "default", + "metadata": { + "department": "Engineering", + "title": "Senior Developer" + }, + "status": "online", + "statusMessage": "Available", + "lastActiveAt": 1699900000, + "hasBlockedMe": false, + "blockedByMe": false, + "tags": ["premium", "verified"], + "deactivatedAt": 0, + "scope": "admin", + "joinedAt": 1699850000 +} +``` + + + +## Best Practices + + + + Choose member scopes carefully: ADMIN for full control, MODERATOR for moderation tasks, PARTICIPANT for regular members. Limit admin count to maintain control. + + + Add multiple members in a single call instead of multiple individual calls to improve performance and reduce API overhead. + + + Check the result HashMap for each UID - some additions may succeed while others fail. Handle each case appropriately in your UI. + + + Verify user UIDs exist before attempting to add them to avoid unnecessary API errors. + + + +## Troubleshooting + + + + **Symptom:** `addMembersToGroup()` fails with "Insufficient permissions" error. + + **Cause:** User is not an admin or moderator of the group. + + **Solution:** Only admins and moderators can add members. Check the user's scope before showing add member options. + + + **Symptom:** Result HashMap shows errors for some UIDs. + + **Cause:** UIDs may not exist, users may already be members, or users may be banned. + + **Solution:** Check the error message for each UID in the result HashMap. Verify UIDs exist and users aren't already members or banned. + + + **Symptom:** Adding members to private group fails. + + **Cause:** Private groups have restricted membership. + + **Solution:** Ensure you have admin or moderator permissions. Private groups require explicit member additions by authorized users. + + + +--- + +## Next Steps + + + + Remove members from groups + + + Update member roles and permissions + + + Fetch list of group members + + + Handle real-time group events + + diff --git a/sdk/android/group-change-member-scope.mdx b/sdk/android/group-change-member-scope.mdx index 13303f53b..057bc3443 100644 --- a/sdk/android/group-change-member-scope.mdx +++ b/sdk/android/group-change-member-scope.mdx @@ -1,12 +1,34 @@ --- title: "Change Member Scope" +sidebarTitle: "Change Scope" +description: "Update group member roles and permissions using the Android SDK" --- + +```kotlin +// Change member scope (admin only) +CometChat.updateGroupMemberScope( + "UID", + "GUID", + CometChatConstants.SCOPE_MODERATOR, + object : CallbackListener() { + override fun onSuccess(message: String) { } + override fun onError(e: CometChatException) { } + }) +``` + +**Available Scopes:** +- `SCOPE_ADMIN` - Full control over group +- `SCOPE_MODERATOR` - Can moderate members and content +- `SCOPE_PARTICIPANT` - Regular member (default) + +**Note:** Only group admins can change member scopes. + ## Change Scope of a Group Member -In order to change the scope of a group member, you can use the `changeGroupMemberScope()`. +Use `updateGroupMemberScope()` to change a member's role. Only group **Admins** can change scopes. @@ -63,9 +85,7 @@ The default scope of any member is `participant`. Only the **Admin** of the grou ## Real-Time Group Member Scope Changed Events -*In other words, as a member of a group, how do I know when someone's scope is changed when my app is running?* - -In order to receive real-time events whenever a group member's scope changes, you will need to override the `onGroupMemberScopeChanged()` method of the `GroupListener` class +When a member's scope changes, group members receive a real-time event in `onGroupMemberScopeChanged()` of the `GroupListener` class. @@ -95,11 +115,7 @@ CometChat.addGroupListener("ListenerID", object : CometChat.GroupListener() { ## Missed Group Member Scope Changed Events -*In other words, as a member of a group, how do I know when someone's scope is changed when my app is not running?* - -When you retrieve the list of previous messages if a member's scope has been changed for any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. - -For the group member scope changed event, in the `Action` object received, the following fields can help you get the relevant information- +When fetching message history, scope changes appear as `Action` messages with these fields: 1. `action` - `scopeChanged` 2. `actionOn` - User object containing the details of the user whos scope has been changed @@ -107,3 +123,69 @@ For the group member scope changed event, in the `Action` object received, the f 4. `actionFor` - Group object containing the details of the group in which the member scope was changed 5. `oldScope` - The original scope of the member 6. `newScope` - The updated scope of the member + + +Always remove group listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## Best Practices + + + + Keep the number of admins small (2-3) to maintain clear ownership and control. Too many admins can lead to conflicts and management issues. + + + Use the moderator role for trusted members who help manage the group without full admin privileges. This provides a middle tier of authority. + + + Prompt admins to confirm before changing member scopes, especially when promoting to admin or demoting from admin/moderator roles. + + + Listen for scope change events to maintain an audit log of permission changes for security and accountability. + + + +## Troubleshooting + + + + **Symptom:** `updateGroupMemberScope()` fails with "Insufficient permissions" error. + + **Cause:** User is not an admin of the group. + + **Solution:** Only group admins can change member scopes. Moderators cannot change scopes. Check the user's scope before showing this option. + + + **Symptom:** Admin cannot change their own scope. + + **Cause:** Admins cannot demote themselves to prevent accidental loss of admin access. + + **Solution:** Have another admin change your scope, or use [transfer-group-ownership](/sdk/android/transfer-group-ownership) if you want to step down. + + + **Symptom:** Scope change fails with "Invalid scope" error. + + **Cause:** Scope value is not one of the valid constants. + + **Solution:** Use only `CometChatConstants.SCOPE_ADMIN`, `SCOPE_MODERATOR`, or `SCOPE_PARTICIPANT`. + + + +--- + +## Next Steps + + + + Add new members with specific scopes + + + Remove members from groups + + + Transfer group ownership to another admin + + + Fetch list of group members + + diff --git a/sdk/android/group-kick-member.mdx b/sdk/android/group-kick-member.mdx index fbbd7cb5c..ab68edbdc 100644 --- a/sdk/android/group-kick-member.mdx +++ b/sdk/android/group-kick-member.mdx @@ -1,17 +1,29 @@ --- -title: "Ban/Kick Member From A Group" +title: "Kick/Ban Member" +sidebarTitle: "Kick Member" +description: "Kick, ban, and unban group members using the Android SDK" --- + +```kotlin +// Kick a member (can rejoin) +CometChat.kickGroupMember("UID", "GUID", object : CallbackListener() { + override fun onSuccess(message: String) { } + override fun onError(e: CometChatException) { } +}) -There are certain actions that can be performed on the group members: +// Ban a member (cannot rejoin without unban) +CometChat.banGroupMember("UID", "GUID", callback) + +// Unban a member +CometChat.unbanGroupMember("UID", "GUID", callback) +``` -1. Kick a member from the group -2. Ban a member from the group -3. Unban a member from the group -4. Update the scope of the member of the group +**Note:** Only admins and moderators can kick, ban, or unban members. + -All of the above actions can only be performed by the **Admin** or the **Moderator** of the group. +Admins and moderators can kick, ban, or unban group members. Kicked users can rejoin; banned users cannot until unbanned. ## Kick a Group Member @@ -171,15 +183,11 @@ The unbanned user can now rejoin the group. ## Get List of Banned Members for a Group -In order to fetch the list of banned groups members for a group, you can use the `BannedGroupMembersRequest` class. To use this class i.e to create an object of the BannedGroupMembersRequest class, you need to use the `BannedGroupMembersRequestBuilder` class. The `BannedGroupMembersRequestBuilder` class allows you to set the parameters based on which the banned group members are to be fetched. - -The `BannedGroupMembersRequestBuilder` class allows you to set the below parameters: - -The `GUID` of the group for which the banned members are to be fetched must be specified in the constructor of the `GroupMembersRequestBuilder` class. +Use `BannedGroupMembersRequestBuilder` with the group GUID to fetch banned members. ### Set Limit -This method sets the limit i.e. the number of banned members that should be fetched in a single iteration. +Set the number of banned members to fetch per request. @@ -204,7 +212,7 @@ val bannedGroupMembersRequest = BannedGroupMembersRequestBuilder(GUID) ### Set Search Keyword -This method allows you to set the search string based on which the banned group members are to be fetched. +Filter banned members by a search string. @@ -227,9 +235,9 @@ val bannedGroupMembersRequest = BannedGroupMembersRequestBuilder(GUID) -Finally, once all the parameters are set to the builder class, you need to call the `build()` method to get the object of the `BannedGroupMembersRequest` class. +### Fetch Banned Members -Once you have the object of the `BannedGroupMembersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `GroupMember` objects containing n number of banned members where n is the limit set in the builder class. +After configuring the builder, call `build()` then `fetchNext()` to retrieve banned members. @@ -278,13 +286,7 @@ bannedGroupMembersRequest?.fetchNext(object :CometChat.CallbackListener @@ -327,11 +329,7 @@ CometChat.addGroupListener(MainActivity::class.java.simpleName, object : GroupLi ## Missed Group Member Kicked/Banned Events -*In other words, as a member of a group, how do I know when someone is banned/kicked when my app is not running?* - -When you retrieve the list of previous messages if a member has been kicked/banned/unbanned from any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. - -For group member kicked event, the details can be obtained using the below fields of the `Action` class- +When fetching message history, kick/ban/unban actions appear as `Action` messages with these fields: 1. `action` - `kicked` 2. `actionBy` - User object containing the details of the user who has kicked the member @@ -351,3 +349,69 @@ For group member unbanned event, the details can be obtained using the below fie 2. `actionBy` - User object containing the details of the user who has unbanned the member 3. `actionOn` - User object containing the details of the member that has been unbanned 4. `actionFor` - Group object containing the details of the Group from which the member was unbanned + + +Always remove group listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## Best Practices + + + + Use kick for temporary removal (user can rejoin). Use ban for permanent removal (requires unban to rejoin). Choose based on severity of violation. + + + Always prompt admins/moderators to confirm before kicking or banning members, as these actions immediately remove users from the group. + + + Store ban reasons in your database or use metadata to track why members were banned for future reference and appeals. + + + Use `BannedGroupMembersRequest` to review banned members list and consider unbanning users after appropriate time periods. + + + +## Troubleshooting + + + + **Symptom:** Operation fails with "Insufficient permissions" error. + + **Cause:** User is not an admin or moderator of the group. + + **Solution:** Only admins and moderators can kick or ban members. Check the user's scope before showing these options. + + + **Symptom:** Operation fails when trying to kick or ban an admin. + + **Cause:** Admins have elevated permissions and cannot be kicked or banned by moderators. + + **Solution:** Only group owners or other admins can remove admin members. Consider changing their scope first using [group-change-member-scope](/sdk/android/group-change-member-scope). + + + **Symptom:** `unbanGroupMember()` fails with "User not banned" error. + + **Cause:** The user is not currently banned from the group. + + **Solution:** Fetch the banned members list first using `BannedGroupMembersRequest` to verify the user is banned. + + + +--- + +## Next Steps + + + + Add new members to groups + + + Update member roles and permissions + + + Fetch list of group members + + + Handle real-time group events + + diff --git a/sdk/android/groups-overview.mdx b/sdk/android/groups-overview.mdx index 99e877026..ff74d1727 100644 --- a/sdk/android/groups-overview.mdx +++ b/sdk/android/groups-overview.mdx @@ -1,10 +1,52 @@ --- title: "Groups" sidebarTitle: "Overview" +description: "Manage group creation, membership, and permissions in the Android SDK" --- + +Choose your path: +- **Create Groups** → [create-group](/sdk/android/create-group) - Public, private, password-protected groups +- **Join Groups** → [join-group](/sdk/android/join-group) - Participate in group conversations +- **Retrieve Groups** → [retrieve-groups](/sdk/android/retrieve-groups) - Fetch groups list +- **Manage Members** → [group-add-members](/sdk/android/group-add-members) - Add, remove, update members + -Groups help your users to converse together in a single space. You can have three types of groups- private, public and password protected. +Groups let your users converse together in a single space. CometChat supports three group types — public, private, and password-protected — with three member roles: admin, moderator, and participant. -Each group includes three kinds of users- admin, moderator, member. +## Features + +| Feature | Description | Guide | +| --- | --- | --- | +| Create Group | Create public, private, or password-protected groups | [Create Group](/sdk/android/create-group) | +| Retrieve Groups | Fetch groups list with filters and search | [Retrieve Groups](/sdk/android/retrieve-groups) | +| Join Group | Join groups to participate in conversations | [Join Group](/sdk/android/join-group) | +| Leave Group | Leave groups to stop receiving updates | [Leave Group](/sdk/android/leave-group) | +| Delete Group | Permanently delete groups (admin only) | [Delete Group](/sdk/android/delete-group) | +| Update Group | Modify group details and settings | [Update Group](/sdk/android/update-group) | +| Retrieve Members | Fetch group members with filters | [Retrieve Members](/sdk/android/retrieve-group-members) | +| Add Members | Add users to groups with specific roles | [Add Members](/sdk/android/group-add-members) | +| Kick/Ban Members | Remove or ban members from groups | [Kick Member](/sdk/android/group-kick-member) | +| Change Scope | Update member roles and permissions | [Change Scope](/sdk/android/group-change-member-scope) | +| Transfer Ownership | Transfer group ownership to another member | [Transfer Ownership](/sdk/android/transfer-group-ownership) | + + +--- + +## Next Steps + + + + Create public, private, or password-protected groups + + + Join existing groups to participate in conversations + + + Fetch and filter groups list + + + Add, remove, and update group members + + diff --git a/sdk/android/interactive-messages.mdx b/sdk/android/interactive-messages.mdx index d6f468c0e..f54a7b14e 100644 --- a/sdk/android/interactive-messages.mdx +++ b/sdk/android/interactive-messages.mdx @@ -1,12 +1,38 @@ --- title: "Interactive Messages" +sidebarTitle: "Interactive Messages" +description: "Send and receive interactive messages with embedded forms, buttons, and other UI elements using the CometChat Android SDK." --- + +```kotlin +// Create interactive message with form +val interactiveData = JSONObject().apply { + put("title", "Demo Form") + put("formFields", JSONArray().apply { + put(JSONObject().apply { + put("elementType", "textInput") + put("elementId", "element1") + put("label", "Name") + }) + }) +} + +val interactiveMessage = InteractiveMessage("receiverId", CometChatConstants.RECEIVER_TYPE_USER, "form", interactiveData) +CometChat.sendInteractiveMessage(interactiveMessage, object : CallbackListener() { + override fun onSuccess(message: InteractiveMessage) { } + override fun onError(e: CometChatException) { } +}) + +// Mark as interacted +CometChat.markAsInteracted(messageId, "elementId", callback) +``` + An `InteractiveMessage` is a specialized object that encapsulates an interactive unit within a chat message, such as an embedded form that users can fill out directly within the chat interface. This enhances user engagement by making the chat experience more interactive and responsive to user input. -### InteractiveMessage +## InteractiveMessage `InteractiveMessage` is a chat message with embedded interactive content. It can contain various properties: @@ -19,37 +45,18 @@ An `InteractiveMessage` is a specialized object that encapsulates an interactive | `allowSenderInteraction` | A boolean determining whether the message sender can interact with the message. Default is set to false. | | | `interactionGoal` | An `InteractionGoal` object encapsulating the intended outcome of interacting with the `InteractiveMessage`. Default is set to none. | | -### Interaction +## Interaction An Interaction represents a user action involved with an `InteractiveMessage`. It includes: * `elementId`: An identifier for a specific interactive element. * `interactedAt`: A timestamp indicating when the interaction occurred. -### Mark as Interacted +## Mark as Interacted This method marks a message as interacted by identifying it with the provided Id. it also logs the interactive element associated with the interaction. - -```java -int messageId = 1; -String elementId = "elementId"; -CometChat.markAsInteracted(messageId, elementId, new CometChat.CallbackListener() { - @Override - public void onSuccess(Void unused) { - - } - - @Override - public void onError(CometChatException e) { - - } -}); -``` - - - ```kotlin val messageId = 1 @@ -65,12 +72,27 @@ CometChat.markAsInteracted(messageId, elementId, object : CometChat.CallbackList } }) ``` - + +```java +int messageId = 1; +String elementId = "elementId"; +CometChat.markAsInteracted(messageId, elementId, new CometChat.CallbackListener() { + @Override + public void onSuccess(Void unused) { + + } + + @Override + public void onError(CometChatException e) { + } +}); +``` + -### Goal Completion +## Goal Completion A key feature of `InteractiveMessage` is checking whether a user's interactions with the message meet the defined `InteractionGoal` @@ -83,20 +105,63 @@ You would be tracking every interaction users perform on an `InteractiveMessage` | All of Specific Interactions | The goal is completed when all specified interactions occur. | CometChatConstants.INTERACTION\_TYPE\_ALL\_OF | | None | The goal is never completed. | CometChatConstants.INTERACTION\_TYPE\_NONE | -### InteractionGoal +## InteractionGoal The `InteractionGoal` represents the desired outcome of an interaction with an InteractiveMessage. It includes: * `elementIds`: A list of identifiers for the interactive elements. * `type`: The type of interaction goal from the `CometChatConstants`. -### Sending InteractiveMessages +## Send an Interactive Message The `InteractiveMessage` can be sent using the `sendInteractiveMessage` method of the CometChat class. The method requires an `InteractiveMessage` object and a `CallbackListener` for handling the response. +Before sending interactive messages, ensure you have [initialized the SDK](/sdk/android/setup) and [logged in a user](/sdk/android/authentication-overview). + Here is an example of how to use it: + +```kotlin +val interactiveData = JSONObject() +try { + interactiveData.put("title", "Demo Form") + val jsonArray = JSONArray() + val textInput = JSONObject() + textInput.put("elementType", "textInput") + textInput.put("elementId", "element1") + textInput.put("label", "Name") + textInput.put("optional", false) + textInput.put("maxLines", 1) + jsonArray.put(textInput) + interactiveData.put("formFields", jsonArray) + val submitElement = JSONObject() + submitElement.put("elementType", "button") + submitElement.put("elementId", "element8") + submitElement.put("buttonText", "Submit") + submitElement.put("disableAfterInteracted", true) + val action = JSONObject() + action.put("actionType", "urlNavigation") + action.put("url", "https://www.cometchat.com/") + submitElement.put("action", action) + interactiveData.put("submitElement", submitElement) +} catch (e: JSONException) { + throw RuntimeException(e) +} + +val interactiveMessage = InteractiveMessage(receiverId, receiverType, "form", interactiveData) + +CometChat.sendInteractiveMessage(interactiveMessage, object : CometChat.CallbackListener() { + override fun onSuccess(interactiveMessage: InteractiveMessage) { + // This block is executed when the InteractiveMessage is sent successfully. + } + + override fun onError(e: CometChatException) { + // This block is executed if an error occurs while sending the InteractiveMessage. + } +}) +``` + ```java JSONObject interactiveData = new JSONObject(); @@ -139,63 +204,28 @@ CometChat.sendInteractiveMessage(interactiveMessage, new CometChat.CallbackListe } }); ``` - - - -```kotlin -val interactiveData = JSONObject() -try { - interactiveData.put("title", "Demo Form") - val jsonArray = JSONArray() - val textInput = JSONObject() - textInput.put("elementType", "textInput") - textInput.put("elementId", "element1") - textInput.put("label", "Name") - textInput.put("optional", false) - textInput.put("maxLines", 1) - jsonArray.put(textInput) - interactiveData.put("formFields", jsonArray) - val submitElement = JSONObject() - submitElement.put("elementType", "button") - submitElement.put("elementId", "element8") - submitElement.put("buttonText", "Submit") - submitElement.put("disableAfterInteracted", true) - val action = JSONObject() - action.put("actionType", "urlNavigation") - action.put("url", "https://www.cometchat.com/") - submitElement.put("action", action) - interactiveData.put("submitElement", submitElement) -} catch (e: JSONException) { - throw RuntimeException(e) -} - -val interactiveMessage = InteractiveMessage(receiverId, receiverType, "form", interactiveData) - -CometChat.sendInteractiveMessage(interactiveMessage, object : CometChat.CallbackListener() { - override fun onSuccess(interactiveMessage: InteractiveMessage) { - // This block is executed when the InteractiveMessage is sent successfully. - } - - override fun onError(e: CometChatException) { - // This block is executed if an error occurs while sending the InteractiveMessage. - } -}) -``` - - - -### Event Listeners +## Real-time Events -CometChat SDK provides event listeners to handle real-time events related to `InteractiveMessage`. +CometChat SDK provides event listeners to handle real-time events related to `InteractiveMessage`. For more details on listener management, see [Real-Time Listeners](/sdk/android/real-time-listeners). On `InteractiveMessage` Received The `onInteractiveMessageReceived` event listener is triggered when an InteractiveMessage is received. Here is an example: + +```kotlin +CometChat.addMessageListener("UNIQUE_ID", object : CometChat.MessageListener() { + override fun onInteractiveMessageReceived(interactiveMessage: InteractiveMessage) { + // This block is executed when an InteractiveMessage is received. + // Here you can define logic to handle the received InteractiveMessage and display it in your chat interface. + } +}) +``` + ```java CometChat.addMessageListener("UNIQUE_ID", new CometChat.MessageListener() { @@ -206,30 +236,30 @@ CometChat.addMessageListener("UNIQUE_ID", new CometChat.MessageListener() { } }); ``` - - - -```kotlin -CometChat.addMessageListener("UNIQUE_ID", object : CometChat.MessageListener() { - override fun onInteractiveMessageReceived(interactiveMessage: InteractiveMessage) { - // This block is executed when an InteractiveMessage is received. - // Here you can define logic to handle the received InteractiveMessage and display it in your chat interface. - } -}) -``` - - - -### On Interaction Goal Completed + +Always remove listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## On Interaction Goal Completed The `onInteractionGoalCompleted` event listener is invoked when an interaction goal is achieved. Here is an example: + +```kotlin +CometChat.addMessageListener("UNIQUE_ID", object : CometChat.MessageListener() { + override fun onInteractionGoalCompleted(interactionReceipt: InteractionReceipt) { + // This block is executed when an interaction goal is completed. + // Here you can specify the actions your application should take once an interaction goal is achieved, such as updating the UI or notifying the user. + } +}) +``` + ```java CometChat.addMessageListener("UNIQUE_ID", new CometChat.MessageListener() { @@ -240,25 +270,206 @@ CometChat.addMessageListener("UNIQUE_ID", new CometChat.MessageListener() { } }); ``` - + - -```kotlin -CometChat.addMessageListener("UNIQUE_ID", object : CometChat.MessageListener() { - override fun onInteractionGoalCompleted(interactionReceipt: InteractionReceipt) { - // This block is executed when an interaction goal is completed. - // Here you can specify the actions your application should take once an interaction goal is achieved, such as updating the UI or notifying the user. - } -}) +These event listeners offer your application a way to provide real-time updates in response to incoming interactive messages and goal completions, contributing to a more dynamic and responsive user chat experience. + +## InteractiveMessage Payload Structure + + + +The `InteractiveMessage` object contains interactive content like forms and buttons: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | long | Unique message identifier | +| `muid` | String | Developer-defined message ID | +| `sender` | [User](#user-object-interactive) | User who sent the message | +| `receiver` | AppEntity | Message receiver ([User](#user-object-interactive) or Group) | +| `type` | String | Message type (e.g., `"form"`, `"card"`) | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `category` | String | Message category. Value: `"interactive"` | +| `sentAt` | long | Unix timestamp when sent | +| `metadata` | JSONObject | Custom message metadata | +| `conversationId` | String | Associated conversation ID | +| `interactiveData` | JSONObject | Interactive content data | +| `tags` | Array\ | Message tags | +| `interactionGoal` | [InteractionGoal](#interactiongoal-object) | Goal for interactions | +| `interactions` | Array\<[Interaction](#interaction-object)\> | User interactions | +| `allowSenderInteraction` | boolean | If sender can interact | + +**Sample InteractiveMessage Object:** + +```json +{ + "id": 12345, + "muid": "msg_abc123", + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default" + }, + "receiver": { + "uid": "user_456", + "name": "Jane Smith" + }, + "type": "form", + "receiverType": "user", + "category": "interactive", + "sentAt": 1699900000, + "metadata": {"priority": "high"}, + "conversationId": "user_123_user_456", + "interactiveData": { + "title": "Demo Form", + "formFields": [ + { + "elementType": "textInput", + "elementId": "element1", + "label": "Name", + "optional": false, + "maxLines": 1 + } + ], + "submitElement": { + "elementType": "button", + "elementId": "element8", + "buttonText": "Submit", + "disableAfterInteracted": true, + "action": { + "actionType": "urlNavigation", + "url": "https://www.cometchat.com/" + } + } + }, + "tags": ["interactive"], + "interactionGoal": { + "type": "anyOf", + "elementIds": ["element1", "element8"] + }, + "interactions": [], + "allowSenderInteraction": false +} ``` - + - + -These event listeners offer your application a way to provide real-time updates in response to incoming interactive messages and goal completions, contributing to a more dynamic and responsive user chat experience. +The `InteractionGoal` object defines the desired outcome of interactions: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `type` | String | Goal type. Values: `"none"`, `"anyAction"`, `"anyOf"`, `"allOf"` | +| `elementIds` | Array\ | Target element IDs for the goal | + +**Sample InteractionGoal Object:** + +```json +{ + "type": "anyOf", + "elementIds": ["button_1", "input_1"] +} +``` + + -## Usage + + +The `Interaction` object represents a user action on an interactive element: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `elementId` | String | Identifier of the interacted element | +| `interactedAt` | long | Unix timestamp when interaction occurred | + +**Sample Interaction Object:** + +```json +{ + "elementId": "element1", + "interactedAt": 1699900500 +} +``` + + + + + +The nested `User` object in `sender` contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + +**Sample User Object:** + +```json +{ + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "link": "https://example.com/profile/user_123", + "role": "default", + "metadata": {"department": "engineering"}, + "status": "online", + "statusMessage": "Available", + "lastActiveAt": 1699900000, + "hasBlockedMe": false, + "blockedByMe": false, + "tags": ["premium"], + "deactivatedAt": 0 +} +``` + + + +## Best Practices + + + + Always validate your JSONObject structure before sending interactive messages. Ensure all required fields (elementType, elementId, etc.) are present to avoid runtime errors. + + + Assign unique element IDs to each interactive component within a message. This ensures accurate tracking of user interactions and prevents conflicts when processing interaction events. + + + Choose the right interaction goal type based on your use case. Use `ANY` for simple acknowledgments, `ANY_OF` for optional selections, and `ALL_OF` for required form completions. + + + Implement the `onInteractionGoalCompleted` listener to track when users complete interactive elements. This enables you to trigger follow-up actions or update your UI accordingly. + + + +--- -An `InteractiveMessage` is constructed with the receiver's UID, the receiver type, the interactive type, and interactive data as a JSONObject. Once created, the `InteractiveMessage` can be sent using CometChat's `sendInteractiveMessage()` method. Incoming `InteractiveMessages` can be received and processed via CometChat's message listener framework. +## Next Steps + + + + Learn how to send text, media, and custom messages + + + Mention specific users in messages for better engagement + + + Handle incoming messages with real-time listeners + + + Manage event listeners for real-time updates + + diff --git a/sdk/android/join-group.mdx b/sdk/android/join-group.mdx index 514297a66..91734f86d 100644 --- a/sdk/android/join-group.mdx +++ b/sdk/android/join-group.mdx @@ -1,12 +1,27 @@ --- -title: "Join A Group" +title: "Join Group" +sidebarTitle: "Join Group" +description: "Join public, private, and password-protected groups using the Android SDK" --- + +```kotlin +// Join a public group +CometChat.joinGroup("GUID", CometChatConstants.GROUP_TYPE_PUBLIC, "", + object : CometChat.CallbackListener() { + override fun onSuccess(group: Group) { } + override fun onError(e: CometChatException) { } + }) + +// Join a password-protected group +CometChat.joinGroup("GUID", CometChatConstants.GROUP_TYPE_PASSWORD, "password123", callback) +``` + ## Join a Group -In order to start participating in group conversations, you will have to join a group. You can do so using the `joinGroup()` method. +Use `joinGroup()` to start participating in a group conversation. @@ -50,25 +65,23 @@ CometChat.joinGroup(GUID,groupType,password,object:CometChat.CallbackListener -The `joinGroup()` method takes the below parameters +The `joinGroup()` method takes the following parameters: | Parameter | Description | | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `GUID` | The GUID of the group you would like to join | -| `groupType` | Type of the group. CometChat provides 3 types of groups viz. 1.CometChatConstants.GROUP\_TYPE\_PUBLIC (public) 2.CometChatConstants.GROUP\_TYPE\_PASSWORD (password) 3.CometChatConstants.GROUP\_TYPE\_PRIVATE (private) | -| `password` | Password is mandatory in case of a password protected group. | +| `groupType` | Type of the group. CometChat provides 3 types of groups: 1. `CometChatConstants.GROUP_TYPE_PUBLIC` (public) 2. `CometChatConstants.GROUP_TYPE_PASSWORD` (password) 3. `CometChatConstants.GROUP_TYPE_PRIVATE` (private) | +| `password` | Password is mandatory for password-protected groups. | Once you have joined a group successfully, you can send and receive messages in that group. -CometChat keeps a track of the groups joined and you do not need to join the group everytime you want to communicate in the group. +CometChat keeps track of the groups you have joined, so you do not need to join the group every time you want to communicate in it. You can identify if a group is joined using the `hasJoined` parameter in the `Group` object. -## Real-time Group Member Joined Events +## Real-Time Group Member Joined Events -*In other words, as a member of a group, how do I know if someone joins the group when my app is running?* - -If a user joins any group, the members of the group receive a real-time event in the `onGroupMemberJoined()` method of the `GroupListener` class. +When a user joins a group, members receive a real-time event in `onGroupMemberJoined()` of the `GroupListener` class. @@ -98,12 +111,71 @@ CometChat.addGroupListener(UNIQUE_LISTENER_ID, object : GroupListener() { ## Missed Group Member Joined Events -*In other words, as a member of a group, how do I know if someone joins the group when my app is not running?* - -When you retrieve the list of previous messages if a member has joined any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. - -For the group member joined event, in the `Action` object received, the following fields can help you get the relevant information- +When fetching message history, if a member joined a group the logged-in user is part of, the list will contain an `Action` message with these fields: 1. `action` - `joined` 2. `actionBy` - User object containing the details of the user who joined the group -3. `actionFor`- Group object containing the details of the group the user has joined +3. `actionFor` - Group object containing the details of the group the user has joined + + +Always remove group listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## Best Practices + + + + Use the `hasJoined` property of the Group object to avoid attempting to join groups you're already a member of. + + + Always prompt users for passwords when joining password-protected groups. Store passwords securely if needed for re-authentication. + + + Register group listeners to receive real-time notifications when other users join groups you're a member of. + + + +## Troubleshooting + + + + **Symptom:** `joinGroup()` fails with "Already a member" error. + + **Cause:** User is already a member of the group. + + **Solution:** Check the `hasJoined` property before calling `joinGroup()`. CometChat tracks joined groups automatically. + + + **Symptom:** `joinGroup()` fails with "Incorrect password" error for password-protected groups. + + **Cause:** Wrong password provided for a password-protected group. + + **Solution:** Verify the password is correct. Passwords are case-sensitive. + + + **Symptom:** `joinGroup()` fails with "Cannot join private group" error. + + **Cause:** Attempting to join a private group without an invitation. + + **Solution:** Private groups require an admin or moderator to add members. Use the [group-add-members](/sdk/android/group-add-members) functionality instead. + + + +--- + +## Next Steps + + + + Leave groups you no longer want to participate in + + + Fetch list of group members + + + Start sending messages in the group + + + Handle real-time group events + + diff --git a/sdk/android/key-concepts.mdx b/sdk/android/key-concepts.mdx index 71f0f4bf3..740a3a871 100644 --- a/sdk/android/key-concepts.mdx +++ b/sdk/android/key-concepts.mdx @@ -1,25 +1,49 @@ --- title: "Key Concepts" +sidebarTitle: "Key Concepts" +description: "Understand core CometChat concepts including users, groups, authentication, and messaging" --- + +**Core Entities:** +- **Users**: Identified by UID (alphanumeric with underscore/hyphen) +- **Groups**: Identified by GUID (alphanumeric with underscore/hyphen) +- **Auth Token**: Per user per device, generated server-side +- **Roles**: Categories for grouping similar users (e.g., "Premium") -### CometChat Dashboard +**Group Types:** +- **Public**: Any user can join +- **Password**: Requires password to join +- **Private**: Invitation only + +**Member Scopes:** +- **Admin**: Full control (change scopes, kick/ban, update/delete group) +- **Moderator**: Moderate members (kick/ban participants, update group) +- **Participant**: Send and receive messages/calls + +**Message Categories:** message, custom, action, call + +**Connection Modes:** Auto (WebSocket in foreground only) | Manual (explicit connect/disconnect) + + +## CometChat Dashboard The CometChat Dashboard enables you to create new apps (projects) and manage your existing apps. How many apps to create? -Ideally, you should create two apps- one for development and one for production. And you should use a single app irrespective of the number of platforms.\ -Do not create separate apps for every platform if you do, your users on different platforms will not be able to communicate with each other! +Ideally, you should create two apps—one for development and one for production. Use a single app regardless of the number of platforms. + +Do not create separate apps for every platform. If you do, your users on different platforms will not be able to communicate with each other! -* For every app, a unique App ID is generated. This App ID will be required when integrating CometChat within your app. -* Along with the App ID, you will need to create an Auth Key (from the Dashboard) which can be used for user authentication. +* For every app, a unique App ID is generated. This App ID is required when integrating CometChat within your app. +* Along with the App ID, you need to create an Auth Key (from the Dashboard) which can be used for user authentication. -### Auth & Rest API Keys +## Auth & Rest API Keys You can generate two types of keys from the dashboard. @@ -28,11 +52,11 @@ You can generate two types of keys from the dashboard. | Auth Key | The Auth Key can be used to create & login users. | In your client side code (during development) | | Rest API Key | The Rest API Key can be used to perform any CometChat operation. | In your server side code | -### Users +## Users A user is anyone who uses CometChat. -### UID +## UID * Each user is uniquely identified using UID. * The UID is typically the primary ID of the user from your database. @@ -43,21 +67,21 @@ UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and othe -### Auth Token +## Auth Token -* A single user can have multiple auth tokens. The auth tokens should be per user per device. -* It should be generated by API call ideally, via server to server call. The auth token should then be given to CometChat for login. -* An Auth Token can only be deleted via dashboard or using REST API. +* A single user can have multiple auth tokens. Auth tokens should be per user per device. +* Auth tokens should ideally be generated via a server-to-server API call. The auth token should then be passed to CometChat for login. +* An Auth Token can only be deleted via the dashboard or using the REST API. -### Authentication +## Authentication To allow a user to use CometChat, the user must log in to CometChat. -**CometChat does not handle user management.** You must handle user registration and login at your end. Once the user is logged into your app/site, you can log in the user to CometChat **programmatically**. So the user does not ever directly login to CometChat. +**CometChat does not handle user management.** You must handle user registration and login at your end. Once the user is logged into your app/site, you can log in the user to CometChat **programmatically**. The user never directly logs in to CometChat. -**CometChat does not handle friends management.** If you want to associate friends with your users, you must handle friends management in your app. Once two users are friends (i.e. they have accepted each other as friends), then you can associate them as friends in CometChat. +**CometChat does not handle friends management.** If you want to associate friends with your users, you must handle friends management in your app. Once two users are friends (i.e., they have accepted each other as friends), you can associate them as friends in CometChat. -### Typical Workflow +## Typical Workflow | Your App | Your Server | CometChat | | ----------------------------- | ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ | @@ -66,20 +90,20 @@ To allow a user to use CometChat, the user must log in to CometChat. | User sends a friend request | You display the request to the potential friend | No action required | | User accepts a friend request | You display the users as friends | You add both the users as friends using the Rest API | -### User Roles +## User Roles A role is a category for a group of similar users. For example, you may want to group your premium users using the role "Premium". You then use this to filter users or enable/disable features by writing conditional code. -### User List +## User List * The User List can be used to build the **Contacts** or **Who's Online** view in your app. * The list of users can be different based on the logged-in user. -### Groups +## Groups A group can be used for multiple users to communicate with each other on a particular topic/interest. -### GUID +## GUID * Each group is uniquely identified using GUID. * The GUID is typically the primary ID of the group from your database. If you do not store group information in your database, you can generate a random string for use as GUID. @@ -90,7 +114,7 @@ GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and oth -### Types +## Group Types CometChat supports three different types of groups: @@ -100,10 +124,12 @@ CometChat supports three different types of groups: | Password | All users | Any user with a valid password can choose to join | | Private | Only users part of the group | Invited users will be auto-joined | -### Members +## Members Once a participant joins a group, they become a member of the group. Members are part of the group indefinitely i.e. they will keep receiving messages, calls & notifications. To stop, the participant must either be kicked, banned or intentionally leave the group. +## Member Scopes + CometChat supports three different types of member scopes in a group: | Member | Default | Privileges | @@ -112,18 +138,20 @@ CometChat supports three different types of member scopes in a group: | Moderator | - | - Change scope of moderator or participant. - Update group - Kick & Ban Participants - Send & Receive Messages & Calls | | Participant | Any other user is assigned Participant scope | - Send & Receive Messages & Calls | -### Messaging +## Messaging -Any message in CometChat can belong to either one of the below categories +Any message in CometChat can belong to either one of the below categories. -| Category | Description | -| -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| message | Any message belonging to the category `message` can belong to either one of the below types: 1. text 2. image 3. video 4. audio 5. file | -| custom | Custom messages are an option available for developers to send custom data across to users/groups. To send any additional data that does not fit in the default categories and types provided by CometChat, you can use the custom messages. | -| action | Action messages are system-generated messages. These can belong to either of the below types:1. groupMember - when the action is performed on a group member 2. message - when the action is performed on a message | -| call | These are call-related messages. These can belong to either one of the two types:1. audio 2. video | +## Message Categories -### Auto Mode Connection +| Category | Types | Description | +| -------- | ----- | ----------- | +| `message` | `text`, `image`, `video`, `audio`, `file` | Standard messages | +| `custom` | Developer-defined | Custom data sent to users/groups that does not fit default categories (e.g., location, polls) | +| `action` | `groupMember`, `message` | System-generated messages when an action is performed on a group member or a message | +| `call` | `audio`, `video` | Call-related messages | + +## Auto Mode Connection @@ -136,7 +164,7 @@ Know more about auto mode connection [click here](/sdk/android/connection-behavi | App in foreground | Connected with WebSocket | | App in background | Immediately disconnected with WebSocket | -### Manual Mode Connection +## Manual Mode Connection @@ -148,3 +176,42 @@ Know more about manual mode connection [click here](/sdk/android/connection-beha | ----------------- | ------------------------------------------------------------------------------------------------------------------ | | App in foreground | Call `CometChat.connect()` to create the WebSocket connection | | App in background | Disconnect the WebSocket connection if no ping is received within 30 seconds after the app goes in the background. | + +## Glossary + +| Term | Definition | Learn More | +| ---- | ---------- | ---------- | +| UID | Unique User Identifier — alphanumeric string you assign to each user | [Users Overview](/sdk/android/users-overview) | +| GUID | Group Unique Identifier — alphanumeric string you assign to each group | [Groups Overview](/sdk/android/groups-overview) | +| Auth Key | Development-only credential for quick testing. Never use in production | [Authentication](/sdk/android/authentication-overview) | +| Auth Token | Secure, per-user token generated via REST API. Use in production | [Authentication](/sdk/android/authentication-overview) | +| REST API Key | Server-side credential for REST API calls. Never expose in client code | [CometChat Dashboard](https://app.cometchat.com) | +| Receiver Type | Specifies if a message target is a `user` or `group` | [Send Message](/sdk/android/send-message) | +| Scope | Group member scope: `admin`, `moderator`, or `participant` | [Change Member Scope](/sdk/android/change-member-scope) | +| Listener | Callback handler for real-time events (messages, presence, calls, groups) | [All Real-Time Listeners](/sdk/android/all-real-time-listeners) | +| Conversation | A chat thread between two users or within a group | [Retrieve Conversations](/sdk/android/retrieve-conversations) | +| Metadata | Custom JSON data attached to users, groups, or messages | [Send Message](/sdk/android/send-message) | +| Tags | String labels for categorizing users, groups, conversations, or messages | [Message Filtering](/sdk/android/message-filtering) | +| RequestBuilder | Builder pattern class for constructing filtered/paginated queries | [Message Filtering](/sdk/android/message-filtering) | +| AppSettings | Configuration object for initializing the SDK (App ID, Region, presence) | [Setup SDK](/sdk/android/setup) | +| Transient Message | Ephemeral message not stored on server (typing indicators, live reactions) | [Transient Messages](/sdk/android/transient-messages) | +| Interactive Message | Message with actionable UI elements (forms, cards, buttons) | [Interactive Messages](/sdk/android/interactive-messages) | + +--- + +## Next Steps + + + + Install and initialize the CometChat Android SDK + + + Learn how to log users into CometChat + + + Start sending messages to users and groups + + + Create and manage groups in your application + + diff --git a/sdk/android/leave-group.mdx b/sdk/android/leave-group.mdx index baa6f2fce..559410a35 100644 --- a/sdk/android/leave-group.mdx +++ b/sdk/android/leave-group.mdx @@ -1,12 +1,28 @@ --- -title: "Leave A Group" +title: "Leave Group" +sidebarTitle: "Leave Group" +description: "Leave groups to stop receiving messages and updates using the Android SDK" --- + +```kotlin +// Leave a group +CometChat.leaveGroup("GUID", object : CometChat.CallbackListener() { + override fun onSuccess(message: String) { } + override fun onError(e: CometChatException) { } +}) + +// Listen for member left events +CometChat.addGroupListener("LISTENER_ID", object : GroupListener() { + override fun onGroupMemberLeft(action: Action?, leftUser: User?, leftGroup: Group?) { } +}) +``` + ## Leave a Group -In order to stop receiving updates and messages for any particular joined group, you will have to leave the group using the `leaveGroup()` method. +Use `leaveGroup()` to stop receiving updates and messages for a group. @@ -53,11 +69,9 @@ CometChat.leaveGroup(GUID,object :CometChat.CallbackListener(){ Once a group is left, the user will no longer receive any updates or messages pertaining to the group. -## Real-time Group Member Left Events - -*In other words, as a member of a group, how do I know if someone has left it when my app is running?* +## Real-Time Group Member Left Events -If a user leaves a group, the members of the group receive a real-time event in the `onGroupMemberLeft()` method of the `GroupListener` class. +When a user leaves a group, members receive a real-time event in `onGroupMemberLeft()` of the `GroupListener` class. @@ -87,12 +101,64 @@ CometChat.addGroupListener(UNIQUE_LISTENER_ID, object : GroupListener() { ## Missed Group Member Left Events -*In other words, as a member of a group, how do I know if someone has left it when my app is not running?* - -When you retrieve the list of previous messages if a member has left any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. - -For the group member left event, in the `Action` object received, the following fields can help you get the relevant information- +When fetching message history, if a member left a group the logged-in user is part of, the list will contain an `Action` message with these fields: 1. `action` - `left` 2. `actionBy` - User object containing the details of the user who left the group 3. `actionFor` - Group object containing the details of the group the user has left + + +Always remove group listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## Best Practices + + + + Prompt users to confirm before leaving a group, especially for private groups where rejoining requires an invitation. + + + After leaving a group, consider clearing cached messages and group data from local storage to free up space. + + + Update your UI immediately when receiving leave events to reflect the current group membership status. + + + +## Troubleshooting + + + + **Symptom:** `leaveGroup()` fails with "Not a member" error. + + **Cause:** User is not currently a member of the group. + + **Solution:** Check the `hasJoined` property of the Group object before attempting to leave. + + + **Symptom:** `leaveGroup()` fails when trying to leave as the last admin. + + **Cause:** Groups require at least one admin. The last admin cannot leave without transferring ownership or deleting the group. + + **Solution:** Either [transfer ownership](/sdk/android/transfer-group-ownership) to another member or [delete the group](/sdk/android/delete-group) if you want to remove it entirely. + + + +--- + +## Next Steps + + + + Rejoin groups or join new ones + + + Permanently delete groups you own + + + Fetch list of available groups + + + Handle real-time group events + + diff --git a/sdk/android/login-listeners.mdx b/sdk/android/login-listeners.mdx index 9acb5ea90..3e837035b 100644 --- a/sdk/android/login-listeners.mdx +++ b/sdk/android/login-listeners.mdx @@ -1,88 +1,148 @@ --- title: "Login Listeners" +sidebarTitle: "Login Listeners" +description: "Android Activity and Fragment lifecycle patterns for CometChat login and logout event handling" --- + - -The CometChat SDK provides you with real-time updates for the `login` and `logout` events. This can be achieved using the `LoginListener` class provided. LoginListener consists of 4 events that can be triggered. These are as follows: - -| Callback Method | Information | -| ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| loginSuccess(User user) | Informs you that the login was successful and provides you with a user object containing the data for the user that logged in | -| loginFailure(CometChatException e) | Informs you about the failure while logging in the user and provides you with the reason for the failure wrapped in an object of the `CometChatException` class | -| logoutSuccess() | Informs you about the user being logged out successfully. | -| logoutFailure(CometChatException e) | Informs you about the failure while logging out the user. The reason for the failure can be obtained from the object of the `CometChatException` class provided. | - -To add the `LoginListener`, you need to use the `addLoginListener()` method provided by the SDK which takes a unique identifier for the listener and of the the `LoginListener` class itself. - - - -```java -CometChat.addLoginListener("UNIQUE_ID", new CometChat.LoginListener() { - @Override - public void loginSuccess(User user) { - Log.d("LoginListener", "loginSuccess " + user.toString()); - } - - @Override - public void loginFailure(CometChatException e) { - Log.d("LoginListener", "loginFailure " + e.getMessage()); - } - - @Override - public void logoutSuccess() { - Log.d("LoginListener", "logoutSuccess "); - } - - @Override - public void logoutFailure(CometChatException e) { - Log.d("LoginListener", "logoutFailure "); - } -}); -``` - - - - ```kotlin -CometChat.addLoginListener("UNIQUE_ID", object : LoginListener() { - override fun loginSuccess(user: User) { - Log.d("LoginListener", "loginSuccess $user") - } - - override fun loginFailure(e: CometChatException) { - Log.d("LoginListener", "loginFailure " + e.message) - } - - override fun logoutSuccess() { - Log.d("LoginListener", "logoutSuccess ") - } - - override fun logoutFailure(e: CometChatException) { - Log.d("LoginListener", "logoutFailure ") - } +// Add login listener +CometChat.addLoginListener("LISTENER_ID", object : LoginListener() { + override fun loginSuccess(user: User) { /* Login successful */ } + override fun loginFailure(e: CometChatException) { /* Login failed */ } + override fun logoutSuccess() { /* Logout successful */ } + override fun logoutFailure(e: CometChatException) { /* Logout failed */ } }) + +// Remove listener +CometChat.removeLoginListener("LISTENER_ID") ``` + - +This page covers Android-specific lifecycle patterns for `LoginListener`. For the basic add/remove API, see [Authentication](/sdk/android/authentication-overview#login-listener). - +## Android Activity/Fragment Example -In order to stop receiving events related to login and logout you need to use the `removeLoginListener()` method provided by the SDK and pas the ID of the listener that needs to be removed. +Register the listener in `onCreate()` and remove it in `onDestroy()` to follow the Android lifecycle: ```java -CometChat.removeLoginListener("UNIQUE_ID"); +public class MainActivity extends AppCompatActivity { + + private static final String LISTENER_ID = "LOGIN_LISTENER"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + CometChat.addLoginListener(LISTENER_ID, new CometChat.LoginListener() { + @Override + public void loginSuccess(User user) { + Log.d("LoginListener", "loginSuccess: " + user.getName()); + // Initialize user-specific data, navigate to main screen, etc. + } + + @Override + public void loginFailure(CometChatException e) { + Log.d("LoginListener", "loginFailure: " + e.getMessage()); + } + + @Override + public void logoutSuccess() { + Log.d("LoginListener", "logoutSuccess"); + // Clean up resources, navigate to login screen, etc. + } + + @Override + public void logoutFailure(CometChatException e) { + Log.d("LoginListener", "logoutFailure: " + e.getMessage()); + } + }); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + CometChat.removeLoginListener(LISTENER_ID); + } +} ``` - - ```kotlin -CometChat.removeLoginListener("UNIQUE_ID") +class MainActivity : AppCompatActivity() { + + private val LISTENER_ID = "LOGIN_LISTENER" + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + CometChat.addLoginListener(LISTENER_ID, object : LoginListener() { + override fun loginSuccess(user: User) { + Log.d("LoginListener", "loginSuccess: ${user.name}") + // Initialize user-specific data, navigate to main screen, etc. + } + + override fun loginFailure(e: CometChatException) { + Log.d("LoginListener", "loginFailure: ${e.message}") + } + + override fun logoutSuccess() { + Log.d("LoginListener", "logoutSuccess") + // Clean up resources, navigate to login screen, etc. + } + + override fun logoutFailure(e: CometChatException) { + Log.d("LoginListener", "logoutFailure: ${e.message}") + } + }) + } + + override fun onDestroy() { + super.onDestroy() + CometChat.removeLoginListener(LISTENER_ID) + } +} ``` - - + + +Always remove login listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## Best Practices + + + + Use `loginSuccess()` to initialize user-specific data (fetch conversations, register message listeners) and `logoutSuccess()` to clean up resources and navigate to the login screen. + + + In `loginFailure()`, parse the `CometChatException` to show user-friendly error messages (e.g., "Invalid credentials", "Network error"). Provide retry options for transient failures. + + + Register login listeners before calling `CometChat.login()` to ensure you don't miss any events. Register in `onCreate()` or before authentication attempts. + + + Remove all listeners (message, user, group, call) in `logoutSuccess()` to prevent receiving events after logout. This ensures a clean session termination. + + + +--- + +## Next Steps + + + + Login methods, Auth Key vs Auth Token, and logout + + + Register listeners for messages, users, groups, and calls + + + Monitor connection status to CometChat servers + + + Initialize the SDK before logging in users + + diff --git a/sdk/android/mentions.mdx b/sdk/android/mentions.mdx index ce0320209..01b4a9bb9 100644 --- a/sdk/android/mentions.mdx +++ b/sdk/android/mentions.mdx @@ -1,21 +1,55 @@ --- title: "Mentions" +sidebarTitle: "Mentions" +description: "Send messages with user mentions, retrieve mentioned users, and filter messages by mention metadata using the CometChat Android SDK." --- + +```kotlin +// Send message with mention to user +val textMessage = TextMessage("UID", "Hello <@uid:cometchat-uid-1>", CometChatConstants.RECEIVER_TYPE_USER) +CometChat.sendMessage(textMessage, object : CallbackListener() { + override fun onSuccess(message: TextMessage) { } + override fun onError(e: CometChatException) { } +}) -## Mentions +// Check if logged-in user was mentioned +val wasMentioned = message.hasMentionedMe() -Mentions are a powerful tool for enhancing communication in messaging platforms. They streamline interaction by allowing users to easily engage and collaborate with particular individuals, especially in group conversations. +// Get list of mentioned users +val mentionedUsers = message.mentionedUsers +``` + -Mentions in messages enable users to refer to specific individuals within a conversation. +Mentions let users refer to specific individuals in a conversation using the `<@uid:UID>` format. They're especially useful in group chats for directing messages at particular participants. ## Send Mentioned Messages -Every User object has a String unique identifier associated with them which can be found in a property called uid. To mention a user in a message, the message text should contain the uid in following format:` <@uid:UID_OF_THE_USER>`. For example, to mention the user with UID cometchat-uid-1 in a text message, your text should be `"<@uid:cometchat-uid-1>"` +To mention a user, embed `<@uid:UID>` in your message text. For example: `"Hello <@uid:cometchat-uid-1>"` - + + + +```kotlin +private val receiverID = "UID" +private val messageText = "Hello <@uid:cometchat-uid-1>" +private val receiverType = CometChatConstants.RECEIVER_TYPE_USER + +val textMessage = TextMessage(receiverID, messageText, receiverType) +CometChat.sendMessage(textMessage, object : CallbackListener() { + override fun onSuccess(textMessage: TextMessage) { + Log.d(TAG, "Message sent successfully: $textMessage") + } + + override fun onError(e: CometChatException) { + Log.d(TAG, "Message sending failed with exception: " + e.message) + } +}) +``` + + ```java private String receiverID = "UID"; private String messageText = "Hello, <@uid:cometchat-uid-1>"; @@ -35,14 +69,17 @@ CometChat.sendMessage(textMessage, new CometChat.CallbackListener() } }); ``` - + + - + + + ```kotlin -private val receiverID = "UID" +private val receiverID = "GUID" private val messageText = "Hello <@uid:cometchat-uid-1>" -private val receiverType = CometChatConstants.RECEIVER_TYPE_USER +private val receiverType = CometChatConstants.RECEIVER_TYPE_GROUP val textMessage = TextMessage(receiverID, messageText, receiverType) CometChat.sendMessage(textMessage, object : CallbackListener() { @@ -55,10 +92,8 @@ CometChat.sendMessage(textMessage, object : CallbackListener() { } }) ``` - - - + ```java private String receiverID = "GUID"; private String messageText = "Hello <@uid:cometchat-uid-1>"; @@ -78,27 +113,8 @@ CometChat.sendMessage(textMessage, new CometChat.CallbackListener() } }); ``` - - - -```kotlin -private val receiverID = "GUID" -private val messageText = "Hello <@uid:cometchat-uid-1>" -private val receiverType = CometChatConstants.RECEIVER_TYPE_GROUP - -val textMessage = TextMessage(receiverID, messageText, receiverType) -CometChat.sendMessage(textMessage, object : CallbackListener() { - override fun onSuccess(textMessage: TextMessage) { - Log.d(TAG, "Message sent successfully: $textMessage") - } - - override fun onError(e: CometChatException) { - Log.d(TAG, "Message sending failed with exception: " + e.message) - } -}) -``` - + @@ -111,7 +127,7 @@ You can mention user in text message and media messages captions ## Mentioned Messages -By default, the SDK will fetch all the messages irrespective of the fact that the logged-in user is mentioned or not in the message. The SDK has other optional filters such as tag info and blocked info. +By default, the SDK will fetch all the messages irrespective of the fact that the logged-in user is mentioned or not in the message. The SDK has other optional filters such as tag info and blocked info. For more filtering options, see [Additional Message Filtering](/sdk/android/additional-message-filtering). | Setting | Description | | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | @@ -123,7 +139,34 @@ By default, the SDK will fetch all the messages irrespective of the fact that th To get a list of messages in a conversation where users are mentioned along with the user tags of the mentioned users. - + + + +```kotlin +val UID = "cometchat-uid-1" + +val messagesRequest = MessagesRequest.MessagesRequestBuilder() + .setLimit(50) + .setUID(UID) + .mentionsWithTagInfo(true) + .build() + +messagesRequest.fetchPrevious(object : CometChat.CallbackListener>() { + override fun onSuccess(messages: List) { + for (messageObj in messages) { + for (user in messageObj.mentionedUsers) { + Log.e(TAG, "tag: ${user.tags}") + } + } + } + + override fun onError(e: CometChatException) { + Log.e(TAG, "onError: " + e) + } +}) +``` + + ```java String UID = "cometchat-uid-1"; @@ -149,16 +192,19 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( } }); ``` - + + - + + + ```kotlin -val UID = "cometchat-uid-1" +val GUID = "cometchat-guid-1" val messagesRequest = MessagesRequest.MessagesRequestBuilder() .setLimit(50) - .setUID(UID) + .setGUID(GUID) .mentionsWithTagInfo(true) .build() @@ -176,10 +222,8 @@ messagesRequest.fetchPrevious(object : CometChat.CallbackListener - - + ```java String GUID = "cometchat-guid-1"; @@ -205,24 +249,35 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( } }); ``` - + + + + - +## Mentions With Blocked Info + +To get a list of messages in a conversation where users are mentioned along with the blocked relationship of the mentioned users with the logged-in user. + + + + + ```kotlin -val GUID = "cometchat-guid-1" +val UID = "cometchat-uid-1" val messagesRequest = MessagesRequest.MessagesRequestBuilder() .setLimit(50) - .setGUID(GUID) - .mentionsWithTagInfo(true) + .setUID(UID) + .mentionsWithBlockedInfo(true) .build() messagesRequest.fetchPrevious(object : CometChat.CallbackListener>() { override fun onSuccess(messages: List) { for (messageObj in messages) { for (user in messageObj.mentionedUsers) { - Log.e(TAG, "tag: ${user.tags}") + Log.e(TAG, "isBlockedByMe: ${user.isBlockedByMe}") + Log.e(TAG, "isHasBlockedMe: ${user.isHasBlockedMe}") } } } @@ -232,17 +287,8 @@ messagesRequest.fetchPrevious(object : CometChat.CallbackListener - - - -## Mentions With Blocked Info - -To get a list of messages in a conversation where users are mentioned along with the blocked relationship of the mentioned users with the logged-in user. - - - + ```java String UID = "cometchat-uid-1"; @@ -269,16 +315,19 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( } }); ``` - + + - + + + ```kotlin -val UID = "cometchat-uid-1" +val GUID = "cometchat-guid-1" val messagesRequest = MessagesRequest.MessagesRequestBuilder() .setLimit(50) - .setUID(UID) + .setGUID(GUID) .mentionsWithBlockedInfo(true) .build() @@ -297,10 +346,8 @@ messagesRequest.fetchPrevious(object : CometChat.CallbackListener - - + ```java String GUID = "cometchat-guid-1"; @@ -327,42 +374,15 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( } }); ``` - - - -```kotlin -val GUID = "cometchat-guid-1" - -val messagesRequest = MessagesRequest.MessagesRequestBuilder() - .setLimit(50) - .setGUID(GUID) - .mentionsWithBlockedInfo(true) - .build() - -messagesRequest.fetchPrevious(object : CometChat.CallbackListener>() { - override fun onSuccess(messages: List) { - for (messageObj in messages) { - for (user in messageObj.mentionedUsers) { - Log.e(TAG, "isBlockedByMe: ${user.isBlockedByMe}") - Log.e(TAG, "isHasBlockedMe: ${user.isHasBlockedMe}") - } - } - } - - override fun onError(e: CometChatException) { - Log.e(TAG, "onError: " + e) - } -}) -``` - + -## Get Users Mentioned In a Particular Message +## Get Mentioned Users from a Message -To retrieve the list of users mentioned in the particular message, you can use the `message.getMentionedUsers()` method. This method will return an array containing the mentioned users, or an empty array if no users were mentioned in the message. +Use `getMentionedUsers()` on any message object to retrieve the list of users mentioned in it. Returns an empty list if no users were mentioned. @@ -386,18 +406,47 @@ message.mentionedUsers To check if the logged-in user has been mentioned in a particular message we can use the `hasMentionedMe()` method on any `BaseMessage`. This method will return a boolean value, `true` if the logged-in user has been mentioned, otherwise `false`. + +```kotlin +message.hasMentionedMe +``` + ```java message.hasMentionedMe() ``` - + - -```kotlin -message.hasMentionedMe -``` +## Best Practices - + + + Mentions are ideal for drawing attention in group conversations. Use them when you need to ensure specific users see critical messages or action items. + + + Always verify that the user ID exists before including it in a mention. Invalid UIDs will display as plain text rather than interactive mentions. + + + When a user is mentioned, consider sending a push notification to ensure they're alerted even when not actively using the app. This enhances engagement and responsiveness. + + - +--- + +## Next Steps + + + + Learn how to send text, media, and custom messages + + + Handle incoming messages with real-time listeners + + + Create messages with embedded forms and interactive elements + + + Fetch user lists to enable mention autocomplete + + diff --git a/sdk/android/message-structure-and-hierarchy.mdx b/sdk/android/message-structure-and-hierarchy.mdx index 4ed4ed248..9abac689e 100644 --- a/sdk/android/message-structure-and-hierarchy.mdx +++ b/sdk/android/message-structure-and-hierarchy.mdx @@ -1,87 +1,183 @@ --- -title: "Message Structure And Hierarchy" +title: "Message Structure and Hierarchy" +sidebarTitle: "Message Structure" +description: "Understand message categories, types, and hierarchy in the CometChat Android SDK" --- + +**Message Categories:** +- **Message**: text, image, video, audio, file +- **Custom**: Developer-defined types (e.g., location) +- **Interactive**: form, card, customInteractive +- **Action**: groupMember, message (system-generated) +- **Call**: audio, video (with status: initiated, ongoing, ended, etc.) -The below diagram helps you better understand the various message categories and types that a CometChat message can belong to. +**Check message type:** +```kotlin +when (message.category) { + CometChatConstants.CATEGORY_MESSAGE -> { /* Handle message */ } + CometChatConstants.CATEGORY_CUSTOM -> { /* Handle custom */ } + CometChatConstants.CATEGORY_ACTION -> { /* Handle action */ } + CometChatConstants.CATEGORY_CALL -> { /* Handle call */ } +} +``` + + +Every message in CometChat belongs to a category and has a specific type within that category. Understanding this hierarchy helps you handle different message types correctly. + +## Message Hierarchy -As you can see in the above diagram, every message belongs to a particular category. A message can belong to either one of the 4 categories - -1. Message -2. Custom -3. Action -4. Call - -Each category can be further be classified into types. +## Categories Overview + +| Category | Types | Description | +| --- | --- | --- | +| `message` | `text`, `image`, `video`, `audio`, `file` | Standard user messages | +| `custom` | Developer-defined | Custom data (location, polls, etc.) | +| `interactive` | `form`, `card`, `customInteractive` | Messages with actionable UI elements | +| `action` | `groupMember`, `message` | System-generated events | +| `call` | `audio`, `video` | Call-related messages | + +## Checking Message Category and Type + +Use `getCategory()` and `getType()` to determine how to handle a received message: + + + +```java +String category = message.getCategory(); +String type = message.getType(); + +switch (category) { + case CometChatConstants.CATEGORY_MESSAGE: + if (type.equals(CometChatConstants.MESSAGE_TYPE_TEXT)) { + TextMessage textMsg = (TextMessage) message; + Log.d(TAG, "Text: " + textMsg.getText()); + } else if (type.equals(CometChatConstants.MESSAGE_TYPE_IMAGE)) { + MediaMessage mediaMsg = (MediaMessage) message; + Log.d(TAG, "Image URL: " + mediaMsg.getAttachment().getFileUrl()); + } + break; + case CometChatConstants.CATEGORY_CUSTOM: + CustomMessage customMsg = (CustomMessage) message; + Log.d(TAG, "Custom type: " + type + ", data: " + customMsg.getCustomData()); + break; + case CometChatConstants.CATEGORY_ACTION: + Action actionMsg = (Action) message; + Log.d(TAG, "Action: " + actionMsg.getAction()); + break; + case CometChatConstants.CATEGORY_CALL: + Call callMsg = (Call) message; + Log.d(TAG, "Call status: " + callMsg.getCallStatus()); + break; +} +``` + + +```kotlin +val category = message.category +val type = message.type + +when (category) { + CometChatConstants.CATEGORY_MESSAGE -> { + if (type == CometChatConstants.MESSAGE_TYPE_TEXT) { + val textMsg = message as TextMessage + Log.d(TAG, "Text: ${textMsg.text}") + } else if (type == CometChatConstants.MESSAGE_TYPE_IMAGE) { + val mediaMsg = message as MediaMessage + Log.d(TAG, "Image URL: ${mediaMsg.attachment?.fileUrl}") + } + } + CometChatConstants.CATEGORY_CUSTOM -> { + val customMsg = message as CustomMessage + Log.d(TAG, "Custom type: $type, data: ${customMsg.customData}") + } + CometChatConstants.CATEGORY_ACTION -> { + val actionMsg = message as Action + Log.d(TAG, "Action: ${actionMsg.action}") + } + CometChatConstants.CATEGORY_CALL -> { + val callMsg = message as Call + Log.d(TAG, "Call status: ${callMsg.callStatus}") + } +} +``` + + ## Message -A message belonging to the category `message` can be classified into either 1 of the below types: - -1. text - A plain text message -2. image- An image message -3. video- A video message -4. audio- An audio message -5. file- A file message +Standard user messages. Types: `text`, `image`, `video`, `audio`, `file`. ## Custom -In the case of messages that belong to the `custom` category, there are no predefined types. Custom messages can be used by developers to send messages that do not fit in the default category and types provided by CometChat. For messages with the category `custom`, the developers can set their own type to uniquely identify the custom message. A very good example of a custom message would be the sharing of location co-ordinates. In this case, the developer can decide to use the custom message with type set to `location`. +Developer-defined messages for content that doesn't fit standard categories (e.g., location sharing, polls). Set your own `type` string to identify the custom message. ## Interactive -An `InteractiveMessage` is a specialized object that encapsulates an interactive unit within a chat message, such as an embedded form that users can fill out directly within the chat interface. This enhances user engagement by making the chat experience more interactive and responsive to user input. +Messages with embedded UI elements users can interact with directly in chat. -1. form- for interactive form -2. card- for interactive card -3. customInteractive- for custom interaction messages +Types: `form`, `card`, `customInteractive`. - -to know about Interactive messages please [click here](/sdk/android/interactive-messages) - +See [Interactive Messages](/sdk/android/interactive-messages) for full details. ## Action -Action messages are system-generated messages. Messages belonging to the `action` category can further be classified into one of the below types: - -1. groupMember - action performed on a group member. -2. message - action performed on a message. +System-generated messages triggered by actions on group members or messages. -Action messages hold another property called `action` which actually determine the action that has been performed For the type `groupMember` the action can be either one of the below: +Types: `groupMember`, `message`. -1. joined - when a group member joins a group -2. left - when a group member leaves a group -3. kicked - when a group member is kicked from the group -4. banned - when a group member is banned from the group -5. unbanned - when a group member is unbanned from the group -6. added - when a user is added to the group -7. scopeChanged - When the scope of a group member is changed. +The `action` property specifies what happened: -For the type `message`, the action can be either one of the below: +**For `groupMember`:** `joined`, `left`, `kicked`, `banned`, `unbanned`, `added`, `scopeChanged` -1. edited - when a message is edited. -2. deleted - when a message is deleted. +**For `message`:** `edited`, `deleted` ## Call -Messages with the category `call` are Calling related messages. These can belong to either one of the 2 types +Call-related messages. Types: `audio`, `video`. + +The `status` property indicates the call state: `initiated`, `ongoing`, `canceled`, `rejected`, `unanswered`, `busy`, `ended`. -1. audio -2. video -The call messages have a property called status that helps you figure out the status of the call. The status can be either one of the below values: +## Best Practices + + + + Always check the message category using `message.getCategory()` before casting to specific message types (TextMessage, MediaMessage, etc.) to avoid ClassCastException errors. + + + Implement handlers for all message categories and types your app supports. Use a default case to handle unknown or future message types gracefully. + + + Use custom messages with unique types for content that doesn't fit standard categories (e.g., location sharing, polls, stickers). This keeps your implementation flexible and extensible. + + + Use `MessagesRequest.setCategories()` to fetch only the message categories you need. This improves performance and reduces unnecessary data transfer. + + + +--- -1. initiated - when a is initiated to a user/group -2. ongoing - when the receiver of the call has accepted the call -3. canceled - when the call has been canceled by the initiator of the call -4. rejected - when the call has been rejected by the receiver of the call -5. unanswered - when the call was not answered by the receiver. -6. busy - when the receiver of the call was busy on another call. -7. ended - when the call was successfully completed and ended by either the initiator or receiver. +## Next Steps + + + + Send text, media, and custom messages + + + Handle incoming messages of all types + + + Filter messages by category, type, and other parameters + + + Create and handle interactive form and card messages + + diff --git a/sdk/android/messaging-overview.mdx b/sdk/android/messaging-overview.mdx index 36e57feff..4923d2c82 100644 --- a/sdk/android/messaging-overview.mdx +++ b/sdk/android/messaging-overview.mdx @@ -1,12 +1,93 @@ --- title: "Messaging" sidebarTitle: "Overview" +description: "Send, receive, and manage messages with CometChat's Android SDK messaging features" --- + +```kotlin +// Send a text message +val textMessage = TextMessage("UID", "Hello!", CometChatConstants.RECEIVER_TYPE_USER) +CometChat.sendMessage(textMessage, callback) -Messaging is one of the core features of CometChat. We've thoughtfully created methods to help you send, receive and fetch message history. +// Register a message listener +CometChat.addMessageListener("LISTENER_ID", object : CometChat.MessageListener() { + override fun onTextMessageReceived(message: TextMessage) { } + override fun onMediaMessageReceived(message: MediaMessage) { } + override fun onCustomMessageReceived(message: CustomMessage) { } +}) -At the minimum, you must add code for [sending messages](/sdk/android/send-message) and [receiving messages](/sdk/android/receive-messages). +// Remove listener +CometChat.removeMessageListener("LISTENER_ID") +``` -Once you've implemented that, you can proceed to more advanced features like [typing indicators](/sdk/android/typing-indicators) and [delivery & read receipts](/sdk/android/delivery-read-receipts). +Key paths: +- **Send** → Text, media, custom messages +- **Receive** → Real-time listeners +- **Advanced** → Threads, reactions, mentions, interactive messages + + +Messaging is one of the core features of CometChat. The SDK provides methods to send, receive, edit, delete, and fetch message history for both one-on-one and group conversations. + +At minimum, implement [sending messages](/sdk/android/send-message) and [receiving messages](/sdk/android/receive-messages). Once those are working, layer in advanced features as needed. + +## Features + +| Feature | Description | Guide | +| --- | --- | --- | +| Send Messages | Send text, media, and custom messages to users and groups | [Send Message](/sdk/android/send-message) | +| Receive Messages | Listen for real-time incoming messages using `MessageListener` | [Receive Messages](/sdk/android/receive-messages) | +| Edit Message | Modify a sent message after delivery | [Edit Message](/sdk/android/edit-message) | +| Delete Message | Remove a message from a conversation | [Delete Message](/sdk/android/delete-message) | +| Threaded Messages | Reply to a specific message to create a thread | [Threaded Messages](/sdk/android/threaded-messages) | +| Typing Indicators | Show when a user is actively typing | [Typing Indicators](/sdk/android/typing-indicators) | +| Delivery & Read Receipts | Track when messages are delivered and read | [Delivery & Read Receipts](/sdk/android/delivery-read-receipts) | +| Reactions | Add emoji reactions to any message | [Reactions](/sdk/android/reactions) | +| Mentions | Tag users in group conversations with `@` | [Mentions](/sdk/android/mentions) | +| Transient Messages | Send ephemeral messages not stored on the server | [Transient Messages](/sdk/android/transient-messages) | +| Interactive Messages | Send messages with embedded forms, cards, and buttons | [Interactive Messages](/sdk/android/interactive-messages) | +| Message Filtering | Fetch messages with filters by type, category, and more | [Message Filtering](/sdk/android/additional-message-filtering) | + +## Message Types + +CometChat supports three message types out of the box: + +| Type | Class | Use Case | +| --- | --- | --- | +| Text | `TextMessage` | Plain text chat | +| Media | `MediaMessage` | Images, videos, audio, files | +| Custom | `CustomMessage` | Location, polls, or any JSON data | + +For a full breakdown of message categories and hierarchy, see [Message Structure](/sdk/android/message-structure-and-hierarchy). + +--- + +## Next Steps + + + + Send text, media, and custom messages to users and groups + + + Handle real-time incoming messages with listeners + + + Understand message categories, types, and hierarchy + + + Show real-time typing status in conversations + + + Create organized conversation threads + + + Add emoji reactions to messages + + + Tag users in group conversations + + + Send messages with buttons and forms + + diff --git a/sdk/android/overview.mdx b/sdk/android/overview.mdx index a55d7d6ce..93d1c075f 100644 --- a/sdk/android/overview.mdx +++ b/sdk/android/overview.mdx @@ -1,301 +1,135 @@ --- title: "Overview" +sidebarTitle: "Overview" +description: "Get started with the CometChat Android SDK - install, initialize, and add real-time chat to your Android application" --- + - -This guide demonstrates how to add chat to an Android application. Before you begin, we strongly recommend you read the [Key Concepts](/sdk/android/key-concepts) guide. - -#### **I want to integrate with my app** - -1. [Get your application keys](./overview#get-your-application-keys) -2. [Add the CometChat dependency](./overview#add-the-cometchat-dependency) -3. [Initialize CometChat](./overview#initialise-cometchat) -4. [Register and Login your user](./overview#register-and-login-your-user) -5. [Integrate Our UI Kits](./overview#integrate-our-ui-kits) - -#### **I want to explore a sample app (includes UI)** - -Import the app into Android Studio and follow the steps mentioned in the `README.md` file. - -[Java Chat App](https://github.com/cometchat-pro/android-java-chat-app/) - -[Kotlin Chat App](https://github.com/cometchat-pro/android-kotlin-chat-app) - -### Get your Application Keys - -[Signup for CometChat](https://app.cometchat.com) and then: - -1. Create a new app -2. Head over to the **API Keys** section and note the **Auth Key**, **App ID** & **Region** - - -Minimum Requirements - -* Android API Level 21 -* Android API level 24 (in case you are using the calls SDKS) -* Androidx Compatibility - - - -### Add the CometChat Dependency - -First, add the repository URL to the **project level**`build.gradle` file in the `repositories` block under the `allprojects` section. - - - -```groovy -allprojects { - repositories { - maven { - url "https://dl.cloudsmith.io/public/cometchat/cometchat/maven/" - } - } -} -``` - - - - - -Then, add CometChat to the **app level**`build.gradle` file in the `dependencies` section. - - - -```groovy -dependencies { - implementation "com.cometchat:chat-sdk-android:4.1.8" -} -``` - - - - - - - -v2.4+ onwards, Voice & Video Calling functionality has been moved to a separate library. In case you plan to use the calling feature, please add the Calling dependency `implementation 'com.cometchat:calls-sdk-android:4.3.3'` in the dependencies section of the app-level `build.gradle` file. - - - -Finally, add the below lines `android` section of the **app level** gradle file - - - -```groovy -android { - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } -} -``` - - - - - -### Initialise CometChat - -The `init()` method initializes the settings required for CometChat. The `init()` method takes the below parameters: - -1. appID - You CometChat App ID -2. appSettings - An object of the AppSettings class can be created using the AppSettingsBuilder class. The region field is mandatory and can be set using the `setRegion()` method. - -The `AppSettings` class allows you to configure three settings: - -* **Region**: The region where you app was created. -* [Presence Subscription](/sdk/android/user-presence): Represents the subscription type for user presence (real-time online/offline status) -* **autoEstablishSocketConnection(boolean value)**: This property takes a boolean value which when set to true informs the SDK to manage the web-socket connection internally. If set to false, it informs the SDK that the web-socket connection will be managed manually. The default value for this parameter is true. For more information on this, please check the Managing Web-Socket connections manually section. The default value for this property is **true.** -* **overrideAdminHost(adminHost: string)**: This method takes the admin URL as input and uses this admin URL instead of the default admin URL. This can be used in case of dedicated deployment of CometChat. -* **overrideClientHost(clientHost: string)**: This method takes the client URL as input and uses this client URL instead of the default client URL. This can be used in case of dedicated deployment of CometChat. - - - -```java -String appID = "APP_ID"; // Replace with your App ID -String region = "REGION"; // Replace with your App Region ("eu" or "us") - -AppSettings appSettings = new AppSettings.AppSettingsBuilder() - .subscribePresenceForAllUsers() - .setRegion(region) - .autoEstablishSocketConnection(true) - .build(); - -CometChat.init(this, appID, appSettings, new CometChat.CallbackListener () { - @Override - public void onSuccess(String successMessage) { - Log.d(TAG, "Initialization completed successfully"); - } - - @Override - public void onError(CometChatException e) { - Log.d(TAG, "Initialization failed with exception: " + e.getMessage()); - } -}); -``` - - - - ```kotlin -val appID: String = "APP_ID" // Replace with your App ID -val region: String = "REGION" // Replace with your App Region ("eu" or "us") +// 1. Add dependency to build.gradle +implementation "com.cometchat:chat-sdk-android:4.1.8" +// 2. Initialize (run once at app start) val appSettings = AppSettings.AppSettingsBuilder() - .subscribePresenceForAllUsers() - .setRegion(region) - .autoEstablishSocketConnection(true) - .build() - -CometChat.init( - this, - appID, - appSettings, - object : CometChat.CallbackListener() { - override fun onSuccess(p0: String?) { - Log.d(TAG, "Initialization completed successfully") - } - - override fun onError(p0: CometChatException?) { - Log.d(TAG, "Initialization failed with exception: " + p0?.message) - } - } -) -``` - - - - - -Make sure you replace the `APP_ID` with your CometChat **App ID** and `region` with your **App Region** in the above code. - -### Register and Login your User - -Once initialization is successful, you will need to create a user. - -To create users on the fly, you can use the `createUser()` method. This method takes a `User` object and the `Auth Key` as input parameters and returns the created `User` object if the request is successful. - - - -```java -String authKey = "AUTH_KEY"; // Replace with your App Auth Key -User user = new User(); -user.setUid("USER_UID"); // Replace with the UID for the user to be created -user.setName("USER_NAME"); // Replace with the name of the user - -CometChat.createUser(user, authKey, new CometChat.CallbackListener () { - @Override - public void onSuccess(User user) { - Log.d("createUser", user.toString()); - } - - @Override - public void onError(CometChatException e) { - Log.e("createUser", e.getMessage()); - } -}); -``` - - - - -```kotlin -val authKey = "AUTH_KEY" // Replace with your App Auth Key -val user = User() -user.uid = "USER_UID" // Replace with the UID for the user to be created -user.name = "USER_NAME" // Replace with the name of the user - -CometChat.createUser(user, authKey, object : CometChat.CallbackListener() { - override fun onSuccess(user: User) { - Log.d("createUser", user.toString()) - } - - override fun onError(e: CometChatException) { - Log.e("createUser", e.message) - } -}) -``` - - - - - -Make sure that `UID` and `name` are specified as these are mandatory fields to create a user. - -Once you have created the user successfully, you will need to log the user into CometChat using the `login()` method. - -We recommend you call the CometChat `login()` method once your user logs into your app. The `login()` method needs to be called only once. - - - -This straightforward authentication method is ideal for proof-of-concept (POC) development or during the early stages of application development. For production environments, however, we strongly recommend using an [Auth Token](/sdk/android/authentication-overview#login-using-auth-token) instead of an Auth Key to ensure enhanced security. - - - - - -```java -String UID = "user1"; // Replace with the UID of the user to login -String authKey = "AUTH_KEY"; // Replace with your App Auth Key - -if (CometChat.getLoggedInUser() == null) { - CometChat.login(UID, authKey, new CometChat.CallbackListener () { - @Override - public void onSuccess(User user) { - Log.d(TAG, "Login Successful : " + user.toString()); - } - - @Override - public void onError(CometChatException e) { - Log.d(TAG, "Login failed with exception: " + e.getMessage()); - } - }); -} else { - // User already logged in -} -``` - - - - -```kotlin -val UID: String = "user1" // Replace with the UID of the user to login -val authKey: String = "AUTH_KEY" // Replace with your App Auth Key - -if (CometChat.getLoggedInUser() == null) { - CometChat.login( - UID, - authKey, - object : CometChat.CallbackListener() { - override fun onSuccess(p0: User?) { - Log.d(TAG, "Login Successful : " + p0?.toString()) - } + .subscribePresenceForAllUsers() + .setRegion("REGION") + .build() +CometChat.init(context, "APP_ID", appSettings, callback) - override fun onError(p0: CometChatException?) { - Log.d(TAG, "Login failed with exception: " + p0?.message) - } - } - ) -} else { - // User already logged in -} +// 3. Login user +CometChat.login("UID", "AUTH_KEY", callback) // Dev only ``` - - - +**Required Credentials:** App ID, Region, Auth Key (dev) or Auth Token (prod) +**Get from:** [CometChat Dashboard](https://app.cometchat.com) → Your App → API & Auth Keys + -Make sure you replace the `AUTH_KEY` with your App **Auth Key** in the above code. - -The `login()` method returns the `User` object containing all the information of the logged-in user. + +`CometChat.init()` must be called before any other SDK method. Calling `login()`, `sendMessage()`, or registering listeners before `init()` will fail. + +**Auth Key** is for development/testing only. In production, generate **Auth Tokens** on your server using the REST API and pass them to the client. Never expose Auth Keys in production client code. + -UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. +This guide demonstrates how to add chat to an Android application. Before you begin, we strongly recommend reading the [Key Concepts](/sdk/android/key-concepts) guide. + +## Requirements + +| Requirement | Minimum Version | +| --- | --- | +| Android API Level | 21 | +| Android API Level (with Calls SDK) | 24 | +| Java | 8 | +| AndroidX | Required | + +## Getting Started + + + + [Sign up for CometChat](https://app.cometchat.com) and create an app. Note your App ID, Region, and Auth Key from the Dashboard. + + + Add the SDK to your project and initialize it with your credentials. See [Setup SDK](/sdk/android/setup). + + + Log in users with Auth Key (development) or Auth Token (production). See [Authentication](/sdk/android/authentication-overview). + + + Send your first message, make a call, or manage users and groups. + + + +## Features + + + + 1:1 and group chat, threads, reactions, typing indicators, read receipts, file sharing, and interactive messages. + + + Ringing flows, direct call sessions, standalone calling, recording, and screen sharing. + + + Create, retrieve, and manage users. Track online/offline presence and block/unblock users. + + + Public, private, and password-protected groups with member management, roles, and ownership transfer. + + + +## Sample Apps - +Import the app into Android Studio and follow the steps mentioned in the `README.md` file. -### Integrate our UI Kits + + + Java sample app + + + Kotlin sample app + + + +## UI Kits + +Refer to the [Android UI Kit](/ui-kit/android/overview) for pre-built, customizable components. + +## Resources + + + + UIDs, GUIDs, auth tokens, and core SDK concepts + + + Message categories, types, and hierarchy + + + Latest SDK version and release notes + + + Migration guide for V3 users + + + Steps to publish your Android app + + + +--- -* Please refer [Android Java UI Kit](/ui-kit/android/overview) section to integrate Android Java UI Kit inside your app. +## Next Steps + + + + Detailed installation and initialization guide for the Android SDK + + + Understand core concepts like users, groups, messages, and conversations + + + Learn about secure authentication with Auth Tokens for production apps + + + Start sending text, media, and custom messages to users and groups + + diff --git a/sdk/android/presenter-mode.mdx b/sdk/android/presenter-mode.mdx index e68bfd29d..90ef33505 100644 --- a/sdk/android/presenter-mode.mdx +++ b/sdk/android/presenter-mode.mdx @@ -1,44 +1,43 @@ --- title: "Presenter Mode" +sidebarTitle: "Presenter Mode" +description: "Create webinar-style calling experiences with presenters and viewers using the Android SDK" --- + +```kotlin +// Join as presenter +val presenterSettings = CometChatCalls.PresentationSettingsBuilder(context) + .setDefaultLayoutEnable(true) + .setIsPresenter(true) // true = presenter, false = viewer + .setEventListener(eventListener) + .build() + +CometChatCalls.joinPresentation(callToken, presenterSettings, videoContainer, object : CometChatCalls.CallbackListener() { + override fun onSuccess(s: String) { } + override fun onError(e: CometChatException) { } +}) +``` -## Overview - -The Presenter Mode feature allows developers to create a calling service experience in which: - -1. There are one or more users who are presenting their video, audio and/or screen (Maximum 5) -2. Viewers who are consumers of that presentation. (They cannot send their audio, video or screen streams out). -3. The total number of presenters and viewers can go up to 100. -4. Features such as mute/unmute audio, show/hide camera capture, recording, etc. will be enabled only for the Presenter in this mode. -5. Other call participants will not get these features. Hence they act like passive viewers in the call. - -Using this feature developers can create experiences such as: - -1. All hands calls -2. Keynote speeches -3. Webinars -4. Talk shows -5. Online classes and many more... - -About this guide - -This guide demonstrates how to start a presentation into an Android application. Before you begin, we strongly recommend you read the calling setup guide. +**Use Cases:** Webinars, online classes, keynote speeches, all-hands meetings +**Capacity:** Up to 5 presenters + 95 viewers (100 total) + -Before starting a call session you have to generate a call token. You need to call this method for the call token. -Start Presentation Session -The most important class that will be used in the implementation is the `PresentationSettings` class. This class allows you to set the various parameters for the Presentation Mode. In order to set the various parameters of the `PresentationSettings` class, you need to use the `PresentationSettingsBuilder` class. Below are the various options available with the `PresentationSettings` class. +Presenter Mode enables broadcast-style calling — up to 5 presenters share content with passive viewers (up to 100 total participants). Use it for webinars, keynotes, all-hands meetings, online classes, or talk shows. -`PresentationSettingsBuilder` class takes the 1 mandatory parameter as a part of the constructor: +| Role | Capabilities | Max Count | +| ---- | ------------ | --------- | +| Presenter | Video, audio, screen sharing, mute controls, recording | 5 | +| Viewer | Watch and listen only (no outgoing streams) | Up to 100 total | -1. Context of the application. +Before starting a presentation, generate a call token using `generateToken()` as described in [Call Session](/sdk/android/direct-calling#generate-call-token). -You will also need to set the User Type, There are 2 type of users in Presenter Mode, `Presenter` & `Participant` , You can set this `PresentationSettingsBuilder `by using the following method` isPresenter(true/false)` +## Start Presentation Session -A basic example of how to start a Presentation: +Configure the presentation using `PresentationSettingsBuilder`. Set `setIsPresenter(true)` for presenters or `setIsPresenter(false)` for viewers. @@ -188,11 +187,14 @@ The `CometChatCallsEventsListener` listener provides you with the below callback ## Settings -The `PresentationSettings` class is the most important class when it comes to the implementation of the Calling feature. This is the class that allows you to customize the overall calling experience. The properties for the call/conference can be set using the `PresentationSettingsBuilder` class. This will eventually give you and object of the `PresentationSettings` class which you can pass to the joinPresentation() method to start the call. +The `PresentationSettings` class is the most important class when it comes to implementing the Calling feature. This class allows you to customize the overall calling experience. The properties for the call/conference can be set using the `PresentationSettingsBuilder` class. This gives you an object of the `PresentationSettings` class which you can pass to the `joinPresentation()` method to start the call. + +The **mandatory** parameters required for any call/conference to work are: -The **mandatory** parameters that are required to be present for any call/conference to work are: +- Context - context of the activity/application +- RelativeLayout - A RelativeLayout object in which the calling UI is loaded -Context - context of the activity/application RelativeLayout - A RelativeLayout object in which the calling UI is loaded. The options available for customization of calls are: +The options available for customization of calls are: | Setting | Description | | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -210,6 +212,76 @@ Context - context of the activity/application RelativeLayout - A RelativeLayout | `showRecordingButton(boolean value)` | If set to `true`, it displays the Recording Button. If set to `false`, it hides the Recording Button. Default value = `false` | | `setEventListener(new CometChatCallsEventsListener())` | The `CometChatCallsEventsListener` listener provides you callbacks. | -In case you wish to achieve a completely customised UI for the Calling experience, you can do so by embedding default android buttons to the screen as per your requirement and then use the below methods to achieve different functionalities for the embedded buttons. +If you want to achieve a completely customized UI for the Calling experience, you can do so by embedding default Android buttons to the screen as per your requirement and then use the methods below to achieve different functionalities for the embedded buttons. + +For the use case where you want to align your own custom buttons and not use the default layout provided by CometChat, you can embed the buttons in your layout and use the methods below to perform the corresponding operations: + + + +Always remove call event listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +--- -For the use case where you wish to align your own custom buttons and not use the default layout provided by CometChat you can embed the buttons in your layout and use the below methods to perform the corresponding operations: +## Best Practices + + + + Keep the number of presenters to 5 or fewer for optimal performance. More presenters increase bandwidth requirements and can degrade the experience for viewers. + + + Clearly distinguish between presenters and viewers using `setIsPresenter()`. Presenters have full control (audio, video, screen share), while viewers are passive consumers. + + + Test your presentation with close to 100 participants to ensure your infrastructure can handle the load. Monitor bandwidth and CPU usage during testing. + + + Show visual indicators to users about their role (presenter vs viewer) and available controls. Viewers should understand they cannot send audio/video. + + + Implement logic to handle scenarios where a presenter disconnects unexpectedly. Consider promoting another user to presenter or ending the session gracefully. + + + +## Troubleshooting + + + + **Symptom:** Viewers join the presentation but cannot see the presenter's video stream. + + **Cause:** Presenter may have started with video muted, or network issues are preventing video transmission. + + **Solution:** Ensure the presenter has `startWithVideoMuted(false)` in their settings. Check network connectivity and bandwidth for both presenter and viewers. + + + **Symptom:** `joinPresentation()` fails with "Invalid call token" error. + + **Cause:** The call token is invalid or expired. + + **Solution:** Ensure you're using a valid call token generated for the specific session. Verify the token hasn't expired and matches the session ID. + + + **Symptom:** User cannot join as presenter even though `setIsPresenter(true)` is set. + + **Cause:** The maximum limit of 5 presenters has been reached. + + **Solution:** Wait for an existing presenter to leave, or join as a viewer instead. Implement logic to check presenter count before allowing users to join as presenters. + + + +## Next Steps + + + + Implement standard one-on-one and group calling + + + Customize the call UI appearance and layout + + + Record presentation sessions for later playback + + + Configure idle timeout for inactive sessions + + diff --git a/sdk/android/publishing-app-on-playstore.mdx b/sdk/android/publishing-app-on-playstore.mdx index 7e2815d76..8df05acb9 100644 --- a/sdk/android/publishing-app-on-playstore.mdx +++ b/sdk/android/publishing-app-on-playstore.mdx @@ -1,28 +1,43 @@ --- title: "Publishing App On PlayStore" +sidebarTitle: "Publishing to Play Store" +description: "Guide to preparing and publishing your CometChat Android app on Google Play Store" --- + + +Publishing checklist: +1. **Reduce app size** - Remove unused resources, enable ProGuard/R8 +2. **Prepare signed bundle** - Generate signed AAB with proper versioning +3. **Create Play Console account** - One-time $25 fee +4. **Upload app bundle** - Submit to Google Play Console +5. **Complete store listing** - Add screenshots, description, privacy policy + +**Key tools:** Android Studio → Build → Generate Signed Bundle/APK +**Play Console:** [https://play.google.com/console](https://play.google.com/console) + + -Publishing an App on PlayStore is an easy task once you are done with your development. Below we have mentioned few steps which will help you to understand more in detail. +Publishing an app on the Play Store is straightforward once you have completed your development. Below are a few steps that will help you understand the process in more detail. -### 1. Reduce App Size. +### 1. Reduce App Size -App Size is a prior concern before publishing App on the Play Store. **Android Studio** provides few plugins and tools that will help you reduce the app size. One way to reduce app size is to remove unused resources. Android Studio provides an option "**Remove unused Resources**" under "**Refactor**" Menu which will help you to remove unused resources from your app. +App size is a key concern before publishing on the Play Store. **Android Studio** provides plugins and tools that help you reduce app size. One way to reduce app size is to remove unused resources. Android Studio provides an option called "**Remove unused Resources**" under the "**Refactor**" menu, which helps you remove unused resources from your app. -Please check below link to get different ways to reduce your app size. +Check the link below for different ways to reduce your app size: [https://developer.android.com/topic/performance/reduce-apk-size](https://developer.android.com/topic/performance/reduce-apk-size) *** -### 2. Prepare the Signed Release App Bundle. +### 2. Prepare the Signed Release App Bundle -Before preparing the signed release app bundle, we suggest you to check that the **android:debuggable** attribute should not be present in your manifest file. Also you need to maintain\*\* versionCode \*\*and **versionName** available in ( app-level ) **build.gradle** for each release. +Before preparing the signed release app bundle, ensure that the **android:debuggable** attribute is not present in your manifest file. Also, maintain the **versionCode** and **versionName** in your (app-level) **build.gradle** for each release. @@ -41,28 +56,93 @@ android { -After above steps you need to sign your app and upload it to the play store. Android Studio provides an option to generate a signed app bundle. From the menu bar, click **Build > Build > Generate Signed Bundle/APK**. After that you can reuse your existing key or create a new one if you don't have one +After the above steps, you need to sign your app and upload it to the Play Store. Android Studio provides an option to generate a signed app bundle. From the menu bar, click **Build > Build > Generate Signed Bundle/APK**. After that, you can reuse your existing key or create a new one if you don't have one. -For more details please check below link. +For more details, check the link below: [https://developer.android.com/studio/publish/app-signing](https://developer.android.com/studio/publish/app-signing) *** -### 3 Upload an App. +### 3. Upload an App -To upload an app on Google Play Store, you need google account, sign-in to your google account or create a new if you don't have one. Then click on the below mentioned link. +To upload an app on Google Play Store, you need a Google account. Sign in to your Google account or create a new one if you don't have one. Then click on the link below: [https://play.google.com/apps/publish/](https://play.google.com/apps/publish/) -If you already have merchant account then it will show you list of your published apps or else it will tell you to sign as merchant account and you need to pay one-time charge. +If you already have a merchant account, it shows you a list of your published apps. Otherwise, it prompts you to sign up as a merchant account and pay a one-time charge. -It will charge you a once in a lifetime fee i.e 25$. Just do it to start uploading your first app. Later you can publish other apps through this account and you won't be charged. +This is a one-time fee of $25. Pay it to start uploading your first app. You can publish other apps through this account later without being charged again. -Check below link for more details on how to create and upload app in play store. +Check the link below for more details on how to create and upload an app to the Play Store: [https://developer.android.com/studio/publish/upload-bundle](https://developer.android.com/studio/publish/upload-bundle) + +--- + +## Best Practices + + + + Always test your signed release build on multiple devices before uploading to Play Store. Release builds may behave differently than debug builds due to ProGuard/R8 optimizations. + + + Increment `versionCode` for every release (even minor updates) and update `versionName` to reflect the user-facing version. Google Play requires increasing version codes for updates. + + + Store your app signing key securely and create backups. Losing your signing key means you cannot update your app on Play Store. Consider using Google Play App Signing for additional security. + + + Enable code shrinking and obfuscation in your release build to reduce app size and protect your code. Test thoroughly after enabling to catch any ProGuard-related issues. + + + Provide high-quality screenshots and promotional graphics that showcase your app's key features. Good visuals significantly impact download rates. + + + +## Troubleshooting + + + + **Symptom:** Google Play Console rejects your app bundle upload. + + **Cause:** Version code is not higher than the previous release, or the bundle is not properly signed. + + **Solution:** Ensure `versionCode` in build.gradle is higher than the current production version. Verify the bundle is signed with the correct keystore. Check for any validation errors in the Play Console. + + + **Symptom:** App works in debug mode but crashes in release build. + + **Cause:** ProGuard/R8 may be removing necessary code, or debug-specific code is causing issues. + + **Solution:** Review ProGuard rules and add keep rules for CometChat SDK classes. Remove any `android:debuggable="true"` from your manifest. Test the release build thoroughly before publishing. + + + **Symptom:** Play Console warns about large app size affecting downloads. + + **Cause:** Unused resources, large assets, or unoptimized libraries are included in the bundle. + + **Solution:** Use Android Studio's "Remove Unused Resources" feature. Enable resource shrinking in build.gradle. Consider using Android App Bundle format which automatically optimizes for different device configurations. + + + +## Next Steps + + + + Review SDK setup and configuration + + + Optimize WebSocket connection for production + + + Understand API rate limiting for production + + + Official Android publishing documentation + + diff --git a/sdk/android/rate-limits.mdx b/sdk/android/rate-limits.mdx index 17581b144..8a0484814 100644 --- a/sdk/android/rate-limits.mdx +++ b/sdk/android/rate-limits.mdx @@ -1,34 +1,176 @@ --- title: "Rate Limits" +sidebarTitle: "Rate Limits" +description: "Understand CometChat REST API rate limits and how to handle rate limit responses" --- + +**Rate Limits:** +- **Core Operations:** 10,000 requests/minute (login, create/delete user, create/join group) +- **Standard Operations:** 20,000 requests/minute (all other operations) -### CometChat REST API Rate Limits +**Rate Limit Response:** +- **Status Code:** 429 (Too Many Requests) +- **Headers:** `Retry-After`, `X-Rate-Limit-Reset`, `X-Rate-Limit`, `X-Rate-Limit-Remaining` + +**Best Practice:** Monitor `X-Rate-Limit-Remaining` header and implement exponential backoff when approaching limits. + + + + +CometChat applies rate limits to REST API requests to ensure fair usage and platform stability. Understanding them helps you build applications that handle high traffic gracefully. + +## Rate Limit Tiers + +| Operation Type | Limit | Examples | +| --- | --- | --- | +| Core Operations | 10,000 requests/min | Login, create/delete user, create/join group | +| Standard Operations | 20,000 requests/min | All other operations | -The rate limits below are for general applications. Rate limits can be adjusted on a per need basis, depending on your use-case and plan. The rate limits are cumulative. For example: If the rate limit for core operations is 100 requests per minute, then you can either login a user, add user to a group, remove a user from a group, etc for total 100 requests per minute. +Rate limits are cumulative within each tier. For example, if you make 5,000 login requests and 5,000 create user requests in one minute, you've hit the 10,000 core operations limit. Rate limits can be adjusted on a per-need basis depending on your use case and plan. -1. **Core Operations:** Core operations are rate limited to `10,000` requests per minute. Core operations include user login, create/delete user, create/join group cumulatively. -2. **Standard Operations:** Standard operations are rate limited to `20,000` requests per minute. Standard operations include all other operations cumulatively. - -## What happens when rate limit is reached ? +## What Happens When the Rate Limit Is Reached? -The request isn't processed and a response is sent containing a 429 response code. Along with the response code there will be couple of headers sent which specifies the time in seconds that you must wait before you can try request again. +The request isn't processed and a response is sent containing a 429 response code. Along with the response code, a couple of headers are sent that specify the time in seconds you must wait before you can try the request again. `Retry-After: 15` `X-Rate-Limit-Reset: 1625143246` -## Is there any endpoint that returns rate limit of all resources ? +## Response Headers -No, we don't provide a rate-limit endpoint. +CometChat includes rate limit information in response headers: -However, we do provide the following response headers that you can use to confirm the app's current rate limit and monitor the number of requests remaining in the current minute: +| Header | Description | +| --- | --- | +| `X-Rate-Limit` | Your current rate limit | +| `X-Rate-Limit-Remaining` | Requests remaining in current window | +| `Retry-After` | Seconds to wait before retrying (on 429) | +| `X-Rate-Limit-Reset` | Unix timestamp when limit resets (on 429) | + +## Rate Limit Endpoint + +CometChat does not provide a dedicated rate-limit endpoint. Use the response headers below to monitor your current limit and remaining requests: `X-Rate-Limit: 700` `X-Rate-Limit-Remaining: 699` + +## Handling Rate Limits + +When you exceed the rate limit, CometChat returns HTTP `429 Too Many Requests`. Implement exponential backoff to handle this gracefully: + + + +```java +private void callWithRetry(Runnable apiCall, int maxRetries, int attempt) { + try { + apiCall.run(); + } catch (Exception e) { + if (e instanceof CometChatException && + ((CometChatException) e).getCode().equals("TOO_MANY_REQUEST") && + attempt < maxRetries) { + long waitTime = (long) Math.pow(2, attempt) * 1000; + Log.d(TAG, "Rate limited. Retrying in " + (waitTime / 1000) + "s..."); + new Handler(Looper.getMainLooper()).postDelayed( + () -> callWithRetry(apiCall, maxRetries, attempt + 1), + waitTime + ); + } else { + Log.e(TAG, "Max retries exceeded or non-rate-limit error: " + e.getMessage()); + } + } +} +``` + + +```kotlin +fun callWithRetry(apiCall: () -> Unit, maxRetries: Int = 3, attempt: Int = 0) { + try { + apiCall() + } catch (e: CometChatException) { + if (e.code == "TOO_MANY_REQUEST" && attempt < maxRetries) { + val waitTime = (2.0.pow(attempt) * 1000).toLong() + Log.d(TAG, "Rate limited. Retrying in ${waitTime / 1000}s...") + Handler(Looper.getMainLooper()).postDelayed( + { callWithRetry(apiCall, maxRetries, attempt + 1) }, + waitTime + ) + } else { + Log.e(TAG, "Max retries exceeded or non-rate-limit error: ${e.message}") + } + } +} +``` + + + +--- + +## Best Practices + + + + Always check the `X-Rate-Limit-Remaining` header in API responses to track how many requests you have left in the current minute. Implement logic to slow down requests when approaching the limit. + + + When you receive a 429 response, implement exponential backoff retry logic. Use the `Retry-After` header value to determine when to retry the request. + + + Group multiple operations into batch requests where the API supports it. This reduces the total number of API calls and helps stay within rate limits. + + + Cache user lists, group information, and other frequently accessed data locally to reduce the number of API calls. Only fetch fresh data when necessary. + + + If your application consistently hits rate limits, contact CometChat support to discuss increasing limits based on your use case and plan. Rate limits can be adjusted per-need basis. + + + +## Troubleshooting + + + + **Symptom:** Application frequently receives 429 (Too Many Requests) responses. + + **Cause:** Your application is making too many API requests within the rate limit window (1 minute). + + **Solution:** Implement request throttling on the client side. Monitor `X-Rate-Limit-Remaining` header and slow down requests when approaching the limit. Consider caching data locally to reduce API calls. If legitimate high volume, contact support for limit increase. + + + **Symptom:** Unclear when rate limit will reset after hitting the limit. + + **Cause:** Misunderstanding the `X-Rate-Limit-Reset` header format. + + **Solution:** The `X-Rate-Limit-Reset` header contains a Unix timestamp (seconds since epoch). Convert it to a readable date/time in your timezone. The `Retry-After` header provides seconds to wait, which is often simpler to use. + + + **Symptom:** Rate limits are hit even though the app doesn't seem to be making many requests. + + **Cause:** Multiple instances of your app (different users, devices, or servers) share the same rate limit pool. Background sync or polling may be consuming quota. + + **Solution:** Rate limits are per app, not per user or device. Audit all API calls including background operations. Implement efficient polling strategies (use WebSocket for real-time updates instead of polling). Consider request prioritization to ensure critical operations succeed. + + + +## Next Steps + + + + Configure SDK for optimal API usage + + + Use WebSocket for real-time updates instead of polling + + + Receive updates via WebSocket to reduce API calls + + + Explore REST API endpoints and rate limits + + diff --git a/sdk/android/reactions.mdx b/sdk/android/reactions.mdx index b7a903654..ffc173372 100644 --- a/sdk/android/reactions.mdx +++ b/sdk/android/reactions.mdx @@ -1,10 +1,78 @@ --- title: "Reactions" +sidebarTitle: "Reactions" +description: "Add, remove, and fetch message reactions in real-time using the CometChat Android SDK. Includes listener events and helper methods for updating UI." --- + + + +```kotlin +// Add reaction +CometChat.addReaction(messageId, "😊", object : CallbackListener() { + override fun onSuccess(message: BaseMessage) { } + override fun onError(e: CometChatException) { } +}) + +// Remove reaction +CometChat.removeReaction(messageId, "😊", object : CallbackListener() { + override fun onSuccess(message: BaseMessage) { } + override fun onError(e: CometChatException) { } +}) + +// Fetch reactions for a message +val reactionRequest = ReactionRequest.ReactionRequestBuilder() + .setMessageId(messageId) + .setLimit(30) + .build() +reactionRequest.fetchNext(callback) + +// Listen for real-time reaction events +CometChat.addMessageListener(listenerID, object : MessageReactionListener { + override fun onMessageReactionAdded(reaction: MessageReaction) { } + override fun onMessageReactionRemoved(reaction: MessageReaction) { } +}) +``` + + +```java +// Add reaction +CometChat.addReaction(messageId, "😊", new CallbackListener() { + @Override + public void onSuccess(BaseMessage message) { } + @Override + public void onError(CometChatException e) { } +}); + +// Remove reaction +CometChat.removeReaction(messageId, "😊", new CallbackListener() { + @Override + public void onSuccess(BaseMessage message) { } + @Override + public void onError(CometChatException e) { } +}); + +// Fetch reactions for a message +ReactionRequest reactionRequest = new ReactionRequest.ReactionRequestBuilder() + .setMessageId(messageId) + .setLimit(30) + .build(); +reactionRequest.fetchNext(callback); + +// Listen for real-time reaction events +CometChat.addMessageListener(listenerID, new MessageReactionListener() { + @Override + public void onMessageReactionAdded(MessageReaction reaction) { } + @Override + public void onMessageReactionRemoved(MessageReaction reaction) { } +}); +``` + + + -Enhance user engagement in your chat application with message reactions. Users can express their emotions using reactions to messages. This feature allows users to add or remove reactions, and to fetch all reactions on a message. You can also listen to reaction events in real-time. Let's see how to work with reactions in CometChat's Android SDK. +Reactions let users respond to messages with emoji. You can add or remove reactions, fetch all reactions on a message, listen for reaction events in real time, and update your UI when reactions change. ## Add a Reaction @@ -53,7 +121,7 @@ CometChat.addReaction(messageId, emoji, object : CometChat.CallbackListener -You can react on Text, Media and Custom messages. +You can react on Text, Media and Custom messages. For more details on message types, see [Message Structure & Hierarchy](/sdk/android/message-structure-and-hierarchy). @@ -111,7 +179,7 @@ To get all reactions for a specific message, first create a `ReactionRequest` us | `setMessageId(int value)` | Specifies the unique identifier of the message for which you want to fetch reactions. This parameter is mandatory as it tells the SDK which message's reactions are being requested. | | `setReaction(String value)` | Filters the reactions fetched by the specified reaction type (e.g., "😊", "😂", "👍"). When set, this method will cause the ReactionRequest to only retrieve details of the provided reaction for the given message. | -## Fetch Next +### Fetch Next The `fetchNext()` method fetches the next set of reactions for the message. @@ -235,7 +303,7 @@ reactionRequest.fetchPrevious(object : CometChat.CallbackListener @@ -285,7 +353,7 @@ To stop listening for reaction events, remove the listener as follows: ```java private String listenerID = "UNIQUE_LISTENER_ID"; -CometChat.removeMessageListener("reactionListener"); +CometChat.removeMessageListener(listenerID); ``` @@ -294,13 +362,17 @@ CometChat.removeMessageListener("reactionListener"); ```kotlin val listenerID = "UNIQUE_LISTENER_ID" -CometChat.removeMessageListener("reactionListener") +CometChat.removeMessageListener(listenerID) ``` + +Always remove listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + ## Get Reactions List To retrieve the list of reactions reacted on particular message, you can use the `message.getReactions()` method. This method will return an array containing the reactions, or an empty array if no one reacted on the message. @@ -400,3 +472,120 @@ val modifiedBaseMessage: BaseMessage = CometChatHelper.updateMessageWithReaction After calling this method, the `message` instance's reactions are updated. You can then use `message.getReactions()` to get the latest reactions and refresh your UI accordingly. + +## Reaction Payload Structure + + + +When fetching reactions for a message, each `Reaction` object contains the following fields: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | String | Unique reaction identifier | +| `messageId` | long | ID of the message that was reacted to | +| `reaction` | String | The reaction emoji | +| `uid` | String | UID of the user who reacted | +| `reactedAt` | long | Unix timestamp when reaction was added | +| `reactedBy` | [User](#user-object-reactions) | User who added the reaction | + +**Sample Reaction Object:** + +```json +{ + "id": "reaction_123", + "messageId": 12345, + "reaction": "👍", + "uid": "user_123", + "reactedAt": 1699900000, + "reactedBy": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default", + "tags": ["premium", "verified"] + } +} +``` + + + + + +The nested `User` object in `reactedBy` contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + + + + + +When accessing reactions on a message via `message.getReactions()`, each `ReactionCount` object contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `reaction` | String | The reaction emoji | +| `count` | int | Total count of this reaction | +| `reactedByMe` | boolean | Whether the logged-in user has reacted with this emoji | + +**Sample ReactionCount Object:** + +```json +{ + "reaction": "👍", + "count": 5, + "reactedByMe": true +} +``` + + + +## Best Practices + + + + Define a specific set of allowed emoji reactions in your app to maintain consistency and prevent inappropriate reactions. Consider using a reaction picker UI component with predefined options. + + + Update your UI immediately when a user adds or removes a reaction, then handle the server response. This provides instant feedback and improves perceived performance. + + + Always use `CometChatHelper.updateMessageWithReactionInfo()` when receiving real-time reaction events to keep your message objects synchronized with the latest reaction data. + + + Consider implementing limits on the number of reactions per message or per user to prevent spam and maintain a clean UI. + + + +--- + +## Next Steps + + + + Learn how to send text, media, and custom messages + + + Handle real-time message events with listeners + + + Organize conversations with message threads + + + Tag users in messages with @mentions + + diff --git a/sdk/android/real-time-listeners.mdx b/sdk/android/real-time-listeners.mdx index 04e653071..bb7aad396 100644 --- a/sdk/android/real-time-listeners.mdx +++ b/sdk/android/real-time-listeners.mdx @@ -1,8 +1,29 @@ --- -title: "All Real Time Listeners" +title: "All Real-Time Listeners" +sidebarTitle: "Real-Time Listeners" +description: "Register and manage real-time event listeners for users, groups, messages, and calls in the Android SDK" --- + +```kotlin +// User listener +CometChat.addUserListener("LISTENER_ID", object : CometChat.UserListener() { + override fun onUserOnline(user: User) { } + override fun onUserOffline(user: User) { } +}) + +// Message listener +CometChat.addMessageListener("LISTENER_ID", object : CometChat.MessageListener() { + override fun onTextMessageReceived(message: TextMessage) { } + override fun onMediaMessageReceived(message: MediaMessage) { } +}) + +// Remove listeners (important!) +CometChat.removeUserListener("LISTENER_ID") +CometChat.removeMessageListener("LISTENER_ID") +``` + CometChat provides 4 listeners viz. @@ -20,7 +41,7 @@ The `UserListener` class provides you with live events related to users. Below a | `onUserOnline(User user)` | This method is triggered when a user comes online and is available to chat. The details of the user can be obtained from the user object received as the method parameter. | | `onUserOffline(User user)` | This method is triggered when a user goes offline. The details of the user can be obtained from the User object received as the parameter. | -To add the `UserListener`, you need to use the `addUserListener()` method provided by the `CometChat` class. +Use `addUserListener()` to register and `removeUserListener()` to unregister. Call remove in `onPause()`. @@ -40,22 +61,27 @@ CometChat.addUserListener(UNIQUE_LISTENER_ID, new CometChat.UserListener() { - + +```kotlin +CometChat.addUserListener(UNIQUE_LISTENER_ID, object : CometChat.UserListener() { + override fun onUserOnline(user: User) { -where `UNIQUE_LISTENER_ID` is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events. + } -Once the activity/fragment where the `UserListener` is declared is not in use, you need to remove the listener using the `removeUserListener()` method which takes the id of the listener to be removed as the parameter. We suggest you call this method in the `onPause()` method of the activity/fragment. + override fun onUserOffline(user: User) { - - -```java -CometChat.removeUserListener(UNIQUE_LISTENER_ID) + } +}) ``` + +Always remove listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + ## Group Listener The `GroupListener` class provides you with all the real-time events related to groups. Below are the callback methods provided by the `GroupListener` class. @@ -70,7 +96,7 @@ The `GroupListener` class provides you with all the real-time events related to | `onGroupMemberScopeChanged(Action action, User updatedBy, User updatedUser, String scopeChangedTo, String scopeChangedFrom, Group group)` | This method is triggered when the scope of any Group Member has been changed. All the members that are a part of that group receive this event | | `onMemberAddedToGroup(Action action, User addedby, User userAdded, Group addedTo)` | This method is triggered when a user is added to any group. All the members including the user himself receive this event. | -To add the `GroupListener`, you need to use the `addGroupListener()` method provided by the `CometChat` class. +Use `addGroupListener()` to register and `removeGroupListener()` to unregister. Call remove in `onPause()`. @@ -138,27 +164,6 @@ CometChat.addGroupListener("UNIQUE_ID", object : CometChat.GroupListener() { -where `UNIQUE_LISTENER_ID` is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events. - -Once the activity/fragment where the `GroupListener` is declared is not in use, you need to remove the listener using the `removeGroupListener()` method which takes the id of the listener to be removed as the parameter. We suggest you call this method in the `onPause()` method of the activity/fragment. - - - -```java -CometChat.removeGroupListener(UNIQUE_LISTENER_ID); -``` - - - - -```kotlin -CometChat.removeGroupListener(UNIQUE_LISTENER_ID) -``` - - - - - ## Message Listener The `MessageListener` class provides you with live events related to messages. Below are the callback methods provided by the `MessageListener` class. @@ -180,7 +185,7 @@ The `MessageListener` class provides you with live events related to messages. B | `onMessageReactionAdded(ReactionEvent reactionEvent)` | This event is triggered when a reaction is added to a message in a user/group conversation. | | `onMessageReactionRemoved(ReactionEvent reactionEvent)` | This event is triggered when a reaction is remove from a message in a user/group conversation. | -To add the `MessageListener`, you need to use the `addMessageListener()` method provided by the `CometChat` class. +Use `addMessageListener()` to register and `removeMessageListener()` to unregister. Call remove in `onPause()`. @@ -312,10 +317,6 @@ CometChat.addMessageListener(UNIQUE_LISTENER_ID, object : CometChat.MessageListe -where `UNIQUE_LISTENER_ID` is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events. - -Once the activity/fragment where the `MessageListener` is declared is not in use, you need to remove the listener using the `removeMessageListener()` method which takes the id of the listener to be removed as the parameter. We suggest you call this method in the `onPause()` method of the activity/fragment. - ## Call Listener The `CallListener` class provides you with live events related to calls. Below are the callback methods provided by the `CallListener` class. @@ -327,7 +328,7 @@ The `CallListener` class provides you with live events related to calls. Below a | `onOutgoingCallRejected(Call call)` | This event is triggered when the call initiated by the logged-in user is rejected by the recipient. The details of the call can be obtained from the Call object received as the method parameter | | `onIncomingCallCancelled(Call call)` | This event is triggered when an incoming call is canceled by the initiator of the call. The details of the call can be obtained from the Call object received as the method parameter | -To add the `CallListener`, you need to use the `addCallListener()` method provided by the `CometChat` class. +Use `addCallListener()` to register and `removeCallListener()` to unregister. Call remove in `onPause()`. @@ -382,6 +383,38 @@ CometChat.addCallListener(UNIQUE_LISTENER_ID, object : CometChat.CallListener() -Where `UNIQUE_LISTENER_ID` is the unique identifier for the listener. Make sure that no two listeners are added with the same listener ID as this could lead to unexpected behavior resulting in loss of events. +## Best Practices + + + + Remove all listeners in `onDestroy()` or `onPause()` to prevent memory leaks. Store listener IDs as constants to ensure consistent removal. + + + Use unique, descriptive listener IDs (e.g., "ChatActivity_MessageListener") to avoid conflicts. Never reuse the same ID for different listeners. + + + Register listeners in `onCreate()` or `onResume()` to ensure you don't miss any events. Register before fetching messages or loading conversations. + + + Implement all relevant callback methods even if you don't need them immediately. This prevents crashes if the SDK triggers unexpected events. + + + +--- -Once the activity/fragment where the `MessageListener` is declared is not in use, you need to remove the listener using the `removeMessageListener()` method which takes the id of the listener to be removed as the parameter. We suggest you call this method in the `onPause()` method of the activity/fragment. +## Next Steps + + + + Handle incoming messages with message listeners + + + Track user online/offline status with user listeners + + + Handle incoming calls with call listeners + + + Monitor group events with group listeners + + diff --git a/sdk/android/receive-messages.mdx b/sdk/android/receive-messages.mdx index 6cd321878..f025cd587 100644 --- a/sdk/android/receive-messages.mdx +++ b/sdk/android/receive-messages.mdx @@ -1,21 +1,28 @@ --- title: "Receive A Message" +sidebarTitle: "Receive Messages" +description: "Receive real-time messages, fetch missed and unread messages, retrieve message history, search messages, and get unread counts using the CometChat Android SDK." --- + +| Field | Value | +| --- | --- | +| Key Classes | `CometChat.MessageListener`, `MessagesRequestBuilder` | +| Key Methods | `addMessageListener()`, `fetchPrevious()`, `fetchNext()`, `getUnreadMessageCount()` | +| Listener Events | `onTextMessageReceived`, `onMediaMessageReceived`, `onCustomMessageReceived` | +| Prerequisites | SDK initialized, user logged in | -Receiving messages with CometChat has two parts: - -1. Adding a listener to receive [Real-time Messages](/sdk/android/receive-messages#real-time-messages) when your app is running -2. Calling a method to retrieve [Missed Messages](/sdk/android/receive-messages#missed-messages) when your app was not running + -## Real-time Messages +Receiving messages with CometChat has two parts: -*In other words, as a recipient, how do I receive messages when my app is running?* +1. Adding a [real-time listener](#real-time-messages) to receive messages while your app is running +2. Fetching [missed](#missed-messages), [unread](#unread-messages), or [historical](#message-history) messages when your app starts up or the user scrolls back -For every activity or fragment you wish to receive messages in, you need to register the `MessageListener` using the `addMessageListener()` method. +## Real-Time Messages -We suggest adding the listener in the `onResume()` method of the activity or the fragment where you wish to receive these events in. +Register a `MessageListener` to receive incoming messages as they arrive. For every activity or fragment you wish to receive messages in, register the listener using `addMessageListener()`. We suggest adding it in the `onResume()` method. @@ -39,9 +46,7 @@ CometChat.addMessageListener(listenerID, new CometChat.MessageListener() { } }); ``` - - ```kotlin val listenerID:String = "UNIQUE_LISTENER_ID" @@ -60,16 +65,14 @@ CometChat.addMessageListener(listenerID, object : MessageListener() { } }) ``` - - | Parameter | Description | | ------------ | ---------------------------------------------------------------------------------------------- | | `listenerID` | An ID that uniquely identifies that listener. We recommend using the activity or fragment name | -We recommend you remove the listener once the activity or fragment is not in use. Typically, this can be added in the `onPause()` method. +Remove the listener when you no longer need it. Typically, this can be added in the `onPause()` method. @@ -78,40 +81,30 @@ private String listenerID = "UNIQUE_LISTENER_ID"; CometChat.removeMessageListener(listenerID); ``` - - ```kotlin val listenerID:String = "UNIQUE_LISTENER_ID" CometChat.removeMessageListener(listenerID) ``` - - +Always remove listeners when they're no longer needed (e.g., in `onPause()`). Failing to do so can cause memory leaks and duplicate event handling. + + 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 the message history (say, last 100 messages) 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 them as they were 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 they were offline. - -Calling the `fetchNext()` method on the same object repeatedly allows you to fetch all the offline messages for the logged in user in a paginated manner - -### Fetch Missed Messages of a particular one-on-one conversation +Fetch messages that arrived while your app was offline. Use `getLastDeliveredMessageId()` to find where you left off, then call `fetchNext()` to get everything after that point. Call `fetchNext()` repeatedly on the same request object to paginate. + + ```java @@ -143,11 +136,9 @@ messagesRequest.fetchNext(new CometChat.CallbackListener>() { public void onError(CometChatException e) { Log.d(TAG, "Message fetching failed with exception: " + e.getMessage()); } -}); +}); ``` - - ```kotlin lateinit var messagesRequest: MessagesRequest @@ -183,13 +174,10 @@ messagesRequest.fetchNext(object : CallbackListener>() { } }) ``` - - - -### Fetch Missed Messages of a particular group conversation - + + ```java @@ -221,12 +209,10 @@ messagesRequest.fetchNext(new CometChat.CallbackListener>() { } }); ``` - - ```kotlin -lateinit var messagesRequest: MessagesReques +lateinit var messagesRequest: MessagesRequest val latestId = CometChat.getLastDeliveredMessageId() val limit: Int = 30 val GUID: String = "cometchat-uid-1" @@ -257,21 +243,20 @@ messagesRequest.fetchNext(object : CallbackListener>() { override fun onError(e: CometChatException) { Log.d(TAG, "Message fetching failed with exception: " + e.message) } -}) +}) ``` - - + + -## 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. +## Unread Messages -### Fetch Unread Messages of a particular one-on-one conversation +Fetch unread messages by adding `setUnread(true)` to the builder. Use `fetchPrevious()` to retrieve them. + + ```java @@ -301,9 +286,7 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( } }); ``` - - ```kotlin val UID:String = "cometchat-uid-1" @@ -330,13 +313,10 @@ messagesRequest.fetchPrevious(object : CallbackListener>() { } }) ``` - - - -### Fetch Unread Messages of a particular group conversation - + + ```java @@ -366,9 +346,7 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( } }); ``` - - ```kotlin val GUID:String = "cometchat-guid-1" @@ -395,26 +373,22 @@ messagesRequest.fetchPrevious(object : CallbackListener>() { } }) ``` - - + + -Base Message - 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 message history for a user or a group conversation?* -### Fetch Message History of a particular one-on-one conversation +## Message History -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. +Fetch the full conversation history using `fetchPrevious()`. Call it repeatedly on the same request object to paginate — useful for implementing upward scrolling. + + ```java @@ -442,11 +416,9 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( public void onError(CometChatException e) { Log.d(TAG, "Message fetching failed with exception: " + e.getMessage()); } -}); +}); ``` - - ```kotlin val limit: Int = 30 @@ -471,19 +443,12 @@ messagesRequest.fetchPrevious(object : CallbackListener>() { override fun onError(e: CometChatException) { Log.d(TAG, "Message fetching failed with exception: " + e.message) } -}) +}) ``` - - - -Calling the `fetchPrevious()` method on the same object repeatedly allows you to fetch all the previous messages in a paginated way. - -### Fetch Message History of a particular group 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 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. - + + ```java @@ -513,9 +478,7 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( } }); ``` - - ```kotlin val limit: Int = 30 @@ -542,36 +505,18 @@ messagesRequest.fetchPrevious(object : CallbackListener>() { } }) ``` - - + + -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. ## Search Messages -In other words, as a logged-in user, how do I search a message? - -In order to do this, you can use the `setSearchKeyword()` method. This method accepts string as input. When set, the SDK will fetch messages which match the `searchKeyword`. - - - -By default, the searchKey is searched only in message text. However, if you enable `Conversation & Advanced Search`, the searchKey will be searched in message text, file name, mentions & mime type of the file. - -The `Conversation & Advanced Search` is only available in `Advanced` & `Custom` [plans](https://www.cometchat.com/pricing). If you're already on one of these plans, please enable the `Conversation & Advanced Search` from [CometChat Dashboard](https://app.cometchat.com) (Open your app, navigate to Chats -> Settings -> General Configuration) - - - -| Feature | Basic Search | Advance Search | -| ---------------- | ------------ | -------------- | -| Text search | ✅ | ✅ | -| File name search | ❌ | ✅ | -| Mentions search | ❌ | ✅ | -| Mime type search | ❌ | ✅ | - -### Search Messages in a particular one-on-one conversation +Add `setSearchKeyword()` to the builder to filter messages by keyword. + + ```java @@ -598,9 +543,7 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( } }); ``` - - ```kotlin val limit: Int = 30 @@ -624,13 +567,10 @@ messagesRequest.fetchPrevious(object : CallbackListener>() { } }) ``` - - - -### Search Messages in a particular group conversation - + + ```java @@ -657,9 +597,7 @@ messagesRequest.fetchPrevious(new CometChat.CallbackListener>( } }); ``` - - ```kotlin val limit = 30 @@ -683,65 +621,65 @@ messagesRequest.fetchPrevious(object : CallbackListener>() { } }) ``` - - + + -## Unread Messages Count - -*In other words, as a logged-in user, how do I find out the number of unread messages that I have?* - -### Fetch Unread Message Count of a particular one-on-one conversation +### Search Capabilities -*In other words, how do I find out the number of unread messages I have from a particular user?* +By default, search only matches message text. With `Conversation & Advanced Search` enabled, it also matches file names, mentions, and MIME types. -In order to get the unread message count for a particular user (with respect to the logged-in user), you can use the `getUnreadMessageCountForUser()`. +| Feature | Basic Search | Advanced Search | +| ---------------- | ------------ | --------------- | +| Text search | ✅ | ✅ | +| File name search | ❌ | ✅ | +| Mentions search | ❌ | ✅ | +| Mime type search | ❌ | ✅ | -This method has the two variants: + +`Conversation & Advanced Search` is available on `Advanced` and `Custom` [plans](https://www.cometchat.com/pricing). Enable it from the [CometChat Dashboard](https://app.cometchat.com) under Chats → Settings → General Configuration. + - - -```java -CometChat.getUnreadMessageCountForUser(String UID, Callbacks); -``` - +## Unread Message Count - -```kotlin -CometChat.getUnreadMessageCountForUser(UID: String, Callbacks) -``` +CometChat provides several methods to get unread counts at different scopes. All return results via a callback listener with a `HashMap` mapping IDs to counts. - +Each method accepts an optional boolean parameter to exclude messages from blocked users. - +| Method | Scope | Returns | +| --- | --- | --- | +| `getUnreadMessageCountForUser(UID)` | Single user conversation | `HashMap` | +| `getUnreadMessageCountForGroup(GUID)` | Single group conversation | `HashMap` | +| `getUnreadMessageCountForAllUsers()` | All user conversations | `HashMap` | +| `getUnreadMessageCountForAllGroups()` | All group conversations | `HashMap` | +| `getUnreadMessageCount()` | Everything | `HashMap<"user"/"group", HashMap>` | -If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to `true`: +### Single Conversation ```java -CometChat.getUnreadMessageCountForUser(String UID, boolean hideMessagesFromBlockedUsers, Callbacks); -``` - - - - -```kotlin -CometChat.getUnreadMessageCountForUser(UID: String, hideMessagesFromBlockedUsers: Boolean, Callbacks) -``` +// One-on-one +private String UID = "cometchat-uid-1"; - +CometChat.getUnreadMessageCountForUser(UID, new CometChat.CallbackListener>() { + @Override + public void onSuccess(HashMap stringIntegerHashMap) { + // handle success + } - + @Override + public void onError(CometChatException e) { + // handle error + } +}); - - -```java -private String UID = "cometchat-uid-1" +// Group +private String GUID = "cometchat-guid-1"; -CometChat.getUnreadMessageCountForUser(UID, new CometChat.CallbackListener>() { +CometChat.getUnreadMessageCountForGroup(GUID, new CometChat.CallbackListener>(){ @Override public void onSuccess(HashMap stringIntegerHashMap) { // handle success @@ -753,13 +691,12 @@ CometChat.getUnreadMessageCountForUser(UID, new CometChat.CallbackListener - ```kotlin +// One-on-one val UID: String = "cometchat-uid-1" - + CometChat.getUnreadMessageCountForUser(UID, object : CallbackListener?>() { override fun onSuccess(stringIntegerHashMap: HashMap?) { // handle success @@ -769,66 +706,32 @@ CometChat.getUnreadMessageCountForUser(UID, object : CallbackListener - - - -In the `onSuccess()` callback, you will receive a Hashmap which will contain the `UID` of the user as the key and the unread message count as the value. - -### Fetch Unread Message Count of 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 two variants: - - -```java -CometChat.getUnreadMessageCountForGroup(String GUID, Callbacks); -``` +// Group +val GUID: String = "cometchat-guid-1" - +CometChat.getUnreadMessageCountForGroup(GUID, object : CallbackListener?>() { + override fun onSuccess(stringIntegerHashMap: HashMap?) { + // handle success + } - -```kotlin -CometChat.getUnreadMessageCountForUser(GUID: String, Callbacks) + override fun onError(e: CometChatException) { + // handle error + } +}) ``` - - -If you wish to ignore the messages from blocked users you can use the following syntax setting the boolean parameter to `true`: +### All Conversations ```java -CometChat.getUnreadMessageCountForGroup(String GUID, boolean hideMessagesFromBlockedUsers, Callbacks); -``` - - - - -```kotlin -CometChat.getUnreadMessageCountForUser(GUID: String, hideMessagesFromBlockedUsers: Boolean, Callbacks) -``` - - - - - - - -```java -private String GUID = "cometchat-guid-1" - -CometChat.getUnreadMessageCountForGroup(GUID, new CometChat.CallbackListener>(){ +// All users and groups combined +CometChat.getUnreadMessageCount(new CometChat.CallbackListener>>() { @Override - public void onSuccess(HashMap stringIntegerHashMap) { + public void onSuccess(HashMap> stringHashMapHashMap) { // handle success } @@ -837,94 +740,37 @@ CometChat.getUnreadMessageCountForGroup(GUID, new CometChat.CallbackListener - - -```kotlin -val GUID: String = "cometchat-guid-1" - -CometChat.getUnreadMessageCountForGroup(GUID, object : CallbackListener?>() { - override fun onSuccess(stringIntegerHashMap: HashMap?) { - // handle success - } - override fun onError(e: CometChatException) { - // handle error - } +// All user conversations only +CometChat.getUnreadMessageCountForAllUsers(new CometChat.CallbackListener>() { + @Override + public void onSuccess(HashMap stringIntegerHashMap) { + // Handle Success } -) -``` - - - - - -In the `onSuccess()` callback, you will receive a Hashmap which will contain the `GUID` of the group as the key and the unread message count as the value. - -### Fetch Unread Message Count of 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 counts, you can use the `getUnreadMessageCount()` method. This method has two variants: - - - -```java -CometChat.getUnreadMessageCount(Callbacks); -``` - - - - -```kotlin -CometChat.getUnreadMessageCount(Callbacks) -``` - - - - - -If you wish to ignore the messages from blocked users you can use the following syntax setting the boolean parameter to `true`: - - - -```java -CometChat.getUnreadMessageCount(boolean hideMessagesFromBlockedUsers, Callbacks); -``` - - - - -```kotlin -CometChat.getUnreadMessageCount(hideMessagesFromBlockedUsers: Boolean, Callbacks) -``` - - - + @Override + public void onError(CometChatException e) { + // Handle Error + } +}); - - -```java -CometChat.getUnreadMessageCount(new CometChat.CallbackListener>>() { +// All group conversations only +CometChat.getUnreadMessageCountForAllGroups(new CometChat.CallbackListener>() { @Override - public void onSuccess(HashMap> stringHashMapHashMap) { - // handle success + public void onSuccess(HashMap stringIntegerHashMap) { + // Handle success } @Override public void onError(CometChatException e) { - // handle error + // Handle Error } -}); +}); ``` - - ```kotlin +// All users and groups combined CometChat.getUnreadMessageCount(object : CallbackListener?>?>() { override fun onSuccess(stringHashMapHashMap: HashMap?>?) { // handle success @@ -934,175 +780,267 @@ CometChat.getUnreadMessageCount(object : CallbackListener - - - -In the `onSuccess()` callback, you will receive a hashmap having two keys: - -1. `user` - The value for this key holds another hashmap that holds the `UIDs` of users and their corresponding unread message counts. -2. `group` - The value for this key holds another hashmap that holds the `GUIDs` of groups and their corresponding unread message counts. - -### Fetch Unread Message Count of all one-on-one conversations - -*In other words, how do I find out the number of unread messages I have for every user?* - -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: +// All user conversations only +CometChat.getUnreadMessageCountForAllUsers(object : CometChat.CallbackListener>() { + override fun onSuccess(stringIntegerHashMap: HashMap) { + Log.d(TAG,"onSuccess: ${stringIntegerHashMap.size}") + } - - -```java -CometChat.getUnreadMessageCountForAllUsers(Callbacks); -``` + override fun onError(e: CometChatException) { + Log.d(TAG,"onError: ${e.message}") + } +}) - +// All group conversations only +CometChat.getUnreadMessageCountForAllGroups(object : CometChat.CallbackListener>() { + override fun onSuccess(stringIntegerHashMap: HashMap) { + Log.d(TAG,"onSuccess: ${stringIntegerHashMap.size}") + } - -```kotlin -CometChat.getUnreadMessageCountForAllUsers(Callbacks) + override fun onError(e: CometChatException) { + Log.e(TAG,"onError: ${e.message}") + } +}) ``` - - -If you wish to ignore the messages from blocked users you can use the following syntax setting the boolean parameter to `true`: - - - -```java -CometChat.getUnreadMessageCountForAllUsers(boolean hideMessagesFromBlockedUsers, Callbacks); -``` - - - - -```kotlin -CometChat.getUnreadMessageCountForAllUsers(hideMessagesFromBlockedUsers: Boolean, Callbacks) -``` - - +### Excluding Blocked Users - +Pass `true` as the second argument to any of the methods above to ignore messages from blocked users: ```java -CometChat.getUnreadMessageCountForAllUsers(new CometChat.CallbackListener>() { -@Override - public void onSuccess(HashMap stringIntegerHashMap) { - // Handle Success -} - -@Override - public void onError(CometChatException e) { - // Handle Error -} -}); +CometChat.getUnreadMessageCountForUser(UID, true, Callbacks); +CometChat.getUnreadMessageCountForGroup(GUID, true, Callbacks); +CometChat.getUnreadMessageCount(true, Callbacks); +CometChat.getUnreadMessageCountForAllUsers(true, Callbacks); +CometChat.getUnreadMessageCountForAllGroups(true, Callbacks); ``` - - ```kotlin -CometChat.getUnreadMessageCountForAllUsers(object : CometChat.CallbackListener>() { -override fun onSuccess(stringIntegerHashMap: HashMap) { - Log.d(TAG,"onSuccess: ${stringIntegerHashMap.size}") -} - -override fun onError(e: CometChatException) { - Log.d(TAG,"onError: ${e.message}") -} -}) +CometChat.getUnreadMessageCountForUser(UID, true, Callbacks) +CometChat.getUnreadMessageCountForGroup(GUID, true, Callbacks) +CometChat.getUnreadMessageCount(true, Callbacks) +CometChat.getUnreadMessageCountForAllUsers(true, Callbacks) +CometChat.getUnreadMessageCountForAllGroups(true, Callbacks) ``` - - -In the `onSuccess()` callback, you will receive a Hashmap that will contain the `UIDs` of users as the key and the unread message counts as the values. - -### Fetch Unread Message Count of all group conversations -*In other words, how do I find out the number of unread messages for every group?* +## Get Message Details -In order to fetch the unread message counts for all groups, you can use the `getUnreadMessageCountForAllGroups()` method. - -This method has two variants: +Use `getMessageDetails()` to fetch a specific message by its ID. Returns the full message object (`TextMessage`, `MediaMessage`, `CustomMessage`, or other `BaseMessage` subclass). ```java -CometChat.getUnreadMessageCountForAllGroups(Callbacks); -``` - - - - -```kotlin -CometChat.getUnreadMessageCountForAllGroups(Callbacks) -``` +int messageId = MESSAGE_ID; - - - - -If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to `true`: +CometChat.getMessageDetails(messageId, new CometChat.CallbackListener() { + @Override + public void onSuccess(BaseMessage message) { + Log.d(TAG, "Message details fetched: " + message.toString()); + } - - -```java -CometChat.getUnreadMessageCountForAllGroups(boolean hideMessagesFromBlockedUsers, Callbacks); + @Override + public void onError(CometChatException e) { + Log.d(TAG, "Error fetching message details: " + e.getMessage()); + } +}); ``` - - ```kotlin -CometChat.getUnreadMessageCountForAllGroups(hideMessagesFromBlockedUsers: Boolean, Callbacks) -``` - - +val messageId: Int = MESSAGE_ID - - - - -```java -CometChat.getUnreadMessageCountForAllGroups(new CometChat.CallbackListener>() { -@Override - public void onSuccess(HashMap stringIntegerHashMap) { - // Handle success -} +CometChat.getMessageDetails(messageId, object : CometChat.CallbackListener() { + override fun onSuccess(message: BaseMessage) { + Log.d(TAG, "Message details fetched: $message") + } -@Override - public void onError(CometChatException e) { - // Handle Error -} -}); + override fun onError(e: CometChatException) { + Log.d(TAG, "Error fetching message details: ${e.message}") + } +}) ``` - + - -```kotlin -CometChat.getUnreadMessageCountForAllGroups(object : CometChat.CallbackListener>() { -override fun onSuccess(stringIntegerHashMap: HashMap) { - Log.d(TAG,"onSuccess: ${stringIntegerHashMap.size}") -} +| Parameter | Type | Description | +| ----------- | ----- | -------------------------------- | +| `messageId` | `int` | The ID of the message to fetch | + +--- -override fun onError(e: CometChatException) { - Log.e(TAG,"onError: ${e.message}") +## Next Steps + + + + Track when messages are delivered and read by recipients + + + Show real-time typing status in conversations + + + Advanced filtering options for message history + + + Send text, media, and custom messages + + + + +## Message Payload Structure + + + +The `BaseMessage` object returned by SDK methods contains the following fields. `TextMessage`, `MediaMessage`, and `CustomMessage` extend this base structure with additional type-specific fields. + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | long | Unique message identifier | +| `muid` | String | Developer-defined message ID for deduplication | +| `sender` | [User](#user-object) | User who sent the message | +| `receiver` | AppEntity | Message receiver ([User](#user-object) or [Group](#group-object)) | +| `receiverUid` | String | Receiver's unique identifier | +| `type` | String | Message type. Values: `"text"`, `"image"`, `"video"`, `"audio"`, `"file"`, `"custom"` | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `category` | String | Message category. Values: `"message"`, `"action"`, `"call"`, `"custom"` | +| `sentAt` | long | Unix timestamp when message was sent | +| `deliveredAt` | long | Unix timestamp when message was delivered | +| `readAt` | long | Unix timestamp when message was read | +| `metadata` | JSONObject | Custom message metadata set by developer | +| `readByMeAt` | long | Unix timestamp when logged-in user read the message | +| `deliveredToMeAt` | long | Unix timestamp when message was delivered to logged-in user | +| `deletedAt` | long | Unix timestamp when message was deleted (0 if not deleted) | +| `editedAt` | long | Unix timestamp when message was edited (0 if not edited) | +| `deletedBy` | String | UID of user who deleted the message (null if not deleted) | +| `editedBy` | String | UID of user who edited the message (null if not edited) | +| `updatedAt` | long | Unix timestamp of last message update | +| `conversationId` | String | Associated conversation identifier | +| `parentMessageId` | long | Parent message ID for threaded messages (0 if not a reply) | +| `replyCount` | int | Number of replies in thread | +| `unreadRepliesCount` | int | Number of unread replies in thread | +| `mentionedUsers` | Array\<[User](#user-object)\> | List of users mentioned in the message | +| `hasMentionedMe` | boolean | Whether the logged-in user is mentioned | +| `reactions` | Array\<[ReactionCount](#reactioncount-object)\> | List of reaction counts on the message | +| `rawMessage` | JSONObject | Raw JSON message data | +| `quotedMessageId` | long | ID of the quoted message (0 if not quoting) | +| `quotedMessage` | BaseMessage | The quoted message object (null if not quoting) | + +**Sample BaseMessage Object:** + +```json +{ + "id": 12345, + "muid": "msg_abc123", + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default", + "lastActiveAt": 1699900000 + }, + "receiver": { + "uid": "user_456", + "name": "Jane Smith", + "avatar": "https://example.com/avatar2.png" + }, + "receiverUid": "user_456", + "type": "text", + "receiverType": "user", + "category": "message", + "sentAt": 1699900000, + "deliveredAt": 1699900001, + "readAt": 1699900002, + "metadata": { + "priority": "high", + "customField": "value" + }, + "readByMeAt": 1699900002, + "deliveredToMeAt": 1699900001, + "deletedAt": 0, + "editedAt": 0, + "deletedBy": null, + "editedBy": null, + "updatedAt": 1699900000, + "conversationId": "user_123_user_456", + "parentMessageId": 0, + "replyCount": 5, + "unreadRepliesCount": 2, + "mentionedUsers": [], + "hasMentionedMe": false, + "reactions": [ + { + "reaction": "👍", + "count": 3, + "reactedByMe": true + } + ], + "rawMessage": {}, + "quotedMessageId": 0, + "quotedMessage": null } -}) ``` - - - - -In the `onSuccess()` callback, you will receive a hashmap which will contain the `GUIDs` of the groups as the key and the unread message counts as the values. + + + + +The nested `User` object (used in `sender`, `receiver`, `mentionedUsers`) contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + + + + + +The nested `Group` object (used in `receiver` for group messages) contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `guid` | String | Unique identifier of the group | +| `name` | String | Display name of the group | +| `type` | String | Group type. Values: `"public"`, `"private"`, `"password"` | +| `icon` | String | URL to group icon | +| `description` | String | Group description | +| `owner` | String | UID of group owner | +| `metadata` | JSONObject | Custom data set by developer | +| `membersCount` | int | Number of group members | +| `tags` | Array\ | List of tags for group identification | +| `hasJoined` | boolean | Whether logged-in user has joined | +| `scope` | String | User's scope in group. Values: `"admin"`, `"moderator"`, `"participant"` | + + + + + +The nested `ReactionCount` object (used in `reactions` array) contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `reaction` | String | The reaction emoji | +| `count` | int | Total count of this reaction | +| `reactedByMe` | boolean | Whether the logged-in user has reacted with this emoji | + + diff --git a/sdk/android/recording.mdx b/sdk/android/recording.mdx index 8f497bb70..e37887fd5 100644 --- a/sdk/android/recording.mdx +++ b/sdk/android/recording.mdx @@ -1,18 +1,32 @@ --- title: "Recording" +sidebarTitle: "Recording" +description: "Record calls and access recordings from the Dashboard using the Android SDK" --- + +```kotlin +// Configure call settings with recording +val callSettings = CallSettingsBuilder(this, videoContainer) + .setSessionId(sessionId) + .showCallRecordButton(true) + .startRecordingOnCallStart(false) + .build() + +// Start recording manually +CallManager.getInstance().startRecording() + +// Stop recording +CallManager.getInstance().stopRecording() +``` + -This section will guide you to implement call recording feature for the voice and video calls. +Record voice and video calls for playback, compliance, or archival purposes. Recording is built on top of the [Call Session](/sdk/android/direct-calling) — add recording listeners to your call settings and optionally control recording programmatically. ## Implementation -Once you have decided to implement [Default Calling](/sdk/android/default-calling) or [Direct Calling](/sdk/android/direct-calling) calling and followed the steps to implement them. Just few additional listeners and methods will help you quickly implement call recording in your app. - -You need to make changes in the CometChat.startCall method and add the required listeners for recording. Please make sure your callSettings is configured accordingly for [Default Calling](/sdk/android/default-calling) or [Direct Calling](/sdk/android/direct-calling). - -A basic example of how to make changes to implement recording for a direct/default call: +Add `onRecordingStarted` and `onRecordingStopped` callbacks to your `OngoingCallListener` when building call settings. These fire for all participants when any user starts or stops recording. @@ -56,9 +70,9 @@ CometChat.startCall(callSettings, object : OngoingCallListener { ## Settings for call recording -The `CallSettings`class allows you to customise the overall calling experience. The properties for the call/conference can be set using the `CallSettingsBuilder` class. This will eventually give you and object of the `CallSettings` class which you can pass to the `startCall()` method to start the call. +The `CallSettings` class allows you to customize the overall calling experience. The properties for the call/conference can be set using the `CallSettingsBuilder` class. This gives you an object of the `CallSettings` class which you can pass to the `startCall()` method to start the call. -The options available for recording of calls are: +The options available for recording calls are: | Setting | Description | | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | @@ -67,8 +81,6 @@ The options available for recording of calls are: ### Start Recording -You can use the startRecording() method to start call recording. - ```java @@ -88,8 +100,6 @@ CallManager.getInstance().startRecording() ### Stop Recording -You can use the stopRecording() method to stop call recording. - ```java @@ -106,3 +116,40 @@ CallManager.getInstance().stopRecording() + + +## Best Practices + + + + Always notify participants when recording starts or stops. Use the `onRecordingStarted()` and `onRecordingStopped()` callbacks to display visual indicators (e.g., a red recording icon) in your UI. + + + Ensure your app has the necessary permissions to record audio and video. Request permissions at runtime and handle cases where users deny recording permissions. + + + Only enable `startRecordingOnCallStart(true)` when required by your use case (e.g., compliance, training). For most applications, let users manually control recording to respect privacy. + + + Recordings are stored on CometChat servers and can be accessed from the [Dashboard](https://app.cometchat.com). Inform users where they can find their recordings after the call ends. + + + +--- + +## Next Steps + + + + Implement complete calling workflow with incoming/outgoing call UI + + + Start and manage call sessions with video and audio controls + + + Retrieve and display call history for users and groups + + + Access and manage call recordings from the CometChat Dashboard + + diff --git a/sdk/android/resources-overview.mdx b/sdk/android/resources-overview.mdx deleted file mode 100644 index 59e91dad5..000000000 --- a/sdk/android/resources-overview.mdx +++ /dev/null @@ -1,11 +0,0 @@ ---- -title: "Resources" ---- - - - -We have a number of resources that will help you while integrating CometChat in your app. - -You can begin with the [all real-time listeners](/sdk/android/real-time-listeners) guide. - -If you're upgrading from v1, we recommend reading our [upgrading from v3](/sdk/android/upgrading-from-v3-guide) guide. diff --git a/sdk/android/retrieve-conversations.mdx b/sdk/android/retrieve-conversations.mdx index 62aa4de7a..8f1454178 100644 --- a/sdk/android/retrieve-conversations.mdx +++ b/sdk/android/retrieve-conversations.mdx @@ -1,22 +1,37 @@ --- title: "Retrieve Conversations" +sidebarTitle: "Retrieve Conversations" +description: "Fetch, filter, tag, and search conversations using the CometChat Android SDK." --- + +```kotlin +// Fetch conversations with filters +val conversationsRequest = ConversationsRequestBuilder() + .setLimit(50) + .setConversationType(CometChatConstants.CONVERSATION_TYPE_USER) + .build() -Conversations provide the last messages for every one-on-one and group conversation the logged-in user is a part of. This makes it easy for you to build a **Recent Chat** list. +conversationsRequest.fetchNext(object : CallbackListener>() { + override fun onSuccess(conversations: List) { } + override fun onError(e: CometChatException) { } +}) -## Retrieve List of Conversations +// Get specific conversation +CometChat.getConversation("UID_or_GUID", "user", callback) +``` + -*In other words, as a logged-in user, how do I retrieve the latest conversations that I've been a part of?* +Conversations provide the last messages for every one-on-one and group conversation the logged-in user is a part of. This makes it easy for you to build a **Recent Chat** list. -To fetch the list of conversations, you can use the `ConversationsRequest` class. To use this class i.e. to create an object of the `ConversationsRequest` class, you need to use the `ConversationsRequestBuilder` class. The `ConversationsRequestBuilder` class allows you to set the parameters based on which the conversations are to be fetched. +## Retrieve List of Conversations -The `ConversationsRequestBuilder` class allows you to set the below parameters: +Use `ConversationsRequestBuilder` to configure filters, then call `fetchNext()` to retrieve up to 50 conversations per request. ### Set Limit -This method sets the limit i.e. the number of conversations that should be fetched in a single iteration. +Set the number of conversations to fetch per request. @@ -41,12 +56,7 @@ val conversationRequest = ConversationsRequestBuilder() ### Set Conversation Type -This method can be used to fetch user or group conversations specifically. The `conversationType` variable can hold one of the below two values: - -* user - Only fetches user conversation. -* group - Only fetches group conversations. - -If none is set, the list of conversations will include both user and group conversations. +Filter by conversation type: `user` for one-on-one or `group` for group conversations. If not set, both types are returned. @@ -73,7 +83,7 @@ val conversationsRequest = ConversationsRequestBuilder() ### With User and Group Tags -This method can be used to fetch the user/group tags in the `Conversation` Object. By default the value is false. +Use `withUserAndGroupTags(true)` to include user/group tags in the response. Default is `false`. @@ -100,7 +110,7 @@ val conversationsRequest = ConversationsRequestBuilder() ### Set User Tags -This method fetches user conversations that have the specified tags. +Fetch user conversations where the user has specific tags. @@ -131,6 +141,8 @@ conversationsRequest = ConversationsRequestBuilder() ### Set Group Tags +Fetch group conversations where the group has specific tags. + ```java @@ -162,7 +174,13 @@ This method fetches group conversations that have the specified tags. ### With Tags -This method makes sure that the tags associated with the conversations are returned along with the other details of the conversations. The default value for this parameter is `false` +Use `withTags(true)` to include conversation tags in the response. Default is `false`. + +When `withTags(true)` is set, each conversation's `tags` field will be populated. Access tags using `getTags()`. + +| Additional Field | Getter | Return Type | Description | +| --- | --- | --- | --- | +| tags | `getTags()` | `List` | Tags associated with the conversation | @@ -189,7 +207,7 @@ val conversationsRequest = ConversationsRequestBuilder() ### Set Tags -This method helps you fetch the conversations based on the specified tags. +Fetch conversations that have specific tags. @@ -220,7 +238,7 @@ val conversationsRequest = ConversationsRequestBuilder() ### Include Blocked Users -This method helps you fetch the conversations of users whom the logged-in user has blocked. +Use `includeBlockedUsers(true)` to include conversations with users you've blocked. @@ -247,7 +265,7 @@ val conversationsRequest = ConversationsRequestBuilder() ### With Blocked Info -This method helps you fetch the conversations of users whom the logged-in user has blocked. +Use `withBlockedInfo(true)` to include blocked user information in the response. @@ -274,7 +292,7 @@ val conversationsRequest = ConversationsRequestBuilder() ### Search Conversations -This method helps you search a conversation based on User or Group name. +Use `setSearchKeyword()` to search conversations by user or group name. @@ -307,7 +325,7 @@ val conversationsRequest = ConversationsRequestBuilder() ### Unread Conversations -This method helps you fetch unread conversations. +Use `setUnread(true)` to fetch only conversations with unread messages. @@ -338,11 +356,9 @@ val conversationsRequest = ConversationsRequestBuilder() -Finally, once all the parameters are set to the builder class, you need to call the `build()` method to get the object of the `ConversationsRequest` class. - -Once you have the object of the `ConversationsRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `Conversation` objects containing X number of conversations depending on the limit set. +### Fetch Conversations -A Maximum of only 50 Conversations can be fetched at once. +After configuring the builder, call `build()` to create the request, then `fetchNext()` to retrieve conversations. Maximum 50 per request. Call `fetchNext()` repeatedly on the same object to paginate. @@ -352,38 +368,33 @@ ConversationsRequest conversationsRequest = new ConversationsRequest.Conversatio conversationsRequest.fetchNext(new CometChat.CallbackListener>() { @Override public void onSuccess(List conversations) { - // Hanlde list of conversations - + // Handle list of conversations } @Override public void onError(CometChatException e) { - // Hanlde failure + // Handle failure } }); ``` - - ```kotlin var conversationRequest: ConversationsRequest? = null -val LIMIT:Int=30 +val LIMIT: Int = 30 conversationRequest = ConversationsRequest.ConversationsRequestBuilder().setLimit(LIMIT).build() conversationRequest?.fetchNext(object : CometChat.CallbackListener>() { override fun onSuccess(p0: List?) { - //Handle List of Conversations + // Handle List of Conversations } override fun onError(p0: CometChatException?) { - //Handle Failure + // Handle Failure } }) ``` - - The `Conversation` object consists of the following fields: @@ -398,18 +409,13 @@ The `Conversation` object consists of the following fields: ## Tag Conversation -*In other words, as a logged-in user, how do I tag a conversation?* - -To tag a specific conversation, you can use the `tagConversation()` method. The `tagConversation()` method accepts three parameters. +Use `tagConversation()` to add tags to a conversation. -1. `conversationWith`: UID/GUID of the user/group whose conversation you want to fetch. - -2. `conversationType`: The `conversationType` variable can hold one of the below two values: - - 1. user - Only fetches user conversation. - 2. group - Only fetches group conversations. - -3. `tags`: The `tags` variable will be a list of tags you want to add to a conversation. +| Parameter | Description | +| --- | --- | +| `conversationWith` | UID or GUID of the user/group | +| `conversationType` | `user` or `group` | +| `tags` | List of tags to add | @@ -466,15 +472,12 @@ The tags for conversations are one-way. This means that if user A tags a convers ## Retrieve Single Conversation -*In other words, as a logged-in user, how do I retrieve a specific conversation?* - -In order to fetch a specific conversation, you can use the `getConversation` method. The `getConversation` method accepts two parameters. +Use `getConversation()` to fetch a specific conversation. -1. `conversationWith`: UID/GUID of the user/group whose conversation you want to fetch. -2. `conversationType`: The `conversationType` variable can hold one of the below two values: - -* user - Only fetches user conversation. -* group - Only fetches group conversations. +| Parameter | Description | +| --- | --- | +| `conversationWith` | UID or GUID of the user/group | +| `conversationType` | `user` or `group` | @@ -515,7 +518,7 @@ CometChat.getConversation(conversationWith, conversationType, new CometChat.Call ## Convert Messages to Conversations -As per our [Receive Messages](/sdk/android/receive-messages) guide, for real-time messages, you will always receive `Message` objects and not `Conversation` objects. Thus, you will need a mechanism to convert the `Message` object to a `Conversation` object. You can use the `getConversationFromMessage` method for this purpose. +Use `CometChatHelper.getConversationFromMessage()` to convert a received message into a `Conversation` object. Useful for updating your Recent Chats list when receiving real-time messages. @@ -539,3 +542,189 @@ val conversation = CometChatHelper.getConversationFromMessage(message) While converting a `Message` object to a `Conversation` object, the `unreadMessageCount` & `tags` will not be available in the `Conversation` object. The unread message count needs to be managed in your client-side code. + +## Conversation Payload Structure + + + +The `Conversation` object returned by SDK methods contains the following fields: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `conversationId` | String | Unique conversation identifier | +| `conversationType` | String | Type of conversation. Values: `"user"`, `"group"` | +| `conversationWith` | AppEntity | The [User](#user-object-conversation) or [Group](#group-object-conversation) in the conversation | +| `lastMessage` | [BaseMessage](#lastmessage-object) | Last message in the conversation | +| `unreadMessageCount` | int | Number of unread messages in the conversation | +| `unreadMentionsCount` | int | Number of unread mentions in the conversation | +| `updatedAt` | long | Unix timestamp of last conversation update | +| `tags` | Array\ | List of tags for conversation organization | +| `lastReadMessageId` | long | ID of the last read message | +| `latestMessageId` | long | ID of the latest message | + +**Sample Conversation Object:** + +```json +{ + "conversationId": "user_123_user_456", + "conversationType": "user", + "conversationWith": { + "uid": "user_456", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default", + "lastActiveAt": 1699900000, + "tags": ["premium", "verified"] + }, + "lastMessage": { + "id": 12345, + "muid": "msg_abc123", + "type": "text", + "receiverType": "user", + "category": "message", + "sentAt": 1699900000, + "deliveredAt": 1699900001, + "readAt": 1699900002, + "metadata": {"priority": "high"}, + "conversationId": "user_123_user_456" + }, + "unreadMessageCount": 3, + "unreadMentionsCount": 1, + "updatedAt": 1699900000, + "tags": ["important", "work"], + "lastReadMessageId": 12340, + "latestMessageId": 12345 +} +``` + + + + + +When `conversationType` is `"user"`, the `conversationWith` field contains a User object: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + + + + + +When `conversationType` is `"group"`, the `conversationWith` field contains a Group object: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `guid` | String | Unique identifier of the group | +| `name` | String | Display name of the group | +| `type` | String | Group type. Values: `"public"`, `"private"`, `"password"` | +| `icon` | String | URL to group icon | +| `description` | String | Group description | +| `owner` | String | UID of group owner | +| `metadata` | JSONObject | Custom data set by developer | +| `membersCount` | int | Number of group members | +| `tags` | Array\ | List of tags for group identification | +| `hasJoined` | boolean | Whether logged-in user has joined | +| `scope` | String | User's scope in group. Values: `"admin"`, `"moderator"`, `"participant"` | +| `createdAt` | long | Unix timestamp when group was created | +| `updatedAt` | long | Unix timestamp of last group update | + + + + + +The `lastMessage` field contains a BaseMessage object with key fields: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | long | Unique message identifier | +| `muid` | String | Developer-defined message ID | +| `type` | String | Message type. Values: `"text"`, `"image"`, `"video"`, `"audio"`, `"file"`, `"custom"` | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `category` | String | Message category. Values: `"message"`, `"action"`, `"call"` | +| `sentAt` | long | Unix timestamp when message was sent | +| `deliveredAt` | long | Unix timestamp when message was delivered | +| `readAt` | long | Unix timestamp when message was read | +| `metadata` | JSONObject | Custom message metadata | +| `conversationId` | String | Associated conversation identifier | +| `sender` | User | User who sent the message | + + + +## Best Practices + + + + Set a reasonable limit (30-50) and fetch conversations in batches. Maximum limit is 50 conversations per request. + + + Use `setConversationType()` to fetch only user or group conversations when building separate tabs or views. + + + Enable "Conversation & Advanced Search" in Dashboard and use `setSearchKeyword()` for better user experience with many conversations. + + + Tag conversations (e.g., "archived", "pinned", "important") to organize and filter conversations based on user preferences. + + + When converting messages to conversations, manage unread counts in your app since they're not included in the conversion. + + + +## Troubleshooting + + + + **Symptom:** `fetchNext()` returns an empty list even though conversations exist. + + **Cause:** User hasn't sent or received any messages, or filters are too restrictive. + + **Solution:** Remove filters temporarily to verify conversations exist. Check if the user has participated in any chats. + + + **Symptom:** `setSearchKeyword()` doesn't filter conversations. + + **Cause:** "Conversation & Advanced Search" feature not enabled in Dashboard. + + **Solution:** Enable the feature in [CometChat Dashboard](https://app.cometchat.com) under Chats → Settings → General Configuration. Available in Advanced & Custom plans only. + + + **Symptom:** `setUnread(true)` doesn't filter unread conversations. + + **Cause:** "Conversation & Advanced Search" feature not enabled. + + **Solution:** Enable the feature in Dashboard. This is a premium feature available in Advanced & Custom plans. + + + +--- + +## Next Steps + + + + Remove conversations from the list + + + Show when users are typing + + + Track message delivery and read status + + + Start sending messages in conversations + + diff --git a/sdk/android/retrieve-group-members.mdx b/sdk/android/retrieve-group-members.mdx index 46001c72a..c4735260c 100644 --- a/sdk/android/retrieve-group-members.mdx +++ b/sdk/android/retrieve-group-members.mdx @@ -1,20 +1,33 @@ --- title: "Retrieve Group Members" +sidebarTitle: "Retrieve Members" +description: "Fetch and filter group members list using the Android SDK" --- + +```kotlin +// Fetch group members with filters +val groupMembersRequest = GroupMembersRequestBuilder("GUID") + .setLimit(30) + .setSearchKeyword("search") + .setScopes(listOf("admin", "moderator")) + .build() + +groupMembersRequest.fetchNext(object : CallbackListener>() { + override fun onSuccess(members: List) { } + override fun onError(e: CometChatException) { } +}) +``` + ## Retrieve the List of Group Members -In order to fetch the list of groups members for a group, you can use the `GroupMembersRequest` class. To use this class i.e to create an object of the `GroupMembersRequest` class, you need to use the `GroupMembersRequestBuilder` class. The `GroupMembersRequestBuilder` class allows you to set the parameters based on which the groups are to be fetched. - -The `GroupMembersRequestBuilder` class allows you to set the below parameters: - -The `GUID` of the group for which the members are to be fetched must be specified in the constructor of the `GroupMembersRequestBuilder` class. +Use `GroupMembersRequestBuilder` with the group's GUID to configure filters, then call `fetchNext()`. ### Set Limit -This method sets the limit i.e. the number of members that should be fetched in a single iteration. +Set the number of members to fetch per request. @@ -39,7 +52,7 @@ val groupMembersRequest = GroupMembersRequestBuilder(GUID) ### Set Search Keyword -This method allows you to set the search string based on which the group members are to be fetched. +Filter members by a search string. @@ -64,7 +77,7 @@ val groupMembersRequest = GroupMembersRequestBuilder(GUID) ### Set Scopes -This method allows you to fetch group members based on the specified scopes. +Filter members by scope (e.g., `"admin"`, `"moderator"`, `"participant"`). @@ -93,9 +106,9 @@ val groupMembersRequest = GroupMembersRequestBuilder(GUID) -Finally, once all the parameters are set to the builder class, you need to call the `build()` method to get the object of the `GroupMembersRequest` class. +### Fetch Members -Once you have the object of the `GroupMembersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `GroupMember` objects containing N number of members depending on the limit set. +After configuring the builder, call `build()` then `fetchNext()` to retrieve members. @@ -141,3 +154,117 @@ groupMembersRequest?.fetchNext(object:CometChat.CallbackListener + + +## GroupMember Payload Structure + + + +The `GroupMember` object extends `User` and contains all User fields plus group-specific fields: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | +| `scope` | String | Member's scope in the group. Values: `"admin"`, `"moderator"`, `"participant"` | +| `joinedAt` | long | Unix timestamp when member joined the group | + +**Sample GroupMember Object:** + +```json +{ + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "link": "https://example.com/profile", + "role": "default", + "metadata": { + "department": "Engineering", + "title": "Senior Developer" + }, + "status": "online", + "statusMessage": "Available", + "lastActiveAt": 1699900000, + "hasBlockedMe": false, + "blockedByMe": false, + "tags": ["premium", "verified"], + "deactivatedAt": 0, + "scope": "admin", + "joinedAt": 1699850000 +} +``` + + + +## Best Practices + + + + Set an appropriate limit (e.g., 30) and call `fetchNext()` multiple times to load members in batches, especially for large groups. + + + Use `setScopes()` to fetch only admins and moderators when building admin panels or management interfaces. + + + Use `setSearchKeyword()` to allow users to quickly find specific members by name or UID in large groups. + + + Cache fetched member lists locally to reduce API calls and improve performance when users navigate back to member lists. + + + +## Troubleshooting + + + + **Symptom:** `fetchNext()` returns an empty list even though the group has members. + + **Cause:** The GUID is incorrect, or filters are too restrictive. + + **Solution:** Verify the GUID is correct. Remove filters temporarily to check if members exist. + + + **Symptom:** `fetchNext()` fails with "Not a member" error. + + **Cause:** User is not a member of the group (for private groups). + + **Solution:** Ensure the user has joined the group before fetching members. Use [join-group](/sdk/android/join-group) first. + + + **Symptom:** `setScopes()` returns all members instead of filtered results. + + **Cause:** Scope values must be lowercase strings: "admin", "moderator", "participant". + + **Solution:** Use lowercase scope values. Check the [Group Class](/sdk/android/create-group#group-class) for valid scope constants. + + + +--- + +## Next Steps + + + + Add new members to groups + + + Remove members from groups + + + Update member roles and permissions + + + Fetch list of available groups + + diff --git a/sdk/android/retrieve-groups.mdx b/sdk/android/retrieve-groups.mdx index 7b1878481..0d67b011f 100644 --- a/sdk/android/retrieve-groups.mdx +++ b/sdk/android/retrieve-groups.mdx @@ -1,18 +1,36 @@ --- title: "Retrieve Groups" +sidebarTitle: "Retrieve Groups" +description: "Fetch and filter groups list using the Android SDK" --- + +```kotlin +// Fetch groups with filters +val groupsRequest = GroupsRequestBuilder() + .setLimit(30) + .joinedOnly(true) + .setSearchKeyWord("search") + .build() + +groupsRequest.fetchNext(object : CallbackListener>() { + override fun onSuccess(groups: List) { } + override fun onError(e: CometChatException) { } +}) -## Retrieve List of Groups +// Get specific group details +CometChat.getGroup("GUID", callback) +``` + -*In other words, as a logged-in user, how do I retrieve the list of groups I've joined and groups that are available?* +## Retrieve List of Groups -In order to fetch the list of groups, you can use the `GroupsRequest` class. To use this class i.e to create an object of the `GroupsRequest` class, you need to use the `GroupsRequestBuilder` class. The `GroupsRequestBuilder` class allows you to set the parameters based on which the groups are to be fetched. +Use `GroupsRequestBuilder` to configure filters, then call `fetchNext()` to retrieve groups. ### Set Limit -This method sets the limit i.e. the number of groups that should be fetched in a single iteration. +Set the number of groups to fetch per request. @@ -37,7 +55,7 @@ val groupsRequest = GroupsRequestBuilder() ### Set Search Keyword -This method allows you to set the search string based on which the groups are to be fetched. +Filter groups by a search string. @@ -62,7 +80,7 @@ val groupsRequest = GroupsRequestBuilder() ### Joined Only -This method when used, will ask CometChat to only return the groups that the user has joined or is a part of. +Return only groups the logged-in user has joined. @@ -87,7 +105,7 @@ val groupsRequest = GroupsRequestBuilder() ### Set Tags -This method accepts a list of tags based on which the list of groups is to be fetched. The list fetched will only contain the groups that have been tagged with the specified tags. +Filter groups by tags. Only groups tagged with the specified tags are returned. @@ -120,7 +138,7 @@ val groupsRequest = GroupsRequestBuilder() ### With Tags -This property when set to true will fetch tags data along with the list of groups. +Include tag data in the response when set to `true`. @@ -145,11 +163,9 @@ val groupsRequest = GroupsRequestBuilder() -Finally, once all the parameters are set to the builder class, you need to call the `build()` method to get the object of the `GroupsRequest` class. +### Fetch Groups -Once you have the object of the `GroupsRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `Group` objects containing N number of groups depending on the limit set. - -The list of groups fetched will only have the public and password type groups. The private groups will only be available if the user is a member of that private group. +After configuring the builder, call `build()` then `fetchNext()` to retrieve groups. Public and password-protected groups are always included. Private groups only appear if the user is a member. @@ -195,9 +211,7 @@ override fun onError(e: CometChatException) { ## Retrieve Particular Group Details -*In other words, as a logged-in user, how do I retrieve information for a specific group?* - -To get the information of a group, you can use the `getGroup()` method. +Use `getGroup()` to fetch details for a specific group by GUID. @@ -244,9 +258,9 @@ override fun onError(p0: CometChatException?) { On success, the `Group` object containing the details of the group is returned. -## Get online group member count +## Get Online Group Member Count -To get the total count of online users in particular groups, you can use the `getOnlineGroupMemberCount()` method. +Use `getOnlineGroupMemberCount()` to get the count of online members for specific groups. @@ -292,3 +306,119 @@ override fun onError(e: CometChatException) { This method returns a `Hashmap` with the GUID of the group as the key and the online member count for that group as the value. + +## Group Payload Structure + + + +The `Group` object returned by SDK methods contains the following fields: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `guid` | String | Unique identifier of the group | +| `name` | String | Display name of the group | +| `type` | String | Group type. Values: `"public"`, `"private"`, `"password"` | +| `password` | String | Password for protected groups (null for public/private groups) | +| `icon` | String | URL to group icon image | +| `description` | String | Description of the group | +| `owner` | String | UID of the group owner | +| `metadata` | JSONObject | Custom data set by developer. Can contain any key-value pairs | +| `createdAt` | long | Unix timestamp when group was created | +| `updatedAt` | long | Unix timestamp of last group update | +| `hasJoined` | boolean | Whether the logged-in user has joined this group | +| `joinedAt` | long | Unix timestamp when logged-in user joined the group | +| `scope` | String | Logged-in user's scope in group. Values: `"admin"`, `"moderator"`, `"participant"` | +| `membersCount` | int | Total number of members in the group | +| `tags` | Array\ | List of tags for group identification and filtering | +| `isBannedFromGroup` | boolean | Whether the logged-in user is banned from this group | + +**Sample Group Object:** + +```json +{ + "guid": "group_123", + "name": "Developers", + "type": "public", + "password": null, + "icon": "https://example.com/icon.png", + "description": "A group for developers", + "owner": "user_123", + "metadata": { + "category": "tech", + "isVerified": true + }, + "createdAt": 1699800000, + "updatedAt": 1699900000, + "hasJoined": true, + "joinedAt": 1699850000, + "scope": "admin", + "membersCount": 25, + "tags": ["official", "support"], + "isBannedFromGroup": false +} +``` + + + + +## Best Practices + + + + Set an appropriate limit (e.g., 30) and call `fetchNext()` multiple times to load groups in batches, improving performance and user experience. + + + Use `joinedOnly(true)` to fetch only groups the user has joined, reducing unnecessary data transfer and improving load times. + + + Use `setSearchKeyWord()` to allow users to quickly find specific groups by name, especially in apps with many groups. + + + Organize groups with tags and use `setTags()` to fetch groups by category (e.g., "sports", "work", "friends"). + + + +## Troubleshooting + + + + **Symptom:** `fetchNext()` returns an empty list even though groups exist. + + **Cause:** Filters may be too restrictive, or the user hasn't joined any groups (when using `joinedOnly(true)`). + + **Solution:** Remove filters temporarily to verify groups exist. Check if `joinedOnly()` is appropriate for your use case. + + + **Symptom:** Private groups are missing from the fetched list. + + **Cause:** Private groups only appear if the user is a member. + + **Solution:** This is expected behavior. Users must be added to private groups by admins before they can see them. + + + **Symptom:** `getGroup()` fails with "Group not found" error. + + **Cause:** The GUID is incorrect, or the group has been deleted. + + **Solution:** Verify the GUID is correct. Check if the group still exists by fetching the groups list first. + + + +--- + +## Next Steps + + + + Create new groups for your users + + + Join groups to participate in conversations + + + Fetch list of group members + + + Learn more about advanced filtering options + + diff --git a/sdk/android/retrieve-users.mdx b/sdk/android/retrieve-users.mdx index ecbf5de36..815fcd793 100644 --- a/sdk/android/retrieve-users.mdx +++ b/sdk/android/retrieve-users.mdx @@ -1,12 +1,38 @@ --- title: "Retrieve Users" +sidebarTitle: "Retrieve Users" +description: "Fetch user details, retrieve user lists with filters, and get online user counts using the Android SDK" --- + +```kotlin +// Get logged-in user +val user = CometChat.getLoggedInUser() + +// Fetch user list with filters +val usersRequest = UsersRequestBuilder() + .setLimit(30) + .setSearchKeyword("john") + .hideBlockedUsers(true) + .build() + +usersRequest.fetchNext(object : CallbackListener>() { + override fun onSuccess(list: List) { } + override fun onError(e: CometChatException) { } +}) + +// Get specific user details +CometChat.getUser("UID", object : CometChat.CallbackListener() { + override fun onSuccess(user: User) { } + override fun onError(e: CometChatException) { } +}) +``` + ## Retrieve Logged In User Details -You can get the details of the logged-in user using the `getLoggedInUser()` method. This method can also be used to check if the user is logged in or not. If the method returns `null`, it indicates that the user is not logged in and you need to log the user into CometChat. +Use `getLoggedInUser()` to get the details of the logged-in user. Returns `null` if no user is logged in. @@ -29,13 +55,11 @@ This method will return a `User` object containing all the information related t ## Retrieve List of Users -In order to fetch the list of users, you can use the `UsersRequest` class. To use this class i.e to create an object of the UsersRequest class, you need to use the `UsersRequestBuilder` class. The `UsersRequestBuilder` class allows you to set the parameters based on which the users are to be fetched. - -The `UsersRequestBuilder` class allows you to set the below parameters: +Use `UsersRequestBuilder` to configure filters, then call `fetchNext()` to retrieve users. ### Set Limit -This method sets the limit i.e. the number of users that should be fetched in a single iteration. +Set the number of users to fetch per request. @@ -60,7 +84,7 @@ val usersRequest = UsersRequestBuilder() ### Set Search Keyword -This method allows you to set the search string based on which the users are to be fetched. +Filter users by a search string. @@ -87,7 +111,7 @@ val usersRequest = UsersRequestBuilder() ### Search In -This method allows you to define in which user property should the searchKeyword be searched. This method only works in combination with `setSearchKeyword()`. By default the keyword is searched in both UID & Name. +Define which user property the `searchKeyword` should be searched in. Works only with `setSearchKeyword()`. By default, searches both UID and Name. @@ -122,12 +146,12 @@ val usersRequest = UsersRequestBuilder() ### Set Status -The status based on which the users are to be fetched. The status parameter can contain one of the below two values: +The status based on which the users are to be fetched. The status parameter can contain one of the following values: -* CometChat.USER\_STATUS.ONLINE - will return the list of only online users. -* CometChat.USER\_STATUS.OFFLINE - will return the list of only offline users. +* `CometChat.USER_STATUS.ONLINE` - Returns the list of only online users. +* `CometChat.USER_STATUS.OFFLINE` - Returns the list of only offline users. -If this parameter is not set, will return all the available users. +If this parameter is not set, all available users will be returned. @@ -152,11 +176,11 @@ val usersRequest = UsersRequestBuilder() -If this parameter is not set, will return all users. +If this parameter is not set, all users will be returned. ### Hide Blocked Users -This method is used to determine if the blocked users should be returned as a part of the user list. If set to `true`, the user list will not contain the users blocked by the logged-in user. +Exclude users blocked by the logged-in user from the results. Default is `false`. @@ -183,7 +207,7 @@ val usersRequest = UsersRequestBuilder() ### Set Roles -This method allows you to fetch the users based on multiple roles. +Filter users by one or more roles. @@ -216,7 +240,7 @@ val usersRequest = UsersRequestBuilder() ### Friends Only -This property when set to true will return only the friends of the logged-in user. +Return only friends of the logged-in user when set to `true`. @@ -243,7 +267,7 @@ val usersRequest = UsersRequestBuilder() ### Set Tags -This method accepts a list of tags based on which the list of users is to be fetched. The list fetched will only contain the users that have been tagged with the specified tags. +Filter users by tags. Only users tagged with the specified tags are returned. @@ -276,7 +300,7 @@ val usersRequest = UsersRequestBuilder() ### With Tags -This property when set to true will fetch tags data along with the list of users. +Include tag data in the response when set to `true`. @@ -303,7 +327,7 @@ val usersRequest = UsersRequestBuilder() ### Set UIDs -This method accepts a list of UIDs based on which the list of users is fetched. A maximum of 25 users can be fetched. +Fetch specific users by their UIDs. Maximum 25 UIDs per request. @@ -336,7 +360,7 @@ val usersRequest = UsersRequestBuilder() ### Sort By -This method allows you to sort the User List by a specific property of User. By default the User List is sorted by `status => name => UID` . If `name` is pass to the `sortBy()` method the user list will be sorted by `name => UID`. +Sort the user list by a specific property. Default sort order is `status => name => UID`. @@ -363,7 +387,7 @@ val usersRequest = UsersRequestBuilder() ### Sort By Order -This method allows you to sort the User List in a specific order. By default the user list is sorted in ascending order. +Sort the user list in ascending or descending order. Default is ascending. @@ -388,9 +412,9 @@ val usersRequest = UsersRequestBuilder() -Finally, once all the parameters are set to the builder class, you need to call the `build()` method to get the object of the `UsersRequest` class. +### Fetch Users -Once you have the object of the `UsersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `User` objects containing X number of users depending on the limit set. +After configuring the builder, call `build()` then `fetchNext()` to retrieve users. Call `fetchNext()` repeatedly on the same object to paginate. @@ -438,7 +462,7 @@ override fun onError(e: CometChatException) { ## Retrieve Particular User Details -To get the information of a user, you can use the `getUser()` method. +Use `getUser()` to fetch details for a specific user by UID. @@ -486,9 +510,9 @@ The `getUser()` method takes the following parameters: On success, the `User` object containing the details of the user is returned. -## Get online user count +## Get Online User Count -To get the total count of online users for your app, you can use the `getOnlineUserCount()` method. +Use `getOnlineUserCount()` to get the total count of online users for your app. @@ -524,3 +548,87 @@ override fun onError(e: CometChatException) { + +## User Payload Structure + + + +The `User` object returned by SDK methods contains the following fields: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for role-based access control | +| `metadata` | JSONObject | Custom data set by developer. Can contain any key-value pairs | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message set by user | +| `lastActiveAt` | long | Unix timestamp of last activity (milliseconds) | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification and filtering | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + +**Sample User Object:** + +```json +{ + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "link": "https://example.com/profile/user_123", + "role": "default", + "metadata": { + "customKey": "customValue", + "preferences": { + "theme": "dark", + "notifications": true + } + }, + "status": "online", + "statusMessage": "Available", + "lastActiveAt": 1699900000000, + "hasBlockedMe": false, + "blockedByMe": false, + "tags": ["premium", "verified"], + "deactivatedAt": 0 +} +``` + + + +## Best Practices + + + + Always set a reasonable limit (20-50 users) and use `fetchNext()` to paginate through results. This prevents memory issues and improves performance when dealing with large user bases. + + + Use `UsersRequestBuilder` filters (search, status, roles, tags) to fetch only the users you need rather than fetching all users and filtering client-side. This reduces bandwidth and improves response times. + + + Store frequently accessed user details locally to reduce API calls. Use `getLoggedInUser()` to check login status without making network requests. + + + You can chain multiple filters like `setSearchKeyword()`, `hideBlockedUsers()`, and `setUserStatus()` to create precise queries that return exactly the users you need. + + + +## Next Steps + + + + Track and subscribe to user online/offline status updates + + + Block and unblock users to control interactions + + + Create, update, and manage user accounts + + + Start sending messages to retrieved users + + diff --git a/sdk/android/send-message.mdx b/sdk/android/send-message.mdx index 254f6254b..dc412ca27 100644 --- a/sdk/android/send-message.mdx +++ b/sdk/android/send-message.mdx @@ -1,106 +1,62 @@ --- -title: "Send A Message" +title: "Send Messages" +sidebarTitle: "Send Messages" +description: "Send text, media, and custom messages to users and groups using the CometChat Android SDK." --- + - -Using CometChat, you can send three types of messages: - -1. A [Text Message](/sdk/android/send-message#text-message), the most common and standard message type. -2. A [Media Message](/sdk/android/send-message#media-message), for sending photos, videos and files. -3. A [Custom Message](/sdk/android/send-message#custom-message), for sending completely custom data using JSON structures. -4. A [Interactive Messages](/sdk/android/interactive-messages), for sending end-user interactive messages of type form, card and custom Interactive - -You can also send metadata along with a text or media 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 `JSONObject` to it. - - - -```java -JSONObject metadata = new JSONObject(); -metadata.put("latitude", "50.6192171633316"); -metadata.put("longitude", "-72.68182268750002"); -textMessage.setMetadata(metadata); -``` - - - - ```kotlin -val metadata = JSONObject() -metadata.put("latitude", "50.6192171633316") -metadata.put("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. - - - -```java -List tags = new ArrayList<>(); -tags.add("pinned"); -textMessage.setTags(tags); -``` +// Text message +val textMessage = TextMessage("UID", "Hello!", CometChatConstants.RECEIVER_TYPE_USER) +CometChat.sendMessage(textMessage, object : CallbackListener() { + override fun onSuccess(msg: TextMessage) { } + override fun onError(e: CometChatException) { } +}) - +// Media message (file upload) +val mediaMessage = MediaMessage("UID", File("/path/to/file.jpg"), + CometChatConstants.MESSAGE_TYPE_IMAGE, CometChatConstants.RECEIVER_TYPE_USER) +CometChat.sendMediaMessage(mediaMessage, object : CallbackListener() { + override fun onSuccess(msg: MediaMessage) { } + override fun onError(e: CometChatException) { } +}) - -```kotlin -val tags: MutableList = ArrayList() -tags.add("pinned") -textMessage.setTags(tags) +// Custom message +val customData = JSONObject().apply { put("latitude", "19.07"); put("longitude", "72.87") } +val customMessage = CustomMessage("UID", CometChatConstants.RECEIVER_TYPE_USER, "LOCATION", customData) +CometChat.sendCustomMessage(customMessage, object : CallbackListener() { + override fun onSuccess(msg: CustomMessage) { } + override fun onError(e: CometChatException) { } +}) ``` - - - - -### Set Quoted Message - -To set a quoted message for a message, use the `setQuotedMessageId()` and `setQuotedMessage()` method of the TextMessage class. This method accepts the ID of the message to be quoted. +| Type | Method | Receiver Types | +| --- | --- | --- | +| Text | `sendMessage()` | `RECEIVER_TYPE_USER`, `RECEIVER_TYPE_GROUP` | +| Media | `sendMediaMessage()` | `RECEIVER_TYPE_USER`, `RECEIVER_TYPE_GROUP` | +| Custom | `sendCustomMessage()` | `RECEIVER_TYPE_USER`, `RECEIVER_TYPE_GROUP` | + - - -```java -textMessage.setQuotedMessageId(10); -textMessage.setQuotedMessage(); // Pass base message object here that you want to quote. -``` - - +CometChat supports three types of messages: - -```kotlin -textMessage.quotedMessageId = 0 -textMessage.quotedMessage = // Pass base message object here that you want to quote. -``` +| Type | Method | Use Case | +| --- | --- | --- | +| [Text](#text-message) | `sendMessage()` | Plain text messages | +| [Media](#media-message) | `sendMediaMessage()` | Images, videos, audio, files | +| [Custom](#custom-message) | `sendCustomMessage()` | Location, polls, or any JSON data | - +You can also send [Interactive Messages](/sdk/android/interactive-messages) for forms, cards, and custom UI elements. - +## Text Message -Once the text message object is ready, you need to use the `sendMessage()` method to send the text message to the recipient. +Send a text message using `sendMessage()` with a `TextMessage` object. ```java private String receiverID = "UID"; -private String messageText = "Hello CoemtChat!"; +private String messageText = "Hello CometChat!"; private String receiverType = CometChatConstants.RECEIVER_TYPE_USER; TextMessage textMessage = new TextMessage(receiverID, messageText, receiverType); @@ -117,9 +73,7 @@ CometChat.sendMessage(textMessage, new CometChat.CallbackListener() } }); ``` - - ```kotlin private val receiverID = "UID" @@ -138,9 +92,7 @@ CometChat.sendMessage(textMessage, object : CallbackListener() { } }) ``` - - ```java private String receiverID = "GUID"; @@ -161,9 +113,7 @@ CometChat.sendMessage(textMessage, new CometChat.CallbackListener() } }); ``` - - ```kotlin private val receiverID = "GUID" @@ -181,31 +131,90 @@ CometChat.sendMessage(textMessage, object : CallbackListener() { } }) ``` + + + +The `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 content | Yes | +| `receiverType` | `CometChatConstants.RECEIVER_TYPE_USER` or `RECEIVER_TYPE_GROUP` | Yes | + +On success, `sendMessage()` returns a `TextMessage` object containing all information about the sent message. + +### Add Metadata + +Attach custom JSON data to the message: + + + +```java +JSONObject metadata = new JSONObject(); +metadata.put("latitude", "50.6192171633316"); +metadata.put("longitude", "-72.68182268750002"); +textMessage.setMetadata(metadata); +``` + + +```kotlin +val metadata = JSONObject() +metadata.put("latitude", "50.6192171633316") +metadata.put("longitude", "-72.68182268750002") +textMessage.setMetadata(metadata) +``` + + +### Add Tags + +Tag messages for easy filtering later: + + +```java +List tags = new ArrayList<>(); +tags.add("pinned"); +textMessage.setTags(tags); +``` + + +```kotlin +val tags: MutableList = ArrayList() +tags.add("pinned") +textMessage.setTags(tags) +``` + -The `TextMessage` class constructor takes the following parameters: +### Quote a Message -| Parameter | Description | | -| -------------- | ---------------------------------------------------------------------------------------------------------------------------- | -------- | -| `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- `CometChatConstants.RECEIVER_TYPE_USER` (user) or `CometChatConstants.RECEIVER_TYPE_GROUP` (group) | Required | +Reply to a specific message by setting its ID: -When a text message is sent successfully, the response will include a `TextMessage` object which includes all information related to the sent message. + + +```java +textMessage.setQuotedMessageId(10); +``` + + +```kotlin +textMessage.quotedMessageId = 10 +``` + + ## Media Message -*In other words, as a sender, how do I send a media message like photos, videos & files?* +Send images, videos, audio, or files using `sendMediaMessage()`. -To send a media message to any user or group, you need to use the `sendMediaMessage()` method and pass a `MediaMessage` object to it. +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 ### Add Metadata -To send custom data along with a media message, you can use the `setMetadata` method and pass a `JSONObject` to it. - ```java @@ -214,9 +223,7 @@ metadata.put("latitude", "50.6192171633316"); metadata.put("longitude", "-72.68182268750002"); mediaMessage.setMetadata(metadata); ``` - - ```kotlin val metadata = JSONObject() @@ -224,36 +231,28 @@ metadata.put("latitude", "50.6192171633316") metadata.put("longitude", "-72.68182268750002") mediaMessage.setMetadata(metadata) ``` - - -### Add Caption(Text along with Media Message) +### Add Caption -To send a caption with a media message, you can use `setCaption` method and pass text to it. +Add text along with the media: ```java mediaMessage.setCaption("Message Caption"); ``` - - ```kotlin mediaMessage.setCaption("Message 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. - ```java @@ -261,23 +260,19 @@ List tags = new ArrayList<>(); tags.add("pinned"); mediaMessage.setTags(tags); ``` - - ```kotlin val tags: MutableList = ArrayList() tags.add("pinned") mediaMessage.setTags(tags) ``` - - -There are 2 ways you can send Media Messages using the CometChat SDK: +### Upload a File -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. +Pass a `File` object directly. CometChat uploads it to its servers and returns the URL in the success response. @@ -301,9 +296,7 @@ CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener - ```kotlin private val receiverID = "UID" @@ -322,9 +315,7 @@ CometChat.sendMediaMessage(mediaMessage, object : CallbackListener } }) ``` - - ```java private String receiverID = "GUID"; @@ -346,9 +337,7 @@ CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener - ```kotlin private val receiverID = "GUID" @@ -367,21 +356,21 @@ CometChat.sendMediaMessage(mediaMessage, object : CallbackListener } }) ``` - - The `MediaMessage` class constructor takes the following parameters: -| Parameter | Description | | -| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | -| `receiverId` | The UID or GUID of the recipient | Required | -| `file` | The file object to be sent | Required | -| `messageType` | The type of the message that needs to be sent. Options:
1. `CometChatConstants.MESSAGE_TYPE_IMAGE` (image)
2. `CometChatConstants.MESSAGE_TYPE_VIDEO` (video)
3. `CometChatConstants.MESSAGE_TYPE_AUDIO` (audio)
4. `CometChatConstants.MESSAGE_TYPE_FILE` (file) | Required | -| `receiverType` | The type of the receiver to whom the message is to be sent. Options: `CometChatConstants.RECEIVER_TYPE_USER` (user) or `CometChatConstants.RECEIVER_TYPE_GROUP` (group) | Optional | +| Parameter | Description | Required | +| --- | --- | --- | +| `receiverId` | The UID or GUID of the recipient | Yes | +| `file` | The `File` object to upload | Yes | +| `messageType` | `MESSAGE_TYPE_IMAGE`, `MESSAGE_TYPE_VIDEO`, `MESSAGE_TYPE_AUDIO`, or `MESSAGE_TYPE_FILE` | Yes | +| `receiverType` | `RECEIVER_TYPE_USER` or `RECEIVER_TYPE_GROUP` | Yes | + +### Send a URL -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 that is available in the MediaMessage class. For more information, you can refer to the below code snippet: +Send media hosted on your server or cloud storage using the `Attachment` class: @@ -409,9 +398,7 @@ CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener - ```kotlin val receiverId = "recipient_UID" @@ -435,9 +422,7 @@ CometChat.sendMediaMessage(mediaMessage, object : CallbackListener - ```java String receiverId = "recipient_GUID"; @@ -463,9 +448,7 @@ CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener - ```kotlin val receiverId = "recipient_GUID" @@ -489,20 +472,16 @@ CometChat.sendMediaMessage(mediaMessage, object : CallbackListener - -When a media message is sent successfully, the response will include a `MediaMessage` object which includes all information related to the sent message. - -If you wish to send a caption or some text along with the Media Message, you can use the `caption field` provided by the MediaMessage class. To set the caption you can use the `setCaption()` method and at the receiver end, you can obtain the caption using the `getCaption()` method. As with text messages, the metadata field can be used with media messages as well. Any additional information can be passed along with the media message as a `JSONObject`. +On success, `sendMediaMessage()` returns a `MediaMessage` object. -## Multiple Attachments in a Media Message +## Multiple Attachments -Starting version 3.0.9 & above the SDK supports sending of multiple attachments in a single media message. As in case for single attachment in a media message, there are two ways you can send Media Messages using the CometChat SDK: +Starting version 3.0.9, the SDK supports sending multiple attachments in a single media message. -1. **By providing an array of files:** You can now share a List 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 are sent in the success response of the `sendMediaMessage()` method. +### Upload Multiple Files @@ -608,14 +587,14 @@ CometChat.sendMediaMessage(mediaMessage, object : CallbackListener1. `CometChatConstants.MESSAGE_TYPE_IMAGE`
2. `CometChatConstants.MESSAGE_TYPE_VIDEO`
3. `CometChatConstants.MESSAGE_TYPE_AUDIO`
4. `CometChatConstants.MESSAGE_TYPE_FILE` | -| **receiverType** | The type of the receiver to whom the message is to be sent.
1. `CometChatConstants.RECEIVER_TYPE_USER`
2. `CometChatConstants.RECEIVER_TYPE_GROUP` | +| Parameter | Description | Required | +| --- | --- | --- | +| `receiverId` | The `UID` or `GUID` of the recipient | Yes | +| `files` | A `List` of files to upload | Yes | +| `messageType` | `MESSAGE_TYPE_IMAGE`, `MESSAGE_TYPE_VIDEO`, `MESSAGE_TYPE_AUDIO`, or `MESSAGE_TYPE_FILE` | Yes | +| `receiverType` | `RECEIVER_TYPE_USER` or `RECEIVER_TYPE_GROUP` | Yes | -2. **By providing the URL of the multiple files:** The second way to send multiple attachments using the CometChat SDK is to provide the SDK with the URL of multiple files 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: +### Send Multiple URLs @@ -748,50 +727,36 @@ CometChat.sendMediaMessage(mediaMessage, object : CallbackListener ```java -CustomMessage customMessage = new CustomMessage(receiverId, receiverType,customType, customData) +CustomMessage customMessage = new CustomMessage(receiverId, receiverType, customType, customData) ``` - - ```kotlin val customMessage = 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` - 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 JSONObject. +| Parameter | Description | Required | +| --- | --- | --- | +| `receiverId` | UID of the user or GUID of the group | Yes | +| `receiverType` | `RECEIVER_TYPE_USER` or `RECEIVER_TYPE_GROUP` | Yes | +| `customType` | Developer-defined type string (e.g., `"LOCATION"`, `"POLL"`) | Yes | +| `customData` | The payload as a `JSONObject` | Yes | -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. +You can also set a subtype using `setSubtype()` to further classify the custom message. ### 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. - ```java @@ -813,7 +778,7 @@ customMessage.setTags(tags) -Once the object of `CustomMessage` class is ready you can send the custom message using the `sendCustomMessage()` method. +Once the message object is ready, call `sendCustomMessage()`. @@ -916,15 +881,11 @@ CometChat.sendCustomMessage(customMessage, object : CallbackListener -The above sample explains how custom messages can be used to share the location with a user. Similarly, you can send custom messages to groups. - -On success, you will receive an object of `CustomMessage` class. - -### Update Conversation +On success, `sendCustomMessage()` returns a `CustomMessage` object. -*How can I decide whether the custom message should update the last message of a conversation?* +### Control Conversation Update -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(boolean value)` method of the Custom Message. +By default, a custom message updates the conversation's last message. To prevent this: @@ -1033,11 +994,9 @@ CometChat.sendCustomMessage(customMessage, object : CallbackListener -### Custom Notification Body +### Custom Notification Text -*How can i customise the notification body of custom message?* - -To add a custom notification body for `Push, Email & SMS` notification of a custom message you can use the `setConversationText(text: string)` method of Custom Message class. +Set custom text for push, email, and SMS notifications: @@ -1148,6 +1107,175 @@ CometChat.sendCustomMessage(customMessage, object : CallbackListener -It is also possible to send interactive messages from CometChat , to know more [click here](/sdk/android/interactive-messages) +It is also possible to send interactive messages from CometChat, to know more [click here](/sdk/android/interactive-messages) + +--- + +## Next Steps + + + + Listen for incoming messages in real time + + + Modify sent messages after delivery + + + Remove messages from conversations + + + Send messages with embedded forms and buttons + + + +## Message Payload Structure + + + +The `BaseMessage` object returned by SDK methods contains the following fields. `TextMessage`, `MediaMessage`, and `CustomMessage` extend this base structure with additional type-specific fields. + +| Parameter | Type | Description | +|-----------|------|-------------| +| `id` | long | Unique message identifier | +| `muid` | String | Developer-defined message ID for deduplication | +| `sender` | [User](#user-object) | User who sent the message | +| `receiver` | AppEntity | Message receiver ([User](#user-object) or [Group](#group-object)) | +| `receiverUid` | String | Receiver's unique identifier | +| `type` | String | Message type. Values: `"text"`, `"image"`, `"video"`, `"audio"`, `"file"`, `"custom"` | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `category` | String | Message category. Values: `"message"`, `"action"`, `"call"`, `"custom"` | +| `sentAt` | long | Unix timestamp when message was sent | +| `deliveredAt` | long | Unix timestamp when message was delivered | +| `readAt` | long | Unix timestamp when message was read | +| `metadata` | JSONObject | Custom message metadata set by developer | +| `readByMeAt` | long | Unix timestamp when logged-in user read the message | +| `deliveredToMeAt` | long | Unix timestamp when message was delivered to logged-in user | +| `deletedAt` | long | Unix timestamp when message was deleted (0 if not deleted) | +| `editedAt` | long | Unix timestamp when message was edited (0 if not edited) | +| `deletedBy` | String | UID of user who deleted the message (null if not deleted) | +| `editedBy` | String | UID of user who edited the message (null if not edited) | +| `updatedAt` | long | Unix timestamp of last message update | +| `conversationId` | String | Associated conversation identifier | +| `parentMessageId` | long | Parent message ID for threaded messages (0 if not a reply) | +| `replyCount` | int | Number of replies in thread | +| `unreadRepliesCount` | int | Number of unread replies in thread | +| `mentionedUsers` | Array\<[User](#user-object)\> | List of users mentioned in the message | +| `hasMentionedMe` | boolean | Whether the logged-in user is mentioned | +| `reactions` | Array\<[ReactionCount](#reactioncount-object)\> | List of reaction counts on the message | +| `rawMessage` | JSONObject | Raw JSON message data | +| `quotedMessageId` | long | ID of the quoted message (0 if not quoting) | +| `quotedMessage` | BaseMessage | The quoted message object (null if not quoting) | + +**Sample BaseMessage Object:** + +```json +{ + "id": 12345, + "muid": "msg_abc123", + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default", + "lastActiveAt": 1699900000 + }, + "receiver": { + "uid": "user_456", + "name": "Jane Smith", + "avatar": "https://example.com/avatar2.png" + }, + "receiverUid": "user_456", + "type": "text", + "receiverType": "user", + "category": "message", + "sentAt": 1699900000, + "deliveredAt": 1699900001, + "readAt": 1699900002, + "metadata": { + "priority": "high", + "customField": "value" + }, + "readByMeAt": 1699900002, + "deliveredToMeAt": 1699900001, + "deletedAt": 0, + "editedAt": 0, + "deletedBy": null, + "editedBy": null, + "updatedAt": 1699900000, + "conversationId": "user_123_user_456", + "parentMessageId": 0, + "replyCount": 5, + "unreadRepliesCount": 2, + "mentionedUsers": [], + "hasMentionedMe": false, + "reactions": [ + { + "reaction": "👍", + "count": 3, + "reactedByMe": true + } + ], + "rawMessage": {}, + "quotedMessageId": 0, + "quotedMessage": null +} +``` + + + + + +The nested `User` object (used in `sender`, `receiver`, `mentionedUsers`) contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + + + + + +The nested `Group` object (used in `receiver` for group messages) contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `guid` | String | Unique identifier of the group | +| `name` | String | Display name of the group | +| `type` | String | Group type. Values: `"public"`, `"private"`, `"password"` | +| `icon` | String | URL to group icon | +| `description` | String | Group description | +| `owner` | String | UID of group owner | +| `metadata` | JSONObject | Custom data set by developer | +| `membersCount` | int | Number of group members | +| `tags` | Array\ | List of tags for group identification | +| `hasJoined` | boolean | Whether logged-in user has joined | +| `scope` | String | User's scope in group. Values: `"admin"`, `"moderator"`, `"participant"` | + + + + + +The nested `ReactionCount` object (used in `reactions` array) contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `reaction` | String | The reaction emoji | +| `count` | int | Total count of this reaction | +| `reactedByMe` | boolean | Whether the logged-in user has reacted with this emoji | + + diff --git a/sdk/android/session-timeout.mdx b/sdk/android/session-timeout.mdx index 23470881f..914448860 100644 --- a/sdk/android/session-timeout.mdx +++ b/sdk/android/session-timeout.mdx @@ -1,27 +1,42 @@ --- -title: "Session Timeout Flow" +title: "Session Timeout" +sidebarTitle: "Session Timeout" +description: "Configure idle timeout and auto-termination for inactive call sessions in Android" --- + +```kotlin +// Configure idle timeout (default: 180 seconds) +val callSettings = CometChatCalls.CallSettingsBuilder(context, videoContainer) + .setIdleTimeoutPeriod(300) // 300 seconds = 5 minutes + .setEventListener(object : CometChatCallsEventsListener { + override fun onSessionTimeout() { + // Handle auto-termination due to inactivity + } + }) + .build() +``` -Available since v4.1.0 +**Available since:** v4.1.0 +**Default timeout:** 180 seconds (3 minutes alone) +**Warning shown:** 60 seconds before auto-termination + -## Overview -CometChat Calls SDK provides a mechanism to handle session timeouts for idle participants. By default, if a participant is alone in a call session for 180 seconds (3 minutes), the following sequence is triggered: -1. After 120 seconds of being alone in the session, the participant will see a dialog box +*Available since v4.1.0* -2. This dialog provides options to either: +The Calls SDK automatically terminates call sessions when a participant is alone for too long, preventing abandoned calls from consuming resources. - * Stay in the call - * Leave immediately +When a participant is alone in a session: -3. If no action is taken within the next 60 seconds, the call will automatically end +1. After 120 seconds, a dialog appears with "Stay" or "Leave" options +2. If no action is taken within 60 seconds, the call auto-terminates and `onSessionTimeout` fires -This feature helps manage inactive call sessions and prevents unnecessary resource usage. The idle timeout period ensures that participants don't accidentally remain in abandoned call sessions. +The default timeout is 180 seconds (3 minutes). -### Session Timeout Flow +## Session Timeout Flow @@ -32,3 +47,66 @@ This feature helps manage inactive call sessions and prevents unnecessary resour The `onSessionTimeout` event is triggered when the call automatically terminates due to session timeout, as illustrated in the diagram above. + + +--- + +## Best Practices + + + + Choose a timeout duration that balances resource management with user experience. 180 seconds (3 minutes) is suitable for most use cases, but consider longer durations for scenarios where users may step away briefly. + + + Implement the `onSessionTimeout()` callback to clean up resources and navigate users away from the call screen. Show a clear message explaining why the call ended. + + + Display information about the idle timeout policy in your app's help section or during onboarding so users understand why calls may auto-terminate. + + + Test the timeout flow thoroughly, including the warning dialog and auto-termination, to ensure users have enough time to respond and the experience is smooth. + + + +## Troubleshooting + + + + **Symptom:** Call terminates before the expected timeout period. + + **Cause:** The `setIdleTimeoutPeriod()` value may be set too low, or the timeout is being triggered by other factors. + + **Solution:** Verify the timeout value in your `CallSettingsBuilder`. Ensure you're setting the value in seconds (e.g., 300 for 5 minutes). Check that the call isn't ending due to other reasons like network issues. + + + **Symptom:** Call terminates without showing the 60-second warning dialog. + + **Cause:** The default UI layout may be disabled, or the dialog is being blocked by custom UI elements. + + **Solution:** Ensure `setDefaultLayoutEnable(true)` is set in your call settings. Check that no custom views are overlaying the call UI and blocking the dialog. + + + **Symptom:** The `onSessionTimeout()` callback is not triggered when the call auto-terminates. + + **Cause:** Event listener may not be properly registered or the SDK version doesn't support this callback. + + **Solution:** Verify you're using SDK v4.1.0 or later. Ensure the event listener is registered using `setEventListener()` in your call settings before starting the session. + + + +## Next Steps + + + + Implement one-on-one and group calling with default UI + + + Use Calls SDK without Chat SDK integration + + + Create webinar-style calling experiences + + + Retrieve and display call history + + diff --git a/sdk/android/setup.mdx b/sdk/android/setup.mdx index 55cef555d..1ce5ff43b 100644 --- a/sdk/android/setup.mdx +++ b/sdk/android/setup.mdx @@ -1,31 +1,49 @@ --- title: "Setup" +sidebarTitle: "Setup" +description: "Install and initialize the CometChat Android SDK in your application" --- + +```kotlin +// 1. Add dependency to build.gradle +implementation "com.cometchat:chat-sdk-android:4.1.8" + +// 2. Initialize (run once at app start) +val appSettings = AppSettings.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion("REGION") + .build() +CometChat.init(context, "APP_ID", appSettings, callback) + +// 3. Login user +CometChat.login("UID", "AUTH_KEY", callback) // Dev only +``` - -Skip the create new app step. Your existing v2 app can be migrated to v3. > > Follow steps mentioned in **Add the CometChat dependency** section below to upgrade to latest version of v3 - - - -### Get your Application Keys - -[Signup for CometChat](https://app.cometchat.com) and then: +**Required Credentials:** App ID, Region, Auth Key (dev) or Auth Token (prod) +**Get from:** [CometChat Dashboard](https://app.cometchat.com) → Your App → API & Auth Keys + -1. Create a new app -2. Head over to the **API Keys** section and note the **Auth Key**, **App ID** & **Region** +## Prerequisites - -Minimum Requirement +| Requirement | Minimum Version | +| --- | --- | +| Android API Level | 21 | +| Android API Level (with Calls SDK) | 24 | +| Java | 8 | +| AndroidX | Required | -* Android API Level 21 -* Android API level 24 (in case you are using the calls SDKS) -* Androidx Compatibility +Get your credentials from the [CometChat Dashboard](https://app.cometchat.com): +- App ID +- Region +- Auth Key (for development) - + +**Auth Key** is for development/testing only. In production, generate **Auth Tokens** on your server using the REST API and pass them to the client. Never expose Auth Keys in production client code. + -### Add the CometChat Dependency +## Add the CometChat Dependency ### Gradle @@ -63,11 +81,11 @@ dependencies { -In case you plan to use the calling feature, please add the Calling dependency `implementation 'com.cometchat:calls-sdk-android:4.3.3'` in the dependencies section of the app-level `build.gradle` file. +If you plan to use the calling feature, add the Calling dependency `implementation 'com.cometchat:calls-sdk-android:4.3.3'` in the dependencies section of the app-level `build.gradle` file. -Finally, add the below lines `android` section of the **app level** gradle file. +Finally, add the following lines to the `android` section of the **app level** gradle file. @@ -86,20 +104,24 @@ android { ## Initialize CometChat -The `init()` method initialises the settings required for CometChat. The `init()` method takes the below parameters: +The `init()` method initializes the settings required for CometChat. This method takes the following parameters: -1. appID - You CometChat App ID -2. appSettings - An object of the AppSettings class can be created using the AppSettingsBuilder class. The region field is mandatory and can be set using the `setRegion()` method. +1. `appID` - Your CometChat App ID +2. `appSettings` - An object of the `AppSettings` class created using the `AppSettingsBuilder` class. The region field is mandatory and can be set using the `setRegion()` method. -The `AppSettings` class allows you to configure three settings: +The `AppSettings` class allows you to configure the following settings: -* **Region**: The region where you app was created. -* **[Presence Subscription](/sdk/android/user-presence) :** Represents the subscription type for user presence (real-time online/offline status) -* **autoEstablishSocketConnection(boolean value)**: This property takes a boolean value which when set to true informs the SDK to manage the web-socket connection internally. If set to false, it informs the SDK that the web-socket connection will be managed manually. The default value for this parameter is true. For more information on this, please check the Managing Web-Socket connections manually section. The default value for this property is **true.** -* **overrideAdminHost(adminHost: string)**: This method takes the admin URL as input and uses this admin URL instead of the default admin URL. This can be used in case of dedicated deployment of CometChat. -* **overrideClientHost(clientHost: string)**: This method takes the client URL as input and uses this client URL instead of the default client URL. This can be used in case of dedicated deployment of CometChat. +* **Region**: The region where your app was created. +* **[Presence Subscription](/sdk/android/user-presence)**: Represents the subscription type for user presence (real-time online/offline status). +* **autoEstablishSocketConnection(boolean value)**: When set to `true`, the SDK manages the WebSocket connection internally. When set to `false`, you must manage the WebSocket connection manually. The default value is `true`. For more information, see the Managing Web-Socket connections manually section. +* **overrideAdminHost(adminHost: string)**: Takes the admin URL as input and uses it instead of the default admin URL. This is useful for dedicated deployments of CometChat. +* **overrideClientHost(clientHost: string)**: Takes the client URL as input and uses it instead of the default client URL. This is useful for dedicated deployments of CometChat. -We suggest you call the `init()` method on app startup. +We recommend calling the `init()` method on app startup. + + +`CometChat.init()` must be called before any other SDK method. Calling `login()`, `sendMessage()`, or registering listeners before `init()` will fail. + @@ -161,6 +183,18 @@ CometChat.init(this,appID,appSetting, object : CometChat.CallbackListener @@ -186,3 +220,26 @@ Know more about manual mode connection [click here](/sdk/android/connection-beha | ----------------- | ------------------------------------------------------------------------------------------------------------------ | | App in foreground | Call `CometChat.connect()` to create the WebSocket connection | | App in background | Disconnect the WebSocket connection if no ping is received within 30 seconds after the app goes in the background. | + + +**Auth Key** is for development/testing only. In production, generate **Auth Tokens** on your server using the REST API and pass them to the client. Never expose Auth Keys in production client code. + + +--- + +## Next Steps + + + + Learn about secure authentication with Auth Tokens for production apps + + + Start sending text, media, and custom messages to users and groups + + + Understand core concepts like users, groups, messages, and conversations + + + Create and manage users in your CometChat application + + diff --git a/sdk/android/standalone-calling.mdx b/sdk/android/standalone-calling.mdx index 77b4e628c..5cbc2438c 100644 --- a/sdk/android/standalone-calling.mdx +++ b/sdk/android/standalone-calling.mdx @@ -1,7 +1,36 @@ --- title: "Standalone Calling" +sidebarTitle: "Standalone Calling" +description: "Implement calling functionality using only the CometChat Calls SDK without the Chat SDK" --- + + +```kotlin +// Generate call token +val sessionId = "UNIQUE_SESSION_ID" +val userAuthToken = "USER_AUTH_TOKEN" // From REST API +CometChatCalls.generateToken(sessionId, userAuthToken, object : CometChatCalls.CallbackListener() { + override fun onSuccess(generateToken: GenerateToken) { + // Start session with token + } + override fun onError(e: CometChatException) { } +}) + +// Start call session +val callSettings = CometChatCalls.CallSettingsBuilder(context, videoContainer) + .setDefaultLayoutEnable(true) + .setIsAudioOnly(false) + .build() +CometChatCalls.startSession(callToken, callSettings, callback) + +// End session +CometChatCalls.endSession() +``` + +**Prerequisites:** [Calls SDK setup](/sdk/android/calling-setup), User auth token from REST API + + ## Overview This section demonstrates how to implement calling functionality using only the CometChat Calls SDK, without requiring the Chat SDK. This is ideal for applications that need video/audio calling capabilities without the full chat infrastructure. @@ -502,6 +531,10 @@ CometChatCalls.exitPIPMode() + +Always remove call event listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + ### Switch To Video Call Upgrades an ongoing audio call to a video call. This enables the camera and starts transmitting video to other participants. The remote participant receives the `onCallSwitchedToVideo()` callback. @@ -585,3 +618,75 @@ CometChatCalls.endSession() + +--- + +## Best Practices + + + + Store user auth tokens securely using Android's EncryptedSharedPreferences or KeyStore. Never hardcode tokens in your application code. + + + Use UUIDs or timestamp-based IDs for session IDs to ensure uniqueness. Participants joining the same call must use the same session ID. + + + Auth tokens can expire. Implement token refresh logic and handle token expiration errors gracefully by requesting new tokens from your backend. + + + Always call `CometChatCalls.endSession()` when leaving a call to release camera, microphone, and network resources. Also remove event listeners in `onDestroy()`. + + + Test audio routing on different devices and scenarios (Bluetooth connected, headphones plugged in) to ensure proper audio device selection. + + + +## Troubleshooting + + + + **Symptom:** `generateToken()` fails with "Invalid auth token" error. + + **Cause:** The user auth token is invalid, expired, or incorrectly formatted. + + **Solution:** Verify the auth token was obtained correctly from the REST API. Check token expiration and request a new token if needed. Ensure the token is passed as a string without extra whitespace. + + + **Symptom:** `startSession()` fails with "Invalid call token" error. + + **Cause:** The call token is invalid or the session ID doesn't match. + + **Solution:** Ensure you're using the `GenerateToken` object received from `generateToken()` onSuccess callback. Verify all participants use the same session ID to join the same call. + + + **Symptom:** Call starts but no video is displayed. + + **Cause:** The `RelativeLayout` video container is not properly configured or has zero dimensions. + + **Solution:** Ensure the video container has non-zero width and height in your layout XML. Verify the container is visible and not hidden behind other views. + + + **Symptom:** Video call fails with "Camera permission denied" error. + + **Cause:** The app doesn't have camera and microphone permissions. + + **Solution:** Request `CAMERA` and `RECORD_AUDIO` permissions at runtime before starting a call. Handle permission denial gracefully by showing an explanation to the user. + + + +## Next Steps + + + + Implement calling with Chat SDK integration + + + Customize the call UI appearance and layout + + + Configure idle timeout and auto-termination + + + Record call sessions for later playback + + diff --git a/sdk/android/threaded-messages.mdx b/sdk/android/threaded-messages.mdx index 8a8787c3a..06367b7cd 100644 --- a/sdk/android/threaded-messages.mdx +++ b/sdk/android/threaded-messages.mdx @@ -1,26 +1,70 @@ --- title: "Threaded Messages" +sidebarTitle: "Threaded Messages" +description: "Send, receive, and fetch threaded messages using the CometChat Android SDK." --- + + + +```kotlin +// Send message in thread +val textMessage = TextMessage("UID", "Reply text", CometChatConstants.RECEIVER_TYPE_USER) +textMessage.parentMessageId = 100 +CometChat.sendMessage(textMessage, object : CallbackListener() { + override fun onSuccess(message: TextMessage) { } + override fun onError(e: CometChatException) { } +}) -Messages that are started from a particular message are called Threaded messages or simply threads. Each Thread is attached to a message which is the Parent message for that thread. - -## Send Message in a Thread - -As mentioned in the [Send a Message](/sdk/android/send-message) section, you can send a message to a User or a Group by mentioning the receiver (uid/guid) and `receiverType`(user/group). - -A message can be categorized as: +// Fetch thread messages +val messagesRequest = MessagesRequest.MessagesRequestBuilder() + .setParentMessageId(100) + .setLimit(30) + .build() +messagesRequest.fetchPrevious(callback) + +// Exclude threads from main conversation +val messagesRequest = MessagesRequest.MessagesRequestBuilder() + .setUID("user_uid") + .hideReplies(true) + .build() +``` + + +```java +// Send message in thread +TextMessage textMessage = new TextMessage("UID", "Reply text", CometChatConstants.RECEIVER_TYPE_USER); +textMessage.setParentMessageId(100); +CometChat.sendMessage(textMessage, new CallbackListener() { + @Override + public void onSuccess(TextMessage message) { } + @Override + public void onError(CometChatException e) { } +}); -1. Text Message -2. Media Message -3. Custom Message. +// Fetch thread messages +MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder() + .setParentMessageId(100) + .setLimit(30) + .build(); +messagesRequest.fetchPrevious(callback); + +// Exclude threads from main conversation +MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder() + .setUID("user_uid") + .hideReplies(true) + .build(); +``` + + + -Any of the above messages can be sent in a thread. As a thread is identified with a parent message, the `parentMessageId` must be set for the message. This will indicate that the message to be sent has to be a part of the thread of the message with the specified `parentMessageId`. +Messages that are started from a particular message are called Threaded messages or simply threads. Each Thread is attached to a message which is the Parent message for that thread. -This can be achieved using the `setParentMessageId()` method provided by the object of the `TextMessage`, `MediaMessage` and `CustomMessage` class. The id specified in the `setParentMessageId()` method maps the message sent to the particular thread. +## Send Message in a Thread -**Example to Send a Text Message in a thread in a user conversation.** +Any message type (Text, Media, or Custom) can be sent in a thread. Set the `parentMessageId` using `setParentMessageId()` to indicate which thread the message belongs to. @@ -63,13 +107,11 @@ CometChat.sendMessage(textMessage, object : CallbackListener() { -The above snippet shows how a message with the text "Hello" can be sent in the thread with `parentMessageId` 100. - -Similarly, using the `setparentMessageId()` method, Media and Custom Messages can be sent in threads too. +Media and Custom messages can also be sent in threads using `setParentMessageId()`. ### Receiving Real-Time Messages -The procedure to receive real-time messages is exactly the same as mentioned in the [Receive Messages](/sdk/android/receive-messages). This can be achieved using the `MessageListener` class provided by the SDK. To add a MessageListener, you can use the `addMessageListener()` method of the SDK. The only thing that needs to be checked is if the received message belongs to the active thread. This can be done using `parentMessageId` field of the message object. +Use `MessageListener` to receive real-time thread messages. Check if the received message belongs to the active thread using `getParentMessageId()`. @@ -133,16 +175,9 @@ CometChat.addMessageListener(listenerID, object : MessageListener() { -### Fetch all the messages for any particular thread. - -You can fetch all the messages belonging to a particular thread by using the `MessagesRequest` class. +### Fetch Thread Messages -The `MessageRequestBuilder` builds the `MessageRequest` object using the following functions: - -1. setParentMessageId(): Takes the parentId of the message as argument whose thread needs to be requested. -2. build(): returns the MessageRequest object. - -Once you have the `MessagesRequest` object, you can call the `fetchPrevious()` method to get the latest messages in the thread. In one iteration, a maximum of 100 messages can be fetched. If you wish to fetch the next set of messages, you need to call the fetchPrevious() method again on the same object. +Use `MessagesRequestBuilder` with `setParentMessageId()` to fetch messages belonging to a specific thread. Call `fetchPrevious()` to get messages (max 100 per request). @@ -191,7 +226,7 @@ messagesRequest.fetchPrevious(object : CallbackListener?>() { ## Avoid Threaded Messages in User/Group Conversations -While fetching messages for normal user/group conversations using the `MessagesRequest`, the threaded messages by default will be a part of the list of messages received. In order to exclude the threaded messages from the list of user/group messages, you need to use the `hideReplies()` method of the `MessagesRequestBuilder` class. This method takes a boolean argument which when set to true excludes the messages belonging to threads from the list of messages. +Use `hideReplies(true)` to exclude threaded messages when fetching messages for a conversation. @@ -241,3 +276,36 @@ messagesRequest.fetchPrevious(object : CallbackListener?>() { The above snippet will return messages between the logged in user and `cometchat-uid-1` excluding all the threaded messages belonging to the same conversation. + +## Best Practices + + + + Always use `hideReplies(true)` when fetching messages for main user/group conversations to keep threads separate and maintain a clean conversation view. This prevents thread replies from cluttering the main message list. + + + When receiving real-time messages, always check the `parentMessageId` to determine if a message belongs to an active thread. This ensures proper message routing in your UI. + + + Before allowing users to reply in a thread, verify that the parent message still exists and hasn't been deleted. This prevents orphaned thread messages. + + + +--- + +## Next Steps + + + + Learn how to send text, media, and custom messages + + + Handle real-time message events with listeners + + + Understand message objects and their properties + + + Add emoji reactions to messages + + diff --git a/sdk/android/transfer-group-ownership.mdx b/sdk/android/transfer-group-ownership.mdx index 6663488bf..dd2a0e863 100644 --- a/sdk/android/transfer-group-ownership.mdx +++ b/sdk/android/transfer-group-ownership.mdx @@ -1,14 +1,28 @@ --- title: "Transfer Group Ownership" +sidebarTitle: "Transfer Ownership" +description: "Transfer group ownership to another member using the Android SDK" --- + +```kotlin +// Transfer ownership to another member (owner only) +CometChat.transferGroupOwnership("GUID", "NEW_OWNER_UID", + object : CallbackListener() { + override fun onSuccess(message: String) { } + override fun onError(e: CometChatException) { } + }) +``` -*In other words, as a logged-in user, how do I transfer the ownership of any group if I am the owner of the group?* - -In order to transfer the ownership of any group, the first condition is that you must be the owner of the group. In case you are the owner of the group, you can use the `transferGroupOwnership()` method provided by the `CometChat` class. +**Important:** +- Only the current owner can transfer ownership +- The new owner must be an existing group member +- Original owner becomes a regular admin after transfer +- Required before owner can leave the group + -This will be helpful as the owner is not allowed to leave the group. In case, you as the owner would like to leave the group, you will have to use this method and transfer your ownership first to any other member of the group and only then you will be allowed to leave the group. +Use `transferGroupOwnership()` to transfer ownership to another group member. Only the current owner can do this. The owner must transfer ownership before they can leave the group. @@ -51,3 +65,66 @@ CometChat.transferGroupOwnership(GUID, UID, object : CallbackListener() + + +## Best Practices + + + + Transfer ownership only to trusted, active members who understand group management responsibilities. Consider their activity level and commitment. + + + Inform the new owner before transferring ownership so they're prepared for the responsibility and understand their new role. + + + If you plan to leave the group, transfer ownership first. Owners cannot leave without transferring ownership or deleting the group. + + + Ensure the target member is an active participant (not banned or kicked) before attempting to transfer ownership. + + + +## Troubleshooting + + + + **Symptom:** `transferGroupOwnership()` fails with "Not the owner" error. + + **Cause:** User is not the current owner of the group. + + **Solution:** Only the current owner can transfer ownership. Check `group.getOwner()` to verify ownership. + + + **Symptom:** Transfer fails with "User not found" or "Not a member" error. + + **Cause:** The target UID doesn't exist or is not a member of the group. + + **Solution:** Verify the UID is correct and the user is an active member. Use [retrieve-group-members](/sdk/android/retrieve-group-members) to confirm membership. + + + **Symptom:** `leaveGroup()` fails when user is the owner. + + **Cause:** Owners must transfer ownership before leaving. + + **Solution:** Use `transferGroupOwnership()` first, then call `leaveGroup()`. Alternatively, use [delete-group](/sdk/android/delete-group) to remove the group entirely. + + + +--- + +## Next Steps + + + + Leave the group after transferring ownership + + + Update member roles and permissions + + + Permanently delete the group instead + + + View group members to choose new owner + + diff --git a/sdk/android/transient-messages.mdx b/sdk/android/transient-messages.mdx index efeceec14..36edb4a3f 100644 --- a/sdk/android/transient-messages.mdx +++ b/sdk/android/transient-messages.mdx @@ -1,8 +1,26 @@ --- title: "Transient Messages" +sidebarTitle: "Transient Messages" +description: "Send and receive ephemeral real-time messages that are not stored on the server using the CometChat Android SDK. Ideal for live reactions and temporary indicators." --- + +```kotlin +// Send transient message to user +val data = JSONObject() +data.put("LIVE_REACTION", "heart") +val transientMessage = TransientMessage("UID", CometChatConstants.RECEIVER_TYPE_USER, data) +CometChat.sendTransientMessage(transientMessage) + +// Receive transient messages +CometChat.addMessageListener("LISTENER_ID", object : MessageListener() { + override fun onTransientMessageReceived(transientMessage: TransientMessage) { + Log.d(TAG, "Transient message: ${transientMessage.data}") + } +}) +``` + Transient messages are messages that are sent in real-time only and are not saved or tracked anywhere. The receiver of the message will only receive the message if he is online and these messages cannot be retrieved later. @@ -41,9 +59,7 @@ CometChat.sendTransientMessage(transientMessage) ## Real-time Transient Messages -*In other words, as a recipient, how do I know when someone sends a transient message?* - -You will receive the transient message in the `onTransientMessageReceived()` method of the registered `MessageListener` class. +Use `onTransientMessageReceived` in `MessageListener` to receive transient messages. @@ -79,3 +95,40 @@ The `TransientMessage` class consists of the below parameters: | **receiverId** | Unique Id of the receiver. This can be the Id of the group or the user the transient message is sent to. | | **receiverType** | The type of the receiver - `CometChat.RECEIVER_TYPE.USER` or `CometChat.RECEIVER_TYPE.GROUP` | | **data** | A JSONObject to provide data. | + + +Always remove listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## Best Practices + + + + Transient messages are ideal for temporary events like typing indicators, live reactions, or presence updates. For messages that need to be stored or retrieved later, use regular text or custom messages instead. + + + Since transient messages are sent in real-time, keep the data payload minimal to reduce network overhead and improve delivery speed. Avoid sending large objects or binary data. + + + Transient messages are only delivered to online users. If delivery confirmation is critical, implement a fallback mechanism using regular messages or check user presence before sending. + + + +--- + +## Next Steps + + + + Send text, media, and custom messages that are stored and retrievable + + + Handle real-time message delivery with message listeners + + + Show real-time typing status for users and groups + + + Learn about all available real-time event listeners + + diff --git a/sdk/android/troubleshooting.mdx b/sdk/android/troubleshooting.mdx new file mode 100644 index 000000000..04c3e59a9 --- /dev/null +++ b/sdk/android/troubleshooting.mdx @@ -0,0 +1,229 @@ +--- +title: "Troubleshooting" +sidebarTitle: "Troubleshooting" +description: "Common failure modes and fixes for the CometChat Android SDK." +--- + +Find solutions to common issues when building with the CometChat Android SDK. + + + +| Issue | Fix | +|-------|-----| +| `init()` fails | Verify App ID and Region from [Dashboard](https://app.cometchat.com) | +| Login fails with "UID not found" | Create user via Dashboard or REST API first | +| SDK methods fail | Ensure `init()` completes before calling other methods | +| No real-time events | Check WebSocket connection, verify listeners registered | +| Build fails | Check Gradle dependency version and repository URL | + + + +--- + +## Initialization & Authentication + +| Symptom | Cause | Fix | +|---------|-------|-----| +| `init()` fails with "App ID not found" | Invalid App ID or Region | Verify credentials in [Dashboard](https://app.cometchat.com) → API & Auth Keys | +| `init()` fails silently | Missing credentials | Double-check App ID and Region are non-null strings | +| "CometChat not initialized" | `init()` not awaited | Ensure `init()` callback's `onSuccess` fires before calling other methods | +| Login fails with "UID not found" | User doesn't exist | Create user via [Dashboard](https://app.cometchat.com) or [REST API](https://api-explorer.cometchat.com/reference/creates-user) | +| Login fails with "Auth Key is not valid" | Wrong Auth Key | Verify Auth Key in Dashboard. Don't confuse with REST API Key | +| `getLoggedInUser()` returns null | Session cleared or `init()` not called | Call `init()` on every app start before checking session | +| Auth Token expired | Token lifetime exceeded | Generate new token via [REST API](https://api-explorer.cometchat.com/reference/create-authtoken) | +| User appears offline after login | Presence not configured | Use `subscribePresenceForAllUsers()` in `AppSettingsBuilder` | + +--- + +## Gradle & Build Issues + +| Symptom | Cause | Fix | +|---------|-------|-----| +| `Could not resolve com.cometchat:chat-sdk-android` | Missing repository | Add `https://dl.cloudsmith.io/public/cometchat/cometchat/maven/` to your project-level `build.gradle` repositories block | +| Duplicate class errors at compile time | Multiple versions of a dependency | Run `./gradlew dependencies` to find conflicts. Use `exclude group:` or force a single version | +| `Manifest merger failed` | Conflicting `minSdkVersion` | Set `minSdkVersion` to at least 21 (24 if using the Calls SDK) in your app-level `build.gradle` | +| `Java 8 features not supported` | Missing compile options | Add `compileOptions { sourceCompatibility JavaVersion.VERSION_1_8; targetCompatibility JavaVersion.VERSION_1_8 }` to your `android` block | +| Build succeeds but crashes on launch | AndroidX not enabled | Add `android.useAndroidX=true` and `android.enableJetifier=true` to `gradle.properties` | +| Calls SDK not found | Not added to Gradle | Add `implementation 'com.cometchat:calls-sdk-android:4.x.x'` to your app-level `build.gradle` | + +--- + +## Permissions + +| Symptom | Cause | Fix | +|---------|-------|-----| +| App crashes when sending media | Missing storage permission | Request `READ_EXTERNAL_STORAGE` / `READ_MEDIA_IMAGES` at runtime before file access | +| No audio in calls | `RECORD_AUDIO` not granted | Request `RECORD_AUDIO` permission before initiating or joining a call | +| No video in calls | `CAMERA` not granted | Request `CAMERA` permission before initiating or joining a video call | +| Permission dialog never appears | Permission not declared in manifest | Add the required `` tags to `AndroidManifest.xml` | +| Permission permanently denied | User selected "Don't ask again" | Use `shouldShowRequestPermissionRationale()` to detect this and direct the user to app settings | + +--- + +## ProGuard & Release Builds + +| Symptom | Cause | Fix | +|---------|-------|-----| +| App works in debug but crashes in release | ProGuard stripping SDK classes | Add CometChat ProGuard rules (see below) to your `proguard-rules.pro` | +| `ClassNotFoundException` in release build | Obfuscation removing required class | Ensure `-keep class com.cometchat.**` rules are applied | +| JSON parsing errors in release | Model class fields renamed by ProGuard | Add `-keepclassmembers` rules for SDK model classes | +| Calls SDK crashes in release | Calls SDK classes obfuscated | Add `-keep class com.cometchat.calls.**` to your ProGuard rules | + +Add these rules to your `proguard-rules.pro`: + +``` +-keep class com.cometchat.** { *; } +-keep class com.cometchat.calls.** { *; } +-dontwarn com.cometchat.** +``` + +--- + +## Messaging + +| Symptom | Cause | Fix | +|---------|-------|-----| +| `sendMessage()` fails | Not logged in or invalid receiver | Ensure `login()` completes. Verify receiver UID/GUID exists | +| Messages sent but not received | Listener not registered | Register `addMessageListener()` with `onTextMessageReceived` | +| Duplicate messages | Multiple listeners with same ID | Use unique listener IDs. Remove old listeners before re-registering | +| Wrong conversation | Wrong receiver type | Use `CometChatConstants.RECEIVER_TYPE_USER` for 1:1, `RECEIVER_TYPE_GROUP` for groups | +| Media upload fails | File too large or unsupported | Check limits. Supported: PNG, JPG, GIF, MP4, MP3, WAV | +| Metadata not appearing | Set after send | Call `setMetadata()` before the send method | +| Pagination not working | New request object | Reuse the same `MessagesRequest` for `fetchPrevious()`/`fetchNext()` | +| Thread replies in main chat | Missing filter | Add `.hideReplies(true)` to `MessagesRequestBuilder` | +| Deleted messages showing | Missing filter | Add `.hideDeletedMessages(true)` | + +--- + +## Groups + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Cannot join group | Invalid GUID | Verify GUID. Create group first if needed | +| Cannot send to group | Not a member | Join group first with `joinGroup()` | +| Cannot kick/ban members | Insufficient scope | Only admins and moderators can kick/ban | +| Can't join private group | Requires invite | Private groups require an admin to add you | +| Owner can't leave | Ownership not transferred | Call `transferGroupOwnership()` first | +| Password join fails | Wrong password | Pass correct password as the third parameter | +| `fetchNext()` returns same results | New request object | Reuse the same `GroupsRequest` instance | +| Scope filter returns nothing | Invalid strings | Use `"admin"`, `"moderator"`, `"participant"` | +| Cannot demote admin | Not owner | Only the group owner can demote admins | +| Kicked user can still see group | Kick vs ban | Use `banGroupMember()` to prevent rejoining | + +--- + +## Calling + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Calls SDK not found | Not added to Gradle | Add `implementation 'com.cometchat:calls-sdk-android:4.x.x'` | +| No audio/video | Permissions denied | Request `CAMERA` and `RECORD_AUDIO` permissions at runtime before the call | +| Call not connecting | Session ID mismatch | Verify both participants use the same session ID | +| One-way audio | Firewall blocking WebRTC | Check network config. Corporate networks may block WebRTC ports | +| Incoming call not showing | Listener not registered | Register `addCallListener()` at the Application or root Activity level | +| Call ended event not received | Wrong callback | Use `onCallEndedMessageReceived` in `CallListener` for call-end messages, `onCallEnded` in `OngoingCallListener` for session events | +| Black screen after joining | View not visible or sized | Ensure the call container `View` has non-zero dimensions and is visible | +| Calls crash in release build | ProGuard stripping classes | Add `-keep class com.cometchat.calls.** { *; }` to `proguard-rules.pro` | + +--- + +## WebSocket & Connection + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Real-time events not received | WebSocket disconnected | Check `getConnectionStatus()`. Reconnect if needed | +| WebSocket fails | Firewall blocking | Check network config. Corporate firewalls may block WebSocket connections | +| Connection drops frequently | Network instability | Use `addConnectionListener()` to monitor and reconnect | +| Stuck in "connecting" | Network or config issue | Verify network, `appId`, and `region` | +| `featureThrottled` status | Feature rate-limited | Reduce frequency of the throttled feature. Listen for `onFeatureThrottled` in `ConnectionListener` | +| No events after login | Auto-connect disabled | Call `CometChat.connect()` manually if `autoEstablishSocketConnection(false)` | +| `connect()` doesn't work | Not logged in | Ensure user is logged in first | + +--- + +## Listeners & Lifecycle + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Events not firing | Registered before init | Register after `init()` and `login()` complete | +| Duplicate events | Multiple listeners with same ID | Remove old listeners before adding new ones | +| Missing events after navigation | Listeners removed on `onPause()` | Re-register in `onResume()` when the screen becomes active | +| Events fire after screen is closed | Listeners not removed | Remove listeners in `onPause()` or `onDestroy()` | +| Receipt events not triggering | Receipts not sent | Call `markAsDelivered()`/`markAsRead()` explicitly | +| Memory leaks | Listeners holding Activity reference | Always remove listeners in `onDestroy()` | + +--- + +## Typing, Receipts & Reactions + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Typing indicator stuck | `endTyping()` not called | Call on send, focus loss, or after 3–5s timeout using a `Handler` | +| Double-tick not showing | `markAsDelivered()` not called | Call on message fetch and real-time receive | +| Group receipts missing | Feature not enabled | Enable "Enhanced Messaging Status" in Dashboard | +| `onMessagesDeliveredToAll`/`onMessagesReadByAll` not firing | Not registered | Register these callbacks in `MessageListener` for group-level receipt events | +| Reaction not appearing | UI not synced | Call `updateMessageWithReactionInfo()` on events | +| Duplicate reactions | No check before adding | Use `getReactedByMe()` first | + +--- + +## AI Features + +| Symptom | Cause | Fix | +|---------|-------|-----| +| AI features not appearing | Not enabled | Enable in [Dashboard](https://app.cometchat.com) → AI Features | +| AI Agents not responding | Not configured | Configure Agent in Dashboard. Agents only respond to text | +| `onAIAssistantEventReceived` not firing | Listener not registered | Register `AIAssistantListener` after login | +| Moderation always PENDING | Rules not configured | Configure rules in Dashboard → Moderation → Rules | +| Agentic messages not arriving | Wrong listener | Use `MessageListener` with `onAIAssistantMessageReceived` | + +--- + +## Upgrading from V3 + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Build fails after upgrade | Old artifact still in Gradle | Replace `com.cometchat:chat-sdk-android:3.x.x` with `com.cometchat:chat-sdk-android:4.x.x` | +| Calls SDK not working | Wrong artifact version | Use `com.cometchat:calls-sdk-android:4.x.x` | +| Both versions installed | Gradle conflict | Remove the v3 dependency and sync project | + +--- + +## Error Codes + +For the complete SDK error code reference, see [Error Codes](/sdk/android/error-codes). Common errors you'll encounter: + +| Code | Description | Resolution | +|------|-------------|------------| +| `ERR_UID_NOT_FOUND` | User doesn't exist | Create user via Dashboard or REST API | +| `AUTH_ERR_AUTH_TOKEN_NOT_FOUND` | Invalid or expired auth token | Generate new token via REST API | +| `MISSING_PARAMETERS` | Required parameter not provided | Check method signature and pass all required params | +| `NOT_INITIALIZED` | `init()` not called | Call `CometChat.init()` before any other method | +| `USER_NOT_LOGGED_IN` | No active session | Call `login()` first | +| `ERR_GUID_NOT_FOUND` | Group doesn't exist | Create group or verify GUID | +| `ERR_NOT_A_MEMBER` | Not a group member | Join group first | +| `TOO_MANY_REQUEST` | Rate limit exceeded | See [Rate Limits](/sdk/android/rate-limits) | +| `FAILED_TO_FETCH` | Network issue | Check internet connection and API endpoint | +| `NO_WEBSOCKET_CONNECTION` | WebSocket disconnected | Check connection status, wait for reconnect | + +--- + +## Next Steps + + + + Installation and initialization guide + + + Recommended patterns and practices + + + Complete SDK error code reference + + + AI Agents, Moderation, and Copilot + + + Open a support ticket + + diff --git a/sdk/android/typing-indicators.mdx b/sdk/android/typing-indicators.mdx index 452fd1f4a..ace6930f7 100644 --- a/sdk/android/typing-indicators.mdx +++ b/sdk/android/typing-indicators.mdx @@ -1,19 +1,35 @@ --- title: "Typing Indicators" +sidebarTitle: "Typing Indicators" +description: "Send and receive real-time typing indicators using the CometChat Android SDK." --- + +```kotlin +// Start typing indicator +val typingIndicator = TypingIndicator("UID", CometChatConstants.RECEIVER_TYPE_USER) +CometChat.startTyping(typingIndicator) -## Send a Typing Indicator +// Stop typing indicator +CometChat.endTyping(typingIndicator) -*In other words, as a sender, how do I let the recipient(s) know that I'm typing?* +// Listen for typing events +CometChat.addMessageListener("LISTENER_ID", object : MessageListener() { + override fun onTypingStarted(indicator: TypingIndicator) { } + override fun onTypingEnded(indicator: TypingIndicator) { } +}) +``` + + +## Send a Typing Indicator ### Start Typing -You can use the `startTyping()` method to inform the receiver that the logged in user has started typing. The receiver will receive this information in the `onTypingStarted()` method of the `MessageListener` class. In order to send the typing indicator, you need to use the `TypingIndicator` class. +Use `startTyping()` with a `TypingIndicator` object to notify the receiver that you're typing. - + ```java TypingIndicator typingIndicator = new TypingIndicator(UID, CometChatConstants.RECEIVER_TYPE_USER); @@ -22,7 +38,7 @@ CometChat.startTyping(typingIndicator); - + ```kotlin val typingIndicator =TypingIndicator(UID,CometChatConstants.RECEIVER_TYPE_USER) @@ -31,7 +47,7 @@ CometChat.startTyping(typingIndicator) - + ```java TypingIndicator typingIndicator = new TypingIndicator(GUID, CometChatConstants.RECEIVER_TYPE_GROUP); @@ -40,7 +56,7 @@ CometChat.startTyping(typingIndicator); - + ```kotlin val typingIndicator = TypingIndicator(GUID,CometChatConstants.RECEIVER_TYPE_GROUP) @@ -53,10 +69,10 @@ CometChat.startTyping(typingIndicator) ### Stop Typing -You can use the `endTyping()` method to inform the receiver that the logged in user has stopped typing. The receiver will receive this information in the `onTypingEnded()` method of the `MessageListener` class. In order to send the typing indicator, you need to use the `TypingIndicator` class. +Use `endTyping()` to notify the receiver that you've stopped typing. - + ```java TypingIndicator typingIndicator = new TypingIndicator(UID, CometChatConstants.RECEIVER_TYPE_USER); @@ -65,7 +81,7 @@ CometChat.endtyping(typingIndicator); - + ```kotlin val typingIndicator = TypingIndicator(UID,CometChatConstants.RECEIVER_TYPE_USER) @@ -74,7 +90,7 @@ CometChat.endTyping(typingIndicator) - + ```java TypingIndicator typingIndicator = new TypingIndicator(GUID, CometChatConstants.RECEIVER_TYPE_GROUP); @@ -83,7 +99,7 @@ CometChat.endTyping(typingIndicator); - + ```kotlin val typingIndicator = TypingIndicator(GUID,CometChatConstants.RECEIVER_TYPE_GROUP) @@ -95,17 +111,12 @@ CometChat.endTyping(typingIndicator) -Custom Data - -You can use the `metadata` field of the `TypingIndicator` class to pass additional data along with the typing indicators. The metadata field is a JSONObject and can be set using the `setMetadata()` method of the `TypingIndicator` class. This data will be received at the receiver end and can be obtained using the `getMetadata()` method. - +Use `setMetadata()` on `TypingIndicator` to pass additional custom data. Retrieve it with `getMetadata()` on the receiver side. ## Real-time Typing Indicators -*In other words, as a recipient, how do I know when someone is typing?* - -You will receive the typing indicators in the `onTypingStarted()` and the `onTypingEnded()` method of the registered `MessageListener` class. +Use `onTypingStarted` and `onTypingEnded` in `MessageListener` to receive typing events. @@ -152,3 +163,130 @@ The `TypingIndicator` class consists of the below parameters: | `receiverId` | `UID` of the receiver. This is the ID of the group or the user the typing indicator is being sent to. | | `receiverType` | This parameter indicates if the typing indicator is to be sent to a user or a group. The possible values are: 1. `CometChatConstants.RECEIVER_TYPE_USER` 2. `CometChatConstants.RECEIVER_TYPE_GROUP` | | `metadata` | A JSONObject to provider additional data | + +## TypingIndicator Payload Structure + + + +The `TypingIndicator` object contains information about typing status: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `sender` | [User](#user-object-typing) | User who is typing | +| `receiverId` | String | ID of the receiver (user UID or group GUID) | +| `receiverType` | String | Type of receiver. Values: `"user"`, `"group"` | +| `metadata` | JSONObject | Custom typing metadata | +| `lastTimestamp` | long | Unix timestamp of last typing event | +| `typingStatus` | String | Typing status. Values: `"started"`, `"ended"` | + +**Sample TypingIndicator Object:** + +```json +{ + "sender": { + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "status": "online", + "role": "default" + }, + "receiverId": "user_456", + "receiverType": "user", + "metadata": {}, + "lastTimestamp": 1699900000, + "typingStatus": "started" +} +``` + + + + + +The nested `User` object in `sender` contains: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for access control | +| `metadata` | JSONObject | Custom data set by developer | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message | +| `lastActiveAt` | long | Unix timestamp of last activity | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + + + + +Always remove message listeners when they're no longer needed (e.g., in `onDestroy()` or when navigating away). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +## Best Practices + + + + Don't send typing indicators on every keystroke. Use a debounce mechanism (e.g., 300-500ms delay) to reduce API calls and improve performance. + + + Automatically call `endTyping()` after 3-5 seconds of inactivity to handle cases where users stop typing without sending a message. + + + Always call `endTyping()` immediately after sending a message to clear the typing indicator for recipients. + + + In group chats, display "User1, User2, and 3 others are typing..." to handle multiple simultaneous typers gracefully. + + + Use the metadata field to send additional context like "recording audio" or "uploading photo" for richer status indicators. + + + +## Troubleshooting + + + + **Symptom:** `onTypingStarted()` or `onTypingEnded()` not being called. + + **Cause:** Message listener not registered or removed prematurely. + + **Solution:** Ensure `addMessageListener()` is called before typing indicators are sent. Keep the listener active while the chat is open. + + + **Symptom:** Typing indicator shows indefinitely without clearing. + + **Cause:** `endTyping()` not called when user stops typing or leaves the chat. + + **Solution:** Call `endTyping()` when user sends a message, clears input, or navigates away. Implement auto-timeout after 3-5 seconds. + + + **Symptom:** Performance issues or rate limiting due to excessive typing indicator calls. + + **Cause:** Sending typing indicators on every keystroke without debouncing. + + **Solution:** Implement debouncing to send typing indicators at most once every 300-500ms. + + + +--- + +## Next Steps + + + + Track message delivery and read status + + + Handle incoming messages with listeners + + + Fetch conversation list + + + Learn more about event listeners + + diff --git a/sdk/android/update-group.mdx b/sdk/android/update-group.mdx index 4fbbba0e3..c1ab73da6 100644 --- a/sdk/android/update-group.mdx +++ b/sdk/android/update-group.mdx @@ -1,14 +1,29 @@ --- -title: "Update A Group" +title: "Update Group" +sidebarTitle: "Update Group" +description: "Modify group details, settings, and metadata using the Android SDK" --- + +```kotlin +// Update group details +val group = Group("GUID", "New Name", CometChatConstants.GROUP_TYPE_PUBLIC, "") +group.description = "Updated description" +group.icon = "https://example.com/icon.png" + +CometChat.updateGroup(group, object : CometChat.CallbackListener() { + override fun onSuccess(updatedGroup: Group) { } + override fun onError(e: CometChatException) { } +}) +``` -## Update Group +**Note:** Only admins and moderators can update group details. See [Group Class](/sdk/android/create-group#group-class) for editable fields. + -*In other words, as a group owner, how can I update the group details?* +## Update Group -You can update the existing details of the group using the `updateGroup()` method. +Use `updateGroup()` with a `Group` object containing the fields you want to update. @@ -56,13 +71,13 @@ CometChat.updateGroup(group,object :CometChat.CallbackListener(){ -This method takes an instance of the `Group` class as a parameter which should contain the data that you wish to update. +This method takes an instance of the `Group` class as a parameter, which should contain the data you wish to update. -| Parameter | Description | -| --------- | ---------------------------- | -| `group` | an instance of class `Group` | +| Parameter | Description | +| --------- | ------------------------------ | +| `group` | An instance of the `Group` class | -After the successful update of the group, you will receive an instance of `Group` class containing updated information of the group. +After the successful update of the group, you will receive an instance of the `Group` class containing the updated information of the group. There is no real-time event listener available to receive updated group details when the `updateGroup()` method is called. To get the latest group information, you need to fetch the group details again using the appropriate method. diff --git a/sdk/android/upgrading-from-v3-guide.mdx b/sdk/android/upgrading-from-v3-guide.mdx index 14bccd7a3..82d783b4f 100644 --- a/sdk/android/upgrading-from-v3-guide.mdx +++ b/sdk/android/upgrading-from-v3-guide.mdx @@ -1,12 +1,28 @@ --- title: "Upgrading From V3" +sidebarTitle: "Upgrading from V3" +description: "Migration guide for upgrading from CometChat Android SDK v3 to v4" --- + +**Migration Steps:** +1. Update Maven URL to `https://dl.cloudsmith.io/public/cometchat/cometchat/maven/` +2. Update dependency to `com.cometchat:chat-sdk-android:4.1.7` (or latest v4) +3. Change all imports from `com.cometchat.pro.*` to `com.cometchat.chat.*` +4. Rebuild and test your application -Upgrading from v3.x to v4 is fairly simple. Below are the major changes that are released as a part of CometChat v4: +**Key Changes:** +- Package name changed from `com.cometchat.pro` to `com.cometchat.chat` +- Maven repository URL updated +- API remains largely compatible with v3 -Please follow the [setup](/sdk/android/setup) instructions to upgrade to the latest V4 version. +**Full guide:** Follow the [setup](/sdk/android/setup) instructions for detailed v4 setup. + + + + +Upgrading from v3.x to v4 is straightforward. The API surface is the same — only the Maven URL, dependency artifact, and import package names changed. ## Maven URL Change @@ -40,6 +56,78 @@ dependencies { -## Change The Import Classes Packages +## Change the Import Class Packages + +In v3, the import class package name starts with `com.cometchat.pro.*`. Change it to `com.cometchat.chat.*` everywhere in the project and you are done with the v3 to v4 migration. + +--- + +## Best Practices + + + + After updating imports and dependencies, thoroughly test all CometChat functionality in your app. Pay special attention to initialization, authentication, messaging, and calling features. + + + Use your IDE's find-and-replace feature to update all `com.cometchat.pro` imports to `com.cometchat.chat` across your entire project. Don't miss any files. + + + While the API is largely compatible, review the v4 changelog for any breaking changes that might affect your specific use case. Test edge cases and error handling. + + + Ensure your project-level build.gradle includes the new Maven URL in the repositories section. Clean and rebuild your project after making changes. + + + Perform the migration in a separate git branch so you can easily revert if issues arise. Test thoroughly before merging to your main branch. + + + +## Troubleshooting + + + + **Symptom:** Gradle sync or build fails with "Could not resolve com.cometchat:chat-sdk-android" error. + + **Cause:** Maven repository URL not updated in project-level build.gradle. + + **Solution:** Ensure the new Maven URL `https://dl.cloudsmith.io/public/cometchat/cometchat/maven/` is added to the `allprojects` repositories section in your project-level build.gradle. Clean and rebuild the project. + + + **Symptom:** IDE shows "Cannot resolve symbol" errors for CometChat classes. + + **Cause:** Not all import statements were updated from `com.cometchat.pro.*` to `com.cometchat.chat.*`. + + **Solution:** Use your IDE's find-and-replace feature to search for all occurrences of `com.cometchat.pro` and replace with `com.cometchat.chat`. Rebuild the project and resolve any remaining import errors manually. + + + **Symptom:** App builds successfully but crashes at runtime with ClassNotFoundException or similar errors. + + **Cause:** ProGuard/R8 rules may need updating, or some imports were missed. + + **Solution:** Update ProGuard rules to use the new package name `com.cometchat.chat.*`. Verify all imports are updated. Clean and rebuild the project. Check for any reflection-based code that might reference old package names. + + + **Symptom:** `CometChat.init()` fails with errors after migration. + + **Cause:** Incorrect App ID, Region, or Auth Key, or SDK not properly initialized. + + **Solution:** Verify your App ID and Region are correct in the initialization code. Ensure you're using the v4 initialization pattern. Check that the SDK dependency was properly updated to v4. Review initialization logs for specific error messages. + + + +## Next Steps -In v3 the import class package name start from `com.cometchat.pro.*` . Change it to `com.cometchat.chat.*` everywhere in the project and you are done with v3 to v4 migration + + + Review complete v4 setup instructions + + + Test messaging functionality after migration + + + Update calling features to v4 + + + Verify event listeners work correctly + + diff --git a/sdk/android/user-management.mdx b/sdk/android/user-management.mdx index 9c423f8b9..68f820954 100644 --- a/sdk/android/user-management.mdx +++ b/sdk/android/user-management.mdx @@ -1,30 +1,41 @@ --- title: "User Management" +sidebarTitle: "User Management" +description: "Create, update, and delete user accounts in CometChat using the Android SDK" --- + +```kotlin +// Create user (use API Key - dev/testing only) +val apiKey = "API_KEY" +val user = User() +user.uid = "user1" +user.name = "Kevin" -When a user logs into your app, you need to programmatically login the user into CometChat. But before you log in the user to CometChat, you need to create the user. - -Summing up- +CometChat.createUser(user, apiKey, object : CometChat.CallbackListener() { + override fun onSuccess(user: User) { } + override fun onError(e: CometChatException) { } +}) -**When a user registers in your app** +// Update logged-in user (no API Key needed) +val updatedUser = User() +updatedUser.name = "Andrew Joseph" -1. You add the user details in your database -2. You create a user in CometChat +CometChat.updateCurrentUserDetails(updatedUser, object : CallbackListener() { + override fun onSuccess(user: User) { } + override fun onError(e: CometChatException) { } +}) +``` -**When a user logs into your app** +**Note:** User creation/updates should ideally happen on your backend using [REST API](https://api-explorer.cometchat.com). + -1. You log in the user to your app -2. You [log in the user to CometChat](/sdk/android/authentication-overview) (programmatically) +When a user registers in your app, create them in CometChat. When they log in, [log them into CometChat](/sdk/android/authentication-overview) as well. ## Creating a user -Ideally, user creation should take place at your backend. You can refer our Rest API to learn more about [creating a user](https://api-explorer.cometchat.com/reference/creates-user) and use the appropriate code sample based on your backend language. - -However, if you wish to create users on the fly, you can use the `createUser()` method. This method takes a `User` object and the `API Key` as input parameters and returns the created `User` object if the request is successful. - -For more details on the fields present in the User class, please check [this](/sdk/android/user-management#user-class) +Ideally, user creation should happen on your backend using the [REST API](https://api-explorer.cometchat.com/reference/creates-user). For on-the-fly creation during development, use the `createUser()` method with a `User` object and your API Key. @@ -71,6 +82,10 @@ override fun onError(e: CometChatException) { + +**API Key Security:** The API Key should only be used for development and testing. In production, create and update users on your backend server using the [REST API](https://api-explorer.cometchat.com) to keep your API Key secure. Never expose API Keys in production client code. + + UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. @@ -79,7 +94,7 @@ UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and othe ## Updating a user -Updating a user similar to creating a user should ideally be achieved at your backend using the Restful APIs. For more information, you can check the [update a user](https://api-explorer.cometchat.com/reference/update-user) section. However, this can be achieved on the fly as well using the `updateUser()` method. This method takes a `User` object and the API Key as inputs and returns the updated `User` object on successful execution of the request. +Use `updateUser()` with a `User` object and API Key. Ideally done on your backend via the [REST API](https://api-explorer.cometchat.com/reference/update-user). @@ -126,11 +141,11 @@ override fun onError(e: CometChatException) { -Please make sure the `User` object provided to the `updateUser()` method has the `UID` of the user to be updated set. +Please make sure the `User` object provided to the `updateUser()` method has the `UID` of the user to be updated. ## Updating logged-in user -Updating a logged-in user is similar to updating a user. The only difference being this method does not require an AuthKey. This method takes a `User` object as input and returns the updated `User` object on the successful execution of the request. +Use `updateCurrentUserDetails()` to update the currently logged-in user's profile. No API Key required. @@ -173,11 +188,11 @@ override fun onError(e: CometChatException) { -By using the `updateCurrentUserDetails()` method one can only update the logged-in user irrespective of the UID passed. Also, it is not possible to update the role of a logged-in user. +By using the `updateCurrentUserDetails()` method, you can only update the logged-in user regardless of the UID passed. Also, it is not possible to update the role of a logged-in user. ## Deleting a user -Deleting a user can only be achieved via the Restful APIs. For more information please check the [delete a user](https://api-explorer.cometchat.com/reference/delete-user)section. +Deleting a user can only be achieved via the RESTful APIs. For more information, see the [delete a user](https://api-explorer.cometchat.com/reference/delete-user) section. ## User Class @@ -195,3 +210,118 @@ Deleting a user can only be achieved via the Restful APIs. For more information | hasBlockedMe | No | A boolean that determines if the user has blocked the logged in user | | blockedByMe | No | A boolean that determines if the logged in user has blocked the user | | tags | Yes | A list of tags to identify specific users | + +## User Payload Structure + + + +The `User` object returned by SDK methods contains the following fields: + +| Parameter | Type | Description | +|-----------|------|-------------| +| `uid` | String | Unique identifier of the user | +| `name` | String | Display name of the user | +| `avatar` | String | URL to user's profile picture | +| `link` | String | URL to user's profile page | +| `role` | String | User role for role-based access control | +| `metadata` | JSONObject | Custom data set by developer. Can contain any key-value pairs | +| `status` | String | User online status. Values: `"online"`, `"offline"` | +| `statusMessage` | String | Custom status message set by user | +| `lastActiveAt` | long | Unix timestamp of last activity (milliseconds) | +| `hasBlockedMe` | boolean | Whether this user has blocked the logged-in user | +| `blockedByMe` | boolean | Whether the logged-in user has blocked this user | +| `tags` | Array\ | List of tags for user identification and filtering | +| `deactivatedAt` | long | Unix timestamp when user was deactivated (0 if active) | + +**Sample User Object:** + +```json +{ + "uid": "user_123", + "name": "John Doe", + "avatar": "https://example.com/avatar.png", + "link": "https://example.com/profile/user_123", + "role": "default", + "metadata": { + "customKey": "customValue", + "preferences": { + "theme": "dark", + "notifications": true + } + }, + "status": "online", + "statusMessage": "Available", + "lastActiveAt": 1699900000000, + "hasBlockedMe": false, + "blockedByMe": false, + "tags": ["premium", "verified"], + "deactivatedAt": 0 +} +``` + + + +--- + +## Best Practices + + + + Always create and update users on your backend server using the REST API. This keeps your API Key secure and allows you to validate user data before creating accounts in CometChat. + + + When updating the currently logged-in user's profile, use `updateCurrentUserDetails()` instead of `updateUser()`. This method doesn't require an API Key and is more secure for client-side updates. + + + Ensure UIDs are alphanumeric with only underscores and hyphens. Validate UID format before calling `createUser()` to avoid API errors. + + + Keep user data synchronized between your app's database and CometChat. When a user updates their profile in your app, update it in CometChat as well to maintain consistency. + + + Store additional user information (preferences, settings, custom attributes) in the `metadata` field as JSON. This allows you to extend user profiles without modifying the core schema. + + + +## Troubleshooting + + + + **Symptom:** `createUser()` fails with "User with this UID already exists" error. + + **Cause:** Attempting to create a user with a UID that's already registered in CometChat. + + **Solution:** Check if the user exists before creating by calling `CometChat.getUser()` first. If the user exists, use `updateUser()` instead of `createUser()`. + + + **Symptom:** `createUser()` or `updateUser()` fails with "Invalid API Key" error. + + **Cause:** Incorrect API Key or using Auth Key instead of API Key. + + **Solution:** Verify your API Key in the [CometChat Dashboard](https://app.cometchat.com) under API & Auth Keys. Ensure you're using the API Key (for user management), not the Auth Key (for authentication). + + + **Symptom:** `updateCurrentUserDetails()` doesn't update the user's role. + + **Cause:** The `updateCurrentUserDetails()` method cannot modify user roles for security reasons. + + **Solution:** Update user roles on your backend using the [REST API](https://api-explorer.cometchat.com/reference/update-user). Role changes should be controlled server-side. + + + +## Next Steps + + + + Log users into CometChat after creating their accounts + + + Fetch user lists and search for specific users + + + Track user online/offline status in real-time + + + Implement user blocking and unblocking features + + diff --git a/sdk/android/user-presence.mdx b/sdk/android/user-presence.mdx index 58c0f69f4..4b82b55a4 100644 --- a/sdk/android/user-presence.mdx +++ b/sdk/android/user-presence.mdx @@ -1,30 +1,76 @@ --- title: "User Presence" +sidebarTitle: "User Presence" +description: "Track and subscribe to user online/offline status and presence updates in your Android app" --- + + + +```kotlin +// Subscribe to presence during init +val appSettings = AppSettings.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion("REGION") + .build() +CometChat.init(context, "APP_ID", appSettings, callback) + +// Add user listener +CometChat.addUserListener("LISTENER_ID", object : CometChat.UserListener() { + override fun onUserOnline(user: User?) { + Log.d(TAG, "${user?.name} is online") + } + override fun onUserOffline(user: User?) { + Log.d(TAG, "${user?.name} is offline") + } +}) -User Presence helps us understand if a user is available to chat or not. - -## Real-time Presence - -*In other words, as a logged-in user, how do I know if a user is online or offline?* +// Remove listener (important!) +CometChat.removeUserListener("LISTENER_ID") +``` + + +```java +// Subscribe to presence during init +AppSettings appSettings = new AppSettings.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion("REGION") + .build(); +CometChat.init(context, "APP_ID", appSettings, callback); + +// Add user listener +CometChat.addUserListener("LISTENER_ID", new CometChat.UserListener() { + @Override public void onUserOnline(User user) { + Log.d(TAG, user.getName() + " is online"); + } + @Override public void onUserOffline(User user) { + Log.d(TAG, user.getName() + " is offline"); + } +}); + +// Remove listener (important!) +CometChat.removeUserListener("LISTENER_ID"); +``` + + + -Based on the settings provided in the `AppSettings` class while initializing CometChat using the `init()` method, the logged-in user will receive the presence for the other users in the app. +User Presence lets you know if a user is available to chat. Configure presence subscription during `init()`, then listen for online/offline events via `UserListener`. -In the `AppSettings` class, you can set the type of presence you wish to receive. +## Real-time Presence -For presence subscription, the `AppSettingsBuilder` provides 3 methods : +Based on the `AppSettings` configured during `init()`, the logged-in user receives presence updates for other users. -* `subscribePresenceForAllUsers()` - This will inform the logged-in user when any user in the app comes online or goes offline. -* `subscribePresenceForRoles(List roles)` - This will inform the logged-in user, only when the users with the specified roles come online or go offline. -* `subscribePresenceForFriends()` - This will inform the logged-in user when any of their friends come online or go offline. +`AppSettingsBuilder` provides three subscription options: -If none of the above methods are set, no presence will be sent to the logged-in user. +* `subscribePresenceForAllUsers()` — Receive presence for all users in the app. +* `subscribePresenceForRoles(List roles)` — Receive presence only for users with specified roles. +* `subscribePresenceForFriends()` — Receive presence only for friends. -For every activity or fragment you wish to receive user events in, you need to register the `UserListener` using the `addUserListener()` method. +If none are set, no presence events are sent. -We suggest adding the listener in the `onResume()` method of the activity or the fragment where you wish to receive these events in. +Register `UserListener` in `onResume()` and remove it in `onPause()`. @@ -65,9 +111,7 @@ CometChat.addUserListener(listenerID,object :CometChat.UserListener(){ | ------------ | ----------------------------------------------------------------------------------------------- | | `listenerID` | An ID that uniquely identifies that listener. We recommend using the activity or fragment name. | -You will receive an object of the `User` class in the listener methods. - -We recommend you remove the listener once the activity or fragment is not in use. We suggest adding this method in the `onPause()` method of the activity or the fragment where you wish to receive these events in. +Remove the listener in `onPause()` when the activity or fragment is no longer in use. @@ -90,15 +134,48 @@ CometChat.removeUserListener(listenerID) + +Always remove listeners when they're no longer needed (e.g., in `onPause()` or `onDestroy()`). Failing to remove listeners can cause memory leaks and duplicate event handling. + + ## User List Presence -*In other words, as a logged-in user, when I retrieve the user list, how do I know if a user is online/offline?* +When you [retrieve the list of users](/sdk/android/retrieve-users), each `User` object includes: + +* `status` — `"online"` or `"offline"` +* `lastActiveAt` — Unix timestamp of when the user was last online (useful for "Last seen" indicators) -When you [retrieve the list of users](/sdk/android/retrieve-users), in the [User](/sdk/android/user-management#user-class) object, you will receive 2 keys: +--- -1. `status` - This will hold either of the two values : +## Best Practices -* `online` - This indicates that the user is currently online and available to chat. -* `offline` - This indicates that the user is currently offline and is not available to chat. + + + Use `subscribePresenceForAllUsers()` for small communities (under 1000 users), `subscribePresenceForFriends()` for social apps where users have friend lists, or `subscribePresenceForRoles()` for role-based presence to optimize performance and reduce unnecessary presence updates. + + + Batch presence updates in your UI to avoid excessive re-renders when multiple users change status simultaneously. Consider debouncing presence updates if you're displaying a large user list. + + + Display a "Last seen" indicator using the `lastActiveAt` timestamp for offline users to provide better context about user availability. + + + +--- -2. `lastActiveAt` - In case the user is offline, this field holds the timestamp of the time when the user was last online. This can be used to display a **Last seen** for that user. +## Next Steps + + + + Fetch user lists with presence information + + + Monitor SDK connection state and handle reconnection + + + Learn about user objects and properties + + + Understand all available event listeners + + diff --git a/sdk/android/users-overview.mdx b/sdk/android/users-overview.mdx index ea9156b7a..a58443988 100644 --- a/sdk/android/users-overview.mdx +++ b/sdk/android/users-overview.mdx @@ -1,10 +1,44 @@ --- title: "Users" sidebarTitle: "Overview" +description: "Manage users, retrieve user lists, and track user presence in your Android application" --- + +Choose your user management path: +- **User Management** → [user-management](/sdk/android/user-management) - Create and sync users +- **Retrieve Users** → [retrieve-users](/sdk/android/retrieve-users) - Fetch user lists with filters +- **User Presence** → [user-presence](/sdk/android/user-presence) - Track online/offline status +- **Block Users** → [block-users](/sdk/android/block-users) - Block and unblock users + -The primary aim for our users' functionality is to allow you to quickly retrieve and add users to CometChat. +Users in CometChat map directly to users in your app. Start with [user management](/sdk/android/user-management) to sync your users, then [retrieve users](/sdk/android/retrieve-users) to display them. -You can begin with [user management](/sdk/android/user-management) to sync your users to CometChat. Once that is done, you can [retrieve users](/sdk/android/retrieve-users) and display them in your app. +## Features + +| Feature | Description | Guide | +| --- | --- | --- | +| User Management | Create, update, and sync users with CometChat | [User Management](/sdk/android/user-management) | +| Retrieve Users | Fetch user lists with filters, search, and pagination | [Retrieve Users](/sdk/android/retrieve-users) | +| User Presence | Track real-time online/offline status | [User Presence](/sdk/android/user-presence) | +| Block Users | Block and unblock users to control communication | [Block Users](/sdk/android/block-users) | + +--- + +## Next Steps + + + + Create and sync users with CometChat + + + Fetch user lists with filtering and search + + + Track and display user online/offline status + + + Block and unblock users to control interactions + + diff --git a/sdk/android/video-view-customisation.mdx b/sdk/android/video-view-customisation.mdx index 75040b362..da5f2bc7d 100644 --- a/sdk/android/video-view-customisation.mdx +++ b/sdk/android/video-view-customisation.mdx @@ -1,20 +1,33 @@ --- title: "Video View Customisation" +sidebarTitle: "Video View Customisation" +description: "Customize the main video container appearance, aspect ratio, and UI controls in Android calls" --- + +```kotlin +// Customize main video container +val videoSettings = MainVideoContainerSetting() +videoSettings.mainVideoAspectRatio = CometChatCallsConstants.ASPECT_RATIO_CONTAIN +videoSettings.setFullScreenButtonParams(CometChatCallsConstants.POSITION_BOTTOM_RIGHT, true) +videoSettings.setNameLabelParams(CometChatCallsConstants.POSITION_BOTTOM_LEFT, true, "#333333") +videoSettings.setZoomButtonParams(CometChatCallsConstants.POSITION_BOTTOM_RIGHT, true) -This section will guide you to customise the main video container. - -## Implementation +// Apply to call settings +val callSettings = CometChatCalls.CallSettingsBuilder(context, videoContainer) + .setMainVideoContainerSetting(videoSettings) + .build() +``` -Once you have decided to implement [Default Calling](/sdk/android/direct-calling) or [Direct Calling](/sdk/android/default-calling) calling and followed the steps to implement them. Just few additional methods will help you quickly customize the main video container. +**Prerequisites:** [Default Calling](/sdk/android/default-calling) or [Direct Calling](/sdk/android/direct-calling) setup + -Please make sure your callSettings is configured accordingly for [Default Calling](/sdk/android/direct-calling) or [Direct Calling](/sdk/android/default-calling). +Customize the main video container in your call UI — aspect ratio, button positions, and label visibility. Create a `MainVideoContainerSetting` instance, configure it, and pass it to `CallSettingsBuilder.setMainVideoContainerSetting()`. ## Main Video Container Setting -The `MainVideoContainerSetting` Class is the required in case you want to customise the main video view. You need to pass the Object of the `MainVideoContainerSetting` Class in the `setMainVideoContainerSetting()` method of the `CallSettingsBuilder`. +The `MainVideoContainerSetting` class is required when you want to customize the main video view. You need to pass the object of the `MainVideoContainerSetting` class in the `setMainVideoContainerSetting()` method of the `CallSettingsBuilder`. | Setting | Description | | --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -54,3 +67,66 @@ videoSettings.setUserListButtonParams(CometChatCallsConstants.POSITION_BOTTOM_RI + + +--- + +## Best Practices + + + + Use `ASPECT_RATIO_CONTAIN` to show the entire video without cropping (may have letterboxing). Use `ASPECT_RATIO_COVER` to fill the container completely (may crop edges). Choose based on your UI design requirements. + + + Place UI controls (full screen, zoom, user list buttons) in positions that don't overlap with important content. Test on different screen sizes to ensure controls remain accessible. + + + Choose name label background colors that contrast well with your video content. The default `#333333` works well for most scenarios, but adjust based on your app's theme. + + + Test video view customizations on devices with different screen sizes and aspect ratios (phones, tablets, foldables) to ensure consistent appearance. + + + +## Troubleshooting + + + + **Symptom:** Video doesn't display correctly, appearing stretched, squashed, or cropped unexpectedly. + + **Cause:** Incorrect aspect ratio setting for your use case. + + **Solution:** Switch between `ASPECT_RATIO_CONTAIN` (shows full video with possible letterboxing) and `ASPECT_RATIO_COVER` (fills container, may crop edges) to find the best fit for your layout. + + + **Symptom:** Full screen, zoom, or user list buttons are not showing up. + + **Cause:** Visibility is set to `false` or controls are positioned outside the visible area. + + **Solution:** Verify the visibility parameter is set to `true` in the respective button params methods. Check that the video container has sufficient dimensions to display controls. + + + **Symptom:** Participant names are difficult to read on the video. + + **Cause:** Background color doesn't provide enough contrast with the text. + + **Solution:** Adjust the background color in `setNameLabelParams()` to provide better contrast. Use darker colors (#000000 to #666666) for better text readability. + + + +## Next Steps + + + + Implement one-on-one and group calling with default UI + + + Initiate calls directly without user interaction + + + Create webinar-style calling experiences + + + Use Calls SDK without Chat SDK integration + + diff --git a/sdk/android/webhooks-overview.mdx b/sdk/android/webhooks-overview.mdx index 598d43500..59e15a8c5 100644 --- a/sdk/android/webhooks-overview.mdx +++ b/sdk/android/webhooks-overview.mdx @@ -1,4 +1,41 @@ --- title: "Webhooks" +sidebarTitle: "Webhooks" +description: "Configure server-side event notifications with CometChat webhooks" url: "/fundamentals/webhooks-overview" ---- \ No newline at end of file +--- + + + +Webhooks send real-time HTTP notifications to your server when events occur in CometChat: +- **Message Events** - New messages, edits, deletions +- **User Events** - User creation, login, presence changes +- **Group Events** - Group creation, member joins/leaves +- **Call Events** - Call initiation, acceptance, completion + +**Configuration:** [CometChat Dashboard](https://app.cometchat.com) → Your App → Webhooks +**Learn more:** [Webhooks Overview](/fundamentals/webhooks-overview) + + +Webhooks allow your server to receive real-time notifications when events occur in CometChat. This enables server-side processing, logging, analytics, and integration with other systems without polling the API. + +For detailed information about webhook events, payload formats, security, and configuration, visit the [Webhooks Overview](/fundamentals/webhooks-overview) in the Fundamentals section. + +--- + +## Next Steps + + + + Complete guide to CometChat webhooks + + + Handle client-side events with SDK listeners + + + Server-side API for webhook processing + + + Configure webhooks in your dashboard + + \ No newline at end of file diff --git a/sdk/ionic-legacy/additional-message-filtering.mdx b/sdk/ionic-legacy/additional-message-filtering.mdx index 343228c3b..feab05e15 100644 --- a/sdk/ionic-legacy/additional-message-filtering.mdx +++ b/sdk/ionic-legacy/additional-message-filtering.mdx @@ -23,6 +23,8 @@ Once you have an object of the `MessagesRequest` class, you can call either the 1. fetchNext() - Calling this method will return the messages after the specified parameters. 2. fetchPrevious() - Calling this method will give you messages before the specified parameters. +Both `fetchPrevious()` and `fetchNext()` return an array of [`BaseMessage`](/sdk/reference/messages#basemessage) objects (which may be [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), [`CustomMessage`](/sdk/reference/messages#custommessage), or other subclasses depending on the filters applied). + Since messages are obtained in a paginated manner, a `maximum of 100` messages can be pulled in a single iteration. Calling the `fetchPrevious()`/`fetchNext()` method on the same `MessagesRequest` object will get you the next set of messages. Now that you are clear how to use the `MessagesRequest` class, below are the various options available: diff --git a/sdk/javascript/advanced-overview.mdx b/sdk/javascript/advanced-overview.mdx index 7a8e791f6..d09312b42 100644 --- a/sdk/javascript/advanced-overview.mdx +++ b/sdk/javascript/advanced-overview.mdx @@ -1,8 +1,47 @@ --- title: "Advanced" sidebarTitle: "Overview" +description: "Advanced SDK features including connection management, real-time listeners, login listeners, and WebSocket configuration." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Check connection status +CometChat.getConnectionStatus(); // "connected" | "connecting" | "disconnected" + +// Listen for connection changes +CometChat.addConnectionListener("LISTENER_ID", new CometChat.ConnectionListener({ + onConnected: () => console.log("Connected"), + onDisconnected: () => console.log("Disconnected") +})); + +// Listen for login events +CometChat.addLoginListener("LISTENER_ID", new CometChat.LoginListener({ + loginSuccess: (user) => console.log("Logged in:", user), + logoutSuccess: () => console.log("Logged out") +})); +``` + This section helps you to know about the Connection Listeners. + +--- + +## Next Steps + + + + Monitor and respond to connection state changes + + + Manually manage WebSocket connections + + + Listen for login and logout events + + + Complete reference for all SDK listeners + + diff --git a/sdk/javascript/ai-agents.mdx b/sdk/javascript/ai-agents.mdx index f4b88d96a..05c692013 100644 --- a/sdk/javascript/ai-agents.mdx +++ b/sdk/javascript/ai-agents.mdx @@ -1,57 +1,96 @@ --- title: "AI Agents" +sidebarTitle: "AI Agents" +description: "Integrate AI Agents, AI Moderation, and AI User Copilot into your app using the CometChat JavaScript SDK." --- -# AI Agents Overview + -AI Agents enable intelligent, automated interactions within your application. They can process user messages, trigger tools, and respond with contextually relevant information. For a broader introduction, see the [AI Agents section](/ai-agents). +| Feature | Description | +| --- | --- | +| [AI Agents](#agent-run-lifecycle-and-message-flow) | Intelligent automated conversations with real-time streaming | +| [AI Moderation](/sdk/javascript/ai-moderation) | Automatic content moderation with `PENDING` → `APPROVED` / `DISAPPROVED` flow | +| [AI User Copilot](/fundamentals/ai-user-copilot/overview) | Smart Replies, Conversation Starter, Conversation Summary (Dashboard-enabled) | -> **Note:** -> Currently, an Agent only responds to **Text Messages**. +```javascript +// Listen for real-time AI Agent events (streaming) +CometChat.addAIAssistantListener("LISTENER_ID", new CometChat.AIAssistantListener({ + onAIAssistantEventReceived: (event) => console.log("Event:", event) +})); + +// Listen for persisted agentic messages +CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({ + onAIAssistantMessageReceived: (msg) => console.log("Assistant reply:", msg), + onAIToolResultReceived: (msg) => console.log("Tool result:", msg), + onAIToolArgumentsReceived: (msg) => console.log("Tool args:", msg) +})); + +// Cleanup +CometChat.removeAIAssistantListener("LISTENER_ID"); +CometChat.removeMessageListener("LISTENER_ID"); +``` + +**Prerequisites:** `CometChat.init()` + `CometChat.login()` completed, AI features enabled in [Dashboard](https://app.cometchat.com) +**Event flow:** Run Start → Tool Call(s) → Text Message Stream → Run Finished + + +AI Agents enable intelligent, automated interactions within your application. They process user messages, trigger tools, and respond with contextually relevant information. For a broader introduction, see the [AI Agents section](/ai-agents). + + +Agents only respond to text messages. + ## Agent Run Lifecycle and Message Flow -This section explains how a user’s text message to an Agent becomes a structured "run" which emits real-time events and then produces agentic messages for historical retrieval. -- A user sends a text message to an Agent. -- The platform starts a run and streams real-time events via the **`AIAssistantListener`**. -- After the run completes, persisted Agentic Messages arrive via the **`MessageListener`**. +When a user sends a text message to an Agent: +1. The platform starts a run and streams real-time events via `AIAssistantListener` +2. After the run completes, persisted Agentic Messages arrive via `MessageListener` ### Real-time Events -Events are received via the **`onAIAssistantEventReceived`** method of the **`AIAssistantListener`** class in this general order: - -1. Run Start -2. Zero or more tool call cycles (repeats for each tool invocation): - - Tool Call Start - - Tool Call Arguments - - Tool Call End - - Tool Call Result -3. One or more assistant reply streams: - - Text Message Start - - Text Message Content (multiple times; token/char streaming) - - Text Message End -4. Run Finished - -Notes: -- `Run Start` and `Run Finished` are always emitted. -- `Tool Call` events appear only when a backend or frontend tool is invoked. There can be multiple tool calls in a single run. -- `Text Message` events are always emitted and carry the assistant’s reply incrementally. +Events are received via the **`onAIAssistantEventReceived`** method of the **`AIAssistantListener`** class as [`AIAssistantBaseEvent`](/sdk/reference/messages#aiassistantbaseevent) objects, in this general order: + +Events arrive via `onAIAssistantEventReceived` in this order: + +| Order | Event | Description | +|-------|-------|-------------| +| 1 | Run Start | A new run has begun | +| 2 | Tool Call Start | Agent decided to invoke a tool | +| 3 | Tool Call Arguments | Arguments being passed to the tool | +| 4 | Tool Call End | Tool execution completed | +| 5 | Tool Call Result | Tool's output is available | +| 6 | Text Message Start | Agent started composing a reply | +| 7 | Text Message Content | Streaming content chunks (multiple) | +| 8 | Text Message End | Agent reply is complete | +| 9 | Run Finished | Run finalized; persisted messages follow | + + +`Run Start` and `Run Finished` are always emitted. Tool Call events only appear when tools are invoked — there can be multiple tool call cycles in a single run. Text Message events are always emitted and carry the assistant's reply incrementally. + + +### Event Object Properties + +Every event is an `AIAssistantBaseEvent` with these common properties: + +| Getter | Return Type | Description | +|--------|-------------|-------------| +| `getType()` | `string` | Event type (e.g., `run_started`, `text_message_content`) | +| `getConversationId()` | `string` | The conversation this event belongs to | +| `getMessageId()` | `string` | The message ID associated with the event | +| `getParentMessageId()` | `string` | Parent message ID (for threaded messages) | +| `getRunId()` | `string` | The run ID for this agent execution | +| `getThreadId()` | `string` | The thread ID for this agent execution | +| `getTimestamp()` | `number` | Timestamp of the event | +| `getData()` | `object` | Full event data payload | + +Some events carry additional data: + +| Event | Extra Getter | Description | +|-------|-------------|-------------| +| Text Message Content | `getDelta()` | The streaming text chunk for progressive rendering | +| Tool Call Arguments | `getToolCallId()`, `getDelta()` | Tool call ID and argument chunk | +| Tool Call Result | `getToolCallId()`, `getContent()`, `getRole()` | Tool call ID, result content, and role | - - ```js - const listnerId = "unique_listener_id"; - - // Adding the AIAssistantListener - CometChat.addAIAssistantListener(listnerId, { - onAIAssistantEventReceived: (message) => { - console.log("AIAssistant event received successfully", message); - } - }); - - // Removing the AIAssistantListener - CometChat.removeAIAssistantListener(listnerId); - ``` - ```ts const listnerId: string = "unique_listener_id"; @@ -83,24 +122,71 @@ Notes: ### Agentic Messages These events are received via the **`MessageListener`** after the run completes. -- `AIAssistantMessage`: The full assistant reply. -- `AIToolResultMessage`: The final output of a tool call. -- `AIToolArgumentMessage`: The arguments that were passed to a tool. +- [`AIAssistantMessage`](/sdk/reference/messages#aiassistantmessage): The full assistant reply. +- [`AIToolResultMessage`](/sdk/reference/messages#aitoolresultmessage): The final output of a tool call. +- [`AIToolArgumentMessage`](/sdk/reference/messages#aitoolargumentmessage): The arguments that were passed to a tool. ```js const listnerId = "unique_listener_id"; + // Adding the AIAssistantListener + CometChat.addAIAssistantListener(listnerId, { + onAIAssistantEventReceived: (message) => { + console.log("AIAssistant event received successfully", message); + } + }); + + // Removing the AIAssistantListener + CometChat.removeAIAssistantListener(listnerId); + ``` + + + +### Agentic Messages + +After the run completes, these messages arrive via `MessageListener`: + +| Message Type | Description | +|--------------|-------------| +| `AIAssistantMessage` | The full assistant reply | +| `AIToolResultMessage` | The final output of a tool call | +| `AIToolArgumentMessage` | The arguments passed to a tool | + +Each message type extends [`BaseMessage`](/sdk/reference/messages#basemessage) and has a typed data accessor: + +| Message Type | Data Getter | Data Properties | +|--------------|-------------|-----------------| +| `AIAssistantMessage` | `getAssistantMessageData()` | `getRunId()`, `getThreadId()`, `getText()` | +| `AIToolResultMessage` | `getToolResultMessageData()` | `getRunId()`, `getThreadId()`, `getText()`, `getToolCallId()` | +| `AIToolArgumentMessage` | `getToolArgumentMessageData()` | `getRunId()`, `getThreadId()`, `getToolCalls()` | + +The `getToolCalls()` method on `AIToolArgumentMessage` returns an array of `AIToolCall` objects, each with: + +| Getter | Return Type | Description | +|--------|-------------|-------------| +| `getId()` | `string` | Unique tool call ID | +| `getType()` | `string` | Tool call type | +| `getFunction()` | `AIToolCallFunction` | Function object with `getName()` and `getArguments()` | +| `getDisplayName()` | `string` | Display name of the tool | +| `getExecutionText()` | `string` | Execution description text | + + + + + ```ts + const listnerId: string = "unique_listener_id"; + // Adding the MessageListener CometChat.addMessageListener(listnerId, { - onAIAssistantMessageReceived: (message) => { + onAIAssistantMessageReceived: (message: CometChat.AIAssistantMessage) => { console.log("AI Assistant message received successfully", message); }, - onAIToolResultReceived: (message) => { + onAIToolResultReceived: (message: CometChat.AIToolResultMessage) => { console.log("AI Tool result message received successfully", message); }, - onAIToolArgumentsReceived: (message) => { + onAIToolArgumentsReceived: (message: CometChat.AIToolArgumentMessage) => { console.log("AI Tool argument message received successfully", message); }, }); @@ -109,20 +195,19 @@ These events are received via the **`MessageListener`** after the run completes. CometChat.removeMessageListener(listnerId); ``` - - - ```ts - const listnerId: string = "unique_listener_id"; + + ```js + const listnerId = "unique_listener_id"; // Adding the MessageListener CometChat.addMessageListener(listnerId, { - onAIAssistantMessageReceived: (message: CometChat.AIAssistantMessage) => { + onAIAssistantMessageReceived: (message) => { console.log("AI Assistant message received successfully", message); }, - onAIToolResultReceived: (message: CometChat.AIToolResultMessage) => { + onAIToolResultReceived: (message) => { console.log("AI Tool result message received successfully", message); }, - onAIToolArgumentsReceived: (message: CometChat.AIToolArgumentMessage) => { + onAIToolArgumentsReceived: (message) => { console.log("AI Tool argument message received successfully", message); }, }); @@ -131,4 +216,28 @@ These events are received via the **`MessageListener`** after the run completes. CometChat.removeMessageListener(listnerId); ``` - \ No newline at end of file + + + + +Always remove listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +--- + +## Next Steps + + + + Set up AI-powered chatbots for automated conversations + + + Automatically moderate messages with AI + + + AI-powered features like smart replies and conversation summaries + + + Send text messages that trigger AI Agent responses + + diff --git a/sdk/javascript/ai-integration-quick-reference.mdx b/sdk/javascript/ai-integration-quick-reference.mdx new file mode 100644 index 000000000..e2ce6b1dd --- /dev/null +++ b/sdk/javascript/ai-integration-quick-reference.mdx @@ -0,0 +1,258 @@ +--- +title: "AI Integration Quick Reference" +sidebarTitle: "AI Quick Reference" +description: "Quick reference for AI features in the CometChat JavaScript SDK: AI Agents, AI Moderation, and AI User Copilot." +--- + + + +| Field | Value | +| --- | --- | +| Package | `@cometchat/chat-sdk-javascript` | +| AI Features | AI Agents, AI Moderation, AI User Copilot | +| Activation | Enable from [CometChat Dashboard](https://app.cometchat.com) | +| Prerequisites | `CometChat.init()` + `CometChat.login()` completed | +| AI Agents | Real-time streaming via `AIAssistantListener`, persisted messages via `MessageListener` | +| AI Moderation | Automatic content moderation with `PENDING` → `APPROVED` / `DISAPPROVED` flow | +| AI User Copilot | Smart Replies, Conversation Starter, Conversation Summary (Dashboard-enabled) | + + + +## Overview + +CometChat provides three AI-powered features to enhance your chat application: + +| Feature | Description | Use Case | +| --- | --- | --- | +| [AI Agents](/sdk/javascript/ai-agents) | Intelligent automated conversations with real-time streaming | Chatbots, virtual assistants, automated support | +| [AI Moderation](/sdk/javascript/ai-moderation) | Automatic content moderation for text, image, and video messages | Safety, compliance, content filtering | +| [AI User Copilot](/fundamentals/ai-user-copilot/overview) | Smart replies, conversation starters, and summaries | Enhanced user experience, productivity | + +## AI Features Summary + +### AI Agents + +AI Agents enable intelligent, automated interactions. They process user messages, trigger tools, and respond with contextually relevant information. + +**Key Points:** +- Agents only respond to text messages +- Real-time events via `AIAssistantListener` +- Persisted messages via `MessageListener` +- Event flow: Run Start → Tool Call(s) → Text Message Stream → Run Finished + + + + ```typescript + const listnerId: string = "unique_listener_id"; + + // Adding the AIAssistantListener for real-time events + CometChat.addAIAssistantListener(listnerId, { + onAIAssistantEventReceived: (message: CometChat.AIAssistantBaseEvent) => { + console.log("AIAssistant event received successfully", message); + } + }); + + // Adding the MessageListener for persisted agentic messages + CometChat.addMessageListener(listnerId, { + onAIAssistantMessageReceived: (message: CometChat.AIAssistantMessage) => { + console.log("AI Assistant message received successfully", message); + }, + onAIToolResultReceived: (message: CometChat.AIToolResultMessage) => { + console.log("AI Tool result message received successfully", message); + }, + onAIToolArgumentsReceived: (message: CometChat.AIToolArgumentMessage) => { + console.log("AI Tool argument message received successfully", message); + }, + }); + + // Cleanup - always remove listeners when done + CometChat.removeAIAssistantListener(listnerId); + CometChat.removeMessageListener(listnerId); + ``` + + + ```javascript + const listnerId = "unique_listener_id"; + + // Adding the AIAssistantListener for real-time events + CometChat.addAIAssistantListener(listnerId, { + onAIAssistantEventReceived: (message) => { + console.log("AIAssistant event received successfully", message); + } + }); + + // Adding the MessageListener for persisted agentic messages + CometChat.addMessageListener(listnerId, { + onAIAssistantMessageReceived: (message) => { + console.log("AI Assistant message received successfully", message); + }, + onAIToolResultReceived: (message) => { + console.log("AI Tool result message received successfully", message); + }, + onAIToolArgumentsReceived: (message) => { + console.log("AI Tool argument message received successfully", message); + }, + }); + + // Cleanup - always remove listeners when done + CometChat.removeAIAssistantListener(listnerId); + CometChat.removeMessageListener(listnerId); + ``` + + + +### AI Moderation + +AI Moderation automatically reviews messages for inappropriate content in real-time. + +**Key Points:** +- Supports Text, Image, and Video messages only +- Moderation statuses: `PENDING` → `APPROVED` or `DISAPPROVED` +- Enable and configure rules in the CometChat Dashboard + + + + ```typescript + // Send message and check initial moderation status + const textMessage = new CometChat.TextMessage( + receiverUID, + "Hello, how are you?", + CometChat.RECEIVER_TYPE.USER + ); + + CometChat.sendMessage(textMessage).then( + (message: CometChat.TextMessage) => { + const status: string = message.getModerationStatus(); + + if (status === CometChat.ModerationStatus.PENDING) { + console.log("Message is under moderation review"); + } + }, + (error: CometChat.CometChatException) => { + console.log("Message sending failed:", error); + } + ); + + // Listen for moderation results + const listenerID: string = "MODERATION_LISTENER"; + + CometChat.addMessageListener( + listenerID, + new CometChat.MessageListener({ + onMessageModerated: (message: CometChat.BaseMessage) => { + const status: string = message.getModerationStatus(); + const messageId: number = message.getId(); + + switch (status) { + case CometChat.ModerationStatus.APPROVED: + console.log(`Message ${messageId} approved`); + break; + case CometChat.ModerationStatus.DISAPPROVED: + console.log(`Message ${messageId} blocked`); + break; + } + } + }) + ); + ``` + + + ```javascript + // Send message and check initial moderation status + const textMessage = new CometChat.TextMessage( + receiverUID, + "Hello, how are you?", + CometChat.RECEIVER_TYPE.USER + ); + + CometChat.sendMessage(textMessage).then( + (message) => { + const status = message.getModerationStatus(); + + if (status === CometChat.ModerationStatus.PENDING) { + console.log("Message is under moderation review"); + } + }, + (error) => { + console.log("Message sending failed:", error); + } + ); + + // Listen for moderation results + const listenerID = "MODERATION_LISTENER"; + + CometChat.addMessageListener( + listenerID, + new CometChat.MessageListener({ + onMessageModerated: (message) => { + const status = message.getModerationStatus(); + const messageId = message.getId(); + + switch (status) { + case CometChat.ModerationStatus.APPROVED: + console.log(`Message ${messageId} approved`); + break; + case CometChat.ModerationStatus.DISAPPROVED: + console.log(`Message ${messageId} blocked`); + break; + } + } + }) + ); + ``` + + + +### AI User Copilot + +AI User Copilot provides smart features that enhance user productivity. These features are enabled in the Dashboard and auto-integrate with UI Kits. + +| Feature | Description | Documentation | +| --- | --- | --- | +| Conversation Starter | AI-generated opening lines for new chats | [Learn more](/fundamentals/ai-user-copilot/conversation-starter) | +| Smart Replies | AI-generated response suggestions | [Learn more](/fundamentals/ai-user-copilot/smart-replies) | +| Conversation Summary | AI-generated recap of long conversations | [Learn more](/fundamentals/ai-user-copilot/conversation-summary) | + +## Configuration Options + +### AI Agents Configuration + +| Parameter | Type | Description | +| --- | --- | --- | +| `listenerId` | `string` | Unique identifier for the listener | +| `onAIAssistantEventReceived` | `function` | Callback for real-time streaming events | +| `onAIAssistantMessageReceived` | `function` | Callback for persisted assistant messages | +| `onAIToolResultReceived` | `function` | Callback for tool execution results | +| `onAIToolArgumentsReceived` | `function` | Callback for tool arguments | + +### AI Moderation Configuration + +| Parameter | Type | Description | +| --- | --- | --- | +| `listenerID` | `string` | Unique identifier for the listener | +| `onMessageModerated` | `function` | Callback when moderation result is available | + +### Moderation Status Values + +| Status | Enum | Description | +| --- | --- | --- | +| Pending | `CometChat.ModerationStatus.PENDING` | Message is being processed | +| Approved | `CometChat.ModerationStatus.APPROVED` | Message passed moderation | +| Disapproved | `CometChat.ModerationStatus.DISAPPROVED` | Message was blocked | + +## Next Steps + + + + Full documentation for AI Agents integration + + + Complete AI Moderation implementation guide + + + Smart Replies, Conversation Starter, and Summary + + + Common issues and fixes + + diff --git a/sdk/javascript/ai-moderation.mdx b/sdk/javascript/ai-moderation.mdx index 768de5403..385a8f573 100644 --- a/sdk/javascript/ai-moderation.mdx +++ b/sdk/javascript/ai-moderation.mdx @@ -1,22 +1,46 @@ --- title: "AI Moderation" +sidebarTitle: "AI Moderation" +description: "Automatically moderate chat messages using AI to detect and block inappropriate content in real-time." --- -## Overview + -AI Moderation in the CometChat SDK helps ensure that your chat application remains safe and compliant by automatically reviewing messages for inappropriate content. This feature leverages AI to moderate messages in real-time, reducing manual intervention and improving user experience. +```javascript +let textMessage = new CometChat.TextMessage("UID", "Hello", CometChat.RECEIVER_TYPE.USER); + +// Send message — check moderation status +CometChat.sendMessage(textMessage).then(message => { + const status = message.getModerationStatus(); + // CometChat.ModerationStatus.PENDING | APPROVED | DISAPPROVED +}); + +// Listen for moderation results +CometChat.addMessageListener("MOD_LISTENER", new CometChat.MessageListener({ + onMessageModerated: (message) => { + const status = message.getModerationStatus(); + // Handle APPROVED or DISAPPROVED + } +})); +``` + +**Supported types:** Text, Image, Video messages only +**Statuses:** `PENDING` → `APPROVED` or `DISAPPROVED` + + +AI Moderation automatically reviews messages for inappropriate content in real-time. When a user sends a text, image, or video message, it's held in a `PENDING` state while the moderation service analyzes it, then marked as `APPROVED` or `DISAPPROVED` via the `onMessageModerated` event. + +`getModerationStatus()` is available on [`TextMessage`](/sdk/reference/messages#textmessage) and [`MediaMessage`](/sdk/reference/messages#mediamessage) objects. Custom messages are not subject to moderation. -For a broader understanding of moderation features, configuring rules, and managing flagged messages, see the [Moderation Overview](/moderation/overview). +For configuring moderation rules and managing flagged messages from the dashboard, see the [Moderation Overview](/moderation/overview). ## Prerequisites -Before using AI Moderation, ensure the following: - -1. Moderation is enabled for your app in the [CometChat Dashboard](https://app.cometchat.com) -2. Moderation rules are configured under **Moderation > Rules** -3. You're using CometChat SDK version that supports moderation +1. Moderation enabled in the [CometChat Dashboard](https://app.cometchat.com) +2. Moderation rules configured under **Moderation > Rules** +3. CometChat SDK version that supports moderation ## How It Works @@ -65,6 +89,7 @@ The `getModerationStatus()` method returns one of the following values: | Pending | `CometChat.ModerationStatus.PENDING` | Message is being processed by moderation | | Approved | `CometChat.ModerationStatus.APPROVED` | Message passed moderation and is visible | | Disapproved | `CometChat.ModerationStatus.DISAPPROVED` | Message violated rules and was blocked | +| Unmoderated | `CometChat.ModerationStatus.UNMODERATED` | Message was not subject to moderation (default) | ## Implementation @@ -73,8 +98,8 @@ The `getModerationStatus()` method returns one of the following values: When you send a text, image, or video message, check the initial moderation status: - - ```javascript + + ```typescript const textMessage = new CometChat.TextMessage( receiverUID, "Hello, how are you?", @@ -82,23 +107,23 @@ When you send a text, image, or video message, check the initial moderation stat ); CometChat.sendMessage(textMessage).then( - (message) => { + (message: CometChat.TextMessage) => { // Check moderation status - const status = message.getModerationStatus(); + const status: string = message.getModerationStatus(); if (status === CometChat.ModerationStatus.PENDING) { console.log("Message is under moderation review"); // Show pending indicator in UI } }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Message sending failed:", error); } ); ``` - - ```typescript + + ```javascript const textMessage = new CometChat.TextMessage( receiverUID, "Hello, how are you?", @@ -106,16 +131,16 @@ When you send a text, image, or video message, check the initial moderation stat ); CometChat.sendMessage(textMessage).then( - (message: CometChat.TextMessage) => { + (message) => { // Check moderation status - const status: string = message.getModerationStatus(); + const status = message.getModerationStatus(); if (status === CometChat.ModerationStatus.PENDING) { console.log("Message is under moderation review"); // Show pending indicator in UI } }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Message sending failed:", error); } ); @@ -125,23 +150,23 @@ When you send a text, image, or video message, check the initial moderation stat ### Step 2: Listen for Moderation Results -Register a message listener to receive moderation results in real-time: +Register a message listener to receive moderation results in real-time. The `onMessageModerated` callback receives a [`BaseMessage`](/sdk/reference/messages#basemessage) object: - - ```javascript - const listenerID = "MODERATION_LISTENER"; + + ```typescript + const listenerID: string = "MODERATION_LISTENER"; CometChat.addMessageListener( listenerID, new CometChat.MessageListener({ - onMessageModerated: (message) => { + onMessageModerated: (message: CometChat.BaseMessage) => { if ( message instanceof CometChat.TextMessage || message instanceof CometChat.MediaMessage ) { - const status = message.getModerationStatus(); - const messageId = message.getId(); + const status: string = message.getModerationStatus(); + const messageId: number = message.getId(); switch (status) { case CometChat.ModerationStatus.APPROVED: @@ -164,20 +189,20 @@ Register a message listener to receive moderation results in real-time: // CometChat.removeMessageListener(listenerID); ``` - - ```typescript - const listenerID: string = "MODERATION_LISTENER"; + + ```javascript + const listenerID = "MODERATION_LISTENER"; CometChat.addMessageListener( listenerID, new CometChat.MessageListener({ - onMessageModerated: (message: CometChat.BaseMessage) => { + onMessageModerated: (message) => { if ( message instanceof CometChat.TextMessage || message instanceof CometChat.MediaMessage ) { - const status: string = message.getModerationStatus(); - const messageId: number = message.getId(); + const status = message.getModerationStatus(); + const messageId = message.getId(); switch (status) { case CometChat.ModerationStatus.APPROVED: @@ -300,5 +325,23 @@ Here's a complete implementation showing the full moderation flow: + +Always remove listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + + ## Next Steps -After implementing AI Moderation, consider adding a reporting feature to allow users to flag messages they find inappropriate. For more details, see the [Flag Message](/sdk/javascript/flag-message) documentation. + + + + Allow users to report inappropriate messages manually + + + Build intelligent automated conversations with AI Agents + + + Smart replies, conversation summaries, and more + + + Send text, media, and custom messages + + diff --git a/sdk/javascript/all-real-time-listeners.mdx b/sdk/javascript/all-real-time-listeners.mdx index a70adfb54..1a23c5517 100644 --- a/sdk/javascript/all-real-time-listeners.mdx +++ b/sdk/javascript/all-real-time-listeners.mdx @@ -1,8 +1,56 @@ --- title: "All Real Time Listeners" +description: "Complete reference for all CometChat real-time listeners including User, Group, Message, Call, AI Assistant, and Ongoing Call (Calls SDK) listeners." --- + + +```javascript +// User Listener — online/offline presence +CometChat.addUserListener("ID", new CometChat.UserListener({ + onUserOnline: (user) => { }, + onUserOffline: (user) => { } +})); + +// Message Listener — messages, typing, receipts, reactions +CometChat.addMessageListener("ID", new CometChat.MessageListener({ + onTextMessageReceived: (msg) => { }, + onMediaMessageReceived: (msg) => { }, + onTypingStarted: (indicator) => { }, + onMessagesRead: (receipt) => { } +})); + +// Group Listener — member join/leave/kick/ban/scope changes +CometChat.addGroupListener("ID", new CometChat.GroupListener({ + onGroupMemberJoined: (action, joinedUser, joinedGroup) => { }, + onGroupMemberLeft: (action, leftUser, leftGroup) => { } +})); + +// Call Listener — incoming/outgoing call events +CometChat.addCallListener("ID", new CometChat.CallListener({ + onIncomingCallReceived: (call) => { }, + onOutgoingCallAccepted: (call) => { } +})); + +// Always clean up +CometChat.removeUserListener("ID"); +CometChat.removeMessageListener("ID"); +CometChat.removeGroupListener("ID"); +CometChat.removeCallListener("ID"); +``` + + +Real-time listeners let you receive live events — messages, presence changes, group updates, and call signals — as they happen. The pattern is the same for all four listener types: + +1. Register a listener with a unique ID using `addXListener()` +2. Handle events in the callback methods +3. Remove the listener with `removeXListener()` when it's no longer needed +Each listener ID must be unique. Re-registering with the same ID replaces the previous listener. Always remove listeners on component unmount or page navigation to prevent memory leaks. + + +Always remove listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + CometChat provides 4 listeners viz. @@ -10,29 +58,33 @@ CometChat provides 4 listeners viz. 2. [Group Listener](/sdk/javascript/all-real-time-listeners#group-listener) 3. [Message Listener](/sdk/javascript/all-real-time-listeners#message-listener) 4. [Call Listener](/sdk/javascript/all-real-time-listeners#call-listener) +5. [AI Assistant Listener](/sdk/javascript/all-real-time-listeners#ai-assistant-listener) +6. [Ongoing Call Listener (Calls SDK)](/sdk/javascript/all-real-time-listeners#ongoing-call-listener-calls-sdk) ## User Listener -The `UserListener` class provides you with live events related to users. Below are the callback methods provided by the `UserListener` class. +Receive online/offline presence events for users. | Method | Information | | --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | **onUserOnline(user: CometChat.User)** | This method is triggered when a user comes online and is available to chat. The details of the user can be obtained from the user object received as the method parameter. | | **onUserOffline(user: CometChat.User)** | This method is triggered when a user goes offline. The details of the user can be obtained from the User object received as the parameter. | -To add the `UserListener`, you need to use the `addUserListener()` method provided by the `CometChat` class. +To add the `UserListener`: - -```js -var listenerID = "UNIQUE_LISTENER_ID"; + +```typescript +const listenerID: string = "UNIQUE_LISTENER_ID"; CometChat.addUserListener( listenerID, new CometChat.UserListener({ - onUserOnline: (onlineUser) => { + onUserOnline: (onlineUser: CometChat.User) => { + /* when someuser/friend comes online, user will be received here */ console.log("On User Online:", { onlineUser }); }, - onUserOffline: (offlineUser) => { + onUserOffline: (offlineUser: CometChat.User) => { + /* when someuser/friend went offline, user will be received here */ console.log("On User Offline:", { offlineUser }); }, }) @@ -41,18 +93,16 @@ CometChat.addUserListener( - -```typescript -var listenerID: string = "UNIQUE_LISTENER_ID"; + +```javascript +const listenerID = "UNIQUE_LISTENER_ID"; CometChat.addUserListener( listenerID, new CometChat.UserListener({ - onUserOnline: (onlineUser: CometChat.User) => { - /* when someuser/friend comes online, user will be received here */ + onUserOnline: (onlineUser) => { console.log("On User Online:", { onlineUser }); }, - onUserOffline: (offlineUser: CometChat.User) => { - /* when someuser/friend went offline, user will be received here */ + onUserOffline: (offlineUser) => { console.log("On User Offline:", { offlineUser }); }, }) @@ -63,21 +113,20 @@ CometChat.addUserListener( -where `UNIQUE_LISTENER_ID` is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events. - -Once the `UserListener` is not in use, you need to remove the listener using the `removeUserListener()` method which takes the id of the listener to be removed as the parameter. +Remove the listener when no longer needed: - -```js -CometChat.removeUserListener(UNIQUE_LISTENER_ID); + +```typescript +let listenerID: string = "UNIQUE_LISTENER_ID"; +CometChat.removeUserListener(listenerID); ``` - -```typescript -let listenerID: string = "UNIQUE_LISTENER_ID"; + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; CometChat.removeUserListener(listenerID); ``` @@ -87,7 +136,7 @@ CometChat.removeUserListener(listenerID); ## Group Listener -The `GroupListener` class provides you with all the real-time events related to groups. Below are the callback methods provided by the `GroupListener` class. +Receive events when group members join, leave, are kicked/banned, or have their scope changed. | Method | Information | | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | @@ -99,21 +148,34 @@ The `GroupListener` class provides you with all the real-time events related to | **onGroupMemberScopeChanged(action: CometChat.Action, changedUser: CometChat.User, newScope: string, oldScope: string, changedGroup: CometChat.Group)** | This method is triggered when the scope of any Group Member has been changed. All the members that are a part of that group receive this event | | **onMemberAddedToGroup(action: CometChat.Action, userAdded: CometChat.User, addedBy: CometChat.User, addedTo: CometChat.Group)** | This method is triggered when a user is added to any group. All the members including the user himself receive this event. | -To add the `GroupListener`, you need to use the `addGroupListener()` method provided by the `CometChat` class. +To add the `GroupListener`: - -```js + +```typescript CometChat.addGroupListener( "UNIQUE_LISTENER_ID", new CometChat.GroupListener({ - onGroupMemberJoined: (message, joinedUser, joinedGroup) => { + onGroupMemberJoined: ( + message: CometChat.Action, + joinedUser: CometChat.User, + joinedGroup: CometChat.Group + ) => { console.log("onGroupMemberJoined", { message, joinedUser, joinedGroup }); }, - onGroupMemberLeft: (message, leftUser, leftGroup) => { + onGroupMemberLeft: ( + message: CometChat.Action, + leftUser: CometChat.User, + leftGroup: CometChat.Group + ) => { console.log("onGroupMemberLeft", { message, leftUser, leftGroup }); }, - onGroupMemberKicked: (message, kickedUser, kickedBy, kickedFrom) => { + onGroupMemberKicked: ( + message: CometChat.Action, + kickedUser: CometChat.User, + kickedBy: CometChat.User, + kickedFrom: CometChat.Group + ) => { console.log("onGroupMemberKicked", { message, kickedUser, @@ -121,7 +183,12 @@ CometChat.addGroupListener( kickedFrom, }); }, - onGroupMemberBanned: (message, bannedUser, bannedBy, bannedFrom) => { + onGroupMemberBanned: ( + message: CometChat.Action, + bannedUser: CometChat.User, + bannedBy: CometChat.User, + bannedFrom: CometChat.Group + ) => { console.log("onGroupMemberBanned", { message, bannedUser, @@ -130,10 +197,10 @@ CometChat.addGroupListener( }); }, onGroupMemberUnbanned: ( - message, - unbannedUser, - unbannedBy, - unbannedFrom + message: CometChat.Action, + unbannedUser: CometChat.User, + unbannedBy: CometChat.User, + unbannedFrom: CometChat.Group ) => { console.log("onGroupMemberUnbanned", { message, @@ -143,11 +210,11 @@ CometChat.addGroupListener( }); }, onGroupMemberScopeChanged: ( - message, - changedUser, - newScope, - oldScope, - changedGroup + message: CometChat.Action, + changedUser: CometChat.User, + newScope: string, + oldScope: string, + changedGroup: CometChat.Group ) => { console.log("onGroupMemberScopeChanged", { message, @@ -157,7 +224,12 @@ CometChat.addGroupListener( changedGroup, }); }, - onMemberAddedToGroup: (message, userAdded, addedby, addedTo) => { + onMemberAddedToGroup: ( + message: CometChat.Action, + userAdded: CometChat.User, + addedby: CometChat.User, + addedTo: CometChat.Group + ) => { console.log("onMemberAddedToGroup", { message, userAdded, @@ -171,31 +243,18 @@ CometChat.addGroupListener( - -```typescript + +```javascript CometChat.addGroupListener( "UNIQUE_LISTENER_ID", new CometChat.GroupListener({ - onGroupMemberJoined: ( - message: CometChat.Action, - joinedUser: CometChat.User, - joinedGroup: CometChat.Group - ) => { + onGroupMemberJoined: (message, joinedUser, joinedGroup) => { console.log("onGroupMemberJoined", { message, joinedUser, joinedGroup }); }, - onGroupMemberLeft: ( - message: CometChat.Action, - leftUser: CometChat.User, - leftGroup: CometChat.Group - ) => { + onGroupMemberLeft: (message, leftUser, leftGroup) => { console.log("onGroupMemberLeft", { message, leftUser, leftGroup }); }, - onGroupMemberKicked: ( - message: CometChat.Action, - kickedUser: CometChat.User, - kickedBy: CometChat.User, - kickedFrom: CometChat.Group - ) => { + onGroupMemberKicked: (message, kickedUser, kickedBy, kickedFrom) => { console.log("onGroupMemberKicked", { message, kickedUser, @@ -203,12 +262,7 @@ CometChat.addGroupListener( kickedFrom, }); }, - onGroupMemberBanned: ( - message: CometChat.Action, - bannedUser: CometChat.User, - bannedBy: CometChat.User, - bannedFrom: CometChat.Group - ) => { + onGroupMemberBanned: (message, bannedUser, bannedBy, bannedFrom) => { console.log("onGroupMemberBanned", { message, bannedUser, @@ -217,10 +271,10 @@ CometChat.addGroupListener( }); }, onGroupMemberUnbanned: ( - message: CometChat.Action, - unbannedUser: CometChat.User, - unbannedBy: CometChat.User, - unbannedFrom: CometChat.Group + message, + unbannedUser, + unbannedBy, + unbannedFrom ) => { console.log("onGroupMemberUnbanned", { message, @@ -230,11 +284,11 @@ CometChat.addGroupListener( }); }, onGroupMemberScopeChanged: ( - message: CometChat.Action, - changedUser: CometChat.User, - newScope: string, - oldScope: string, - changedGroup: CometChat.Group + message, + changedUser, + newScope, + oldScope, + changedGroup ) => { console.log("onGroupMemberScopeChanged", { message, @@ -244,12 +298,7 @@ CometChat.addGroupListener( changedGroup, }); }, - onMemberAddedToGroup: ( - message: CometChat.Action, - userAdded: CometChat.User, - addedby: CometChat.User, - addedTo: CometChat.Group - ) => { + onMemberAddedToGroup: (message, userAdded, addedby, addedTo) => { console.log("onMemberAddedToGroup", { message, userAdded, @@ -265,21 +314,20 @@ CometChat.addGroupListener( -where `UNIQUE_LISTENER_ID` is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events. - -Once the `GroupListener` is not in use, you need to remove the listener using the `removeGroupListener()` method which takes the id of the listener to be removed as the parameter. +Remove the listener when no longer needed: - -```js -CometChat.removeGroupListener(UNIQUE_LISTENER_ID); + +```typescript +let listenerID: string = "UNIQUE_LISTENER_ID"; +CometChat.removeGroupListener(listenerID); ``` - -```typescript -let listenerID: string = "UNIQUE_LISTENER_ID"; + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; CometChat.removeGroupListener(listenerID); ``` @@ -289,7 +337,7 @@ CometChat.removeGroupListener(listenerID); ## Message Listener -The `MessageListener` class provides you with live events related to messages. Below are the callback methods provided by the `MessageListener` class. +Receive events for incoming messages, typing indicators, read/delivery receipts, message edits/deletes, reactions, and moderation results. | Method | Information | | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | @@ -308,11 +356,16 @@ The `MessageListener` class provides you with live events related to messages. B | **onMessageReactionAdded(receipt: CometChat.ReactionEvent)** | This event is triggered when a reaction is added to a message in a user/group conversation. | | **onMessageReactionRemoved(receipt: CometChat.ReactionEvent)** | This event is triggered when a reaction is remove from a message in a user/group conversation. | | **onMessageModerated(message: CometChat.BaseMessage)** | This event is triggered when a message sent by the logged-in user is successfully processed by moderation and receives either an `approved` or `disapproved` status. | +| **onMessagesDeliveredToAll(receipt: CometChat.MessageReceipt)** | This event is triggered when a group message is delivered to all members. Group conversations only. | +| **onMessagesReadByAll(receipt: CometChat.MessageReceipt)** | This event is triggered when a group message is read by all members. Group conversations only. | +| **onAIAssistantMessageReceived(message: CometChat.AIAssistantMessage)** | This event is triggered when a persisted AI assistant reply is received after an agent run completes. | +| **onAIToolResultReceived(message: CometChat.AIToolResultMessage)** | This event is triggered when a persisted AI tool result message is received after an agent run completes. | +| **onAIToolArgumentsReceived(message: CometChat.AIToolArgumentMessage)** | This event is triggered when a persisted AI tool argument message is received after an agent run completes. | To add the `MessageListener`, you need to use the `addMessageListener()` method provided by the `CometChat` class. -```js +```javascript CometChat.addMessageListener( "UNIQUE_LISTENER_ID", new CometChat.MessageListener({ @@ -337,7 +390,7 @@ CometChat.addMessageListener( onTypingEnded: (typingIndicator) => { console.log("Typing Ended", typingIndicator); }, - onMessagesDeleted: (message) => { + onMessageDeleted: (message) => { console.log("Message Deleted", message); }, onMessageEdited: (message) => { @@ -361,6 +414,21 @@ CometChat.addMessageListener( onMessageModerated: (message) => { console.log("Message Moderated", message); }, + onMessagesDeliveredToAll: (messageReceipt) => { + console.log("Message Delivered to All", messageReceipt); + }, + onMessagesReadByAll: (messageReceipt) => { + console.log("Message Read by All", messageReceipt); + }, + onAIAssistantMessageReceived: (message) => { + console.log("AI Assistant message received", message); + }, + onAIToolResultReceived: (message) => { + console.log("AI Tool result received", message); + }, + onAIToolArgumentsReceived: (message) => { + console.log("AI Tool arguments received", message); + }, }) ); ``` @@ -417,26 +485,103 @@ CometChat.addMessageListener( onMessageModerated: (message: CometChat.BaseMessage) => { console.log("Message Moderated", message); }, + onMessagesDeliveredToAll: (messageReceipt: CometChat.MessageReceipt) => { + console.log("Message Delivered to All", messageReceipt); + }, + onMessagesReadByAll: (messageReceipt: CometChat.MessageReceipt) => { + console.log("Message Read by All", messageReceipt); + }, + onAIAssistantMessageReceived: (message: CometChat.AIAssistantMessage) => { + console.log("AI Assistant message received", message); + }, + onAIToolResultReceived: (message: CometChat.AIToolResultMessage) => { + console.log("AI Tool result received", message); + }, + onAIToolArgumentsReceived: (message: CometChat.AIToolArgumentMessage) => { + console.log("AI Tool arguments received", message); + }, }) ); ``` - - -where `UNIQUE_LISTENER_ID` is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events. - -Once the `MessageListener` is not in use, you need to remove the listener using the `removeMessageListener()` method which takes the id of the listener to be removed as the parameter. - - -```js -CometChat.removeMessageListener(UNIQUE_LISTENER_ID); +```javascript +CometChat.addMessageListener( + "UNIQUE_LISTENER_ID", + 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); + }, + onMessagesDelivered: (messageReceipt) => { + console.log("Message Delivered", messageReceipt); + }, + onMessagesRead: (messageReceipt) => { + console.log("Message Read", messageReceipt); + }, + onTypingStarted: (typingIndicator) => { + console.log("Typing Started", typingIndicator); + }, + onTypingEnded: (typingIndicator) => { + console.log("Typing Ended", typingIndicator); + }, + onMessageDeleted: (message) => { + console.log("Message Deleted", message); + }, + onMessageEdited: (message) => { + console.log("Message Edited", message); + }, + onInteractiveMessageReceived: (message) => { + console.log("interactive Message received", message); + }, + onInteractionGoalCompleted: (message) => { + console.log("Message interaction goal completed", message); + }, + onTransientMessageReceived: (message) => { + console.log("Transient Message received", message); + }, + onMessageReactionAdded: (reaction) => { + console.log("Message Reaction added", reaction); + }, + onMessageReactionRemoved: (reaction) => { + console.log("Message Reaction removed", reaction); + }, + onMessageModerated: (message) => { + console.log("Message Moderated", message); + }, + onMessagesDeliveredToAll: (messageReceipt) => { + console.log("Message Delivered to All", messageReceipt); + }, + onMessagesReadByAll: (messageReceipt) => { + console.log("Message Read by All", messageReceipt); + }, + onAIAssistantMessageReceived: (message) => { + console.log("AI Assistant message received", message); + }, + onAIToolResultReceived: (message) => { + console.log("AI Tool result received", message); + }, + onAIToolArgumentsReceived: (message) => { + console.log("AI Tool arguments received", message); + }, + }) +); ``` + + +Remove the listener when no longer needed: + + ```typescript let listenerID: string = "UNIQUE_LISTENER_ID"; @@ -445,11 +590,19 @@ CometChat.removeMessageListener(listenerID); + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; +CometChat.removeMessageListener(listenerID); +``` + + + ## Call Listener -The `CallListener` class provides you with live events related to calls. Below are the callback methods provided by the `CallListener` class. +Receive events for incoming and outgoing call state changes. | Method | Information | | ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -457,12 +610,13 @@ The `CallListener` class provides you with live events related to calls. Below a | **onOutgoingCallAccepted(call: CometChat.Call)** | This event is triggered when the call initiated by the logged-in user is accepted by the recipient. The details of the call can be obtained from the Call object received as the method parameter. | | **onOutgoingCallRejected(call: CometChat.Call)** | This event is triggered when the call initiated by the logged-in user is rejected by the recipient. The details of the call can be obtained from the Call object received as the method parameter | | **onIncomingCallCancelled(call: CometChat.Call)** | This event is triggered when an incoming call is canceled by the initiator of the call. The details of the call can be obtained from the Call object received as the method parameter | +| **onCallEndedMessageReceived(call: CometChat.Call)** | This event is triggered when a call ends. The call object contains the final status and duration. | -To add the `CallListener`, you need to use the `addCallListener()` method provided by the `CometChat` class. +To add the `CallListener`: -```js +```javascript CometChat.addCallListener( "UNIQUE_LISTENER_ID", new CometChat.CallListener({ @@ -478,6 +632,9 @@ CometChat.addCallListener( onIncomingCallCancelled(call) { console.log("Incoming call canceled:", call); }, + onCallEndedMessageReceived(call) { + console.log("Call ended message:", call); + }, }) ); ``` @@ -501,6 +658,35 @@ CometChat.addCallListener( onIncomingCallCancelled: (call: CometChat.Call) => { console.log("Incoming call canceled:", call); }, + onCallEndedMessageReceived: (call: CometChat.Call) => { + console.log("Call ended message:", call); + }, + }) +); +``` + + + + +```javascript +CometChat.addCallListener( + "UNIQUE_LISTENER_ID", + new CometChat.CallListener({ + onIncomingCallReceived(call) { + console.log("Incoming call:", call); + }, + onOutgoingCallAccepted(call) { + console.log("Outgoing call accepted:", call); + }, + onOutgoingCallRejected(call) { + console.log("Outgoing call rejected:", call); + }, + onIncomingCallCancelled(call) { + console.log("Incoming call canceled:", call); + }, + onCallEndedMessageReceived(call) { + console.log("Call ended message:", call); + }, }) ); ``` @@ -509,24 +695,357 @@ CometChat.addCallListener( -where `UNIQUE_LISTENER_ID` is the unique identifier for the listener. Please make sure that no two listeners are added with the same listener id as this could lead to unexpected behavior resulting in loss of events. +Remove the listener when no longer needed: + + + +```typescript +let listenerID: string = "UNIQUE_LISTENER_ID"; +CometChat.removeCallListener(listenerID); +``` + + + + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; +CometChat.removeCallListener(listenerID); +``` + + + + + + +--- + +## AI Assistant Listener -Once the `CallListener` is not in use, you need to remove the listener using the `removeCallListener()` method which takes the id of the listener to be removed as the parameter. +The `AIAssistantListener` provides real-time streaming events from AI Agent runs. These events are received as the agent processes a user's message, before the final persisted messages arrive via the `MessageListener`. + +| Method | Information | +| ------ | ----------- | +| **onAIAssistantEventReceived(event: CometChat.AIAssistantBaseEvent)** | This event is triggered for each streaming event during an AI Agent run (run start, tool calls, text message streaming, run finished). See [`AIAssistantBaseEvent`](/sdk/reference/messages#aiassistantbaseevent) for the event structure. | -```js -CometChat.removeCallListener(UNIQUE_LISTENER_ID); +```javascript +const listenerID = "UNIQUE_LISTENER_ID"; +CometChat.addAIAssistantListener( + listenerID, + new CometChat.AIAssistantListener({ + onAIAssistantEventReceived: (event) => { + console.log("AI Assistant event:", event.getType(), event); + }, + }) +); ``` + + +```typescript +const listenerID: string = "UNIQUE_LISTENER_ID"; +CometChat.addAIAssistantListener( + listenerID, + new CometChat.AIAssistantListener({ + onAIAssistantEventReceived: (event: CometChat.AIAssistantBaseEvent) => { + console.log("AI Assistant event:", event.getType(), event); + }, + }) +); +``` + + +To remove the listener: + + + +```javascript +CometChat.removeAIAssistantListener("UNIQUE_LISTENER_ID"); +``` + + +```typescript +CometChat.removeAIAssistantListener("UNIQUE_LISTENER_ID"); +``` + + +## Ongoing Call Listener (Calls SDK) + +The `OngoingCallListener` provides real-time callbacks for active call session events. It is part of the CometChat Calls SDK (not the Chat SDK) and is used with `CallSettingsBuilder.setCallListener()` or `CometChatCalls.addCallEventListener()`. + +All three calling flows — [Call Session](/sdk/javascript/direct-call), [Standalone Calling](/sdk/javascript/standalone-calling), and [Presenter Mode](/sdk/javascript/presenter-mode) — use the same `OngoingCallListener`. + +### Callbacks + +| Callback | Parameter | Description | +|----------|-----------|-------------| +| `onUserJoined(user)` | `user` object | A remote participant joined the call | +| `onUserLeft(user)` | `user` object | A remote participant left the call | +| `onUserListUpdated(userList)` | Array of `user` objects | The participant list changed (join or leave) | +| `onCallEnded()` | — | The call session terminated (1:1 calls only, fires for both participants) | +| `onCallEndButtonPressed()` | — | The local user clicked the end call button | +| `onSessionTimeout()` | — | The call was auto-terminated due to inactivity. *v4.1.0+* | +| `onScreenShareStarted()` | — | The local user started sharing their screen | +| `onScreenShareStopped()` | — | The local user stopped sharing their screen | +| `onUserMuted(event)` | `event` object | A participant's mute state changed | +| `onCallSwitchedToVideo(event)` | `event` object | An audio call was upgraded to video | +| `onMediaDeviceListUpdated(deviceList)` | `deviceList` object | Available audio/video devices changed | +| `onRecordingStarted(event)` | `event` object | A user started recording the call | +| `onRecordingStopped()` | — | Recording was stopped | +| `onError(error)` | [`CometChatCallsException`](/sdk/reference/calls#cometchatcallsexception) | An error occurred during the call session | + +### Callback Parameter Shapes + +**`user` object** (received by `onUserJoined`, `onUserLeft`): + +| Property | Type | Description | +|----------|------|-------------| +| `name` | `string` | Display name of the user | +| `avatar` | `string` | URL of the user's avatar | +| `uid` | `string` | UID of the user | + +**`onUserListUpdated(userList)`** — receives an array of `user` objects with the same shape as above. + +**`onUserMuted(event)`**: + +| Property | Type | Description | +|----------|------|-------------| +| `event.muted.name` | `string` | Name of the muted user | +| `event.muted.uid` | `string` | UID of the muted user | +| `event.muted.isAudioMuted` | `boolean` | Whether audio is muted | +| `event.muted.isVideoMuted` | `boolean` | Whether video is muted | +| `event.mutedBy.name` | `string` | Name of the user who muted | +| `event.mutedBy.uid` | `string` | UID of the user who muted | + +**`onCallSwitchedToVideo(event)`**: +| Property | Type | Description | +|----------|------|-------------| +| `event.sessionId` | `string` | Session ID of the call | +| `event.initiator.name` | `string` | Name of the user who initiated the switch | +| `event.initiator.uid` | `string` | UID of the initiator | +| `event.responder.name` | `string` | Name of the responder | +| `event.responder.uid` | `string` | UID of the responder | + +**`onMediaDeviceListUpdated(deviceList)`**: + +| Property | Type | Description | +|----------|------|-------------| +| `deviceList.videoInputDevices` | [`MediaDeviceInfo[]`](/sdk/reference/calls#mediadeviceinfo) | Available cameras | +| `deviceList.audioInputDevices` | [`MediaDeviceInfo[]`](/sdk/reference/calls#mediadeviceinfo) | Available microphones | +| `deviceList.audioOutputDevices` | [`MediaDeviceInfo[]`](/sdk/reference/calls#mediadeviceinfo) | Available speakers/headphones | + +**`onRecordingStarted(event)`**: + +| Property | Type | Description | +|----------|------|-------------| +| `event.user.name` | `string` | Name of the user who started recording | +| `event.user.uid` | `string` | UID of the user who started recording | + +**`onRecordingStopped()`**, **`onCallEnded()`**, **`onSessionTimeout()`**, **`onCallEndButtonPressed()`**, **`onScreenShareStarted()`**, **`onScreenShareStopped()`** — receive no arguments. + + + +```javascript +const listenerId = "UNIQUE_LISTENER_ID"; + +CometChatCalls.addCallEventListener(listenerId, { + onUserJoined: (user) => { + console.log("User joined:", user); + }, + onUserLeft: (user) => { + console.log("User left:", user); + }, + onCallEnded: () => { + console.log("Call ended"); + }, + onCallEndButtonPressed: () => { + console.log("End call button pressed"); + }, + onError: (error) => { + console.log("Call error:", error); + }, +}); + +// Remove listener when done +CometChatCalls.removeCallEventListener(listenerId); +``` + ```typescript -let listenerID: string = "UNIQUE_LISTENER_ID"; -CometChat.removeCallListener(listenerID); +const listenerId: string = "UNIQUE_LISTENER_ID"; + +CometChatCalls.addCallEventListener(listenerId, { + onUserJoined: (user: any) => { + console.log("User joined:", user); + }, + onUserLeft: (user: any) => { + console.log("User left:", user); + }, + onCallEnded: () => { + console.log("Call ended"); + }, + onCallEndButtonPressed: () => { + console.log("End call button pressed"); + }, + onError: (error: any) => { + console.log("Call error:", error); + }, +}); + +// Remove listener when done +CometChatCalls.removeCallEventListener(listenerId); ``` + + + +--- + +## Login Listener + +Receive events when the user's authentication state changes — login success/failure and logout success/failure. +| Method | Description | +| ------ | ----------- | +| `loginSuccess(user)` | User logged in successfully. Provides the [`User`](/sdk/reference/entities#user) object. | +| `loginFailure(error)` | Login failed. Provides a [`CometChatException`](/sdk/reference/auxiliary#cometchatexception). | +| `logoutSuccess()` | User logged out successfully. | +| `logoutFailure(error)` | Logout failed. Provides a [`CometChatException`](/sdk/reference/auxiliary#cometchatexception). | + + + +```typescript +const listenerID: string = "UNIQUE_LISTENER_ID"; +CometChat.addLoginListener( + listenerID, + new CometChat.LoginListener({ + loginSuccess: (user: CometChat.User) => { + console.log("LoginListener :: loginSuccess", user); + }, + loginFailure: (error: CometChat.CometChatException) => { + console.log("LoginListener :: loginFailure", error); + }, + logoutSuccess: () => { + console.log("LoginListener :: logoutSuccess"); + }, + logoutFailure: (error: CometChat.CometChatException) => { + console.log("LoginListener :: logoutFailure", error); + } + }) +); +``` + + +```javascript +const listenerID = "UNIQUE_LISTENER_ID"; +CometChat.addLoginListener( + listenerID, + new CometChat.LoginListener({ + loginSuccess: (user) => { + console.log("LoginListener :: loginSuccess", user); + }, + loginFailure: (error) => { + console.log("LoginListener :: loginFailure", error); + }, + logoutSuccess: () => { + console.log("LoginListener :: logoutSuccess"); + }, + logoutFailure: (error) => { + console.log("LoginListener :: logoutFailure", error); + } + }) +); +``` + + +Remove the listener when no longer needed: + +```javascript +CometChat.removeLoginListener("UNIQUE_LISTENER_ID"); +``` +## Connection Listener + +Receive events when the WebSocket connection state changes. + +| Method | Description | +| ------ | ----------- | +| `onConnected()` | SDK has an active connection to CometChat servers. | +| `inConnecting()` | SDK is attempting to establish or re-establish a connection. | +| `onDisconnected()` | SDK is disconnected due to network issues or other errors. | +| `onFeatureThrottled()` | A feature has been throttled due to rate limiting. | + + + +```typescript +const listenerID: string = "UNIQUE_LISTENER_ID"; +CometChat.addConnectionListener( + listenerID, + new CometChat.ConnectionListener({ + onConnected: () => { + console.log("ConnectionListener :: connected"); + }, + inConnecting: () => { + console.log("ConnectionListener :: connecting"); + }, + onDisconnected: () => { + console.log("ConnectionListener :: disconnected"); + }, + onFeatureThrottled: () => { + console.log("ConnectionListener :: feature throttled"); + } + }) +); +``` + + +```javascript +const listenerID = "UNIQUE_LISTENER_ID"; +CometChat.addConnectionListener( + listenerID, + new CometChat.ConnectionListener({ + onConnected: () => { + console.log("ConnectionListener :: connected"); + }, + inConnecting: () => { + console.log("ConnectionListener :: connecting"); + }, + onDisconnected: () => { + console.log("ConnectionListener :: disconnected"); + }, + onFeatureThrottled: () => { + console.log("ConnectionListener :: feature throttled"); + } + }) +); +``` + + +Remove the listener when no longer needed: + +```javascript +CometChat.removeConnectionListener("UNIQUE_LISTENER_ID"); +``` + +--- + +## Next Steps + + + + Handle incoming messages in real-time + + + Show when users are typing + + + Track online/offline status of users + + + Monitor SDK connection state changes + + diff --git a/sdk/javascript/authentication-overview.mdx b/sdk/javascript/authentication-overview.mdx index 1df057175..639ce41fe 100644 --- a/sdk/javascript/authentication-overview.mdx +++ b/sdk/javascript/authentication-overview.mdx @@ -1,68 +1,134 @@ --- title: "Authentication" -sidebarTitle: "Overview" +sidebarTitle: "Authentication" +description: "Create users, log in with Auth Key or Auth Token, check login status, and log out using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Check existing session +const user = await CometChat.getLoggedinUser(); -## Create User +// Login with Auth Key (development only) +CometChat.login("cometchat-uid-1", "AUTH_KEY").then(user => console.log("Logged in:", user)); -Before you log in a user, you must add the user to CometChat. +// Login with Auth Token (production) +CometChat.login("AUTH_TOKEN").then(user => console.log("Logged in:", user)); -1. **For proof of concept/MVPs**: Create the user using the [CometChat Dashboard](https://app.cometchat.com). -2. **For production apps**: Use the CometChat [Create User API](https://api-explorer.cometchat.com/reference/creates-user) to create the user when your user signs up in your app. +// Logout +CometChat.logout().then(() => console.log("Logged out")); +``` - +**Create users via:** [Dashboard](https://app.cometchat.com) (testing) | [REST API](https://api-explorer.cometchat.com/reference/creates-user) (production) +**Test UIDs:** `cometchat-uid-1` through `cometchat-uid-5` + + +After [initializing](/sdk/javascript/initialization) the SDK, the next step is to authenticate your user. CometChat provides two login methods — Auth Key for quick development, and Auth Token for production — both accessed through the `login()` method. + +### How It Works + +```mermaid +sequenceDiagram + participant User + participant YourApp as Your App + participant YourServer as Your Server + participant CometChat as CometChat + + User->>YourApp: Signs up / Logs in + YourApp->>YourServer: Authenticate user + YourServer->>CometChat: Create user (REST API, first time only) + CometChat-->>YourServer: User created + YourServer->>CometChat: Create Auth Token (REST API) + CometChat-->>YourServer: Auth Token + YourServer-->>YourApp: Return Auth Token + YourApp->>CometChat: CometChat.login(authToken) + CometChat-->>YourApp: User object (logged in) +``` -We have setup 5 users for testing having UIDs: `cometchat-uid-1`, `cometchat-uid-2`, `cometchat-uid-3`, `cometchat-uid-4` and `cometchat-uid-5`. +## Before You Log In - +### Create a User -Once initialization is successful, you will need to log the user into CometChat using the `login()` method. +A user must exist in CometChat before they can log in. -We recommend you call the CometChat login method once your user logs into your app. The `login()` method needs to be called only once. +- **During development:** Create users from the [CometChat Dashboard](https://app.cometchat.com). Five test users are already available with UIDs `cometchat-uid-1` through `cometchat-uid-5`. +- **In production:** Call the [Create User REST API](https://api-explorer.cometchat.com/reference/creates-user) when a user signs up in your app. - +You can also create users from the client side using `createUser()` (development only): -The CometChat SDK maintains the session of the logged-in user within the SDK. Thus you do not need to call the login method for every session. You can use the CometChat.getLoggedinUser() method to check if there is any existing session in the SDK. This method should return the details of the logged-in user. If this method returns null, it implies there is no session present within the SDK and you need to log the user into CometChat. - - - -## Login using Auth Key + + +```typescript +let authKey: string = "AUTH_KEY"; +let uid: string = "user1"; +let name: string = "Kevin"; -This straightforward authentication method is ideal for proof-of-concept (POC) development or during the early stages of application development. For production environments, however, we strongly recommend using an [AuthToken](#login-using-auth-token) instead of an Auth Key to ensure enhanced security. +let user: CometChat.User = new CometChat.User(uid); +user.setName(name); - +CometChat.createUser(user, authKey).then( + (user: CometChat.User) => { + console.log("User created:", user); + }, + (error: CometChat.CometChatException) => { + console.log("Error:", error); + } +); +``` + -```js -var UID = "UID"; -var authKey = "AUTH_KEY"; +```javascript +let authKey = "AUTH_KEY"; +let uid = "user1"; +let name = "Kevin"; -CometChat.getLoggedinUser().then( +let user = new CometChat.User(uid); +user.setName(name); + +CometChat.createUser(user, authKey).then( (user) => { - if (!user) { - CometChat.login(UID, authKey).then( - (user) => { - console.log("Login Successful:", { user }); - }, - (error) => { - console.log("Login failed with exception:", { error }); - } - ); - } + console.log("User created:", user); }, (error) => { - console.log("Something went wrong", error); + console.log("Error:", error); } ); ``` - + + + +`createUser()` with Auth Key is for development only. In production, create users server-side via the [REST API](https://api-explorer.cometchat.com/reference/creates-user). See [User Management](/sdk/javascript/user-management) for full details. + + +### Check for an Existing Session + +The SDK persists the logged-in user's session locally. Before calling `login()`, always check whether a session already exists — this avoids unnecessary login calls and keeps your app responsive. +```javascript +const user = await CometChat.getLoggedinUser(); +if (user) { + // User is already logged in — proceed to your app +} +``` + +If `getLoggedinUser()` returns `null`, no active session exists and you need to call `login()`. + +## Login with Auth Key + +Auth Key login is the simplest way to get started. Pass a UID and your Auth Key directly from the client. + + +Auth Keys are meant for development and testing only. For production, use [Auth Token login](#login-with-auth-token) instead. Never ship Auth Keys in client-side code. + + + ```typescript -var UID: string = "cometchat-uid-1", - authKey: string = "AUTH_KEY"; +const UID: string = "cometchat-uid-1"; +const authKey: string = "AUTH_KEY"; CometChat.getLoggedinUser().then( (user: CometChat.User) => { @@ -78,39 +144,21 @@ CometChat.getLoggedinUser().then( } }, (error: CometChat.CometChatException) => { - console.log("Some Error Occured", { error }); + console.log("Some Error Occurred", { error }); } ); ``` - - - -| Parameters | Description | -| ---------- | ------------------------------------------------ | -| UID | The UID of the user that you would like to login | -| authKey | CometChat Auth Key | - -After the user logs in, their information is returned in the `User` object on `Promise` resolved. - -## Login using Auth Token - -This advanced authentication procedure does not use the Auth Key directly in your client code thus ensuring safety. - -1. [Create a User](https://api-explorer.cometchat.com/reference/creates-user) via the CometChat API when the user signs up in your app. -2. [Create an Auth Token](https://api-explorer.cometchat.com/reference/create-authtoken) via the CometChat API for the new user and save the token in your database. -3. Load the Auth Token in your client and pass it to the `login()` method. - - -```js -var authToken = "AUTH_TOKEN"; +```javascript +const UID = "cometchat-uid-1"; +const authKey = "AUTH_KEY"; CometChat.getLoggedinUser().then( (user) => { if (!user) { - CometChat.login(authToken).then( + CometChat.login(UID, authKey).then( (user) => { console.log("Login Successful:", { user }); }, @@ -121,16 +169,49 @@ CometChat.getLoggedinUser().then( } }, (error) => { - console.log("Something went wrong", error); + console.log("Some Error Occurred", { error }); } ); ``` + + +```javascript +const UID = "cometchat-uid-1"; +const authKey = "AUTH_KEY"; + +try { + const loggedInUser = await CometChat.getLoggedinUser(); + if (!loggedInUser) { + const user = await CometChat.login(UID, authKey); + console.log("Login Successful:", { user }); + } +} catch (error) { + console.log("Login failed with exception:", { error }); +} +``` + + +| Parameter | Description | +| --------- | ----------- | +| UID | The UID of the user to log in | +| authKey | Your CometChat Auth Key | + +On success, the `Promise` resolves with a [`User`](/sdk/reference/entities#user) object containing the logged-in user's details. +## Login with Auth Token + +Auth Token login keeps your Auth Key off the client entirely. Your server generates a token via the REST API and passes it to the client. + +1. [Create the user](https://api-explorer.cometchat.com/reference/creates-user) via the REST API when they sign up (first time only). +2. [Generate an Auth Token](https://api-explorer.cometchat.com/reference/create-authtoken) on your server and return it to the client. +3. Pass the token to `login()`. + + ```typescript -var authToken: string = "AUTH_TOKEN"; +const authToken: string = "AUTH_TOKEN"; CometChat.getLoggedinUser().then( (user: CometChat.User) => { @@ -146,52 +227,190 @@ CometChat.getLoggedinUser().then( } }, (error: CometChat.CometChatException) => { - console.log("Some Error Occured", { error }); + console.log("Some Error Occurred", { error }); } ); ``` + + +```javascript +const authToken = "AUTH_TOKEN"; + +CometChat.getLoggedinUser().then( + (user) => { + if (!user) { + CometChat.login(authToken).then( + (user) => { + console.log("Login Successful:", { user }); + }, + (error) => { + console.log("Login failed with exception:", { error }); + } + ); + } + }, + (error) => { + console.log("Some Error Occurred", { error }); + } +); +``` + +```javascript +const authToken = "AUTH_TOKEN"; + +try { + const loggedInUser = await CometChat.getLoggedinUser(); + if (!loggedInUser) { + const user = await CometChat.login(authToken); + console.log("Login Successful:", { user }); + } +} catch (error) { + console.log("Login failed with exception:", { error }); +} +``` + -| Parameter | Description | -| --------- | ---------------------------------------------- | -| authToken | Auth Token of the user you would like to login | +| Parameter | Description | +| --------- | ----------- | +| authToken | Auth Token generated on your server for the user | -After the user logs in, their information is returned in the `User` object on the `Promise` resolved. +On success, the `Promise` resolves with a [`User`](/sdk/reference/entities#user) object containing the logged-in user's details. ## Logout -You can use the `logout()` method to log out the user from CometChat. We suggest you call this method once your user has been successfully logged out from your app. +Call `logout()` when your user logs out of your app. This clears the local session. - -```js + +```typescript CometChat.logout().then( - () => { + (loggedOut: Object) => { console.log("Logout completed successfully"); }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Logout failed with exception:", { error }); } ); ``` - - -```typescript + +```javascript CometChat.logout().then( - (loggedOut: Object) => { + () => { console.log("Logout completed successfully"); }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Logout failed with exception:", { error }); } ); ``` + + +```javascript +try { + await CometChat.logout(); + console.log("Logout completed successfully"); +} catch (error) { + console.log("Logout failed with exception:", { error }); +} +``` + + + +--- + +## Login Listener + +You can listen for login and logout events in real time using `LoginListener`. This is useful for updating UI state or triggering side effects when the auth state changes. + +| Callback | Description | +| --- | --- | +| `loginSuccess(event)` | User logged in successfully. Provides the [`User`](/sdk/reference/entities#user) object. | +| `loginFailure(event)` | Login failed. Provides a [`CometChatException`](/sdk/reference/auxiliary#cometchatexception). | +| `logoutSuccess()` | User logged out successfully. | +| `logoutFailure(event)` | Logout failed. Provides a [`CometChatException`](/sdk/reference/auxiliary#cometchatexception). | + +### Add a Listener + + + +```typescript +const listenerID: string = "UNIQUE_LISTENER_ID"; +CometChat.addLoginListener( + listenerID, + new CometChat.LoginListener({ + loginSuccess: (user: CometChat.User) => { + console.log("LoginListener :: loginSuccess", user); + }, + loginFailure: (error: CometChat.CometChatException) => { + console.log("LoginListener :: loginFailure", error); + }, + logoutSuccess: () => { + console.log("LoginListener :: logoutSuccess"); + }, + logoutFailure: (error: CometChat.CometChatException) => { + console.log("LoginListener :: logoutFailure", error); + } + }) +); +``` + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; +CometChat.addLoginListener( + listenerID, + new CometChat.LoginListener({ + loginSuccess: (e) => { + console.log("LoginListener :: loginSuccess", e); + }, + loginFailure: (e) => { + console.log("LoginListener :: loginFailure", e); + }, + logoutSuccess: () => { + console.log("LoginListener :: logoutSuccess"); + }, + logoutFailure: (e) => { + console.log("LoginListener :: logoutFailure", e); + } + }) +); +``` + + +### Remove a Listener + +```javascript +CometChat.removeLoginListener("UNIQUE_LISTENER_ID"); +``` + + +Always remove login listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +--- + +## Next Steps + + + + Send your first text, media, or custom message + + + Create, update, and delete users programmatically + + + Monitor the SDK connection state in real time + + + Complete reference for all SDK event listeners + + diff --git a/sdk/javascript/best-practices.mdx b/sdk/javascript/best-practices.mdx new file mode 100644 index 000000000..b3998e1d7 --- /dev/null +++ b/sdk/javascript/best-practices.mdx @@ -0,0 +1,175 @@ +--- +title: "Best Practices" +sidebarTitle: "Best Practices" +description: "Recommended patterns and practices for building with the CometChat JavaScript SDK." +--- + +Follow these best practices to build reliable, performant, and secure applications with the CometChat JavaScript SDK. Organized by topic — jump to what's relevant for your current work. + +## Initialization & Authentication + +| Practice | Description | +|----------|-------------| +| Initialize once at startup | Call `CometChat.init()` in your entry file (`index.js`, `main.js`, or `App.js`). It only needs to be called once per session. | +| Use environment variables | Store App ID, Region, and Auth Key in environment variables rather than hardcoding them. | +| Check for existing sessions | Before calling `login()`, use `CometChat.getLoggedinUser()` to check if a session already exists. | +| Use Auth Tokens in production | Auth Keys are for development only. Generate Auth Tokens server-side using the [REST API](https://api-explorer.cometchat.com/reference/create-authtoken). | +| Handle token expiry | Implement a mechanism to detect login failures due to expired tokens. Use the [Login Listener](/sdk/javascript/authentication-overview#login-listener) to detect session changes. | +| Logout on sign-out | Always call `CometChat.logout()` when your user signs out to clear the SDK session and stop real-time events. | + +## Listeners + +| Practice | Description | +|----------|-------------| +| Use unique listener IDs | Use descriptive IDs like `"MESSAGE_LISTENER_CHAT_SCREEN"` to avoid accidental overwrites. | +| Register early, remove on cleanup | Register listeners after `login()`. Remove them when the component is destroyed to prevent memory leaks. | +| Keep callbacks lightweight | Avoid heavy processing inside listener callbacks. Dispatch events to your state management layer. | +| Use specific listeners | Only register the listener types you need. Don't register a `GroupListener` if your page only handles messages. | + +## Pagination & Caching + +| Practice | Description | +|----------|-------------| +| Use reasonable limits | Set `setLimit()` to 30-50 for users, messages, and group members. | +| Reuse request objects | Call `fetchNext()`/`fetchPrevious()` on the same request instance. Creating a new object resets the cursor. | +| Cache frequently accessed data | Store user and group objects locally to reduce API calls. | + +## Rate Limits + +| Practice | Description | +|----------|-------------| +| Batch operations | Space out bulk operations using a queue or throttle mechanism. | +| Monitor rate limit headers | Check `X-Rate-Limit-Remaining` in REST API responses to slow down before hitting limits. | +| Distinguish operation types | Core operations (login, create/delete user) share a 10,000/min limit. Standard operations have 20,000/min. Avoid frequent login/logout cycles. | + +## SSR Frameworks + +| Practice | Description | +|----------|-------------| +| Initialize client-side only | CometChat requires browser APIs. Use dynamic imports or `useEffect` for Next.js, Nuxt, etc. | +| Use loading states | Show a loading indicator while the SDK initializes to prevent hydration mismatches. | + +## Messaging + +| Practice | Description | +|----------|-------------| +| Use appropriate message types | Choose text, media, or custom messages based on your content. | +| Add metadata for context | Use `setMetadata()` to attach location, device info, or other contextual data. | +| Handle errors gracefully | Always implement error callbacks to handle network issues or invalid parameters. | +| Validate file types | Before sending media messages, verify the file type matches the message type. | +| Hide deleted/blocked content | Use `hideDeletedMessages(true)` and `hideMessagesFromBlockedUsers(true)` for cleaner lists. | + +## Threaded Messages + +| Practice | Description | +|----------|-------------| +| Track active thread ID | Store the current thread's `parentMessageId` to filter incoming messages. | +| Use hideReplies(true) | Exclude thread replies from main conversation to avoid clutter. | +| Show reply count | Display the number of replies on parent messages to indicate thread activity. | + +## Reactions & Mentions + +| Practice | Description | +|----------|-------------| +| Update UI optimistically | Show reactions immediately, then sync with server response. | +| Use correct mention format | Always use `<@uid:UID>` format for mentions in message text. | +| Highlight mentions in UI | Parse message text and style mentions differently. | + +## Typing Indicators + +| Practice | Description | +|----------|-------------| +| Debounce typing events | Don't call `startTyping()` on every keystroke - debounce to ~300ms intervals. | +| Auto-stop typing | Call `endTyping()` after 3-5 seconds of inactivity or when the user sends a message. | + +## Delivery & Read Receipts + +| Practice | Description | +|----------|-------------| +| Mark as delivered on fetch | Call `markAsDelivered()` when messages are fetched and displayed. | +| Mark as read on view | Call `markAsRead()` when the user actually views/scrolls to a message. | +| Batch receipts | Mark the last message in a batch - all previous messages are automatically marked. | + +## Groups + +| Practice | Description | +|----------|-------------| +| Use meaningful GUIDs | Choose descriptive, unique GUIDs (e.g., `"project-alpha-team"`). | +| Set group type carefully | Group type cannot be changed after creation. Choose between PUBLIC, PASSWORD, and PRIVATE. | +| Add members at creation | Use `createGroupWithMembers()` to add initial members in a single API call. | +| Check hasJoined before joining | Avoid unnecessary API calls by checking the group's `hasJoined` property first. | +| Transfer ownership before leaving | Owners must transfer ownership to another member before they can leave. | +| Use joinedOnly(true) | Filter to joined groups when building sidebars or group lists. | + +## Group Members + +| Practice | Description | +|----------|-------------| +| Batch member additions | Add multiple members in a single `addMembersToGroup()` call. | +| Set appropriate scopes | Assign `PARTICIPANT` by default. Only use `ADMIN` or `MODERATOR` when needed. | +| Handle partial failures | Check each entry in the response array for `"success"` or an error message. | +| Use scope constants | Use `CometChat.GROUP_MEMBER_SCOPE.ADMIN` instead of raw strings. | +| Kick vs. Ban | Use kick when the user can rejoin. Use ban for permanent removal until unbanned. | + +## Calling + +| Practice | Description | +|----------|-------------| +| Initialize Calls SDK after Chat SDK | Always initialize Chat SDK (`CometChat.init()`) before Calls SDK (`CometChatCalls.init()`). | +| Store session ID immediately | Save the session ID from `initiateCall()` response - you'll need it for accept, reject, cancel. | +| Handle all call states | Implement handlers for all listener events (accepted, rejected, cancelled, busy, ended). | +| Generate tokens just-in-time | Generate call tokens immediately before starting a session rather than caching them. | +| Clean up on session end | Always call `CometChatCalls.endSession()` in both `onCallEnded` and `onCallEndButtonPressed` callbacks. | +| Inform users about recording | Always notify participants when recording starts - this is often a legal requirement. | +| Limit presenters to 5 | Additional users should join as viewers. | + +## Custom CSS (Calling) + +| Practice | Description | +|----------|-------------| +| Only use documented CSS classes | Undocumented internal classes may break with SDK updates. | +| Don't resize the grid container | Only customize colors, borders, and visibility. | +| Test across modes | CSS changes may look different in `DEFAULT`, `TILE`, and `SPOTLIGHT` modes. | +| Keep button sizes accessible | Minimum 44x44px for touch targets. | + +## Connection & WebSocket + +| Practice | Description | +|----------|-------------| +| Register connection listener early | Add the listener right after `CometChat.init()` succeeds. | +| Show connection status in UI | Display a banner when disconnected so users know messages may be delayed. | +| Queue actions during disconnection | Queue user actions and retry once `onConnected` fires. | +| Don't poll getConnectionStatus() | Use the listener-based approach instead. | +| Reconnect on app foreground | If you disconnect in background, call `CometChat.connect()` when returning to foreground. | + +## AI Features + +| Practice | Description | +|----------|-------------| +| Register both listeners for AI Agents | Use `AIAssistantListener` for streaming events and `MessageListener` for persisted messages. | +| Handle streaming progressively | Render the assistant's reply token-by-token using `Text Message Content` events. | +| Show pending state for moderation | Display a visual indicator when `getModerationStatus()` returns `PENDING`. | +| Handle disapproved messages gracefully | Show a placeholder or notification so the sender understands what happened. | +| Track pending messages | Maintain a local map of pending message IDs to update UI when moderation results arrive. | + +## Upgrading from V3 + +| Practice | Description | +|----------|-------------| +| Follow the setup guide first | Complete the v4 [setup instructions](/sdk/javascript/setup-sdk) before changing imports. | +| Update all imports at once | Use find-and-replace to change all `@cometchat-pro/chat` imports to `@cometchat/chat-sdk-javascript`. | +| Test incrementally | Test each feature area (messaging, calling, groups) individually after updating. | +| Remove old packages | Uninstall v3 packages (`npm uninstall @cometchat-pro/chat`) to avoid conflicts. | + +--- + +## Next Steps + + + + Common issues and solutions + + + Installation and initialization guide + + diff --git a/sdk/javascript/block-users.mdx b/sdk/javascript/block-users.mdx index 738e07ab1..06f621e94 100644 --- a/sdk/javascript/block-users.mdx +++ b/sdk/javascript/block-users.mdx @@ -1,19 +1,51 @@ --- title: "Block Users" +sidebarTitle: "Block Users" +description: "Block and unblock users, and retrieve the list of blocked users using the CometChat JavaScript SDK." --- + +```javascript +// Block users +await CometChat.blockUsers(["UID1", "UID2"]); -## Block Users +// Unblock users +await CometChat.unblockUsers(["UID1", "UID2"]); -*In other words, as a logged-in user, how do I block a user from sending me messages?* +// Get blocked users list +let request = new CometChat.BlockedUsersRequestBuilder().setLimit(30).build(); +let blockedUsers = await request.fetchNext(); +``` -You can block users using the `blockUsers()` method. Once any user is blocked, all the communication to and from the respective user will be completely blocked. You can block multiple users in a single operation. The `blockUsers()` method takes a `Array` as a parameter which holds the list of `UID's` to be blocked. +**Directions:** `BLOCKED_BY_ME` | `HAS_BLOCKED_ME` | `BOTH` (default) + + +Blocking a user prevents all communication between them and the logged-in user — messages, calls, and presence updates are all suppressed. You can block and unblock users by UID, and fetch the blocked users list with filtering and pagination. + +## Block Users + +Block users to prevent all communication with them. Use `blockUsers()` with an array of UIDs. - + +```typescript +const usersList: String[] = ["UID1", "UID2", "UID3"]; + +CometChat.blockUsers(usersList).then( + (list: Object) => { + console.log("users list blocked", { list }); + }, (error: CometChat.CometChatException) => { + console.log("Blocking user fails with error", error); + } +); +``` + + + + ```javascript -var usersList = ["UID1", "UID2", "UID3"]; +const usersList = ["UID1", "UID2", "UID3"]; CometChat.blockUsers(usersList).then( list => { @@ -26,35 +58,47 @@ list => { - -```typescript -var usersList: String[] = ["UID1", "UID2", "UID3"]; + +```javascript +const usersList = ["UID1", "UID2", "UID3"]; -CometChat.blockUsers(usersList).then( - (list: Object) => { - console.log("users list blocked", { list }); - }, (error: CometChat.CometChatException) => { - console.log("Blocking user fails with error", error); - } -); +try { + const list = await CometChat.blockUsers(usersList); + console.log("users list blocked", { list }); +} catch (error) { + console.log("Blocking user fails with error", error); +} ``` -It returns a Array which contains `UID's` as the keys and "success" or "fail" as the value based on if the block operation for the `UID` was successful or not. +Returns an object with UIDs as keys and `"success"` or `"fail"` as values. Each [`User`](/sdk/reference/entities#user) in the request is processed independently. ## Unblock Users -*In other words, as a logged-in user, how do I unblock a user I previously blocked?* - -You can unblock the already blocked users using the `unblockUsers()` method. You can unblock multiple users in a single operation. The `unblockUsers()` method takes a `Array` as a parameter which holds the list of `UID's` to be unblocked. +Unblock previously blocked users using `unblockUsers()` with an array of UIDs. - + +```typescript +const usersList: String[] = ["UID1", "UID2", "UID3"]; + +CometChat.unblockUsers(usersList).then( + (list: Object) => { + console.log("users list blocked", { list }); + }, (error: CometChat.CometChatException) => { + console.log("Blocking user fails with error", error); + } +); +``` + + + + ```javascript -var usersList = ["UID1", "UID2", "UID3"]; +const usersList = ["UID1", "UID2", "UID3"]; CometChat.unblockUsers(usersList).then( list => { @@ -67,39 +111,44 @@ list => { - -```typescript -var usersList: String[] = ["UID1", "UID2", "UID3"]; + +```javascript +const usersList = ["UID1", "UID2", "UID3"]; -CometChat.unblockUsers(usersList).then( - (list: Object) => { - console.log("users list blocked", { list }); - }, (error: CometChat.CometChatException) => { - console.log("Blocking user fails with error", error); - } -); +try { + const list = await CometChat.unblockUsers(usersList); + console.log("users list unblocked", { list }); +} catch (error) { + console.log("unblocking user fails with error", error); +} ``` -It returns a Array which contains `UID's` as the keys and `success` or `fail` as the value based on if the unblock operation for the `UID` was successful or not. +Returns an object with UIDs as keys and `"success"` or `"fail"` as values. Each [`User`](/sdk/reference/entities#user) in the request is processed independently. ## Get List of Blocked Users -*In other words, as a logged-in user, how do I get a list of all users I've blocked?* - -In order to fetch the list of blocked users, you can use the `BlockedUsersRequest` class. To use this class i.e to create an object of the `BlockedUsersRequest class`, you need to use the `BlockedUsersRequestBuilder` class. The `BlockedUsersRequestBuilder` class allows you to set the parameters based on which the blocked users are to be fetched. - -The `BlockedUsersRequestBuilder` class allows you to set the below parameters: +Use `BlockedUsersRequestBuilder` to fetch blocked users with filtering and pagination. ### Set Limit -This method sets the limit i.e. the number of blocked users that should be fetched in a single iteration. +Sets the number of blocked users to fetch per request. - + +```typescript +let limit: number = 30; +let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() + .setLimit(limit) + .build(); +``` + + + + ```javascript let limit = 30; let blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() @@ -109,24 +158,26 @@ let blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() + + +### Set Search Keyword + +Filters blocked users by a search string. + + ```typescript let limit: number = 30; +let searchKeyword: string = "super"; let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() .setLimit(limit) + .setSearchKeyword(searchKeyword) .build(); ``` - - -### Set Search Keyword - -This method allows you to set the search string based on which the blocked users are to be fetched. - - - + ```javascript let limit = 30; let searchKeyword = "super"; @@ -138,13 +189,23 @@ let blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() + + +### Set Direction + +Filters by block direction: + +- `BLOCKED_BY_ME` — Users blocked by the logged-in user +- `HAS_BLOCKED_ME` — Users who have blocked the logged-in user +- `BOTH` — Both directions (default) + + ```typescript let limit: number = 30; -let searchKeyword: string = "super"; let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() .setLimit(limit) - .setSearchKeyword(searchKeyword) + .setDirection(CometChat.BlockedUsersRequest.directions.BLOCKED_BY_ME) .build(); ``` @@ -152,14 +213,12 @@ let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUs -### Set Direction +Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the `BlockedUsersRequest` class. -* CometChat.BlockedUsersRequest.directions.BLOCKED\_BY\_ME - This will ensure that the list of blocked users only contains the users blocked by the logged in user. -* CometChat.BlockedUsersRequest.directions.HAS\_BLOCKED\_ME - This will ensure that the list of blocked users only contains the users that have blocked the logged in user. -* CometChat.BlockedUsersRequest.directions.BOTH - This will make sure the list of users includes both the above cases. This is the default value for the direction variable if it is not set. +Once you have the object of the `BlockedUsersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of [`User`](/sdk/reference/entities#user) objects containing n number of blocked users where N is the limit set in the builder class. - + ```javascript let limit = 30; let blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() @@ -170,28 +229,33 @@ let blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() + + +After configuring the builder, call `build()` to get the `BlockedUsersRequest` object, then call `fetchNext()` to retrieve blocked users. + + ```typescript let limit: number = 30; let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() .setLimit(limit) - .setDirection(CometChat.BlockedUsersRequest.directions.BLOCKED_BY_ME) .build(); + +blockedUsersRequest.fetchNext().then( + (userList: CometChat.User[]) => { + console.log("Blocked user list received:", userList); + }, (error: CometChat.CometChatException) => { + console.log("Blocked user list fetching failed with error:", error); + } +); ``` - - -Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the `BlockedUsersRequest` class. - -Once you have the object of the `BlockedUsersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `User` objects containing n number of blocked users where N is the limit set in the builder class. - - - + ```javascript -var limit = 30; -var blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() +const limit = 30; +const blockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() .setLimit(limit) .build(); blockedUsersRequest.fetchNext().then( @@ -205,22 +269,32 @@ userList => { - -```typescript -let limit: number = 30; -let blockedUsersRequest: CometChat.BlockedUsersRequest = new CometChat.BlockedUsersRequestBuilder() - .setLimit(limit) - .build(); + -blockedUsersRequest.fetchNext().then( - (userList: CometChat.User[]) => { - console.log("Blocked user list received:", userList); - }, (error: CometChat.CometChatException) => { - console.log("Blocked user list fetching failed with error:", error); - } -); -``` +The `fetchNext()` method returns an array of [`User`](/sdk/reference/entities#user) objects representing blocked users. - +Relevant fields to access on returned users: - +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| blockedByMe | `getBlockedByMe()` | `boolean` | Whether the logged-in user has blocked this user | +| hasBlockedMe | `getHasBlockedMe()` | `boolean` | Whether this user has blocked the logged-in user | + +--- + +## Next Steps + + + + Fetch and filter user lists + + + Track online/offline status of users + + + Create, update, and delete users + + + Report inappropriate messages from users + + diff --git a/sdk/javascript/call-logs.mdx b/sdk/javascript/call-logs.mdx index 74f2b87af..3c5819bfc 100644 --- a/sdk/javascript/call-logs.mdx +++ b/sdk/javascript/call-logs.mdx @@ -1,20 +1,38 @@ --- title: "Call Logs" +sidebarTitle: "Call Logs" +description: "Fetch, filter, and retrieve call history including duration, participants, and recording status using the CometChat Calls SDK." --- + +```javascript +let loggedInUser = await CometChat.getLoggedinUser(); +let authToken = loggedInUser.getAuthToken(); + +// Fetch call logs +let request = new CometChatCalls.CallLogRequestBuilder() + .setLimit(30) + .setAuthToken(authToken) + .setCallCategory("call") + .build(); -## Overview +let logs = await request.fetchNext(); + +// Get details for a specific call session +let details = await CometChatCalls.getCallDetails("SESSION_ID", authToken); +``` -CometChat's Web Call SDK provides a comprehensive way to integrate call logs into your application, enhancing your user experience by allowing users to effortlessly keep track of their communication history. Call logs provide crucial information such as call duration, participants, and more. +**Filters:** `setCallType()`, `setCallStatus()`, `setCallCategory()`, `setCallDirection()`, `setHasRecording()`, `setUid()`, `setGuid()` + -This feature not only allows users to review their past interactions but it also serves as an effective tool to revisit important conversation details. With the flexibility of fetching call logs, filtering them according to specific parameters, and obtaining detailed information of individual calls, application developers can use this feature to build a more robust and interactive communication framework. +Call logs let you retrieve and display call history — who called whom, when, how long, and whether it was recorded. Use `CallLogRequestBuilder` to fetch and filter logs, and `getCallDetails()` to get details for a specific session. -In the following sections, we will guide you through the process of working with call logs, offering a deeper insight into how to optimally use this feature in your Web application. +Before you begin, make sure you've completed the [Calls SDK Setup](/sdk/javascript/calling-setup). -## Fetching Call Logs +## Fetch Call Logs -To fetch all call logs in your 23b application, you should use the `CallLogRequestBuilder`, This builder allows you to customize the call logs fetching process according to your needs. +Build a request with `CallLogRequestBuilder`, then call `fetchNext()` or `fetchPrevious()` to retrieve logs. Call either method repeatedly on the same builder instance to paginate through results. ```javascript let callLogRequestBuilder = new CometChatCalls.CallLogRequestBuilder() @@ -24,7 +42,7 @@ let callLogRequestBuilder = new CometChatCalls.CallLogRequestBuilder() .build() ``` -`CallLogRequestBuilder` has the following settings available. +### Builder Settings | Setting | Description | | ---------------------------------------------------------------------------------------------------- | ------------------------------------------------------------ | @@ -40,7 +58,7 @@ let callLogRequestBuilder = new CometChatCalls.CallLogRequestBuilder() ### Fetch Next -The**`fetchNext()`**method retrieves the next set of call logs. +Retrieves the next set of call logs: ```javascript let callLogRequestBuilder = new CometChatCalls.CallLogRequestBuilder() @@ -60,7 +78,7 @@ callLogRequestBuilder.fetchNext() ### Fetch Previous -The**`fetchPrevious()`**method retrieves the previous set of call logs. +Retrieves the previous set of call logs: ```javascript let callLogRequestBuilder = new CometChatCalls.CallLogRequestBuilder() @@ -78,19 +96,69 @@ callLogRequestBuilder.fetchPrevious() }); ``` -## Get Call Details +The `fetchNext()` and `fetchPrevious()` methods return an array of [`CallLog`](/sdk/reference/calls#calllog) objects. -To retrieve the specific details of a call, use the**`getCallDetails()`**method. This method requires the Auth token of the logged-in user and the session ID along with a callback listener. +## Get Call Details +Retrieve details for a specific call session using `getCallDetails()`: + + + +```typescript +const sessionID: string = "SESSION_ID"; +CometChatCalls.getCallDetails(sessionID, authToken).then( + (callLogs: Array) => { + console.log("Call details:", callLogs); + }, + (error: any) => { + console.log("Error fetching call details:", error); + } +); +``` + + ```javascript -var sessionID = "SESSION_ID"; -CometChatCalls.getCallDetails(sessionID, authToken) -.then((callLogs: Array) => { - console.log(callLogs); - }) - .catch(err => { - console.log(err); - }); +const sessionID = "SESSION_ID"; +CometChatCalls.getCallDetails(sessionID, authToken).then( + (callLogs) => { + console.log("Call details:", callLogs); + }, + (error) => { + console.log("Error fetching call details:", error); + } +); +``` + + +```javascript +const sessionID = "SESSION_ID"; +try { + const callLogs = await CometChatCalls.getCallDetails(sessionID, authToken); + console.log("Call details:", callLogs); +} catch (error) { + console.log("Error fetching call details:", error); +} ``` + + + +Note: Replace `"SESSION_ID"` with the ID of the session you are interested in. + +--- -Note: Replace**`"SESSION_ID"`**with the ID of the session you are interested in. +## Next Steps + + + + Implement the complete ringing call flow + + + Record audio and video calls + + + Start call sessions without the ringing flow + + + Install and initialize the Calls SDK + + diff --git a/sdk/javascript/calling-overview.mdx b/sdk/javascript/calling-overview.mdx index 18bc0ea62..2abcdd6ad 100644 --- a/sdk/javascript/calling-overview.mdx +++ b/sdk/javascript/calling-overview.mdx @@ -1,15 +1,29 @@ --- title: "Calling" sidebarTitle: "Overview" +description: "Overview of CometChat voice and video calling capabilities including ringing, direct call sessions, and standalone calling." --- -## Overview + -CometChat provides voice and video calling capabilities for your web application. This guide helps you choose the right implementation approach based on your use case. +Choose your calling approach: +- **Ringing** → [Default Call](/sdk/javascript/default-call) — Full call flow with notifications, accept/reject +- **Call Session** → [Direct Call](/sdk/javascript/direct-call) — Session-based calling with custom UI +- **Standalone** → [Standalone Calling](/sdk/javascript/standalone-calling) — Calls SDK only, no Chat SDK needed + +```bash +# Install Calls SDK +npm install @cometchat/calls-sdk-javascript +``` + +**Features:** Recording, Virtual Background, Screen Sharing, Custom CSS, Call Logs, Session Timeout + + +CometChat provides three ways to add voice and video calling to your web app. Which one you pick depends on how much of the call flow you want CometChat to handle vs. building yourself. ## Prerequisites -1. CometChat SDK installed and configured. See the [Setup](/sdk/javascript/setup) guide. +1. CometChat Chat SDK installed and configured. See the [Setup](/sdk/javascript/setup-sdk) guide. 2. CometChat Calls SDK added to your project: ```bash @@ -18,58 +32,53 @@ npm install @cometchat/calls-sdk-javascript For detailed setup instructions, see the [Calls SDK Setup](/sdk/javascript/calling-setup) guide. -## Choose Your Implementation +## Choose Your Approach -CometChat offers three approaches to implement calling: +### Ringing (Full Call Flow) - - - Complete calling flow with incoming/outgoing call UI, accept/reject functionality, and call notifications. - - - Direct call session management. Use with Ringing flow or for custom call initiation logic. - - - Calls SDK only implementation without the Chat SDK dependency. - - - -### Ringing - -Use this when you need a complete calling experience with: -- Incoming and outgoing call UI -- Call accept/reject/cancel functionality -- Call notifications via push notifications -- Integration with CometChat messaging +The complete calling experience — incoming/outgoing call UI, accept/reject/cancel, push notifications, and integration with CometChat messaging. Use this when you want CometChat to handle the entire call lifecycle. **Flow:** Initiate call → Receiver gets notified → Accept/Reject → Start session -[Get started with Ringing →](/sdk/javascript/default-call) + + Implement the complete ringing call flow + -### Call Session +### Call Session (Session Management) -Use this when you need to: -- Start a call session after the Ringing flow completes -- Implement custom call initiation logic with your own UI -- Join participants to a shared session using a session ID +Manages the actual call session — generating tokens, starting/ending sessions, configuring the call UI, and handling in-call events. The Ringing flow uses this under the hood after a call is accepted. You can also use it directly if you want to build your own call initiation logic. **Flow:** Generate token → Start session → Manage call → End session -[Get started with Call Session →](/sdk/javascript/direct-call) + + Start and manage call sessions + -### Standalone Calling +### Standalone Calling (No Chat SDK) -Use this when you want: -- Calling functionality without the Chat SDK -- Your own user authentication system -- Minimal SDK footprint +Calling without the Chat SDK. You handle user authentication via the REST API and use only the Calls SDK. Ideal when you need voice/video but not the full chat infrastructure. **Flow:** Get auth token via REST API → Generate call token → Start session -[Get started with Standalone Calling →](/sdk/javascript/standalone-calling) + + Implement calling without the Chat SDK + + +### How They Relate + +```mermaid +flowchart LR + A[Ringing Flow] -->|call accepted| B[Call Session] + C[Custom UI] -->|your logic| B + D[Standalone] -->|REST API auth| B +``` + +All three approaches converge on the Call Session layer to manage the actual media connection. The difference is how you get there — CometChat's ringing flow, your own UI, or standalone without the Chat SDK. ## Features +Once you have calling working, you can add these capabilities: + Record audio and video calls for playback, compliance, or archival purposes. diff --git a/sdk/javascript/calling-setup.mdx b/sdk/javascript/calling-setup.mdx index 7cd07247e..462f74d7d 100644 --- a/sdk/javascript/calling-setup.mdx +++ b/sdk/javascript/calling-setup.mdx @@ -1,93 +1,88 @@ --- -title: "Setup" +title: "Calls SDK Setup" +sidebarTitle: "Setup" +description: "Install and initialize the CometChat Calls SDK for JavaScript to enable voice and video calling in your application." --- + +```bash +npm install @cometchat/calls-sdk-javascript +``` + +```javascript +import { CometChatCalls } from "@cometchat/calls-sdk-javascript"; + +const callAppSetting = new CometChatCalls.CallAppSettingsBuilder() + .setAppId("APP_ID") + .setRegion("REGION") + .build(); -## Get your Application Keys +CometChatCalls.init(callAppSetting).then(() => console.log("Calls SDK ready")); +``` + +**Required:** App ID, Region from [CometChat Dashboard](https://app.cometchat.com) + -[Signup for CometChat](https://app.cometchat.com) and then: +The Calls SDK handles the media layer for voice and video calls — camera, microphone, screen sharing, and the call UI. It's a separate package from the Chat SDK and requires its own initialization. -1. Create a new app -2. Head over to the **Credentials** section and note the **App ID, Auth Key** & **Region** +## Prerequisites -## Add the CometChatCalls Dependency +You need your App ID and Region from the [CometChat Dashboard](https://app.cometchat.com). If you haven't created an app yet, [sign up](https://app.cometchat.com) and create one. -Install the package as NPM module: +If you're using the Chat SDK alongside (i.e., not [Standalone Calling](/sdk/javascript/standalone-calling)), make sure `CometChat.init()` completes before calling `CometChatCalls.init()`. + +## Installation ```bash npm install @cometchat/calls-sdk-javascript ``` - ```bash yarn add @cometchat/calls-sdk-javascript ``` - - -Then, import the `CometChatCalls` class wherever you want to use `CometChatCalls`. - - - -```js -import { CometChatCalls } from "@cometchat/calls-sdk-javascript"; -``` - - +Then import wherever needed: - -```typescript +```javascript import { CometChatCalls } from "@cometchat/calls-sdk-javascript"; ``` - - - - -### Initialize CometChatCalls - -The `init()` method initialises the settings required for `CometChatCalls`. The `init()` method takes a single paramater, that is the instance of `CallAppSettings` class. +## Initialization -The `CallAppSettingsBuilder` class allows you to configure three settings: - -1. **appID:** You CometChat App ID -2. **region**: The region where your app was created -3. **host:** This method takes the client URL as input and uses this client URL instead of the default client URL. This can be used in case of dedicated deployment of CometChat. - -You need to call init() before calling any other method from `CometChatCalls`. We suggest you call the init() method on app startup, preferably in the index.js file. +Call `CometChatCalls.init()` on app startup, after the Chat SDK has initialized (if you're using it). The method takes a `CallAppSettings` object built with `CallAppSettingsBuilder`. - -```js -let appID = "APP_ID"; -let region = "REGION"; + +```typescript +let appID: string = "APP_ID"; +let region: string = "REGION"; -const callAppSetting = new CometChatCalls.CallAppSettingsBuilder() - .setAppId(appID) - .setRegion(region) - .build(); +const callAppSetting: CometChatCalls.CallAppSettings = + new CometChatCalls.CallAppSettingsBuilder() + .setAppId(appID) + .setRegion(region) + .build(); CometChatCalls.init(callAppSetting).then( () => { console.log("CometChatCalls initialization completed successfully"); }, - (error) => { + (error: unknown) => { console.log("CometChatCalls initialization failed with error:", error); } ); ``` - - -```typescript + +```javascript let appID = "APP_ID"; let region = "REGION"; @@ -105,13 +100,59 @@ CometChatCalls.init(callAppSetting).then( } ); ``` - - -Make sure you replace the `APP_ID` with your CometChat **App ID** and `REGION` with your **App Region** in the above code. +Replace `APP_ID` and `REGION` with your credentials from the [Dashboard](https://app.cometchat.com). + + +`CometChatCalls.init()` must be called before any other Calls SDK method. Calling `generateToken()`, `startSession()`, or registering listeners before `init()` will fail. + + +### CallAppSettings Options + +| Method | Description | +| --- | --- | +| `setAppId(appID)` | Your CometChat App ID. Required. | +| `setRegion(region)` | The region where your app was created. Required. | +| `setHost(host)` | Custom client URL for dedicated deployments. Optional. | + +### Initialization Order + +If you're using both the Chat SDK and Calls SDK, initialize them in sequence: + +```javascript +// 1. Chat SDK first +await CometChat.init(appID, appSettings); + +// 2. Then Calls SDK +const callAppSetting = new CometChatCalls.CallAppSettingsBuilder() + .setAppId(appID) + .setRegion(region) + .build(); + +await CometChatCalls.init(callAppSetting); + +// 3. Now both SDKs are ready +``` + +For [Standalone Calling](/sdk/javascript/standalone-calling), you only need `CometChatCalls.init()` — no Chat SDK required. + +--- -| Parameter | Description | -| ----------------- | ---------------------------------------- | -| `callAppSettings` | An object of the `CallAppSettings` class | +## Next Steps + + + + Implement the complete ringing call flow + + + Start and manage call sessions + + + Use Calls SDK without the Chat SDK + + + Compare calling approaches and features + + diff --git a/sdk/javascript/connection-status.mdx b/sdk/javascript/connection-status.mdx index 7c67128bb..8720419e1 100644 --- a/sdk/javascript/connection-status.mdx +++ b/sdk/javascript/connection-status.mdx @@ -1,27 +1,68 @@ --- title: "Connection Status" +description: "Monitor real-time WebSocket connection status and respond to connectivity changes using the CometChat JavaScript SDK." --- + +```javascript +// Get current status: "connecting" | "connected" | "disconnected" +const status = CometChat.getConnectionStatus(); + +// Listen for connection changes +CometChat.addConnectionListener("LISTENER_ID", new CometChat.ConnectionListener({ + onConnected: () => console.log("Connected"), + inConnecting: () => console.log("Connecting..."), + onDisconnected: () => console.log("Disconnected") +})); + +// Cleanup +CometChat.removeConnectionListener("LISTENER_ID"); +``` + + +The CometChat SDK maintains a WebSocket connection to CometChat servers for real-time events. You can check the current connection state and listen for changes — useful for showing connectivity indicators in your UI or queuing operations while offline. -CometChat SDK provides you with a mechanism to get real-time status of the connection to CometChat web-socket servers. +When the connection drops, the SDK automatically attempts to reconnect, cycling through `disconnected` → `connecting` → `connected`. -Connection Status provides you with the below 3 methods to get the status of the connection to CometChat web-socket servers: +## Connection States -| Delegate Method | Information | -| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | -| connecting | This method is triggered when CometChat SDK is trying to establish a connection to the web-socket server. | -| connected | This method is called when CometChat SDK has successfully established a connection and now is connected. | -| disconnected | This method is called when the CometChat SDK gets disconnected due to any issue while maintaining the connection like network fluctuations, etc. | +| Value | Callback | Description | +|-------|----------|-------------| +| `"connected"` | `onConnected()` | SDK has an active connection to CometChat servers | +| `"connecting"` | `inConnecting()` | SDK is attempting to establish or re-establish a connection | +| `"disconnected"` | `onDisconnected()` | SDK is disconnected due to network issues or other errors | +| `"featureThrottled"` | — | A feature has been throttled due to rate limiting | -Once the connection is broken, the disconnected callback is triggered, the SDK automatically tries to establish the connection again, thus going into the connecting state and triggering the `connecting` method. Once the attempt to connect is successful, the `connected` method is triggered thus letting the developer know that the connection is established and is active. +## Get Current Status -To receive real-time connection status, you need to register `ConnectionListener` wherever you wish to receive the real-time status. You can use the `addConnectionListener()` method to do so. +Use `getConnectionStatus()` to check the current connection state at any time: - + +```typescript +const connectionStatus: string = CometChat.getConnectionStatus(); +``` + + + + ```javascript -let listenerID = "UNIQUE_LISTENER_ID"; +const connectionStatus = CometChat.getConnectionStatus(); +``` + + + + + +## Listen for Connection Changes + +Register a `ConnectionListener` to receive real-time connection state updates. We recommend adding this on app startup after `CometChat.init()` completes. + + + +```typescript +let listenerID: string = "UNIQUE_LISTENER_ID"; CometChat.addConnectionListener( listenerID, new CometChat.ConnectionListener({ @@ -40,9 +81,9 @@ CometChat.addConnectionListener( - -```typescript -let listenerID: string = "UNIQUE_LISTENER_ID"; + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; CometChat.addConnectionListener( listenerID, new CometChat.ConnectionListener({ @@ -63,33 +104,25 @@ CometChat.addConnectionListener( - - -We recommend you to add the Connection Listener in your method on app startup, preferably in the index.js file. Once you have successfully initialized CometChat. - - - -You can also get the current connection status by using `getConnectionStatus` property provided by CometChat SDK - - - -```javascript -var connectionStatus = CometChat.getConnectionStatus(); -``` - - - - -```typescript -var connectionStatus: string = CometChat.getConnectionStatus(); -``` + +Always remove connection listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + - - - - -The `CometChat.getConnectionStatus` method will return either of the below 3 values: +--- -1. connecting -2. connected -3. disconnected +## Next Steps + + + + Manually manage WebSocket connections + + + Listen for login and logout events + + + Complete reference for all SDK listeners + + + Install and initialize the CometChat SDK + + diff --git a/sdk/javascript/create-group.mdx b/sdk/javascript/create-group.mdx index 29eed3de5..f6cadc21d 100644 --- a/sdk/javascript/create-group.mdx +++ b/sdk/javascript/create-group.mdx @@ -1,37 +1,66 @@ --- title: "Create A Group" +sidebarTitle: "Create Group" +description: "Create public, private, or password-protected groups and optionally add members during creation using the CometChat JavaScript SDK." --- + +```javascript +// Create a group +let group = new CometChat.Group("GUID", "Group Name", CometChat.GROUP_TYPE.PUBLIC, ""); +let createdGroup = await CometChat.createGroup(group); -## Create a Group +// Create group with members +let members = [new CometChat.GroupMember("UID", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT)]; +let result = await CometChat.createGroupWithMembers(group, members, []); +``` -*In other words, as a logged-in user, how do I create a public, private or password-protected group?* +**Group types:** `PUBLIC` | `PASSWORD` | `PRIVATE` +**Member scopes:** `ADMIN` | `MODERATOR` | `PARTICIPANT` + -You can create a group using `createGroup()` method. This method takes a `Group` object as input. +Create groups for multi-user conversations. You can create a group on its own with `createGroup()`, or create one and add members in a single call with `createGroupWithMembers()`. See the [Group Class](#group-class) reference at the bottom for all available fields. -To create an object of `Group` class, you can use either of the below two constructors: +## Create a Group -1. `new Group(String GUID, String name, String groupType, String password)` -2. `new Group(String GUID, String name, String groupType, String password, String icon, String description)` +Use `createGroup()` to create a new group. Pass a [`Group`](/sdk/reference/entities#group) object with the group details. -The `groupType` needs to be either of the below 3 values: +| Group Type | Constant | Description | +| --- | --- | --- | +| Public | `CometChat.GROUP_TYPE.PUBLIC` | Any user can join | +| Password | `CometChat.GROUP_TYPE.PASSWORD` | Users must provide the correct password | +| Private | `CometChat.GROUP_TYPE.PRIVATE` | Users must be added by an admin/moderator | -1.`CometChat.GROUP_TYPE.PUBLIC` + + +```typescript +const GUID: string = "GUID"; +const groupName: string = "Hello Group!"; +const groupType: string = CometChat.GROUP_TYPE.PUBLIC; +const password: string = ""; + +const group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType, password); -2.`CometChat.GROUP_TYPE.PASSWORD` +CometChat.createGroup(group).then( + (group: CometChat.Group) => { + console.log("Group created successfully:", group); + }, (error: CometChat.CometChatException) => { + console.log("Group creation failed with exception:", error); + } +); +``` -3.`CometChat.GROUP_TYPE.PRIVATE` + - - + ```javascript -var GUID = "GUID"; -var groupName = "Hello Group!"; -var groupType = CometChat.GROUP_TYPE.PUBLIC; -var password = ""; +const GUID = "GUID"; +const groupName = "Hello Group!"; +const groupType = CometChat.GROUP_TYPE.PUBLIC; +const password = ""; -var group = new CometChat.Group(GUID, groupName, groupType, password); +const group = new CometChat.Group(GUID, groupName, groupType, password); CometChat.createGroup(group).then( group => { @@ -44,35 +73,32 @@ CometChat.createGroup(group).then( - -```typescript -var GUID: string = "GUID"; -var groupName: string = "Hello Group!"; -var groupType: string = CometChat.GROUP_TYPE.PUBLIC; -var password: string = ""; - -var group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType, password); - -CometChat.createGroup(group).then( - (group: CometChat.Group) => { - console.log("Group created successfully:", group); - }, (error: CometChat.CometChatException) => { - console.log("Group creation failed with exception:", error); - } -); + +```javascript +const GUID = "GUID"; +const groupName = "Hello Group!"; +const groupType = CometChat.GROUP_TYPE.PUBLIC; +const password = ""; + +const group = new CometChat.Group(GUID, groupName, groupType, password); + +try { + const createdGroup = await CometChat.createGroup(group); + console.log("Group created successfully:", createdGroup); +} catch (error) { + console.log("Group creation failed with exception:", error); +} ``` -The createGroup() method takes the following parameters: +| Parameter | Description | +| --------- | ----------- | +| `group` | An instance of [`Group`](/sdk/reference/entities#group) class | -| Parameter | Description | -| --------- | ---------------------------- | -| `group` | An instance of `Group` class | - -After successful creation of the group, you will receive an instance of `Group` class which contains all the information about the particular group. +On success, returns a [`Group`](/sdk/reference/entities#group) object with the created group's details. @@ -80,28 +106,44 @@ GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and oth -## Add members while creating a group +## Add Members While Creating a Group -You can create a group and add members at the same time using the `createGroupWithMembers()` method. This method takes the `Group` Object, Array of `Group Member` Object to be added & Array of `UIDs` to be banned. +Use `createGroupWithMembers()` to create a group and add members in one operation. -To create an object of `Group` class, you can use either of the below two constructors: +Parameters: +- `group` — The [`Group`](/sdk/reference/entities#group) object +- `members` — Array of [`GroupMember`](/sdk/reference/entities#groupmember) objects to add +- `bannedMembers` — Array of UIDs to ban (can be empty) -1. `new Group(String GUID, String name, String groupType, String password)` -2. `new Group(String GUID, String name, String groupType, String password, String icon, String description)` +Create a [`GroupMember`](/sdk/reference/entities#groupmember) with: `new CometChat.GroupMember(UID, scope)` -The `groupType` needs to be either of the below 3 values: + + +```typescript +let GUID: string = "cometchat-guid-11"; +let UID: string = "cometchat-uid-1"; +let groupName: string = "Hello Group!"; +let groupType: string = CometChat.GROUP_TYPE.PUBLIC; -1. `CometChat.GROUP_TYPE.PUBLIC` -2. `CometChat.GROUP_TYPE.PASSWORD` -3. `CometChat.GROUP_TYPE.PRIVATE` +let group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType); +let members: Array = [ +new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT) +]; +let banMembers: Array = ["cometchat-uid-2"]; -To create an object of `Group Member` class, you can use the below constructor: +CometChat.createGroupWithMembers(group, members, banMembers).then( + (response: Object) => { + console.log("Group created successfully", response); + }, (error: CometChat.CometChatException) => { + console.log("Some error occured while creating group", error) + } +); +``` -* new CometChat.GroupMember(String UID, String scope) + - -```js +```javascript let GUID = "cometchat-guid-11"; let UID = "cometchat-uid-1"; let groupName = "Hello Group!"; @@ -124,36 +166,39 @@ CometChat.createGroupWithMembers(group, members, banMembers).then( - -```typescript -let GUID: string = "cometchat-guid-11"; -let UID: string = "cometchat-uid-1"; -let groupName: string = "Hello Group!"; -let groupType: string = CometChat.GROUP_TYPE.PUBLIC; + +```javascript +let GUID = "cometchat-guid-11"; +let UID = "cometchat-uid-1"; +let groupName = "Hello Group!"; +let groupType = CometChat.GROUP_TYPE.PUBLIC; -let group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType); -let members: Array = [ -new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT) +let group = new CometChat.Group(GUID, groupName, groupType); +let members = [ + new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT) ]; -let banMembers: Array = ["cometchat-uid-2"]; +let banMembers = ["cometchat-uid-2"]; -CometChat.createGroupWithMembers(group, members, banMembers).then( - (response: Object) => { - console.log("Group created successfully", response); - }, (error: CometChat.CometChatException) => { - console.log("Some error occured while creating group", error) - } -); +try { + const response = await CometChat.createGroupWithMembers(group, members, banMembers); + console.log("Group created successfully", response); +} catch (error) { + console.log("Some error occured while creating group", error); +} ``` -This method returns an Object which has two keys: `group` & `members` . The group key has the Group Object which contains all the information of the group which is created. The members key has the `UID` of the users and the value will either be `success` or an `error` message describing why the operation to add/ban the user failed. +Returns an object with two keys: +- `group` — The created [`Group`](/sdk/reference/entities#group) object +- `members` — Object with UIDs as keys and `"success"` or error message as values ## Group Class +The [`Group`](/sdk/reference/entities#group) object has the following fields. Fields marked "Yes" in the Editable column can be modified after creation using `updateGroup()`. + | Field | Editable | Information | | ------------ | --------------------------------------------------------------- | ------------------------------------------------------------------------- | | guid | Needs to be specified at group creation. Cannot be edited later | A unique identifier for a group | @@ -171,3 +216,23 @@ This method returns an Object which has two keys: `group` & `members` . The grou | scope | Yes | Scope of the logged in user. Can be: 1. Admin 2. Moderator 3. Participant | | membersCount | No | The number of members in the groups | | tags | Yes | A list of tags to identify specific groups. | + + +--- + +## Next Steps + + + + Join public, private, or password-protected groups + + + Add users to an existing group + + + Fetch and filter group lists + + + Overview of all group management features + + diff --git a/sdk/javascript/custom-css.mdx b/sdk/javascript/custom-css.mdx index 56698ccf3..59d7637e0 100644 --- a/sdk/javascript/custom-css.mdx +++ b/sdk/javascript/custom-css.mdx @@ -1,39 +1,63 @@ --- title: "Custom CSS" +sidebarTitle: "Custom CSS" +description: "Customize the CometChat calling UI with custom CSS classes for buttons, video containers, name labels, and grid layouts." --- + +```css +/* Key CSS classes for call UI customization */ +.cc-main-container { } /* Outermost call container */ +.cc-bottom-buttons-container { } /* Bottom action buttons bar */ +.cc-name-label { } /* User name text */ +.cc-video-container { } /* Video feed container */ +.cc-end-call-icon-container { } /* End call button */ +.cc-audio-icon-container { } /* Audio toggle button */ +.cc-video-icon-container { } /* Video toggle button */ +.cc-screen-share-icon-container { } /* Screen share button */ +``` -Passing custom CSS allows you to personalize and enhance the user interface of the call screen. +**Modes:** `DEFAULT` | `TILE` | `SPOTLIGHT` + -## Common CSS Classes +The CometChat calling UI exposes CSS classes you can target to style buttons, video containers, labels, and layouts. Your CSS is applied globally — any styles you write for these classes will affect the call UI across all modes (DEFAULT, TILE, SPOTLIGHT). -There are a few common classes used for different modes in the call screen +Before you begin, make sure you've set up [Ringing](/sdk/javascript/default-call) or [Call Session](/sdk/javascript/direct-call). -1. **cc-main-container** - The outermost component of the calling component is represented by a white border in the screenshots above, indicating that it acts as a container for other components. + +Only use CSS classes documented here — targeting undocumented classes may cause UI issues. Avoid resizing the grid container to prevent layout distortions. + -2. **cc-bottom-buttons-container** - The container located at the very bottom of the interface houses various action buttons, such as the mute/unmute audio and video, end call, settings icon, and participants icon, among others. It is represented by the red border in above screenshot. +## CSS Class Reference -3. **cc-name-label** - This class is passed in user name text container in all modes. It is represented by green border in the above screenshots. +### Layout Containers -4. **cc-video-container** - This class is passed to the video container in all modes. It is represented by orange border in the above screenshots. +| Class | Description | +| ----- | ----------- | +| `cc-main-container` | Outermost container for the calling component | +| `cc-video-container` | Video feed container | +| `cc-name-label` | User name text container | -## Bottom Buttons +### Action Buttons -1. **cc-bottom-buttons-container** - This is the container of all the buttons in calling. -2. **cc-bottom-buttons-icon-container** - This is the div of every button in the button bar. +| Class | Description | +| ----- | ----------- | +| `cc-bottom-buttons-container` | Bottom bar containing action buttons (mute, end call, etc.) | +| `cc-bottom-buttons-icon-container` | Individual button wrapper | +| `cc-audio-icon-container` | Audio toggle button | +| `cc-audio-icon-container-muted` | Audio button (muted state) | +| `cc-video-icon-container` | Video toggle button | +| `cc-video-icon-container-muted` | Video button (muted state) | +| `cc-screen-share-icon-container` | Screen share button | +| `cc-switch-video-icon-container` | Switch camera button | +| `cc-end-call-icon-container` | End call button | -### Individual bottom buttons CSS classes +## Examples -* `cc-audio-icon-container` -* `cc-audio-icon-container-muted` -* `cc-video-icon-container` -* `cc-video-icon-container-muted` -* `cc-screen-share-icon-container` -* `cc-switch-video-icon-container` -* `cc-end-call-icon-container` +### Basic Styling -### **Example** +This example applies colored backgrounds to buttons and dotted borders to containers to visualize the layout structure: @@ -77,7 +101,7 @@ There are a few common classes used for different modes in the call screen -The above example results in the below output:- +Here's how it looks across the three call modes: **Mode: `DEFAULT`** @@ -153,20 +177,28 @@ The above example results in the below output:- -The above example results in the below output:- +### Polished UI + +This example shows a more refined customization with rounded buttons, custom spacing, and a patterned video background: +--- -### Guidelines for Customizing the Grid Layout - -* **CSS Classes:** - - * Please ensure that you only apply CSS classes specified in this documentation. Introducing CSS classes not covered here may cause unexpected UI issues. - -* **Grid Container Resizing:** - - * Avoid resizing the grid container. Altering the grid container’s dimensions can negatively impact the grid layout, leading to undesirable visual distortions. - -By following these recommendations, you can maintain a stable and visually consistent grid layout. +## Next Steps + + + + Customize video call layout and participant tiles + + + Apply blur or custom image backgrounds during calls + + + Enable screen sharing and presentation mode + + + Overview of all calling features and approaches + + diff --git a/sdk/javascript/default-call.mdx b/sdk/javascript/default-call.mdx index 180de4d73..707481bac 100644 --- a/sdk/javascript/default-call.mdx +++ b/sdk/javascript/default-call.mdx @@ -1,7 +1,36 @@ --- title: "Ringing" +sidebarTitle: "Ringing" +description: "Implement a complete calling workflow with ringing, incoming/outgoing call UI, accept/reject/cancel functionality, and call session management." --- +{/* TL;DR for Agents and Quick Reference */} + + +```javascript +let sessionId = "SESSION_ID"; + +// Initiate a call +const call = new CometChat.Call("UID", CometChat.CALL_TYPE.VIDEO, CometChat.RECEIVER_TYPE.USER); +await CometChat.initiateCall(call); + +// Listen for call events +CometChat.addCallListener("ID", new CometChat.CallListener({ + onIncomingCallReceived: (call) => { /* show incoming UI */ }, + onOutgoingCallAccepted: (call) => { /* start session */ }, + onOutgoingCallRejected: (call) => { /* dismiss UI */ }, + onIncomingCallCancelled: (call) => { /* dismiss UI */ } +})); + +// Accept / Reject / Cancel +await CometChat.acceptCall(sessionId); +await CometChat.rejectCall(sessionId, CometChat.CALL_STATUS.REJECTED); +await CometChat.rejectCall(sessionId, CometChat.CALL_STATUS.CANCELLED); +``` + +**Flow:** Initiate → Receiver notified → Accept/Reject → Start session + + ## Overview This section explains how to implement a complete calling workflow with ringing functionality, including incoming/outgoing call UI, call acceptance, rejection, and cancellation. Previously known as **Default Calling**. @@ -108,40 +137,36 @@ CometChat.initiateCall(call).then( | `receiverType` | `CometChat.RECEIVER_TYPE.USER` or `CometChat.RECEIVER_TYPE.GROUP` | | `callType` | `CometChat.CALL_TYPE.AUDIO` or `CometChat.CALL_TYPE.VIDEO` | -On success, a `Call` object is returned containing the call details including a unique `sessionId` required for starting the call session. +On success, a [`Call`](/sdk/reference/messages#call) object is returned containing the call details including a unique `sessionId` required for starting the call session. + ## Call Listeners Register the `CallListener` to receive real-time call events. Each listener requires a unique `listenerId` string to prevent duplicate registrations and enable targeted removal. - -```javascript -const listenerId = "UNIQUE_LISTENER_ID"; + +```typescript +const listenerId: string = "UNIQUE_LISTENER_ID"; // Register listener CometChat.addCallListener( listenerId, new CometChat.CallListener({ - onIncomingCallReceived: (call) => { + onIncomingCallReceived: (call: CometChat.Call) => { console.log("Incoming call:", call); - // Show incoming call UI }, - onOutgoingCallAccepted: (call) => { + onOutgoingCallAccepted: (call: CometChat.Call) => { console.log("Outgoing call accepted:", call); - // Receiver accepted, start the call session }, - onOutgoingCallRejected: (call) => { + onOutgoingCallRejected: (call: CometChat.Call) => { console.log("Outgoing call rejected:", call); - // Receiver rejected, dismiss outgoing call UI }, - onIncomingCallCancelled: (call) => { + onIncomingCallCancelled: (call: CometChat.Call) => { console.log("Incoming call cancelled:", call); - // Caller cancelled, dismiss incoming call UI }, - onCallEndedMessageReceived: (call) => { + onCallEndedMessageReceived: (call: CometChat.Call) => { console.log("Call ended message:", call); - // Call ended by remote participant } }) ); @@ -150,28 +175,33 @@ CometChat.addCallListener( CometChat.removeCallListener(listenerId); ``` - -```typescript -const listenerId: string = "UNIQUE_LISTENER_ID"; + +```javascript +const listenerId = "UNIQUE_LISTENER_ID"; // Register listener CometChat.addCallListener( listenerId, new CometChat.CallListener({ - onIncomingCallReceived: (call: CometChat.Call) => { + onIncomingCallReceived: (call) => { console.log("Incoming call:", call); + // Show incoming call UI }, - onOutgoingCallAccepted: (call: CometChat.Call) => { + onOutgoingCallAccepted: (call) => { console.log("Outgoing call accepted:", call); + // Receiver accepted, start the call session }, - onOutgoingCallRejected: (call: CometChat.Call) => { + onOutgoingCallRejected: (call) => { console.log("Outgoing call rejected:", call); + // Receiver rejected, dismiss outgoing call UI }, - onIncomingCallCancelled: (call: CometChat.Call) => { + onIncomingCallCancelled: (call) => { console.log("Incoming call cancelled:", call); + // Caller cancelled, dismiss incoming call UI }, - onCallEndedMessageReceived: (call: CometChat.Call) => { + onCallEndedMessageReceived: (call) => { console.log("Call ended message:", call); + // Call ended by remote participant } }) ); @@ -192,11 +222,27 @@ CometChat.removeCallListener(listenerId); | `onIncomingCallCancelled(call)` | Invoked on the receiver's device when the caller cancels before answering. Dismiss incoming call UI here. | | `onCallEndedMessageReceived(call)` | Invoked when a call ends. The `call` contains final status and duration. Update call history here. | +All call listener callbacks receive a [`Call`](/sdk/reference/messages#call) object. + ## Accept Call When an incoming call is received via `onIncomingCallReceived()`, use `acceptCall()` to accept it. On success, start the call session. + +```typescript +const sessionId: string = call.getSessionId(); // From onIncomingCallReceived + +CometChat.acceptCall(sessionId).then( + (call: CometChat.Call) => { + console.log("Call accepted:", call); + }, + (error: CometChat.CometChatException) => { + console.log("Accept call failed:", error); + } +); +``` + ```javascript const sessionId = call.getSessionId(); // From onIncomingCallReceived @@ -213,27 +259,28 @@ CometChat.acceptCall(sessionId).then( ); ``` + + +## Reject Call + +Use `rejectCall()` to reject an incoming call. Set the status to `CALL_STATUS_REJECTED`. + + ```typescript -const sessionId: string = call.getSessionId(); // From onIncomingCallReceived +const sessionId: string = call.getSessionId(); +const status: string = CometChat.CALL_STATUS.REJECTED; -CometChat.acceptCall(sessionId).then( +CometChat.rejectCall(sessionId, status).then( (call: CometChat.Call) => { - console.log("Call accepted:", call); + console.log("Call rejected:", call); }, (error: CometChat.CometChatException) => { - console.log("Accept call failed:", error); + console.log("Reject call failed:", error); } ); ``` - - -## Reject Call - -Use `rejectCall()` to reject an incoming call. Set the status to `CALL_STATUS_REJECTED`. - - ```javascript const sessionId = call.getSessionId(); @@ -250,28 +297,28 @@ CometChat.rejectCall(sessionId, status).then( ); ``` + + +## Cancel Call + +The caller can cancel an outgoing call before it's answered using `rejectCall()` with status `CALL_STATUS_CANCELLED`. + + ```typescript const sessionId: string = call.getSessionId(); -const status: string = CometChat.CALL_STATUS.REJECTED; +const status: string = CometChat.CALL_STATUS.CANCELLED; CometChat.rejectCall(sessionId, status).then( (call: CometChat.Call) => { - console.log("Call rejected:", call); + console.log("Call cancelled:", call); }, (error: CometChat.CometChatException) => { - console.log("Reject call failed:", error); + console.log("Cancel call failed:", error); } ); ``` - - -## Cancel Call - -The caller can cancel an outgoing call before it's answered using `rejectCall()` with status `CALL_STATUS_CANCELLED`. - - ```javascript const sessionId = call.getSessionId(); @@ -288,21 +335,6 @@ CometChat.rejectCall(sessionId, status).then( ); ``` - -```typescript -const sessionId: string = call.getSessionId(); -const status: string = CometChat.CALL_STATUS.CANCELLED; - -CometChat.rejectCall(sessionId, status).then( - (call: CometChat.Call) => { - console.log("Call cancelled:", call); - }, - (error: CometChat.CometChatException) => { - console.log("Cancel call failed:", error); - } -); -``` - ## Start Call Session @@ -314,15 +346,15 @@ Once the call is accepted, both participants need to start the call session. **Receiver flow:** In the `acceptCall()` success callback, generate a token and start the session. - -```javascript -const sessionId = call.getSessionId(); + +```typescript +const sessionId: string = call.getSessionId(); const loggedInUser = await CometChat.getLoggedinUser(); -const authToken = loggedInUser.getAuthToken(); +const authToken: string = loggedInUser.getAuthToken(); // Step 1: Generate call token CometChatCalls.generateToken(sessionId, authToken).then( - (callToken) => { + (callToken: any) => { // Step 2: Configure call settings const callListener = new CometChatCalls.OngoingCallListener({ onCallEnded: () => { @@ -337,12 +369,12 @@ CometChatCalls.generateToken(sessionId, authToken).then( CometChatCalls.endSession(); // Close calling screen }, - (error) => console.log("End call failed:", error) + (error: CometChat.CometChatException) => console.log("End call failed:", error) ); }, - onUserJoined: (user) => console.log("User joined:", user), - onUserLeft: (user) => console.log("User left:", user), - onError: (error) => console.log("Call error:", error) + onUserJoined: (user: any) => console.log("User joined:", user), + onUserLeft: (user: any) => console.log("User left:", user), + onError: (error: any) => console.log("Call error:", error) }); const callSettings = new CometChatCalls.CallSettingsBuilder() @@ -352,24 +384,24 @@ CometChatCalls.generateToken(sessionId, authToken).then( .build(); // Step 3: Start the session - const htmlElement = document.getElementById("call-container"); + const htmlElement = document.getElementById("call-container") as HTMLElement; CometChatCalls.startSession(callToken, callSettings, htmlElement); }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Token generation failed:", error); } ); ``` - -```typescript -const sessionId: string = call.getSessionId(); + +```javascript +const sessionId = call.getSessionId(); const loggedInUser = await CometChat.getLoggedinUser(); -const authToken: string = loggedInUser.getAuthToken(); +const authToken = loggedInUser.getAuthToken(); // Step 1: Generate call token CometChatCalls.generateToken(sessionId, authToken).then( - (callToken: any) => { + (callToken) => { // Step 2: Configure call settings const callListener = new CometChatCalls.OngoingCallListener({ onCallEnded: () => { @@ -384,12 +416,12 @@ CometChatCalls.generateToken(sessionId, authToken).then( CometChatCalls.endSession(); // Close calling screen }, - (error: CometChat.CometChatException) => console.log("End call failed:", error) + (error) => console.log("End call failed:", error) ); }, - onUserJoined: (user: any) => console.log("User joined:", user), - onUserLeft: (user: any) => console.log("User left:", user), - onError: (error: any) => console.log("Call error:", error) + onUserJoined: (user) => console.log("User joined:", user), + onUserLeft: (user) => console.log("User left:", user), + onError: (error) => console.log("Call error:", error) }); const callSettings = new CometChatCalls.CallSettingsBuilder() @@ -399,10 +431,10 @@ CometChatCalls.generateToken(sessionId, authToken).then( .build(); // Step 3: Start the session - const htmlElement = document.getElementById("call-container") as HTMLElement; + const htmlElement = document.getElementById("call-container"); CometChatCalls.startSession(callToken, callSettings, htmlElement); }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Token generation failed:", error); } ); @@ -421,34 +453,34 @@ To end an active call in the ringing flow, the process differs based on who ends When the user clicks the end call button, the `onCallEndButtonPressed()` callback is triggered. Inside this callback, call `CometChat.endCall()` to notify other participants. On success, call `CometChat.clearActiveCall()` and `CometChatCalls.endSession()` to release resources. - -```javascript + +```typescript onCallEndButtonPressed: () => { CometChat.endCall(sessionId).then( - (call) => { + (call: CometChat.Call) => { console.log("Call ended successfully"); CometChat.clearActiveCall(); CometChatCalls.endSession(); // Close the calling screen }, - (error) => { + (error: CometChat.CometChatException) => { console.log("End call failed:", error); } ); } ``` - -```typescript + +```javascript onCallEndButtonPressed: () => { CometChat.endCall(sessionId).then( - (call: CometChat.Call) => { + (call) => { console.log("Call ended successfully"); CometChat.clearActiveCall(); CometChatCalls.endSession(); // Close the calling screen }, - (error: CometChat.CometChatException) => { + (error) => { console.log("End call failed:", error); } ); @@ -460,8 +492,8 @@ onCallEndButtonPressed: () => { **Remote participant** (receives `onCallEnded()` callback): - -```javascript + +```typescript onCallEnded: () => { CometChat.clearActiveCall(); CometChatCalls.endSession(); @@ -469,8 +501,8 @@ onCallEnded: () => { } ``` - -```typescript + +```javascript onCallEnded: () => { CometChat.clearActiveCall(); CometChatCalls.endSession(); @@ -487,34 +519,58 @@ For more details, see the [End Call Session](/sdk/javascript/direct-call#end-cal If the receiver is already on another call, you can reject the incoming call with `CALL_STATUS_BUSY` status. - -```javascript -const sessionId = call.getSessionId(); -const status = CometChat.CALL_STATUS.BUSY; + +```typescript +const sessionId: string = call.getSessionId(); +const status: string = CometChat.CALL_STATUS.BUSY; CometChat.rejectCall(sessionId, status).then( - (call) => { + (call: CometChat.Call) => { console.log("Busy status sent:", call); }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Busy rejection failed:", error); } ); ``` - -```typescript -const sessionId: string = call.getSessionId(); -const status: string = CometChat.CALL_STATUS.BUSY; + +```javascript +const sessionId = call.getSessionId(); +const status = CometChat.CALL_STATUS.BUSY; CometChat.rejectCall(sessionId, status).then( - (call: CometChat.Call) => { + (call) => { console.log("Busy status sent:", call); }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Busy rejection failed:", error); } ); ``` + + + +Always remove call listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +--- + +## Next Steps + + + + Manage call sessions, tokens, and settings + + + Retrieve and display call history + + + Record audio and video calls + + + Install and initialize the Calls SDK + + diff --git a/sdk/javascript/delete-conversation.mdx b/sdk/javascript/delete-conversation.mdx index b91669f6a..3b99dce50 100644 --- a/sdk/javascript/delete-conversation.mdx +++ b/sdk/javascript/delete-conversation.mdx @@ -1,12 +1,28 @@ --- -title: "Delete A Conversation" +title: "Delete Conversation" +sidebarTitle: "Delete Conversation" +description: "Delete user or group conversations using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Delete user conversation +await CometChat.deleteConversation("UID", "user"); + +// Delete group conversation +await CometChat.deleteConversation("GUID", "group"); +``` + +**Note:** Deletes only for the logged-in user. Use [REST API](https://api-explorer.cometchat.com/reference/resets-user-conversation) to delete for all participants. + -In case you want to delete a conversation, you can use the `deleteConversation()` method. + +This operation is irreversible. Deleted conversations cannot be recovered for the logged-in user. + -This method takes two parameters. The unique id (UID/GUID) of the conversation to be deleted & the type (user/group) of conversation to be deleted. +Use `deleteConversation()` to delete a conversation for the logged-in user. @@ -39,7 +55,7 @@ CometChat.deleteConversation(GUID, type).then( - + ```typescript let UID: string = "UID"; let type: string = "user"; @@ -71,11 +87,30 @@ CometChat.deleteConversation(GUID, type).then( -This method deletes the conversation only for the logged-in user. To delete a conversation for all the users of the conversation, please refer to our REST API documentation [here](https://api-explorer.cometchat.com/reference/resets-user-conversation). +This deletes the conversation only for the logged-in user. To delete for all participants, use the [REST API](https://api-explorer.cometchat.com/reference/resets-user-conversation). -The `deleteConversation()` method takes the following parameters: +| Parameter | Description | Required | +| --- | --- | --- | +| conversationWith | UID or GUID of the conversation to delete | Yes | +| conversationType | `user` or `group` | Yes | + +On success, returns a confirmation message string. + +--- -| Parameter | Description | Required | -| ---------------- | --------------------------------------------------------------------------------- | -------- | -| conversationWith | `UID` of the user or `GUID` of the group whose conversation you want to delete. | YES | -| conversationType | The type of conversation you want to delete . It can be either `user` or `group`. | YES | +## Next Steps + + + + Fetch and filter conversation lists + + + Delete individual messages from conversations + + + Show when users are typing in conversations + + + Track message delivery and read status + + diff --git a/sdk/javascript/delete-group.mdx b/sdk/javascript/delete-group.mdx index 54bcf4137..14fbd6b8d 100644 --- a/sdk/javascript/delete-group.mdx +++ b/sdk/javascript/delete-group.mdx @@ -1,17 +1,48 @@ --- title: "Delete A Group" +sidebarTitle: "Delete Group" +description: "Delete a group permanently using the CometChat JavaScript SDK. Only group admins can perform this operation." --- + +```javascript +// Delete a group (owner only) +await CometChat.deleteGroup("GUID"); +``` + +**Requirement:** Logged-in user must be the owner of the group. + + +Permanently delete a group and all its messages. Only the group owner can perform this operation. + + +This operation is irreversible. Deleted groups and their messages cannot be recovered. + ## Delete a Group -To delete a group you need to use the `deleteGroup()` method. The user must be an `Admin` of the group they are trying to delete. +Use `deleteGroup()` with the group's GUID. - + +```typescript +const GUID: string = "GUID"; + +CometChat.deleteGroup(GUID).then( + (response: boolean) => { + console.log("Group deleted successfully:", response); + }, (error: CometChat.CometChatException) => { + console.log("Group delete failed with exception:", error); + } +); +``` + + + + ```javascript -var GUID = "GUID"; +const GUID = "GUID"; CometChat.deleteGroup(GUID).then( response => { @@ -24,17 +55,16 @@ response => { - -```typescript -var GUID: string = "GUID"; + +```javascript +const GUID = "GUID"; -CometChat.deleteGroup(GUID).then( - (response: boolean) => { - console.log("Group deleted successfully:", response); - }, (error: CometChat.CometChatException) => { - console.log("Group delete failed with exception:", error); - } -); +try { + const response = await CometChat.deleteGroup(GUID); + console.log("Group deleted successfully:", response); +} catch (error) { + console.log("Group delete failed with exception:", error); +} ``` @@ -46,3 +76,25 @@ The `deleteGroup()` method takes the following parameters: | Parameter | Description | | --------- | ---------------------------------------------- | | `GUID` | The GUID of the group you would like to delete | + +On success, the method resolves with `true` (boolean). + + +--- + +## Next Steps + + + + Update group name, icon, description, and metadata + + + Leave a group you are a member of + + + Create public, private, or password-protected groups + + + Overview of all group management features + + diff --git a/sdk/javascript/delete-message.mdx b/sdk/javascript/delete-message.mdx index d84b07614..74c8c90bc 100644 --- a/sdk/javascript/delete-message.mdx +++ b/sdk/javascript/delete-message.mdx @@ -1,22 +1,60 @@ --- -title: "Delete A Message" +title: "Delete Message" +sidebarTitle: "Delete Message" +description: "Delete messages using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +let messageId = "MESSAGE_ID"; -While [deleting a message](/sdk/javascript/delete-message#delete-a-message) is straightforward, receiving events for deleted messages with CometChat has two parts: +// Delete a message +await CometChat.deleteMessage(messageId); -1. Adding a listener to receive [real-time message deletes](/sdk/javascript/delete-message#real-time-message-delete-events) when your app is running. -2. Calling a method to retrieve [missed message deletes](/sdk/javascript/delete-message#missed-message-delete-events) when your app was not running. +// Listen for real-time deletions +CometChat.addMessageListener("ID", new CometChat.MessageListener({ + onMessageDeleted: (message) => { + console.log("Deleted:", message.getId(), message.getDeletedAt()); + } +})); +``` -## Delete a Message +**Who can delete:** Message sender, Group admin, Group moderator +**Deleted fields:** `deletedAt` (timestamp), `deletedBy` (user who deleted) + + + +This operation is irreversible. Deleted messages cannot be recovered. + -*In other words, as a sender, how do I delete a message?* +Deleting a message is straightforward. Receiving delete events has two parts: -In case you have to delete a message, you can use the `deleteMessage()` method. This method takes the message ID of the message to be deleted. +1. Adding a listener for [real-time deletes](#real-time-message-delete-events) when your app is running +2. Fetching [missed deletes](#missed-message-delete-events) when your app was offline + +## Delete a Message + +Use `deleteMessage()` with the message ID. - + +```typescript +let messageId: string = "1"; + +CometChat.deleteMessage(messageId).then( + (message: CometChat.BaseMessage) => { + console.log("Message deleted", message); + }, (error: CometChat.CometChatException) => { + console.log("Message delete failed with error:", error); + } +); +``` + + + + ```javascript let messageId = "ID_OF_THE_MESSAGE_YOU_WANT_TO_DELETE"; @@ -31,24 +69,32 @@ message => { - -```typescript -let messageId: number = 1; + +```javascript +let messageId = "ID_OF_THE_MESSAGE_YOU_WANT_TO_DELETE"; -CometChat.deleteMessage(messageId).then( - (message: CometChat.BaseMessage) => { - console.log("Message deleted", message); - }, (error: CometChat.CometChatException) => { - console.log("Message delete failed with error:", error); - } -); +try { + const message = await CometChat.deleteMessage(messageId); + console.log("Message deleted", message); +} catch (error) { + console.log("Message delete failed with error:", error); +} ``` -Once the message is deleted, In the `onSuccess()` callback, you get an object of the `BaseMessage` class, with the `deletedAt` field set with the timestamp of the time the message was deleted. Also, the `deletedBy` field is set. These two fields can be used to identify if the message is deleted while iterating through a list of messages. +The deleted message object is returned with `deletedAt` (timestamp) and `deletedBy` (UID of deleter) fields set. + +The `deleteMessage()` method returns a [`BaseMessage`](/sdk/reference/messages#basemessage) object. + +Relevant fields to access on the returned message: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| deletedAt | `getDeletedAt()` | `number` | Timestamp when the message was deleted | +| deletedBy | `getDeletedBy()` | `string` | UID of the user who deleted the message | By default, CometChat allows certain roles to delete a message. @@ -61,27 +107,9 @@ By default, CometChat allows certain roles to delete a message. ## Real-time Message Delete Events -*In other words, as a recipient, how do I know when someone deletes a message when my app is running?* - -In order to receive real-time events for a message being deleted, you need to override the `onMessageDeleted()` method of the `MessageListener` class. +Use `onMessageDeleted` in `MessageListener` to receive real-time delete events. - -```javascript -let listenerID = "UNIQUE_LISTENER_ID"; - -CometChat.addMessageListener( -listenerID, -new CometChat.MessageListener({ - onMessageDeleted: message => { - console.log("Deleted Message", message); - } -}) -); -``` - - - ```typescript let listenerID: string = "UNIQUE_LISTENER_ID"; @@ -98,23 +126,66 @@ CometChat.addMessageListener( + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; + +CometChat.addMessageListener( +listenerID, +new CometChat.MessageListener({ + onMessageDeleted: message => { + console.log("Deleted Message", message); + } +}) +); +``` + + + -## Missed Message Delete Events +The `onMessageDeleted` callback receives a [`BaseMessage`](/sdk/reference/messages#basemessage) object with the `deletedAt` and `deletedBy` fields set. -*In other words, as a recipient, how do I know if someone deleted a message when my app was not running?* +Relevant fields to access on the returned message: -When you retrieve the list of previous messages, for the messages that were deleted, the `deletedAt` and the `deletedBy` fields will be set. Also, for example, of the total number of messages for a conversation are 100, and the message with message ID 50 was deleted. Now the message with id 50 will have the `deletedAt` and the `deletedBy` fields set whenever it is pulled from the history. Also, the 101st message will be an Actionc message informing you that the message with id 50 has been deleted. +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| deletedAt | `getDeletedAt()` | `number` | Timestamp when the message was deleted | +| deletedBy | `getDeletedBy()` | `string` | UID of the user who deleted the message | + +## Missed Message Delete Events -For the message deleted event, in the `Action` object received, the following fields can help you get the relevant information- +When fetching message history, deleted messages have `deletedAt` and `deletedBy` fields set. Additionally, an [`Action`](/sdk/reference/messages#action) message is created when a message is deleted. -1. `action` - `deleted` -2. `actionOn` - Updated message object which was deleted. -3. `actionBy` - User object containing the details of the user who has deleted the message. -4. `actionFor` - User/group object having the details of the receiver to which the message was sent. +The [`Action`](/sdk/reference/messages#action) object contains: +- `action` — `deleted` +- `actionOn` — Deleted message object +- `actionBy` — User who deleted the message +- `actionFor` — Receiver (User/Group) +You must be the message sender or a group admin/moderator to delete a message. + -In order to edit or delete a message you need to be either the sender of the message or the admin/moderator of the group in which the message was sent. + +Always remove message listeners when they're no longer needed to prevent memory leaks. + - +--- + +## Next Steps + + + + Edit sent messages in conversations + + + Send text, media, and custom messages + + + Listen for incoming messages in real-time + + + Report inappropriate messages + + diff --git a/sdk/javascript/delivery-read-receipts.mdx b/sdk/javascript/delivery-read-receipts.mdx index 464e319fd..67ff45ed9 100644 --- a/sdk/javascript/delivery-read-receipts.mdx +++ b/sdk/javascript/delivery-read-receipts.mdx @@ -1,729 +1,479 @@ --- title: "Delivery & Read Receipts" +sidebarTitle: "Delivery & Read Receipts" +description: "Mark messages as delivered, read, or unread and receive real-time receipt events using the CometChat JavaScript SDK." --- + +| Method | Description | +| --- | --- | +| `markAsDelivered(message)` | Mark a message as delivered | +| `markAsRead(message)` | Mark a message as read | +| `markConversationAsDelivered(id, type)` | Mark entire conversation as delivered | +| `markConversationAsRead(id, type)` | Mark entire conversation as read | +| `markMessageAsUnread(message)` | Mark a message as unread | +| `getMessageReceipts(messageId)` | Get delivery/read receipts for a message | -## Mark Messages as Delivered - -*In other words, as a recipient, how do I inform the sender that I've received a message?* - -You can mark the messages for a particular conversation as read using the `markAsDelivered()` method. This method takes the below parameters as input: - -| Parameter | Information | -| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `messageId` | The ID of the message above which all the messages for a particular conversation are to be marked as read. | -| `receiverId` | In case of one to one conversation message's sender `UID` will be the receipt's receiver Id. In case of group conversation message's receiver Id will be the receipt's receiver Id. | -| `receiverType` | Type of the receiver. Could be either of the two values( user or group). | -| `senderId` | The `UID` of the sender of the message. | - -Messages for both user & group conversations can be marked as read using this method. - -Ideally, you would like to mark all the messages as delivered for any conversation when the user opens the chat window for that conversation. This includes two scenarios: - -1. **When the list of messages for the conversation is fetched**: In this case you need to obtain the last message in the list of messages and pass the message ID of that message to the markAsDelivered() method. -2. **When the user is on the chat window and a real-time message is received:** In this case you need to obtain the message ID of the message and pass it to the markAsDelivered() method. - - - ```javascript -var messageId = "MESSAGE_ID"; -var receiverId = "MESSAGE_RECEIVER_UID"; -var receiverType = "user"; -var senderId = "MESSAGE_SENDER_UID"; -CometChat.markAsDelivered(messageId, receiverId, receiverType, senderId); -``` - - - - -```javascript -var messageId = "MESSAGE_ID"; -var receiverId = "MESSAGE_RECEIVER_GUID"; -var receiverType = "group"; -var senderId = "MESSAGE_SENDER_UID"; -CometChat.markAsDelivered(messageId, receiverId, receiverType, senderId); -``` - - - - -```typescript -var messageId: string = "MESSAGE_ID"; -var receiverId: string = "MESSAGE_RECEIVER_UID"; -var receiverType: string = "user"; -var senderId: string = "MESSAGE_SENDER_UID"; -CometChat.markAsDelivered(messageId, receiverId, receiverType, senderId); -``` +// Mark as delivered/read (pass message object) +CometChat.markAsDelivered(message); +CometChat.markAsRead(message); - +// Mark entire conversation +CometChat.markConversationAsRead("UID", "user"); - -```typescript -var messageId: string = "MESSAGE_ID"; -var receiverId: string = "MESSAGE_RECEIVER_GUID"; -var receiverType: string = "group"; -var senderId: string = "MESSAGE_SENDER_UID"; -CometChat.markAsDelivered(messageId, receiverId, receiverType, senderId); +// Listen for receipt events +CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({ + onMessagesDelivered: (receipt) => { }, + onMessagesRead: (receipt) => { }, + onMessagesDeliveredToAll: (receipt) => { }, // Groups only + onMessagesReadByAll: (receipt) => { } // Groups only +})); ``` + - +Delivery and read receipts track whether messages have been delivered to and read by recipients. - +## Mark as Delivered -This method will mark all the messages before the messageId specified, for the conversation with receiverId and receiverType(user/group) as delivered. +Use `markAsDelivered()` to mark messages as delivered. You can pass either a message object or individual parameters. -In case you would like to be notified of an error if the receipts fail to go through you can use `.then(successCallback, failureCallback)` of the `markAsDelivered` method. +### Using Message Object - -```javascript -CometChat.markAsDelivered( - message.getId(), - message.getSender().getUid(), - "user", - message.getSender().getUid() -).then( + +```typescript +CometChat.markAsDelivered(message).then( () => { - console.log("mark as delivered success."); + console.log("Marked as delivered successfully"); }, - (error) => { - console.log( - "An error occurred when marking the message as delivered.", - error - ); + (error: CometChat.CometChatException) => { + console.log("Error marking as delivered:", error); } ); ``` - - - + ```javascript -CometChat.markAsDelivered( - message.getId(), - message.getReceiverUid(), - "group", - message.getSender().getUid() -).then( +CometChat.markAsDelivered(message).then( () => { - console.log("mark as delivered success."); + console.log("Marked as delivered successfully"); }, (error) => { - console.log( - "An error occurred when marking the message as delivered.", - error - ); + console.log("Error marking as delivered:", error); } ); ``` - + + +### Using Parameters + +| Parameter | Description | +| --- | --- | +| `messageId` | ID of the message to mark as delivered | +| `receiverId` | For user chats: sender's UID. For groups: group GUID | +| `receiverType` | `"user"` or `"group"` | +| `senderId` | UID of the message sender | + + + +```javascript +let messageId = "MESSAGE_ID"; +let receiverId = "MESSAGE_SENDER_UID"; +let receiverType = "user"; +let senderId = "MESSAGE_SENDER_UID"; - -```typescript -var messageId: string = "MESSAGE_ID"; -var receiverId: string = "MESSAGE_SENDER_UID"; -var receiverType: string = "user"; -var senderId: string = "MESSAGE_SENDER_UID"; CometChat.markAsDelivered(messageId, receiverId, receiverType, senderId).then( () => { - console.log("mark as delivered success."); + console.log("Marked as delivered successfully"); }, - (error: CometChat.CometChatException) => { - console.log( - "An error occurred when marking the message as delivered.", - error - ); + (error) => { + console.log("Error marking as delivered:", error); } ); ``` - + +```javascript +let messageId = "MESSAGE_ID"; +let receiverId = "GROUP_GUID"; +let receiverType = "group"; +let senderId = "MESSAGE_SENDER_UID"; - -```typescript -var messageId: string = "MESSAGE_ID"; -var receiverId: string = "MESSAGE_RECEIVER_GUID"; -var receiverType: string = "group"; -var senderId: string = "MESSAGE_SENDER_UID"; CometChat.markAsDelivered(messageId, receiverId, receiverType, senderId).then( () => { - console.log("mark as delivered success."); + console.log("Marked as delivered successfully"); }, - (error: CometChat.CometChatException) => { - console.log( - "An error occurred when marking the message as delivered.", - error - ); + (error) => { + console.log("Error marking as delivered:", error); } ); ``` - - - - -Another option the CometChat SDK provides is to pass the entire message object to the markAsDelivered() method. - - - -```javascript -CometChat.markAsDelivered(message); -``` - - - - + ```typescript -let message: CometChat.BaseMessage; -CometChat.markAsDelivered(message); -``` - - +let messageId: string = "MESSAGE_ID"; +let receiverId: string = "MESSAGE_SENDER_UID"; +let receiverType: string = "user"; +let senderId: string = "MESSAGE_SENDER_UID"; - - -In case you would like to be notified of an error if the receipts fail to go through you can use `.then(successCallback, failureCallback)` of the `markAsDelivered` method. - - - -```javascript -CometChat.markAsDelivered(message).then( +CometChat.markAsDelivered(messageId, receiverId, receiverType, senderId).then( () => { - console.log("mark as delivered success."); + console.log("Marked as delivered successfully"); }, - (error) => { - console.log( - "An error occurred when marking the message as delivered.", - error - ); + (error: CometChat.CometChatException) => { + console.log("Error marking as delivered:", error); } ); ``` - - - + ```typescript -let message: CometChat.BaseMessage; -CometChat.markAsDelivered(message).then( +let messageId: string = "MESSAGE_ID"; +let receiverId: string = "GROUP_GUID"; +let receiverType: string = "group"; +let senderId: string = "MESSAGE_SENDER_UID"; + +CometChat.markAsDelivered(messageId, receiverId, receiverType, senderId).then( () => { - console.log("mark as delivered success."); + console.log("Marked as delivered successfully"); }, (error: CometChat.CometChatException) => { - console.log( - "An error occurred when marking the message as delivered.", - error - ); + console.log("Error marking as delivered:", error); } ); ``` - - ## Mark Conversation as Delivered -You can mark an entire conversation as delivered for a user or group using the `markConversationAsDelivered()` method. This method takes the below parameters as input: - -| Parameter | Information | -| ------------------ | ------------------------------------------------------------------------------ | -| `conversationWith` | The ID of the user (UID) or group (GUID) for the conversation. | -| `conversationType` | Type of the conversation. Could be either `user` or `group`. | +Use `markConversationAsDelivered()` to mark all messages in a conversation as delivered. -This method will mark all messages in the conversation as delivered. +`markConversationAsDelivered()` resolves with a `string` on success. ```javascript -var conversationWith = "USER_UID"; -var conversationType = "user"; +let conversationWith = "USER_UID"; +let conversationType = "user"; CometChat.markConversationAsDelivered(conversationWith, conversationType).then( (response) => { console.log("Conversation marked as delivered", response); }, (error) => { - console.log("Error marking conversation as delivered", error); + console.log("Error:", error); } ); ``` - ```javascript -var conversationWith = "GROUP_GUID"; -var conversationType = "group"; +let conversationWith = "GROUP_GUID"; +let conversationType = "group"; CometChat.markConversationAsDelivered(conversationWith, conversationType).then( (response) => { console.log("Conversation marked as delivered", response); }, (error) => { - console.log("Error marking conversation as delivered", error); + console.log("Error:", error); } ); ``` - - + ```typescript -var conversationWith: string = "USER_UID"; -var conversationType: string = "user"; +let conversationWith: string = "USER_UID"; +let conversationType: string = "user"; CometChat.markConversationAsDelivered(conversationWith, conversationType).then( (response: string) => { console.log("Conversation marked as delivered", response); }, (error: CometChat.CometChatException) => { - console.log("Error marking conversation as delivered", error); + console.log("Error:", error); } ); ``` - - + ```typescript -var conversationWith: string = "GROUP_GUID"; -var conversationType: string = "group"; +let conversationWith: string = "GROUP_GUID"; +let conversationType: string = "group"; CometChat.markConversationAsDelivered(conversationWith, conversationType).then( (response: string) => { console.log("Conversation marked as delivered", response); }, (error: CometChat.CometChatException) => { - console.log("Error marking conversation as delivered", error); + console.log("Error:", error); } ); ``` -## Mark Messages as Read +## Mark as Read -*In other words, as a recipient, how do I inform the sender I've read a message?* +Use `markAsRead()` to mark messages as read. You can pass either a message object or individual parameters. -You can mark the messages for a particular conversation as read using the `markAsRead()` method. This method takes the below parameters as input: - -| Parameter | Information | -| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `messageId` | The ID of the message above which all the messages for a particular conversation are to be marked as read. | -| `receiverId` | In case of one to one conversation message's sender `UID` will be the receipt's receiver Id. In case of group conversation message's receiver Id will be the receipt's receiver Id | -| `receiverType` | Type of the receiver. Could be either of the two values( user or group) | -| `senderId` | The `UID` of the sender of the message. | - -Messages for both user and group conversations can be marked as read using this method. - -Ideally, you would like to mark all the messages as read for any conversation when the user opens the chat window for that conversation. This includes two scenarios: - -1. **When the list of messages for the conversation is fetched**: In this case you need to obtain the last message in the list of messages and pass the message ID of that message to the markAsRead() method. -2. **When the user is on the chat window and a real-time message is received:** In this case you need to obtain the message ID of the message and pass it to the markAsRead() method +### Using Message Object - -```javascript -var messageId = "MESSAGE_ID"; -var receiverId = "MESSAGE_SENDER_UID"; -var receiverType = "user"; -var senderId = "MESSAGE_SENDER_UID"; -CometChat.markAsRead(messageId, receiverId, receiverType, senderId); -``` - - - - -```javascript -var receiverId = "MESSAGE_RECEIVER_GUID"; -var receiverType = "group"; -var senderId = "MESSAGE_SENDER_UID"; -CometChat.markAsRead(messageId, receiverId, receiverType, senderId); -``` - - - - -```typescript -var messageId: string = "MESSAGE_ID"; -var receiverId: string = "MESSAGE_SENDER_UID"; -var receiverType: string = "user"; -var senderId: string = "MESSAGE_SENDER_UID"; -CometChat.markAsRead(messageId, receiverId, receiverType, senderId); -``` - - - - + ```typescript -var messageId: string = "MESSAGE_ID"; -var receiverId: string = "MESSAGE_RECEIVER_GUID"; -var receiverType: string = "group"; -var senderId: string = "MESSAGE_SENDER_UID"; -CometChat.markAsRead(messageId, receiverId, receiverType, senderId); -``` - - - - - -This method will mark all the messages before the messageId specified, for the conversation with receiverId and receiverType(user/group) as read. - -In case you would like to be notified of an error if the receipts fail to go through you can use `.then(successCallback, failureCallback)` of the `markAsDelivered` method. - - - -```javascript -CometChat.markAsRead( - message.getId(), - message.getSender().getUid(), - "user", - message.getSender().getUid() -).then( +CometChat.markAsRead(message).then( () => { - console.log("mark as read success."); + console.log("Marked as read successfully"); }, - (error) => { - console.log("An error occurred when marking the message as read.", error); + (error: CometChat.CometChatException) => { + console.log("Error marking as read:", error); } ); ``` - - - + ```javascript -CometChat.markAsRead( - message.getId(), - message.getReceiverUid(), - "group", - message.getSender().getUid() -).then( +CometChat.markAsRead(message).then( () => { - console.log("mark as read success."); + console.log("Marked as read successfully"); }, (error) => { - console.log("An error occurred when marking the message as read.", error); + console.log("Error marking as read:", error); } ); ``` - + + +### Using Parameters + + + +```javascript +let messageId = "MESSAGE_ID"; +let receiverId = "MESSAGE_SENDER_UID"; +let receiverType = "user"; +let senderId = "MESSAGE_SENDER_UID"; - -```typescript -var messageId: string = "MESSAGE_ID"; -var receiverId: string = "MESSAGE_SENDER_UID"; -var receiverType: string = "user"; -var senderId: string = "MESSAGE_SENDER_UID"; CometChat.markAsRead(messageId, receiverId, receiverType, senderId).then( () => { - console.log("mark as read success."); + console.log("Marked as read successfully"); }, - (error: CometChat.CometChatException) => { - console.log("An error occurred when marking the message as read.", error); + (error) => { + console.log("Error marking as read:", error); } ); ``` - + +```javascript +let messageId = "MESSAGE_ID"; +let receiverId = "GROUP_GUID"; +let receiverType = "group"; +let senderId = "MESSAGE_SENDER_UID"; - -```typescript -var messageId: string = "MESSAGE_ID"; -var receiverId: string = "MESSAGE_RECEIVER_GUID"; -var receiverType: string = "group"; -var senderId: string = "MESSAGE_SENDER_UID"; CometChat.markAsRead(messageId, receiverId, receiverType, senderId).then( () => { - console.log("mark as read success."); + console.log("Marked as read successfully"); }, - (error: CometChat.CometChatException) => { - console.log("An error occurred when marking the message as read.", error); + (error) => { + console.log("Error marking as read:", error); } ); ``` - - - - - -Another option the CometChat SDK provides is to pass the entire message object to the markAsRead() method. - - - -```javascript -CometChat.markAsRead(message); -``` - - - + ```typescript -let message: CometChat.BaseMessage; -CometChat.markAsRead(message); -``` - - - - +let messageId: string = "MESSAGE_ID"; +let receiverId: string = "MESSAGE_SENDER_UID"; +let receiverType: string = "user"; +let senderId: string = "MESSAGE_SENDER_UID"; -In case you would like to be notified of an error if the receipts fail to go through you can use `.then(successCallback, failureCallback)` of the `markAsDelivered` method. - - - -```javascript -CometChat.markAsRead(message).then( +CometChat.markAsRead(messageId, receiverId, receiverType, senderId).then( () => { - console.log("mark as read success."); + console.log("Marked as read successfully"); }, - (error) => { - console.log("An error occurred when marking the message as read.", error); + (error: CometChat.CometChatException) => { + console.log("Error marking as read:", error); } ); ``` - - - + ```typescript -let message: CometChat.BaseMessage; -CometChat.markAsRead(message).then( +let messageId: string = "MESSAGE_ID"; +let receiverId: string = "GROUP_GUID"; +let receiverType: string = "group"; +let senderId: string = "MESSAGE_SENDER_UID"; + +CometChat.markAsRead(messageId, receiverId, receiverType, senderId).then( () => { - console.log("mark as read success."); + console.log("Marked as read successfully"); }, (error: CometChat.CometChatException) => { - console.log("An error occurred when marking the message as read.", error); + console.log("Error marking as read:", error); } ); ``` - - ## Mark Conversation as Read -You can mark an entire conversation as read for a user or group using the `markConversationAsRead()` method. This method takes the below parameters as input: - -| Parameter | Information | -| ------------------ | ------------------------------------------------------------------------------ | -| `conversationWith` | The ID of the user (UID) or group (GUID) for the conversation. | -| `conversationType` | Type of the conversation. Could be either `user` or `group`. | - -This method will mark all messages in the conversation as read. +Use `markConversationAsRead()` to mark all messages in a conversation as read. ```javascript -var conversationWith = "USER_UID"; -var conversationType = "user"; +let conversationWith = "USER_UID"; +let conversationType = "user"; CometChat.markConversationAsRead(conversationWith, conversationType).then( (response) => { console.log("Conversation marked as read", response); }, (error) => { - console.log("Error marking conversation as read", error); + console.log("Error:", error); } ); ``` - ```javascript -var conversationWith = "GROUP_GUID"; -var conversationType = "group"; +let conversationWith = "GROUP_GUID"; +let conversationType = "group"; CometChat.markConversationAsRead(conversationWith, conversationType).then( (response) => { console.log("Conversation marked as read", response); }, (error) => { - console.log("Error marking conversation as read", error); + console.log("Error:", error); } ); ``` - - + ```typescript -var conversationWith: string = "USER_UID"; -var conversationType: string = "user"; +let conversationWith: string = "USER_UID"; +let conversationType: string = "user"; CometChat.markConversationAsRead(conversationWith, conversationType).then( (response: string) => { console.log("Conversation marked as read", response); }, (error: CometChat.CometChatException) => { - console.log("Error marking conversation as read", error); + console.log("Error:", error); } ); ``` - - + ```typescript -var conversationWith: string = "GROUP_GUID"; -var conversationType: string = "group"; +let conversationWith: string = "GROUP_GUID"; +let conversationType: string = "group"; CometChat.markConversationAsRead(conversationWith, conversationType).then( (response: string) => { console.log("Conversation marked as read", response); }, (error: CometChat.CometChatException) => { - console.log("Error marking conversation as read", error); + console.log("Error:", error); } ); ``` -## Mark Messages as Unread - -The Mark messages as Unread feature allows users to designate specific messages or conversations as unread, even if they have been previously viewed. - -This feature is valuable for users who want to revisit and respond to important messages or conversations later, ensuring they don't forget or overlook them. - -In other words, how I can mark a message as unread? - -You can mark the messages for a particular conversation as unread using the `markMessageAsUnread()` method. This method takes the below parameters as input: +## Mark as Unread -| Parameter | Information | -| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| message | To mark a message as unread, pass a non-null `BaseMessage` instance to the `markMessageAsUnread()` function. All messages below that message in the conversation will contribute to the unread messages count. Example: When User B sends User A a total of 10 messages, and User A invokes the `markMessageAsUnread()` method on the fifth message, all messages located below the fifth message within the conversation list will be designated as unread. This results in a notification indicating there are 5 unread messages in the conversation list. | +Use `markMessageAsUnread()` to mark a message as unread. All messages below that message will contribute to the unread count. You cannot mark your own messages as unread. This method only works for messages received from other users. - -```javascript -CometChat.markMessageAsUnread(message); -``` - - - ```typescript -let message: CometChat.BaseMessage; -CometChat.markMessageAsUnread(message); +CometChat.markMessageAsUnread(message).then( + (conversation: CometChat.Conversation) => { + console.log("Marked as unread successfully", conversation); + console.log("Unread count:", conversation.getUnreadMessageCount()); + }, + (error: CometChat.CometChatException) => { + console.log("Error marking as unread:", error); + } +); ``` - -In case you would like to be notified of an error if the receipts fail to go through you can use `.then(successCallback, failureCallback).` On success, this method returns an updated `Conversation` object with the updated unread message count and other conversation data. +In case you would like to be notified of an error if the receipts fail to go through you can use `.then(successCallback, failureCallback).` On success, this method returns an updated [`Conversation`](/sdk/reference/entities#conversation) object with the updated unread message count and other conversation data. - + ```javascript CometChat.markMessageAsUnread(message).then( (conversation) => { - console.log("mark messages as unread success.", conversation); - console.log("Unread message count:", conversation.getUnreadMessageCount()); + console.log("Marked as unread successfully", conversation); + console.log("Unread count:", conversation.getUnreadMessageCount()); }, (error) => { - console.log("An error occurred when marking the message as unread.", error); - } -); -``` - - - - -```typescript -let message: CometChat.BaseMessage; -CometChat.markMessageAsUnread(message).then( - (conversation: CometChat.Conversation) => { - console.log("mark messages as unread success.", conversation); - console.log("Unread message count:", conversation.getUnreadMessageCount()); - }, - (error: CometChat.CometChatException) => { - console.log("An error occurred when marking the message as unread.", error); + console.log("Error marking as unread:", error); } ); ``` - - -Receive Delivery & Read Receipts +On success, returns an updated [`Conversation`](/sdk/reference/entities#conversation) object with the new unread message count. -*In other words, as a recipient, how do I know when a message I sent has been delivered or read by someone?* +## Real-Time Receipt Events -### Real-time events +Register a `MessageListener` to receive delivery and read receipt events. -1. `onMessagesDelivered()` - This event is triggered when a message is delivered to a user. -2. `onMessagesRead()` - This event is triggered when a message is read by a user. -3. `onMessagesDeliveredToAll()` - This event is triggered when a group message is delivered to all members of the group. This event is only for Group conversations. -4. `onMessagesReadByAll()` - This event is triggered when a group message is read by all members of the group. This event is only for Group conversations. +| Callback | Description | +| --- | --- | +| `onMessagesDelivered` | Message delivered to a user | +| `onMessagesRead` | Message read by a user | +| `onMessagesDeliveredToAll` | Group message delivered to all members | +| `onMessagesReadByAll` | Group message read by all members | - -```javascript -let listenerId = "UNIQUE_LISTENER_ID"; - -CometChat.addMessageListener( - "listenerId", - new CometChat.MessageListener({ - onMessagesDelivered: (messageReceipt) => { - console.log("Message is delivered to a user: ", { messageReceipt }); - }, - onMessagesRead: (messageReceipt) => { - console.log("Message is read by a user: ", { messageReceipt }); - }, - /** This event is only for Group Conversation. */ - onMessagesDeliveredToAll: (messageReceipt) => { - console.log("Message delivered to all members of group: ", { - messageReceipt, - }); - }, - /** This event is only for Group Conversation. */ - onMessagesReadByAll: (messageReceipt) => { - console.log("Message read by all members of group: ", { messageReceipt }); - }, - }) -); -``` - - - ```typescript -let listenerId: string = "UNIQUE_LISTENER_ID"; +let listenerID: string = "UNIQUE_LISTENER_ID"; CometChat.addMessageListener( - listenerId, + listenerID, new CometChat.MessageListener({ onMessagesDelivered: (messageReceipt: CometChat.MessageReceipt) => { - console.log("Message is delivered to a user: ", { messageReceipt }); + console.log("Message delivered:", messageReceipt); }, onMessagesRead: (messageReceipt: CometChat.MessageReceipt) => { - console.log("Message is read by a user: ", { messageReceipt }); + console.log("Message read:", messageReceipt); }, - /** This event is only for Group Conversation. */ onMessagesDeliveredToAll: (messageReceipt: CometChat.MessageReceipt) => { - console.log("Message delivered to all members of group: ", { - messageReceipt, - }); + console.log("Message delivered to all group members:", messageReceipt); }, - /** This event is only for Group Conversation. */ onMessagesReadByAll: (messageReceipt: CometChat.MessageReceipt) => { - console.log("Message read by all members of group: ", { messageReceipt }); - }, + console.log("Message read by all group members:", messageReceipt); + } }) ); ``` - -You will receive events in the form of `MessageReceipt` objects. The message receipt contains the below parameters: +You will receive events in the form of [`MessageReceipt`](/sdk/reference/auxiliary#messagereceipt) objects. The message receipt contains the below parameters: | Parameter | Information | | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | @@ -735,6 +485,8 @@ You will receive events in the form of `MessageReceipt` objects. The message rec | `deliveredAt` | The timestamp of the time when the message was delivered. This will only be present if the receiptType is delivered. | | `readAt` | The timestamp of the time when the message was read. This will only be present when the receiptType is read. | +The `markAsDelivered()` and `markAsRead()` methods are fire-and-forget — they do not return a [`MessageReceipt`](/sdk/reference/auxiliary#messagereceipt) object. Use the listener callbacks above to receive delivery and read confirmations. + ### Missed Receipts You will receive message receipts when you load offline messages. While fetching messages in bulk, the message object will have two fields i.e. `deliveredAt` and `readAt` which hold the timestamp for the time the message was delivered and read respectively. Using these two variables, the delivery and read status for a message can be obtained. @@ -746,48 +498,172 @@ However, for a group message, if you wish to fetch the `deliveredAt` and `readAt To fetch the message receipts, you can use the `getMessageReceipts()` method. - + ```javascript -let messageId = msgId; -CometChat.getMessageReceipts(messageId).then( - (receipts) => { - console.log("Message details fetched:", receipts); - }, - (error) => { - console.log("Error in getting messag details ", error); - } +let listenerID = "UNIQUE_LISTENER_ID"; + +CometChat.addMessageListener( + listenerID, + new CometChat.MessageListener({ + onMessagesDelivered: (messageReceipt) => { + console.log("Message delivered:", messageReceipt); + }, + onMessagesRead: (messageReceipt) => { + console.log("Message read:", messageReceipt); + }, + onMessagesDeliveredToAll: (messageReceipt) => { + console.log("Message delivered to all group members:", messageReceipt); + }, + onMessagesReadByAll: (messageReceipt) => { + console.log("Message read by all group members:", messageReceipt); + } + }) ); ``` - + +You will receive a list of [`MessageReceipt`](/sdk/reference/auxiliary#messagereceipt) objects. + + + +The following features will be available only if the **Enhanced Messaging Status** feature is enabled for your app. + +* `onMessagesDeliveredToAll` event, +* `onMessagesReadByAll` event, +* `deliveredAt` field in a group message, +* `readAt` field in a group message. +* `markMessageAsUnread` method. + + + + +Always remove listeners when no longer needed to prevent memory leaks. + +```javascript +CometChat.removeMessageListener("UNIQUE_LISTENER_ID"); +``` + + +### MessageReceipt Object + +The listener callbacks receive a [`MessageReceipt`](/sdk/reference/auxiliary#messagereceipt) object: + +| Field | Getter | Return Type | Description | +| --- | --- | --- | --- | +| messageId | `getMessageId()` | `string` | ID of the message | +| sender | `getSender()` | [`User`](/sdk/reference/entities#user) | User who triggered the receipt | +| receiverId | `getReceiverId()` | `string` | ID of the receiver | +| receiverType | `getReceiverType()` | `string` | `"user"` or `"group"` | +| receiptType | `getReceiptType()` | `string` | `"delivery"` or `"read"` | +| deliveredAt | `getDeliveredAt()` | `number` | Timestamp when delivered | +| readAt | `getReadAt()` | `number` | Timestamp when read | + +## Get Receipt History + +Use `getMessageReceipts()` to fetch delivery and read receipts for a specific message. Useful for group messages to see which members have received/read the message. + + ```typescript -let messageId: number = 1; +let messageId: number = 123; + CometChat.getMessageReceipts(messageId).then( (receipts: CometChat.MessageReceipt[]) => { - console.log("Message details fetched:", receipts); + console.log("Message receipts:", receipts); }, (error: CometChat.CometChatException) => { - console.log("Error in getting messag details ", error); + console.log("Error fetching receipts:", error); } ); ``` - + +```javascript +let messageId = 123; +CometChat.getMessageReceipts(messageId).then( + (receipts) => { + console.log("Message receipts:", receipts); + }, + (error) => { + console.log("Error fetching receipts:", error); + } +); +``` + -You will receive a list of `MessageReceipt` objects. +Returns an array of [`MessageReceipt`](/sdk/reference/auxiliary#messagereceipt) objects. - +## Missed Receipts -The following features will be available only if the **Enhanced Messaging Status** feature is enabled for your app. +When fetching messages, each message object includes `deliveredAt` and `readAt` timestamps indicating when the message was delivered and read. -* `onMessagesDeliveredToAll` event, -* `onMessagesReadByAll` event, -* `deliveredAt` field in a group message, -* `readAt` field in a group message. -* `markMessageAsUnread` method. +```javascript +let deliveredAt = message.getDeliveredAt(); +let readAt = message.getReadAt(); +``` - + +The following features require **Enhanced Messaging Status** to be enabled for your app: +- `onMessagesDeliveredToAll` event +- `onMessagesReadByAll` event +- `deliveredAt` field in group messages +- `readAt` field in group messages +- `markMessageAsUnread()` method + + +## Mark as Unread + +Use `markAsUnread()` to mark a message as unread. This is useful for "mark as unread" functionality in conversation lists. Pass a message object ([`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), or [`CustomMessage`](/sdk/reference/messages#custommessage)). + + + +```typescript +CometChat.markAsUnread(message).then( + (response: string) => { + console.log("Message marked as unread:", response); + }, + (error: CometChat.CometChatException) => { + console.log("Error marking as unread:", error); + } +); +``` + + +```javascript +CometChat.markAsUnread(message).then( + (response) => { + console.log("Message marked as unread:", response); + }, + (error) => { + console.log("Error marking as unread:", error); + } +); +``` + + + +| Parameter | Type | Description | +|-----------|------|-------------| +| `message` | [`BaseMessage`](/sdk/reference/messages#basemessage) | The message object to mark as unread | + +--- + +## Next Steps + + + + Show real-time typing status in conversations + + + Listen for incoming messages in real time + + + Fetch conversation list with unread counts + + + Complete reference for all SDK event listeners + + diff --git a/sdk/javascript/direct-call.mdx b/sdk/javascript/direct-call.mdx index 0bedb0234..5e640c261 100644 --- a/sdk/javascript/direct-call.mdx +++ b/sdk/javascript/direct-call.mdx @@ -1,18 +1,42 @@ --- title: "Call Session" +sidebarTitle: "Call Session" +description: "Learn how to generate call tokens, start and manage call sessions, configure call settings, and handle call events using the CometChat JavaScript Calls SDK." --- -## Overview + -This section demonstrates how to start a call session in a web application. Previously known as **Direct Calling**. +```javascript +let sessionId = "SESSION_ID"; +let authToken = "AUTH_TOKEN"; +let htmlElement = document.getElementById("call-screen"); +let callListener = { + onCallEnded: () => console.log("Call ended"), + onUserJoined: (user) => console.log("User joined:", user), + onUserLeft: (user) => console.log("User left:", user), +}; + +// Generate call token +const callToken = await CometChatCalls.generateToken(sessionId, authToken); + +// Build call settings +const callSettings = new CometChatCalls.CallSettingsBuilder() + .enableDefaultLayout(true) + .setIsAudioOnlyCall(false) + .setCallListener(callListener) + .build(); -Before you begin, we strongly recommend you read the [calling setup guide](/sdk/javascript/calling-setup). +// Start call session +CometChatCalls.startSession(callToken.token, callSettings, htmlElement); - +// End call session +CometChatCalls.endSession(); +``` + -If you want to implement a complete calling experience with ringing functionality (incoming/outgoing call UI), follow the [Ringing](/sdk/javascript/default-call) guide first. Once the call is accepted, return here to start the call session. +A call session is the active media connection between participants — camera, microphone, screen sharing, and the call UI. Whether you arrive here from the [Ringing flow](/sdk/javascript/default-call), your own custom UI, or [Standalone Calling](/sdk/javascript/standalone-calling), this page covers how to manage the session itself. - +Before you begin, make sure you've completed the [Calls SDK Setup](/sdk/javascript/calling-setup). ## Generate Call Token @@ -20,38 +44,39 @@ A call token is required for secure access to a call session. Each token is uniq You can generate the token just before starting the call, or generate and store it ahead of time based on your use case. -Use the `generateToken()` method to create a call token: +- In the Ringing flow, the session ID comes from the [`Call`](/sdk/reference/messages#call) object after the call is accepted. +- For direct sessions, generate your own unique session ID. - -```javascript + +```typescript const loggedInUser = await CometChat.getLoggedinUser(); const authToken = loggedInUser.getAuthToken(); -const sessionId = "SESSION_ID"; // Random or from Call object in ringing flow +const sessionId: string = "SESSION_ID"; // Random or from Call object in ringing flow CometChatCalls.generateToken(sessionId, authToken).then( - (callToken) => { + (callToken: any) => { console.log("Call token generated:", callToken.token); // Use callToken to start the session }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Token generation failed:", error); } ); ``` - -```typescript + +```javascript const loggedInUser = await CometChat.getLoggedinUser(); const authToken = loggedInUser.getAuthToken(); -const sessionId: string = "SESSION_ID"; // Random or from Call object in ringing flow +const sessionId = "SESSION_ID"; // Random or from Call object in ringing flow CometChatCalls.generateToken(sessionId, authToken).then( - (callToken: any) => { + (callToken) => { console.log("Call token generated:", callToken.token); // Use callToken to start the session }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Token generation failed:", error); } ); @@ -61,9 +86,11 @@ CometChatCalls.generateToken(sessionId, authToken).then( | Parameter | Description | | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------ | -| `sessionId` | The unique random session ID. In case you are using the ringing flow, the session ID is available in the `Call` object. | +| `sessionId` | The unique random session ID. In case you are using the ringing flow, the session ID is available in the [`Call`](/sdk/reference/messages#call) object. | | `authToken` | The user auth token is the logged-in user auth token which you can get by calling `CometChat.getLoggedinUser().getAuthToken()` | +The `Promise` resolves with an object containing a `token` property (string) that you pass to `startSession()`. + ## Start Call Session Use the `startSession()` method to join a call session. This method requires: @@ -72,16 +99,16 @@ Use the `startSession()` method to join a call session. This method requires: 3. An HTML element where the call UI will be rendered - -```javascript + +```typescript const callListener = new CometChatCalls.OngoingCallListener({ - onUserJoined: (user) => { + onUserJoined: (user: any) => { console.log("User joined:", user); }, - onUserLeft: (user) => { + onUserLeft: (user: any) => { console.log("User left:", user); }, - onUserListUpdated: (userList) => { + onUserListUpdated: (userList: any[]) => { console.log("User list updated:", userList); }, onCallEnded: () => { @@ -91,13 +118,13 @@ const callListener = new CometChatCalls.OngoingCallListener({ console.log("End call button pressed"); // Handle end call - see End Call Session section }, - onError: (error) => { + onError: (error: any) => { console.log("Call error:", error); }, - onMediaDeviceListUpdated: (deviceList) => { + onMediaDeviceListUpdated: (deviceList: any[]) => { console.log("Device list updated:", deviceList); }, - onUserMuted: (event) => { + onUserMuted: (event: any) => { console.log("User muted:", event); }, onScreenShareStarted: () => { @@ -106,7 +133,7 @@ const callListener = new CometChatCalls.OngoingCallListener({ onScreenShareStopped: () => { console.log("Screen sharing stopped"); }, - onCallSwitchedToVideo: (event) => { + onCallSwitchedToVideo: (event: any) => { console.log("Call switched to video:", event); }, onSessionTimeout: () => { @@ -120,20 +147,20 @@ const callSettings = new CometChatCalls.CallSettingsBuilder() .setCallListener(callListener) .build(); -const htmlElement = document.getElementById("call-container"); +const htmlElement = document.getElementById("call-container") as HTMLElement; CometChatCalls.startSession(callToken, callSettings, htmlElement); ``` - -```typescript + +```javascript const callListener = new CometChatCalls.OngoingCallListener({ - onUserJoined: (user: any) => { + onUserJoined: (user) => { console.log("User joined:", user); }, - onUserLeft: (user: any) => { + onUserLeft: (user) => { console.log("User left:", user); }, - onUserListUpdated: (userList: any[]) => { + onUserListUpdated: (userList) => { console.log("User list updated:", userList); }, onCallEnded: () => { @@ -143,13 +170,13 @@ const callListener = new CometChatCalls.OngoingCallListener({ console.log("End call button pressed"); // Handle end call - see End Call Session section }, - onError: (error: any) => { + onError: (error) => { console.log("Call error:", error); }, - onMediaDeviceListUpdated: (deviceList: any[]) => { + onMediaDeviceListUpdated: (deviceList) => { console.log("Device list updated:", deviceList); }, - onUserMuted: (event: any) => { + onUserMuted: (event) => { console.log("User muted:", event); }, onScreenShareStarted: () => { @@ -158,7 +185,7 @@ const callListener = new CometChatCalls.OngoingCallListener({ onScreenShareStopped: () => { console.log("Screen sharing stopped"); }, - onCallSwitchedToVideo: (event: any) => { + onCallSwitchedToVideo: (event) => { console.log("Call switched to video:", event); }, onSessionTimeout: () => { @@ -172,7 +199,7 @@ const callSettings = new CometChatCalls.CallSettingsBuilder() .setCallListener(callListener) .build(); -const htmlElement = document.getElementById("call-container") as HTMLElement; +const htmlElement = document.getElementById("call-container"); CometChatCalls.startSession(callToken, callSettings, htmlElement); ``` @@ -205,6 +232,111 @@ Configure the call experience using the following `CallSettingsBuilder` methods: | `setMainVideoContainerSetting(MainVideoContainerSetting)` | Customizes the main video container. See [Video View Customization](/sdk/javascript/video-view-customisation). | | `setIdleTimeoutPeriod(number)` | Sets idle timeout in seconds. Warning appears 60 seconds before auto-termination. Default: `180` seconds. *v4.1.0+* | +## End Call Session + +How you end a call depends on whether you're using the Ringing flow or a direct session. + +### Ringing Flow + +When using the [Ringing](/sdk/javascript/default-call) flow, you must coordinate between the CometChat Chat SDK and the Calls SDK to properly terminate the call and notify all participants. + + +The Ringing flow requires calling methods from both the Chat SDK (`CometChat.endCall()`) and the Calls SDK (`CometChatCalls.endSession()`) to ensure proper call termination and participant notification. + + + + + + +**User who initiates the end call:** + +When the user clicks the end call button in the UI, the `onCallEndButtonPressed()` callback is triggered. You must call `CometChat.endCall()` inside this callback to properly terminate the call and notify other participants. On success, call `CometChat.clearActiveCall()` and `CometChatCalls.endSession()` to release resources. + + + +```typescript +onCallEndButtonPressed: () => { + CometChat.endCall(sessionId).then( + (call: CometChat.Call) => { + console.log("Call ended successfully"); + CometChat.clearActiveCall(); + CometChatCalls.endSession(); + // Close the calling screen + }, + (error: CometChat.CometChatException) => { + console.log("End call failed:", error); + } + ); +} +``` + + +```javascript +onCallEndButtonPressed: () => { + CometChat.endCall(sessionId).then( + (call) => { + console.log("Call ended successfully"); + CometChat.clearActiveCall(); + CometChatCalls.endSession(); + // Close the calling screen + }, + (error) => { + console.log("End call failed:", error); + } + ); +} +``` + + + +**Remote participant** (receives the `onCallEnded()` callback): + +Call `CometChat.clearActiveCall()` to clear the local call state, then call `CometChatCalls.endSession()` to release media resources. + + + +```typescript +onCallEnded: () => { + CometChat.clearActiveCall(); + CometChatCalls.endSession(); + // Close the calling screen +} +``` + + +```javascript +onCallEnded: () => { + CometChat.clearActiveCall(); + CometChatCalls.endSession(); + // Close the calling screen +} +``` + + + +### Session Only Flow + +When using the Session Only flow (direct call without ringing), you only need to call the Calls SDK method to end the session. There's no need to notify the Chat SDK since no call signaling was involved. + + + +```typescript +onCallEndButtonPressed: () => { + CometChatCalls.endSession(); + // Close the calling screen +} +``` + + +```javascript +onCallEndButtonPressed: () => { + CometChatCalls.endSession(); + // Close the calling screen +} +``` + + + ## Call Listeners The `OngoingCallListener` provides real-time callbacks for call session events, including participant changes, call state updates, and error conditions. @@ -219,18 +351,18 @@ Each listener requires a unique `listenerId` string. This ID is used to: - **Enable targeted removal** — Remove specific listeners without affecting others - -```javascript -const listenerId = "UNIQUE_LISTENER_ID"; + +```typescript +const listenerId: string = "UNIQUE_LISTENER_ID"; CometChatCalls.addCallEventListener(listenerId, { - onUserJoined: (user) => { + onUserJoined: (user: any) => { console.log("User joined:", user); }, - onUserLeft: (user) => { + onUserLeft: (user: any) => { console.log("User left:", user); }, - onUserListUpdated: (userList) => { + onUserListUpdated: (userList: any[]) => { console.log("User list updated:", userList); }, onCallEnded: () => { @@ -239,13 +371,13 @@ CometChatCalls.addCallEventListener(listenerId, { onCallEndButtonPressed: () => { console.log("End call button pressed"); }, - onError: (error) => { + onError: (error: any) => { console.log("Call error:", error); }, - onMediaDeviceListUpdated: (deviceList) => { + onMediaDeviceListUpdated: (deviceList: any[]) => { console.log("Device list updated:", deviceList); }, - onUserMuted: (event) => { + onUserMuted: (event: any) => { console.log("User muted:", event); }, onScreenShareStarted: () => { @@ -254,7 +386,7 @@ CometChatCalls.addCallEventListener(listenerId, { onScreenShareStopped: () => { console.log("Screen sharing stopped"); }, - onCallSwitchedToVideo: (event) => { + onCallSwitchedToVideo: (event: any) => { console.log("Call switched to video:", event); }, onSessionTimeout: () => { @@ -266,18 +398,18 @@ CometChatCalls.addCallEventListener(listenerId, { CometChatCalls.removeCallEventListener(listenerId); ``` - -```typescript -const listenerId: string = "UNIQUE_LISTENER_ID"; + +```javascript +const listenerId = "UNIQUE_LISTENER_ID"; CometChatCalls.addCallEventListener(listenerId, { - onUserJoined: (user: any) => { + onUserJoined: (user) => { console.log("User joined:", user); }, - onUserLeft: (user: any) => { + onUserLeft: (user) => { console.log("User left:", user); }, - onUserListUpdated: (userList: any[]) => { + onUserListUpdated: (userList) => { console.log("User list updated:", userList); }, onCallEnded: () => { @@ -286,13 +418,13 @@ CometChatCalls.addCallEventListener(listenerId, { onCallEndButtonPressed: () => { console.log("End call button pressed"); }, - onError: (error: any) => { + onError: (error) => { console.log("Call error:", error); }, - onMediaDeviceListUpdated: (deviceList: any[]) => { + onMediaDeviceListUpdated: (deviceList) => { console.log("Device list updated:", deviceList); }, - onUserMuted: (event: any) => { + onUserMuted: (event) => { console.log("User muted:", event); }, onScreenShareStarted: () => { @@ -301,7 +433,7 @@ CometChatCalls.addCallEventListener(listenerId, { onScreenShareStopped: () => { console.log("Screen sharing stopped"); }, - onCallSwitchedToVideo: (event: any) => { + onCallSwitchedToVideo: (event) => { console.log("Call switched to video:", event); }, onSessionTimeout: () => { @@ -315,133 +447,21 @@ CometChatCalls.removeCallEventListener(listenerId); -### Events - -| Event | Description | -| ----- | ----------- | -| `onCallEnded()` | Invoked when the call session terminates for a 1:1 call. Both participants receive this callback. Only fires for calls with exactly 2 participants. | -| `onSessionTimeout()` | Invoked when the call is auto-terminated due to inactivity (default: 180 seconds). Warning appears 60 seconds before. *v4.1.0+* | -| `onCallEndButtonPressed()` | Invoked when the local user clicks the end call button. For ringing flow, call `CometChat.endCall()`. For standalone, call `CometChatCalls.endSession()`. | -| `onUserJoined(user)` | Invoked when a remote participant joins. The `user` contains UID, name, and avatar. | -| `onUserLeft(user)` | Invoked when a remote participant leaves the call session. | -| `onUserListUpdated(userList)` | Invoked whenever the participant list changes (join or leave events). | -| `onMediaDeviceListUpdated(deviceList)` | Invoked when available audio/video devices change (e.g., new microphone connected). | -| `onUserMuted(event)` | Invoked when a participant's mute state changes. Contains `muted` and `mutedBy` properties. | -| `onScreenShareStarted()` | Invoked when the local user starts sharing their screen. | -| `onScreenShareStopped()` | Invoked when the local user stops sharing their screen. | -| `onCallSwitchedToVideo(event)` | Invoked when an audio call is upgraded to a video call. Contains `sessionId`, `initiator`, and `responder`. | -| `onError(error)` | Invoked when an error occurs during the call session. | - -## End Call Session - -Ending a call session properly is essential to release media resources (camera, microphone, network connections) and update call state across all participants. The termination process differs based on whether you're using the Ringing flow or Session Only flow. - -### Ringing Flow - -When using the [Ringing](/sdk/javascript/default-call) flow, you must coordinate between the CometChat Chat SDK and the Calls SDK to properly terminate the call and notify all participants. - - - -The Ringing flow requires calling methods from both the Chat SDK (`CometChat.endCall()`) and the Calls SDK (`CometChatCalls.endSession()`) to ensure proper call termination and participant notification. - - - - - - - -**User who initiates the end call:** - -When the user clicks the end call button in the UI, the `onCallEndButtonPressed()` callback is triggered. You must call `CometChat.endCall()` inside this callback to properly terminate the call and notify other participants. On success, call `CometChat.clearActiveCall()` and `CometChatCalls.endSession()` to release resources. - - - -```javascript -onCallEndButtonPressed: () => { - CometChat.endCall(sessionId).then( - (call) => { - console.log("Call ended successfully"); - CometChat.clearActiveCall(); - CometChatCalls.endSession(); - // Close the calling screen - }, - (error) => { - console.log("End call failed:", error); - } - ); -} -``` - - -```typescript -onCallEndButtonPressed: () => { - CometChat.endCall(sessionId).then( - (call: CometChat.Call) => { - console.log("Call ended successfully"); - CometChat.clearActiveCall(); - CometChatCalls.endSession(); - // Close the calling screen - }, - (error: CometChat.CometChatException) => { - console.log("End call failed:", error); - } - ); -} -``` - - - -**Remote participant** (receives the `onCallEnded()` callback): + +Always remove call event listeners when they're no longer needed (e.g., on component unmount or when the call screen is closed). Failing to remove listeners can cause memory leaks and duplicate event handling. -Call `CometChat.clearActiveCall()` to clear the local call state, then call `CometChatCalls.endSession()` to release media resources. - - - ```javascript -onCallEnded: () => { - CometChat.clearActiveCall(); - CometChatCalls.endSession(); - // Close the calling screen -} +CometChatCalls.removeCallEventListener("UNIQUE_LISTENER_ID"); ``` - - -```typescript -onCallEnded: () => { - CometChat.clearActiveCall(); - CometChatCalls.endSession(); - // Close the calling screen -} -``` - - + -### Session Only Flow +### Events -When using the Session Only flow (direct call without ringing), you only need to call the Calls SDK method to end the session. There's no need to notify the Chat SDK since no call signaling was involved. +For the full list of callbacks, their descriptions, and parameter shapes, see the [`OngoingCallListener`](/sdk/javascript/all-real-time-listeners#ongoing-call-listener-calls-sdk) reference. -Call `CometChatCalls.endSession()` in the `onCallEndButtonPressed()` callback to release all media resources and disconnect from the call session. +The ringing flow methods (`initiateCall()`, `acceptCall()`, `rejectCall()`, `endCall()`) return [`Call`](/sdk/reference/messages#call) objects. - - -```javascript -onCallEndButtonPressed: () => { - CometChatCalls.endSession(); - // Close the calling screen -} -``` - - -```typescript -onCallEndButtonPressed: () => { - CometChatCalls.endSession(); - // Close the calling screen -} -``` - - - -## Methods +## In-Call Methods These methods are available for performing custom actions during an active call session. Use them to build custom UI controls or implement specific behaviors based on your use case. @@ -454,13 +474,13 @@ These methods can only be called when a call session is active. Toggles between the front and rear camera during a video call. Only supported on mobile browsers. - -```javascript + +```typescript CometChatCalls.switchCamera(); ``` - -```typescript + +```javascript CometChatCalls.switchCamera(); ``` @@ -478,13 +498,13 @@ Controls the local audio stream transmission. When muted, other participants can - `false` — Unmutes the microphone, resumes audio transmission - -```javascript + +```typescript CometChatCalls.muteAudio(true); ``` - -```typescript + +```javascript CometChatCalls.muteAudio(true); ``` @@ -498,13 +518,13 @@ Controls the local video stream transmission. When paused, other participants se - `false` — Resumes the camera, continues video transmission - -```javascript + +```typescript CometChatCalls.pauseVideo(true); ``` - -```typescript + +```javascript CometChatCalls.pauseVideo(true); ``` @@ -515,13 +535,13 @@ CometChatCalls.pauseVideo(true); Starts sharing your screen or a specific application window with other participants. - -```javascript + +```typescript CometChatCalls.startScreenShare(); ``` - -```typescript + +```javascript CometChatCalls.startScreenShare(); ``` @@ -532,13 +552,13 @@ CometChatCalls.startScreenShare(); Stops the current screen sharing session. - -```javascript + +```typescript CometChatCalls.stopScreenShare(); ``` - -```typescript + +```javascript CometChatCalls.stopScreenShare(); ``` @@ -553,13 +573,13 @@ Changes the call UI layout mode dynamically during the call. - `CometChat.CALL_MODE.SPOTLIGHT` — Focus on the active speaker - -```javascript + +```typescript CometChatCalls.setMode(CometChat.CALL_MODE.SPOTLIGHT); ``` - -```typescript + +```javascript CometChatCalls.setMode(CometChat.CALL_MODE.SPOTLIGHT); ``` @@ -567,17 +587,17 @@ CometChatCalls.setMode(CometChat.CALL_MODE.SPOTLIGHT); ### Get Audio Input Devices -Returns a list of available audio input devices (microphones). +Returns a list of available audio input devices (microphones) as [`MediaDeviceInfo[]`](/sdk/reference/calls#mediadeviceinfo). - -```javascript + +```typescript const audioInputDevices = CometChatCalls.getAudioInputDevices(); console.log("Available microphones:", audioInputDevices); ``` - -```typescript + +```javascript const audioInputDevices = CometChatCalls.getAudioInputDevices(); console.log("Available microphones:", audioInputDevices); ``` @@ -586,17 +606,17 @@ console.log("Available microphones:", audioInputDevices); ### Get Audio Output Devices -Returns a list of available audio output devices (speakers/headphones). +Returns a list of available audio output devices (speakers/headphones) as [`MediaDeviceInfo[]`](/sdk/reference/calls#mediadeviceinfo). - -```javascript + +```typescript const audioOutputDevices = CometChatCalls.getAudioOutputDevices(); console.log("Available speakers:", audioOutputDevices); ``` - -```typescript + +```javascript const audioOutputDevices = CometChatCalls.getAudioOutputDevices(); console.log("Available speakers:", audioOutputDevices); ``` @@ -605,17 +625,17 @@ console.log("Available speakers:", audioOutputDevices); ### Get Video Input Devices -Returns a list of available video input devices (cameras). +Returns a list of available video input devices (cameras) as [`MediaDeviceInfo[]`](/sdk/reference/calls#mediadeviceinfo). - -```javascript + +```typescript const videoInputDevices = CometChatCalls.getVideoInputDevices(); console.log("Available cameras:", videoInputDevices); ``` - -```typescript + +```javascript const videoInputDevices = CometChatCalls.getVideoInputDevices(); console.log("Available cameras:", videoInputDevices); ``` @@ -627,13 +647,13 @@ console.log("Available cameras:", videoInputDevices); Sets the active audio input device (microphone) by device ID. - -```javascript + +```typescript CometChatCalls.setAudioInputDevice(deviceId); ``` - -```typescript + +```javascript CometChatCalls.setAudioInputDevice(deviceId); ``` @@ -644,13 +664,13 @@ CometChatCalls.setAudioInputDevice(deviceId); Sets the active audio output device (speaker/headphones) by device ID. - -```javascript + +```typescript CometChatCalls.setAudioOutputDevice(deviceId); ``` - -```typescript + +```javascript CometChatCalls.setAudioOutputDevice(deviceId); ``` @@ -661,13 +681,13 @@ CometChatCalls.setAudioOutputDevice(deviceId); Sets the active video input device (camera) by device ID. - -```javascript + +```typescript CometChatCalls.setVideoInputDevice(deviceId); ``` - -```typescript + +```javascript CometChatCalls.setVideoInputDevice(deviceId); ``` @@ -678,13 +698,13 @@ CometChatCalls.setVideoInputDevice(deviceId); Upgrades an ongoing audio call to a video call. This enables the camera and starts transmitting video to other participants. The remote participant receives the `onCallSwitchedToVideo()` callback. - -```javascript + +```typescript CometChatCalls.switchToVideoCall(); ``` - -```typescript + +```javascript CometChatCalls.switchToVideoCall(); ``` @@ -695,14 +715,91 @@ CometChatCalls.switchToVideoCall(); Terminates the current call session and releases all media resources (camera, microphone, network connections). After calling this method, the call view should be closed. - + ```javascript CometChatCalls.endSession(); ``` + + +## Utility Methods + +### Get Active Call + +Returns the current active [`Call`](/sdk/reference/messages#call) object, or `undefined` if no call is in progress. + +```javascript +const activeCall = CometChat.getActiveCall(); +if (activeCall) { + console.log("Active call session:", activeCall.getSessionId()); +} +``` + +### Clear Active Call + +Clears the locally stored active call state. Call this after ending a call session. + +```javascript +CometChat.clearActiveCall(); +``` + +### Get Call Participant Count + +Returns the number of participants currently in a call session. + + ```typescript -CometChatCalls.endSession(); +let sessionId: string = "SESSION_ID"; +let type: string = "direct"; // "direct" or "default" + +CometChat.getCallParticipantCount(sessionId, type).then( + (count: number) => { + console.log("Participant count:", count); + }, + (error: CometChat.CometChatException) => { + console.log("Error:", error); + } +); +``` + + +```javascript +let sessionId = "SESSION_ID"; +let type = "direct"; // "direct" or "default" + +CometChat.getCallParticipantCount(sessionId, type).then( + (count) => { + console.log("Participant count:", count); + }, + (error) => { + console.log("Error:", error); + } +); ``` + +| Parameter | Type | Description | +|-----------|------|-------------| +| `sessionId` | `string` | The call session ID | +| `type` | `string` | Call type: `"direct"` or `"default"` | + +--- + +## Next Steps + + + + Implement calls with incoming/outgoing ringing UI + + + Record call sessions for playback + + + Customize the video layout and main video container + + + Retrieve and display call history + + diff --git a/sdk/javascript/edit-message.mdx b/sdk/javascript/edit-message.mdx index 8b91b4aea..e7f8d4443 100644 --- a/sdk/javascript/edit-message.mdx +++ b/sdk/javascript/edit-message.mdx @@ -1,23 +1,41 @@ --- -title: "Edit A Message" +title: "Edit Message" +sidebarTitle: "Edit Message" +description: "Edit text and custom messages using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +let receiverID = "UID"; +let receiverType = CometChat.RECEIVER_TYPE.USER; +let messageId = "MESSAGE_ID"; -While [editing a message](/sdk/javascript/edit-message#edit-a-message) is straightforward, receiving events for edited messages with CometChat has two parts: +// Edit a text message +const textMessage = new CometChat.TextMessage(receiverID, "Updated text", receiverType); +textMessage.setId(messageId); +await CometChat.editMessage(textMessage); -1. Adding a listener to receive [real-time message edits](/sdk/javascript/edit-message#real-time-message-edit-events) when your app is running -2. Calling a method to retrieve [missed message edits](/sdk/javascript/edit-message#missed-message-edit-events) when your app was not running +// Listen for real-time edits +CometChat.addMessageListener("edits", new CometChat.MessageListener({ + onMessageEdited: (message) => console.log("Edited:", message) +})); +``` + -## Edit a Message +Editing a message is straightforward. Receiving edit events has two parts: -*In other words, as a sender, how do I edit a message?* +1. Adding a listener for [real-time edits](#real-time-message-edit-events) when your app is running +2. Fetching [missed edits](#missed-message-edit-events) when your app was offline -In order to edit a message, you can use the `editMessage()` method. This method takes an object of the `BaseMessage` class. At the moment, you are only allowed to edit `TextMessage` and `CustomMessage`. Thus, the `BaseMessage` object must either be a Text or a Custom Message. +## Edit a Message + +Use `editMessage()` with a [`TextMessage`](/sdk/reference/messages#textmessage) or [`CustomMessage`](/sdk/reference/messages#custommessage) object. Set the message ID using `setId()`. ### Add/Update Tags -While editing a message, you can update the tags associated with the Message. You can use the `setTags()` method to do so. The tags added while editing a message will replace the tags set when the message was sent. +Use `setTags()` to update tags when editing. New tags replace existing ones. @@ -58,10 +76,31 @@ customMessage.setTags(tags); -Once the message object is ready, you can use the `editMessage()` method and pass the message object to it. +Once the message object is ready, call `editMessage()`. - + +```typescript +let receiverID: string = "RECEIVER_UID"; +let messageText: string = "Hello world!"; +let receiverType: string = CometChat.RECEIVER_TYPE.USER; +let messageId: number = 1; +let textMessage: CometChat.TextMessage = new CometChat.TextMessage(receiverID, messageText, receiverType); + +textMessage.setId(messageId); + +CometChat.editMessage(textMessage).then( + (message: CometChat.TextMessage) => { + console.log("Message Edited", message); + }, (error: CometChat.CometChatException) => { + console.log("Message editing failed with error:", error); + } +); +``` + + + + ```javascript let receiverID = "RECEIVER_UID"; let messageText = "Hello world!"; @@ -82,30 +121,38 @@ message => { - -```typescript -let receiverID: string = "RECEIVER_UID"; -let messageText: string = "Hello world!"; -let receiverType: string = CometChat.RECEIVER_TYPE.USER; -let messageId: number = 1; -let textMessage: CometChat.TextMessage = new CometChat.TextMessage(receiverID, messageText, receiverType); + +```javascript +try { + let receiverID = "RECEIVER_UID"; + let messageText = "Hello world!"; + let receiverType = CometChat.RECEIVER_TYPE.USER; + let messageId = "MESSAGE_ID_OF_THE_MESSAGE_TO_BE_EDITED"; + let textMessage = new CometChat.TextMessage(receiverID, messageText, receiverType); -textMessage.setId(messageId); + textMessage.setId(messageId); -CometChat.editMessage(textMessage).then( - (message: CometChat.TextMessage) => { - console.log("Message Edited", message); - }, (error: CometChat.CometChatException) => { - console.log("Message editing failed with error:", error); - } -); + const message = await CometChat.editMessage(textMessage); + console.log("Message Edited", message); +} catch (error) { + console.log("Message editing failed with error:", error); +} ``` -The object of the edited message will be returned in the `onSuccess()` callback method of the listener. The message object will contain the `editedAt` field set with the timestamp of the time the message was edited. This will help you identify if the message was edited while iterating through the list of messages. The `editedBy` field is also set to the UID of the user who edited the message. +The edited message object is returned with `editedAt` (timestamp) and `editedBy` (UID of editor) fields set. + +The `editMessage()` method returns a [`BaseMessage`](/sdk/reference/messages#basemessage) object (or a subclass like [`TextMessage`](/sdk/reference/messages#textmessage)). + +Relevant fields to access on the returned message: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| editedAt | `getEditedAt()` | `number` | Timestamp when the message was edited | +| editedBy | `getEditedBy()` | `string` | UID of the user who edited the message | By default, CometChat allows certain roles to edit a message. @@ -118,19 +165,17 @@ By default, CometChat allows certain roles to edit a message. ## Real-time Message Edit Events -*In other words, as a recipient, how do I know when someone has edited their message when my app is running?* - -In order to receive real-time events for message being edited, you need to override the `onMessageEdited()` method of the `MessageListener` class. +Use `onMessageEdited` in `MessageListener` to receive real-time edit events. - -```javascript -let listenerID = "UNIQUE_LISTENER_ID"; + +```typescript +let listenerID: string = "UNIQUE_LISTENER_ID"; CometChat.addMessageListener( listenerID, new CometChat.MessageListener({ - onMessageEdited: message => { + onMessageEdited: (message: CometChat.BaseMessage) => { console.log("Edited Message", message); } }) @@ -139,14 +184,14 @@ new CometChat.MessageListener({ - -```typescript -let listenerID: string = "UNIQUE_LISTENER_ID"; + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; CometChat.addMessageListener( listenerID, new CometChat.MessageListener({ - onMessageEdited: (message: CometChat.BaseMessage) => { + onMessageEdited: message => { console.log("Edited Message", message); } }) @@ -157,21 +202,52 @@ new CometChat.MessageListener({ -## Missed Message Edit Events + +Always remove message listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. -*In other words, as a recipient, how do I know when someone edited their message when my app was not running?* +```javascript +CometChat.removeMessageListener("UNIQUE_LISTENER_ID"); +``` + -When you retrieve the list of previous messages, for the message that was edited, the `editedAt` and the `editedBy` fields will be set. Also, for example, of the total number of messages for a conversation are 100, and the message with message ID 50 was edited. Now the message with id 50 will have the `editedAt` and the `editedBy` fields set whenever it is pulled from the history. Also, the 101st message will be and \[Action] message informing you that the message with id 50 has been edited. +The `onMessageEdited` callback receives a [`BaseMessage`](/sdk/reference/messages#basemessage) object with the `editedAt` and `editedBy` fields set. -For the message edited event, in the `Action` object received, the following fields can help you get the relevant information- +Relevant fields to access on the returned message: -1. `action` - `edited` -2. `actionOn` - Updated message object with the edited details. -3. `actionBy` - User object containing the details of the user who has edited the message. -4. `actionFor` - User/group object having the details of the receiver to which the message was sent. +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| editedAt | `getEditedAt()` | `number` | Timestamp when the message was edited | +| editedBy | `getEditedBy()` | `string` | UID of the user who edited the message | - +## Missed Message Edit Events + +When fetching message history, edited messages have `editedAt` and `editedBy` fields set. Additionally, an [`Action`](/sdk/reference/messages#action) message is created when a message is edited. -In order to edit a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent. +The [`Action`](/sdk/reference/messages#action) object contains: +- `action` — `edited` +- `actionOn` — Updated message object +- `actionBy` — User who edited the message +- `actionFor` — Receiver (User/Group) + +You must be the message sender or a group admin/moderator to edit a message. + +--- + +## Next Steps + + + + Remove messages from conversations + + + Send text, media, and custom messages + + + Organize conversations with message threads + + + Listen for incoming messages in real time + + diff --git a/sdk/javascript/error-codes.mdx b/sdk/javascript/error-codes.mdx new file mode 100644 index 000000000..a6a68c010 --- /dev/null +++ b/sdk/javascript/error-codes.mdx @@ -0,0 +1,227 @@ +--- +title: "Error Codes" +sidebarTitle: "Error Codes" +description: "Complete reference for CometChat JavaScript SDK error codes, including client-side validation errors, server-side API errors, and how to handle them." +--- + + + +```javascript +let uid = "UID"; +let authKey = "AUTH_KEY"; + +// All errors are CometChatException objects +try { + await CometChat.login(uid, authKey); +} catch (error) { + console.log(error.code); // e.g., "AUTH_ERR_AUTH_TOKEN_NOT_FOUND" + console.log(error.name); // e.g., "AUTH_ERR_AUTH_TOKEN_NOT_FOUND" + console.log(error.message); // Human-readable description + console.log(error.details); // Additional context (if available) +} +``` + +**Error categories:** Initialization, Login, Calling, Messages, Groups, Users, Conversations, Receipts, AI, Extensions + + +Every error thrown by the CometChat SDK is a [`CometChatException`](/sdk/reference/auxiliary#cometchatexception) object with four properties: + +| Property | Type | Description | +|----------|------|-------------| +| `code` | `string \| number` | Machine-readable error code | +| `name` | `string` | Error name (often matches code) | +| `message` | `string` | Human-readable description | +| `details` | `string` | Additional context or troubleshooting info | + +```typescript +try { + const user = await CometChat.login(authToken); +} catch (error: CometChat.CometChatException) { + switch (error.code) { + case "AUTH_ERR_AUTH_TOKEN_NOT_FOUND": + // Token is invalid or expired — prompt re-login + break; + case "MISSING_PARAMETERS": + // A required parameter was not provided + break; + default: + console.error("Unexpected error:", error.message); + } +} +``` + +## Initialization Errors + +| Code | Message | +|------|---------| +| `MISSING_PARAMETERS` | AppID cannot be empty. Please specify a valid appID. | + +## Login & Authentication Errors + +| Code | Message | +|------|---------| +| `COMETCHAT_INITIALIZATION_NOT_DONE` | Please initialize CometChat before using the login method. | +| `USER_NOT_AUTHORISED` | The authToken of the user is not authorised. Please verify again. | +| `AUTH_ERR_AUTH_TOKEN_NOT_FOUND` | The auth token does not exist. Please make sure you are logged in and have a valid auth token. | +| `LOGIN_IN_PROGRESS` | Please wait until the previous login request ends. | +| `WS_CONNECTION_FAIL` | WebSocket connection failed. | +| `WS_CONNECTION_FALLBACK_FAIL` | WebSocket connection fallback failed. | +| `WS_AUTH_FAIL` | WebSocket username/password not correct. | +| `NO_INTERNET_CONNECTION` | You do not have an internet connection. | +| `USER_NOT_LOGGED_IN` | Please log in to CometChat before calling this method. | + +## Calling Errors + +| Code | Message | +|------|---------| +| `CALL_ALREADY_INITIATED` | There is already a call in progress. | +| `CALL_IN_PROGRESS` | There is already a call in progress. | +| `NOT_INITIALIZED` | Please call CometChat.init() before calling any other methods. | +| `NOT_LOGGED_IN` | Please login before starting a call. | +| `SESSION_ID_REQUIRED` | Please make sure you are passing a correct session ID. | +| `CALL_SETTINGS_REQUIRED` | Please make sure you are passing the call settings object. | +| `JWT_NOT_FOUND` | There was an issue while fetching JWT from API. | + +## Message Errors + +| Code | Message | +|------|---------| +| `INVALID_RECEIVER_TYPE` | Receiver type can be `user` or `group`. | +| `REQUEST_IN_PROGRESS` | Request in progress. | +| `NOT_ENOUGH_PARAMETERS` | Timestamp, MessageId, or updatedAfter is required to use fetchNext(). | +| `INVALID_REASON_ID` | Invalid reasonId provided. | + +## User Errors + +| Code | Message | +|------|---------| +| `INVALID_STATUS_VALUE` | The status parameter accepts only `online` or `offline`. | +| `INVALID_DIRECTION_VALUE` | The direction parameter accepts only `both`, `blockedByMe`, or `hasBlockedMe`. | +| `EMPTY_USERS_LIST` | The users list needs to have at least one UID. | + +## Group Errors + +| Code | Message | +|------|---------| +| `NOT_A_GROUP` | Please use the Group class to construct a new group. | +| `INVALID_SCOPE_VALUE` | Scope can be `admin`, `moderator`, or `participant`. | +| `INVALID_GROUP_TYPE` | Group type can be `public`, `private`, `protected`, or `password`. | +| `ERR_EMPTY_GROUP_PASS` | Password is mandatory to join a group. | + +## Conversation Errors + +| Code | Message | +|------|---------| +| `INVALID_CONVERSATION_TYPE` | Conversation type can be `user` or `group`. | +| `CONVERSATION_NOT_FOUND` | Conversation not found. Check the value of conversationWith and conversationType. | + +## Receipt Errors + +| Code | Message | +|------|---------| +| `MISSING_PARAMETERS` | Expected 4 parameters, received 3. | +| `NO_WEBSOCKET_CONNECTION` | Connection to WebSocket server is broken. Please retry after some time. | +| `RECEIPTS_TEMPORARILY_BLOCKED` | Due to high load, receipts have been blocked for your app. | +| `UNKNOWN_ERROR_OCCURRED` | Unknown error occurred while marking a message as read. | + +## AI Feature Errors + +| Code | Message | +|------|---------| +| `NO_CONVERSATION_STARTER` | Unable to get conversation starter for this conversation. | +| `NO_SMART_REPLY` | Unable to get smart reply for this conversation. | +| `NO_CONVERSATION_SUMMARY` | Unable to get summary of the conversation. | +| `EMPTY_RESPONSE` | Unable to get a suggestion. | +| `ERROR_INVALID_AI_FEATURE` | The provided AI Feature cannot be null or empty. | + +## Extension Errors + +| Code | Message | +|------|---------| +| `ERROR_INVALID_EXTENSION` | The provided extension cannot be null or empty. | +| `ERROR_EXTENSION_NOT_FOUND` | The provided extension could not be found. | + +## Feature Restriction Errors + +| Code | Message | +|------|---------| +| `ERROR_INVALID_FEATURE` | The provided feature cannot be null or empty. | +| `ERROR_FEATURE_NOT_FOUND` | The provided feature could not be found. | + +## Network & API Errors + +| Code | Message | +|------|---------| +| `FAILED_TO_FETCH` | There is an unknown issue with the API request. Check your internet connection. | +| `TOO_MANY_REQUEST` | Too many requests. Wait before sending the next request. | +| `ERR_TOO_MANY_REQUESTS` | Rate limiting. See [Rate Limits](/sdk/javascript/rate-limits). | + +## Validation Errors + +These errors use dynamic codes based on the parameter name (e.g., `INVALID_UID`, `UID_IS_COMPULSORY`): + +| Pattern | Message | +|---------|---------| +| `INVALID_{param}` | The parameter should be a string / number / boolean / object / array. | +| `{param}_IS_COMPULSORY` | The parameter cannot be blank. Please provide a valid value. | +| `{param}_NOT_PROVIDED` | Please provide the required parameter. | +| `ERROR_{param}_EXCEEDED` | Limit exceeded max limit. | +| `INVALID_SEARCH_KEYWORD` | Invalid search keyword. Please provide a valid search keyword. | +| `MISSING_KEY` | The key is missing from the object. | + +## Prosody (WebSocket Server) Errors + +| Code | Message | +|------|---------| +| `ERROR_INVALID_SESSIONID` | The provided sessionId cannot be null or empty. | +| `ERROR_INVALID_TYPE` | The provided type cannot be null or empty. | +| `ERROR_INVALID_GROUPLIST` | Grouplist cannot be null or empty. | + +## General Errors + +| Code | Message | +|------|---------| +| `ERROR_IO_EXCEPTION` | I/O exception occurred. | +| `ERROR_JSON_EXCEPTION` | JSON parsing exception. | +| `ERROR_PASSWORD_MISSING` | Password is mandatory for a password group. | +| `ERROR_LIMIT_EXCEEDED` | Limit exceeded max limit. | +| `ERROR_INVALID_GUID` | Please provide a valid GUID. | +| `ERR_SETTINGS_HASH_OUTDATED` | Settings hash is outdated. | +| `ERR_NO_AUTH` | No authentication credentials found. | + +## Server-Side API Errors + +For REST API error codes (returned by the CometChat backend), see the [Error Guide](/articles/error-guide). Common server-side errors you may encounter in SDK responses: + +| Code | Description | +|------|-------------| +| `AUTH_ERR_EMPTY_APPID` | Empty App ID in headers | +| `AUTH_ERR_INVALID_APPID` | Invalid App ID or does not exist in region | +| `ERR_UID_NOT_FOUND` | User does not exist or is soft deleted | +| `ERR_GUID_NOT_FOUND` | Group does not exist | +| `ERR_NOT_A_MEMBER` | User is not a member of the group | +| `ERR_ALREADY_JOINED` | User has already joined the group | +| `ERR_MESSAGE_ID_NOT_FOUND` | Message does not exist | +| `ERR_PLAN_RESTRICTION` | Feature not available with current plan | +| `ERR_TOO_MANY_REQUESTS` | Rate limit exceeded | + +See the full list in the [Error Guide](/articles/error-guide). + +--- + +## Next Steps + + + + Common issues and solutions + + + Understand and handle rate limits + + + Complete server-side error code reference + + + Recommended patterns for error handling + + diff --git a/sdk/javascript/flag-message.mdx b/sdk/javascript/flag-message.mdx index 350149f5d..e0dd7c2b6 100644 --- a/sdk/javascript/flag-message.mdx +++ b/sdk/javascript/flag-message.mdx @@ -1,7 +1,24 @@ --- title: "Flag Message" +sidebarTitle: "Flag Message" +description: "Flag inappropriate messages for moderation review using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + + +```javascript +// Get available flag reasons +const reasons = await CometChat.getFlagReasons(); + +// Flag a message +await CometChat.flagMessage("MESSAGE_ID", { + reasonId: "spam", + remark: "Promotional content" +}); +``` + + ## Overview Flagging messages allows users to report inappropriate content to moderators or administrators. When a message is flagged, it appears in the [CometChat Dashboard](https://app.cometchat.com) under **Moderation > Flagged Messages** for review. @@ -44,29 +61,29 @@ sequenceDiagram Before flagging a message, retrieve the list of available flag reasons configured in your Dashboard: - - ```javascript + + ```typescript CometChat.getFlagReasons().then( - (reasons) => { + (reasons: CometChat.FlagReason[]) => { console.log("Flag reasons retrieved:", reasons); // reasons is an array of { id, reason } objects // Use these to populate your report dialog UI }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Failed to get flag reasons:", error); } ); ``` - - ```typescript + + ```javascript CometChat.getFlagReasons().then( - (reasons: CometChat.FlagReason[]) => { + (reasons) => { console.log("Flag reasons retrieved:", reasons); // reasons is an array of { id, reason } objects // Use these to populate your report dialog UI }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Failed to get flag reasons:", error); } ); @@ -76,7 +93,7 @@ Before flagging a message, retrieve the list of available flag reasons configure ### Response -The response is an array of flag reason objects: +The response is an array of `FlagReason` objects, each with an `id` and `reason` string: ```javascript [ @@ -94,37 +111,37 @@ The response is an array of flag reason objects: To flag a message, use the `flagMessage()` method with the message ID and a payload containing the reason: - - ```javascript - const messageId = "MESSAGE_ID_TO_FLAG"; - const payload = { - reasonId: "spam", // Required: ID from getFlagReasons() - remark: "This message contains promotional content" // Optional + + ```typescript + const messageId: string = "MESSAGE_ID_TO_FLAG"; + const payload: { reasonId: string; remark?: string } = { + reasonId: "spam", + remark: "This message contains promotional content" }; CometChat.flagMessage(messageId, payload).then( - (response) => { + (response: CometChat.FlagMessageResponse) => { console.log("Message flagged successfully:", response); }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Message flagging failed:", error); } ); ``` - - ```typescript - const messageId: string = "MESSAGE_ID_TO_FLAG"; - const payload: { reasonId: string; remark?: string } = { - reasonId: "spam", - remark: "This message contains promotional content" + + ```javascript + const messageId = "MESSAGE_ID_TO_FLAG"; + const payload = { + reasonId: "spam", // Required: ID from getFlagReasons() + remark: "This message contains promotional content" // Optional }; CometChat.flagMessage(messageId, payload).then( - (response: CometChat.FlagMessageResponse) => { + (response) => { console.log("Message flagged successfully:", response); }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Message flagging failed:", error); } ); @@ -142,6 +159,8 @@ To flag a message, use the `flagMessage()` method with the message ID and a payl ### Response +The `Promise` resolves with a success confirmation object: + ```javascript { "success": true, @@ -149,6 +168,8 @@ To flag a message, use the `flagMessage()` method with the message ID and a payl } ``` +The flagged message is a [`BaseMessage`](/sdk/reference/messages#basemessage) object. You can identify it using `getId()`, `getSender()` (returns a [`User`](/sdk/reference/entities#user)), and `getType()`. + ## Complete Example Here's a complete implementation showing how to build a report message flow: @@ -218,3 +239,22 @@ if (result.success) { showToast("Message reported successfully"); } ``` + +--- + +## Next Steps + + + + Automate content moderation with AI + + + Remove messages from conversations + + + Listen for incoming messages in real time + + + Send text, media, and custom messages + + diff --git a/sdk/javascript/group-add-members.mdx b/sdk/javascript/group-add-members.mdx index 98170bf0b..543b0e6ba 100644 --- a/sdk/javascript/group-add-members.mdx +++ b/sdk/javascript/group-add-members.mdx @@ -1,31 +1,51 @@ --- title: "Add Members To A Group" +sidebarTitle: "Add Members" +description: "Learn how to add members to a group, receive real-time member added events, and handle missed events using the CometChat JavaScript SDK." --- + +```javascript +// Add members to a group +const members = [ + new CometChat.GroupMember("UID", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT) +]; +await CometChat.addMembersToGroup("GUID", members, []); + +// Listen for member added events +CometChat.addGroupListener("listener", new CometChat.GroupListener({ + onMemberAddedToGroup: (message, userAdded, userAddedBy, userAddedIn) => { } +})); +``` + + +Add users to a group programmatically. Only admins and moderators can add members. The added users receive a notification and are immediately part of the group. ## Add Members to Group -You can add members to the group using the `addMembersToGroup()` method. This method takes the below parameters: +Use `addMembersToGroup()` to add members to a [Group](/sdk/reference/entities#group). -1. `GUID` - GUID of the group the members are to be added to. -2. `members` - This is a list of `GroupMember` objects. In order to add members, you need to create an object of the `GroupMember` class. The UID and the scope of the `GroupMember` are mandatory. -3. `bannedMembers` - This is the list of `UID's` that need to be banned from the Group. This can be set to `null` if there are no members to be banned. +| Parameter | Description | +|-----------|-------------| +| `GUID` | The group to add members to | +| `members` | Array of [GroupMember](/sdk/reference/entities#groupmember) objects (UID and scope required) | +| `bannedMembers` | Array of UIDs to ban (can be empty) | - -```javascript -let GUID = "GUID"; -let UID = "UID"; -let membersList = [ + +```typescript +let GUID: string = "GUID"; +let UID: string = "UID"; +let membersList: CometChat.GroupMember[] = [ new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT), ]; CometChat.addMembersToGroup(GUID, membersList, []).then( - (response) => { + (response: Object) => { console.log("response", response); }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Something went wrong", error); } ); @@ -33,19 +53,19 @@ CometChat.addMembersToGroup(GUID, membersList, []).then( - -```typescript -let GUID: string = "GUID"; -let UID: string = "UID"; -let membersList: CometChat.GroupMember[] = [ + +```javascript +let GUID = "GUID"; +let UID = "UID"; +let membersList = [ new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT), ]; CometChat.addMembersToGroup(GUID, membersList, []).then( - (response: Object) => { + (response) => { console.log("response", response); }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Something went wrong", error); } ); @@ -55,31 +75,30 @@ CometChat.addMembersToGroup(GUID, membersList, []).then( -It will return a Array which will contain the `UID` of the users and the value will either be `success` or an error message describing why the operation to add the user to the group. +The method returns a response object where each key is a UID and the value is either `"success"` or an error message describing why that user couldn't be added. ## Real-Time Group Member Added Events -*In other words, as a member of a group, how do I know when someone is added to the group when my app is running?* - - When a group member is added by another member, this event is triggered. When a user joins a group on their own, the joined event is triggered. - -To receive real-time events whenever a new member is added to a group, you need to implement the `onMemberAddedToGroup()` methods of the `GroupListener` class. - -`onMemberAddedToGroup()` - This method is triggered when any user is added to the group so that the logged in user is informed of the other members added to the group. +Implement `onMemberAddedToGroup()` in `GroupListener` to receive real-time notifications when members are added. - -```javascript -var listenerID = "UNIQUE_LISTENER_ID"; + +```typescript +const listenerID: string = "UNIQUE_LISTENER_ID"; CometChat.addGroupListener( listenerID, new CometChat.GroupListener({ - onMemberAddedToGroup: (message, userAdded, userAddedBy, userAddedIn) => { + onMemberAddedToGroup: ( + message: CometChat.Action, + userAdded: CometChat.User, + userAddedBy: CometChat.User, + userAddedIn: CometChat.Group + ) => { console.log("User joined", { message, userAdded, @@ -93,19 +112,14 @@ CometChat.addGroupListener( - -```typescript -var listenerID: string = "UNIQUE_LISTENER_ID"; + +```javascript +const listenerID = "UNIQUE_LISTENER_ID"; CometChat.addGroupListener( listenerID, new CometChat.GroupListener({ - onMemberAddedToGroup: ( - message: CometChat.Action, - userAdded: CometChat.User, - userAddedBy: CometChat.User, - userAddedIn: CometChat.Group - ) => { + onMemberAddedToGroup: (message, userAdded, userAddedBy, userAddedIn) => { console.log("User joined", { message, userAdded, @@ -121,15 +135,40 @@ CometChat.addGroupListener( -## Member Added to Group event in Message History + +Always remove group listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. -*In other words, as a member of a group, how do I know when someone is added to the group when my app is not running?* +```javascript +CometChat.removeGroupListener("UNIQUE_LISTENER_ID"); +``` + -When you retrieve the list of previous messages if a member has been added to any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. +## Missed Member Added Events -For the group member added event, in the `Action` object received, the following fields can help you get the relevant information- +When fetching previous messages, member additions appear as [Action](/sdk/reference/messages#action) messages (a subclass of `BaseMessage`). + +| Field | Value/Type | Description | +|-------|------------|-------------| +| `action` | `"added"` | The action type | +| `actionOn` | [User](/sdk/reference/entities#user) | The user who was added | +| `actionBy` | [User](/sdk/reference/entities#user) | The user who added the member | +| `actionFor` | [Group](/sdk/reference/entities#group) | The group the member was added to | + +--- -1. `action` - `added` -2. `actionOn` - User object containing the details of the user who was added to the group -3. `actionBy` - User object containing the details of the user who added the member to the group -4. `actionFor` - Group object containing the details of the group to which the member was added +## Next Steps + + + + Remove or ban members from a group + + + Promote or demote group members + + + Fetch the list of members in a group + + + Allow users to join public or password-protected groups + + diff --git a/sdk/javascript/group-change-member-scope.mdx b/sdk/javascript/group-change-member-scope.mdx index 422ae34b8..64854db41 100644 --- a/sdk/javascript/group-change-member-scope.mdx +++ b/sdk/javascript/group-change-member-scope.mdx @@ -1,83 +1,82 @@ --- title: "Change Member Scope" +sidebarTitle: "Change Scope" +description: "Learn how to change group member roles (admin, moderator, participant) and receive real-time scope change events using the CometChat JavaScript SDK." --- + - -## Change Scope of a Group Member - -In order to change the scope of a group member, you can use the `changeGroupMemberScope()`. - - - ```javascript -let GUID = "GUID"; -let UID = "UID"; -let scope = CometChat.GROUP_MEMBER_SCOPE.ADMIN; +// Change member scope to admin +await CometChat.updateGroupMemberScope("GUID", "UID", CometChat.GROUP_MEMBER_SCOPE.ADMIN); -CometChat.updateGroupMemberScope(GUID, UID, scope).then( -response => { - console.log("Group member scopped changed", response); -}, error => { - console.log("Group member scopped changed failed", error); -} -); +// Listen for scope change events +CometChat.addGroupListener("listener", new CometChat.GroupListener({ + onGroupMemberScopeChanged: (message, changedUser, newScope, oldScope, changedGroup) => { } +})); ``` + - +Promote or demote group members between admin, moderator, and participant roles. Only admins can change member scopes, and only the group owner can change admin scopes. + +## Change Scope of a Group Member + +Use `updateGroupMemberScope()` to change a member's role within a [Group](/sdk/reference/entities#group). + ```typescript let GUID: string = "GUID"; let UID: string = "UID"; -CometChat.updateGroupMemberScope(GUID, UID, CometChat.GroupMemberScope.Admin).then( +CometChat.updateGroupMemberScope(GUID, UID, CometChat.GROUP_MEMBER_SCOPE.ADMIN).then( (response: boolean) => { - console.log("Group member scopped changed", response); + console.log("Group member scope changed", response); }, (error: CometChat.CometChatException) => { - console.log("Group member scopped changed failed", error); + console.log("Group member scope change failed", error); } ); ``` + +```javascript +let GUID = "GUID"; +let UID = "UID"; +let scope = CometChat.GROUP_MEMBER_SCOPE.ADMIN; + +CometChat.updateGroupMemberScope(GUID, UID, scope).then( +response => { + console.log("Group member scope changed", response); +}, error => { + console.log("Group member scope change failed", error); +} +); +``` + + + This method takes the below parameters: | Parameter | Description | | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `UID` | The UID of the member whose scope you would like to change | -| `GUID` | The GUID of the group for which the member's scope needs to be changed | -| `scope` | The updated scope of the member. This can be either of the 3 values: 1.`CometChat.SCOPE.ADMIN`2.`CometChat.SCOPE.MODERATOR` 3.`CometChat.SCOPE.PARTICIPANT` | +| `UID` | The UID of the member whose scope you want to change | +| `GUID` | The GUID of the group | +| `scope` | The new scope: `CometChat.GROUP_MEMBER_SCOPE.ADMIN`, `CometChat.GROUP_MEMBER_SCOPE.MODERATOR`, or `CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT` | -The default scope of any member is `participant`. Only the **Admin** of the group can change the scope of any participant in the group. +The default scope is `participant`. Only Admins can change member scopes. -## Real-Time Group Member Scope Changed Events +On success, the method resolves with `true` (boolean). -*In other words, as a member of a group, how do I know when someone's scope is changed when my app is running?* +## Real-Time Group Member Scope Changed Events -In order to receive real-time events for the change member scope event, you will need to override the `onGroupMemberScopeChanged()` method of the `GroupListener` class +Implement `onGroupMemberScopeChanged()` in `GroupListener` to receive real-time notifications when a member's scope changes. - -```javascript -let listenerID = "UNIQUE_LISTENER_ID"; - -CometChat.addGroupListener( -listenerID, -new CometChat.GroupListener({ - onGroupMemberScopeChanged: (message, changedUser, newScope, oldScope, changedGroup) => { - console.log("User joined", {message, changedUser, newScope, oldScope, changedGroup}); - } -}) -); -``` - - - ```typescript let listenerID: string = "UNIQUE_LISTENER_ID"; @@ -86,7 +85,7 @@ CometChat.addGroupListener( listenerID, new CometChat.GroupListener({ onGroupMemberScopeChanged: (message: CometChat.Action, changedUser: CometChat.User, newScope: string, oldScope: string, changedGroup: CometChat.Group) => { - console.log("User joined", { message, changedUser, newScope, oldScope, changedGroup }); + console.log("Scope changed", { message, changedUser, newScope, oldScope, changedGroup }); } }) ); @@ -94,19 +93,60 @@ CometChat.addGroupListener( + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; + +CometChat.addGroupListener( +listenerID, +new CometChat.GroupListener({ + onGroupMemberScopeChanged: (message, changedUser, newScope, oldScope, changedGroup) => { + console.log("Scope changed", {message, changedUser, newScope, oldScope, changedGroup}); + } +}) +); +``` + + + + +Always remove group listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + +```javascript +CometChat.removeGroupListener("UNIQUE_LISTENER_ID"); +``` + + ## Missed Group Member Scope Changed Events -*In other words, as a member of a group, how do I know when someone's scope is changed when my app is not running?* +When fetching previous messages, scope changes appear as [Action](/sdk/reference/messages#action) messages (a subclass of `BaseMessage`). -When you retrieve the list of previous messages if a member's scope has been changed for any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. +| Field | Value/Type | Description | +|-------|------------|-------------| +| `action` | `"scopeChanged"` | The action type | +| `actionOn` | [User](/sdk/reference/entities#user) | The user whose scope changed | +| `actionBy` | [User](/sdk/reference/entities#user) | The user who changed the scope | +| `actionFor` | [Group](/sdk/reference/entities#group) | The group | +| `oldScope` | `string` | The previous scope | +| `newScope` | `string` | The new scope | -For the group member scope changed event, in the `Action` object received, the following fields can help you get the relevant information- +--- -1. `action` - `scopeChanged` -2. `actionOn` - User object containing the details of the user whose scope has been changed -3. `actionBy` - User object containing the details of the user who changed the scope of the member -4. `actionFor` - Group object containing the details of the group in which the member scope was changed -5. `oldScope` - The original scope of the member -6. `newScope` - The updated scope of the member +## Next Steps + + + + Transfer ownership of a group to another member + + + Remove or ban members from a group + + + Add new members to a group + + + Fetch the list of members in a group + + diff --git a/sdk/javascript/group-kick-ban-members.mdx b/sdk/javascript/group-kick-ban-members.mdx index 7433686cd..a4099d69f 100644 --- a/sdk/javascript/group-kick-ban-members.mdx +++ b/sdk/javascript/group-kick-ban-members.mdx @@ -1,46 +1,41 @@ --- title: "Ban Or Kick Member From A Group" +sidebarTitle: "Kick & Ban Members" +description: "Learn how to kick, ban, and unban group members, fetch banned member lists, and receive real-time events using the CometChat JavaScript SDK." --- + +```javascript +// Kick a member +await CometChat.kickGroupMember("GUID", "UID"); + +// Ban a member +await CometChat.banGroupMember("GUID", "UID"); -There are certain actions that can be performed on the group members: +// Unban a member +await CometChat.unbanGroupMember("GUID", "UID"); -1. Kick a member from the group -2. Ban a member from the group -3. Unban a member from the group -4. Update the scope of the member of the group +// Fetch banned members +const request = new CometChat.BannedMembersRequestBuilder("GUID").setLimit(30).build(); +const bannedMembers = await request.fetchNext(); +``` + -All the above actions can only be performed by the **Admin** or the **Moderator** of the group. +Remove members from a group by kicking or banning them. Kicked users can rejoin; banned users cannot until they're unbanned. Only admins and moderators can perform these actions. ## Kick a Group Member -The Admin or Moderator of a group can kick a member out of the group using the `kickGroupMember()` method. +Admins or Moderators can remove a member using `kickGroupMember()`. The kicked user can rejoin the group later. - -```javascript -var GUID = "GUID"; -var UID = "UID"; - -CometChat.kickGroupMember(GUID, UID).then( -response => { - console.log("Group member kicked successfully", response); -}, error => { - console.log("Group member kicking failed with error", error); -} -); -``` - - - ```typescript let GUID: string = "GUID"; let UID: string = "UID"; CometChat.kickGroupMember(GUID, UID).then( - (response: Object) => { + (response: boolean) => { console.log("Group member kicked successfully", response); }, (error: CometChat.CometChatException) => { console.log("Group member kicking failed with error", error); @@ -50,9 +45,25 @@ CometChat.kickGroupMember(GUID, UID).then( + +```javascript +let GUID = "GUID"; +let UID = "UID"; + +CometChat.kickGroupMember(GUID, UID).then( + (response) => { + console.log("Group member kicked successfully", response); + }, (error) => { + console.log("Group member kicking failed with error", error); + } +); +``` + + + -The `kickGroupMember()` takes following parameters +The `kickGroupMember()` takes the following parameters: | Parameter | Description | | --------- | ----------------------------------------------------- | @@ -61,34 +72,20 @@ The `kickGroupMember()` takes following parameters The kicked user will be no longer part of the group and can not perform any actions in the group, but the kicked user can rejoin the group. +On success, the method resolves with a `boolean` value (`true`) confirming the operation. + ## Ban a Group Member -The Admin or Moderator of the group can ban a member from the group using the `banGroupMember()` method. +Admins or Moderators can ban a member using `banGroupMember()`. Unlike kicked users, banned users cannot rejoin until unbanned. - -```javascript -var GUID = "GUID"; -var UID = "UID"; - -CometChat.banGroupMember(GUID, UID).then( -response => { - console.log("Group member banned successfully", response); -}, error => { - console.log("Group member banning failed with error", error); -} -); -``` - - - ```typescript let GUID: string = "GUID"; let UID: string = "UID"; CometChat.banGroupMember(GUID, UID).then( - (response: Object) => { + (response: boolean) => { console.log("Group member banned successfully", response); }, (error: CometChat.CometChatException) => { console.log("Group member banning failed with error", error); @@ -98,45 +95,47 @@ CometChat.banGroupMember(GUID, UID).then( + +```javascript +const GUID = "GUID"; +const UID = "UID"; + +CometChat.banGroupMember(GUID, UID).then( +response => { + console.log("Group member banned successfully", response); +}, error => { + console.log("Group member banning failed with error", error); +} +); +``` + + + The `banGroupMember()` method takes the following parameters: | Parameter | Description | | --------- | ------------------------------------------------------ | -| `UID` | The UID of the user to be banned. | -| `GUID` | The GUID of the group from which user is to be banned. | +| `UID` | The UID of the user to be banned | +| `GUID` | The GUID of the group from which user is to be banned | The banned user will be no longer part of the group and can not perform any actions in the group. A banned user cannot rejoin the same group without being unbanned. -## Unban a Banned Group Member from a Group - -Only Admin or Moderators of the group can unban a previously banned member from the group using the `unbanGroupMember()` method. - - - -```javascript -var GUID = "GUID"; -var UID = "UID"; +On success, the method resolves with a `boolean` value (`true`) confirming the operation. -CometChat.unbanGroupMember(GUID, UID).then( -response => { - console.log("Group member unbanned successfully", response); -}, error => { - console.log("Group member unbanning failed with error", error); -} -); -``` +## Unban a Group Member - +Admins or Moderators can unban a previously banned member using `unbanGroupMember()`. + ```typescript let GUID: string = "GUID"; let UID: string = "UID"; CometChat.unbanGroupMember(GUID, UID).then( - (response: Object) => { + (response: boolean) => { console.log("Group member unbanned successfully", response); }, (error: CometChat.CometChatException) => { console.log("Group member unbanning failed with error", error); @@ -146,41 +145,44 @@ CometChat.unbanGroupMember(GUID, UID).then( + +```javascript +const GUID = "GUID"; +const UID = "UID"; + +CometChat.unbanGroupMember(GUID, UID).then( +response => { + console.log("Group member unbanned successfully", response); +}, error => { + console.log("Group member unbanning failed with error", error); +} +); +``` + + + The `unbanGroupMember()` method takes the following parameters | Parameter | Description | | --------- | ---------------------------------------------------- | -| `UID` | The UID of the user to be unbanned. | -| `GUID` | The UID of the group from which user is to be banned | +| `UID` | The UID of the user to be unbanned | +| `GUID` | The GUID of the group from which user is to be unbanned | -The unbanned user can now rejoin the group. +Once unbanned, the user can rejoin the group. -## Get List of Banned Members for a Group +On success, the method resolves with a `boolean` value (`true`) confirming the operation. -In order to fetch the list of banned groups members for a group, you can use the `BannedGroupMembersRequest` class. To use this class i.e to create an object of the BannedGroupMembersRequest class, you need to use the `BannedGroupMembersRequestBuilder` class. The `BannedGroupMembersRequestBuilder` class allows you to set the parameters based on which the banned group members are to be fetched. - -The `BannedGroupMembersRequestBuilder` class allows you to set the below parameters: +## Get List of Banned Members for a Group -The `GUID` of the group for which the banned members are to be fetched must be specified in the constructor of the `GroupMembersRequestBuilder` class. +Use `BannedMembersRequestBuilder` to fetch banned members of a [Group](/sdk/reference/entities#group). The GUID must be specified in the constructor. ### Set Limit -This method sets the limit i.e. the number of banned members that should be fetched in a single iteration. +Sets the number of banned members to fetch per request. - -```javascript -let GUID = "GUID"; -let limit = 30; -let bannedGroupMembersRequest = new CometChat.BannedMembersRequestBuilder(GUID) - .setLimit(limit) - .build(); -``` - - - ```typescript let GUID: string = "GUID"; @@ -192,26 +194,24 @@ let bannedGroupMembersRequest: CometChat.BannedMembersRequest = new CometChat.Ba - - -### Set Search Keyword - -This method allows you to set the search string based on which the banned group members are to be fetched. - - - + ```javascript let GUID = "GUID"; let limit = 30; -let searchKeyword = "super"; let bannedGroupMembersRequest = new CometChat.BannedMembersRequestBuilder(GUID) .setLimit(limit) - .setSearchKeyword(searchKeyword) .build(); ``` + + +### Set Search Keyword + +Filters banned members by a search string. + + ```typescript let GUID: string = "GUID"; @@ -225,32 +225,24 @@ let bannedGroupMembersRequest: CometChat.BannedMembersRequest = new CometChat.Ba - - -Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the `BannedGroupMembersRequest` class. - -Once you have the object of the `BannedGroupMembersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `GroupMember` objects containing n number of banned members where n is the limit set in the builder class. - - - + ```javascript let GUID = "GUID"; let limit = 30; -let bannedMembersRequest = new CometChat.BannedMembersRequestBuilder(GUID) - .setLimit(limit) - .build(); - -bannedMembersRequest.fetchNext().then( -bannedMembers => { - console.log("Banned Group Member list fetched successfully:", bannedMembers); -}, error => { - console.log("Banned Group Member list fetching failed with exception:", error); -} -); +let searchKeyword = "super"; +let bannedGroupMembersRequest = new CometChat.BannedMembersRequestBuilder(GUID) + .setLimit(limit) + .setSearchKeyword(searchKeyword) + .build(); ``` + + +Once configured, call `build()` to create the request, then `fetchNext()` to retrieve banned members. + + ```typescript let GUID: string = "GUID"; @@ -272,39 +264,19 @@ bannedGroupMembersRequest.fetchNext().then( -## Real-Time Group Member Kicked/Banned Events +The `fetchNext()` method returns an array of [`GroupMember`](/sdk/reference/entities#groupmember) objects representing the banned members of the group. -*In other words, as a member of a group, how do I know when someone is banned/kicked when my app is running?* +## Real-Time Kick/Ban/Unban Events -In order to get real-time events for the kick/ban/unban group members you need to override the following methods of the `GroupListener` class. +Implement these `GroupListener` methods to receive real-time notifications: -1. `onGroupMemberKicked()` - triggered when any group member has been kicked. -2. `onGroupMemberBanned()` - triggered when any group member has been banned. -3. `onGroupMemberUnbanned()` - triggered when any group member has been unbanned. +| Method | Triggered When | +|--------|----------------| +| `onGroupMemberKicked()` | A member is kicked | +| `onGroupMemberBanned()` | A member is banned | +| `onGroupMemberUnbanned()` | A member is unbanned | - -```javascript -let listenerID = "UNIQUE_LISTENER_ID"; - -CometChat.addGroupListener( -listenerID, -new CometChat.GroupListener({ - onGroupMemberKicked: (message, kickedUser, kickedBy, kickedFrom) => { - console.log("User kicked", { message, kickedUser, kickedBy, kickedFrom }); - }, - onGroupMemberBanned: (message, bannedUser, bannedBy, bannedFrom) => { - console.log("User banned", { message, bannedUser, bannedBy, bannedFrom }); - }, - onGroupMemberUnbanned: (message, unbannedUser, unbannedBy,unbannedFrom) => { - console.log("User unbanned", {message, unbannedUser, unbannedBy, unbannedFrom}); - } -}) -); -``` - - - ```typescript let listenerID: string = "UNIQUE_LISTENER_ID"; @@ -327,31 +299,84 @@ CometChat.addGroupListener( + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; + +CometChat.addGroupListener( +listenerID, +new CometChat.GroupListener({ + onGroupMemberKicked: (message, kickedUser, kickedBy, kickedFrom) => { + console.log("User kicked", { message, kickedUser, kickedBy, kickedFrom }); + }, + onGroupMemberBanned: (message, bannedUser, bannedBy, bannedFrom) => { + console.log("User banned", { message, bannedUser, bannedBy, bannedFrom }); + }, + onGroupMemberUnbanned: (message, unbannedUser, unbannedBy,unbannedFrom) => { + console.log("User unbanned", {message, unbannedUser, unbannedBy, unbannedFrom}); + } +}) +); +``` + + + + +Always remove group listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + +```javascript +CometChat.removeGroupListener("UNIQUE_LISTENER_ID"); +``` + + ## Missed Group Member Kicked/Banned Events -*In other words, as a member of a group, how do I know when someone is banned/kicked when my app is not running?* +When fetching previous messages, kick/ban/unban actions appear as [Action](/sdk/reference/messages#action) messages (a subclass of `BaseMessage`). -When you retrieve the list of previous messages if a member has been kicked/banned/unbanned from any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. +**Kicked event:** -For group member kicked event, the details can be obtained using the below fields of the `Action` class- +| Field | Value/Type | Description | +|-------|------------|-------------| +| `action` | `"kicked"` | The action type | +| `actionBy` | [User](/sdk/reference/entities#user) | The user who kicked the member | +| `actionOn` | [User](/sdk/reference/entities#user) | The member who was kicked | +| `actionFor` | [Group](/sdk/reference/entities#group) | The group | -1. `action` - `kicked` -2. `actionBy` - User object containing the details of the user who has kicked the member -3. `actionOn` - User object containing the details of the member that has been kicked -4. `actionFor` - Group object containing the details of the Group from which the member was kicked +**Banned event:** -For group member banned event, the details can be obtained using the below fields of the `Action` class- +| Field | Value/Type | Description | +|-------|------------|-------------| +| `action` | `"banned"` | The action type | +| `actionBy` | [User](/sdk/reference/entities#user) | The user who banned the member | +| `actionOn` | [User](/sdk/reference/entities#user) | The member who was banned | +| `actionFor` | [Group](/sdk/reference/entities#group) | The group | -1. `action` - `banned` -2. `actionBy` - User object containing the details of the user who has banned the member -3. `actionOn` - User object containing the details of the member that has been banned -4. `actionFor` - Group object containing the details of the Group from which the member was banned +**Unbanned event:** -For group member unbanned event, the details can be obtained using the below fields of the `Action` class- +| Field | Value/Type | Description | +|-------|------------|-------------| +| `action` | `"unbanned"` | The action type | +| `actionBy` | [User](/sdk/reference/entities#user) | The user who unbanned the member | +| `actionOn` | [User](/sdk/reference/entities#user) | The member who was unbanned | +| `actionFor` | [Group](/sdk/reference/entities#group) | The group | + +--- -1. `action` - `unbanned` -2. `actionBy` - User object containing the details of the user who has unbanned the member -3. `actionOn` - User object containing the details of the member that has been unbanned -4. `actionFor` - Group object containing the details of the Group from which the member was unbanned +## Next Steps + + + + Add new members to a group + + + Promote or demote group members + + + Fetch the list of members in a group + + + Allow members to leave a group + + diff --git a/sdk/javascript/groups-overview.mdx b/sdk/javascript/groups-overview.mdx index 4f3d1c363..94faae6fd 100644 --- a/sdk/javascript/groups-overview.mdx +++ b/sdk/javascript/groups-overview.mdx @@ -1,10 +1,71 @@ --- title: "Groups" sidebarTitle: "Overview" +description: "Overview of group management in the CometChat JavaScript SDK including group types, member roles, and available operations." --- + +| Field | Value | +| --- | --- | +| Package | `@cometchat/chat-sdk-javascript` | +| Key Classes | `CometChat.Group` | +| Group Types | `PUBLIC`, `PRIVATE`, `PASSWORD` | +| Member Roles | `owner`, `admin`, `moderator`, `participant` | +| Key Methods | `createGroup()`, `joinGroup()`, `leaveGroup()`, `deleteGroup()` | +| Prerequisites | SDK initialized, user logged in | +| Related | [Create Group](/sdk/javascript/create-group), [Join Group](/sdk/javascript/join-group), [Retrieve Groups](/sdk/javascript/retrieve-groups) | -Groups help your users to converse together in a single space. You can have three types of groups- private, public and password protected. + -Each group includes three kinds of users- owner, moderator, member. +Groups let users converse together in a shared space. CometChat supports three group types (public, private, password-protected) and four member roles with different permission levels. + +## Group Types + +| Type | Description | Join Behavior | +|------|-------------|---------------| +| **Public** | Open to all users | Any user can join without approval | +| **Private** | Invite-only | Users must be added by admin/moderator | +| **Password** | Protected by password | Users must provide correct password to join | + +## Member Roles + +| Role | Permissions | +|------|-------------| +| **Owner** | Full control: manage members, settings, delete group. Cannot leave without transferring ownership. | +| **Admin** | Manage members (add, kick, ban), change member scope, update group settings | +| **Moderator** | Kick and ban members, moderate content | +| **Member** | Send/receive messages, leave group | + +## Available Operations + +- [Create a Group](/sdk/javascript/create-group) — Create public, private, or password-protected groups +- [Join a Group](/sdk/javascript/join-group) — Join existing groups as a participant +- [Leave a Group](/sdk/javascript/leave-group) — Leave a group you're a member of +- [Update a Group](/sdk/javascript/update-group) — Update group name, description, icon, and settings +- [Delete a Group](/sdk/javascript/delete-group) — Permanently delete a group (owner only) +- [Transfer Ownership](/sdk/javascript/transfer-group-ownership) — Transfer group ownership to another member +- [Retrieve Groups](/sdk/javascript/retrieve-groups) — Fetch and filter the list of groups +- [Retrieve Group Members](/sdk/javascript/retrieve-group-members) — Get the member list for a group +- [Add Members](/sdk/javascript/group-add-members) — Add users to a group +- [Kick & Ban Members](/sdk/javascript/group-kick-ban-members) — Remove or ban members from a group +- [Change Member Scope](/sdk/javascript/group-change-member-scope) — Promote or demote members + +--- + +## Next Steps + + + + Create public, private, or password-protected groups + + + Join existing groups as a participant + + + Fetch and filter the list of groups + + + Get the member list for a group + + diff --git a/sdk/javascript/initialization.mdx b/sdk/javascript/initialization.mdx new file mode 100644 index 000000000..4e196d13e --- /dev/null +++ b/sdk/javascript/initialization.mdx @@ -0,0 +1,167 @@ +--- +title: "Initialization" +description: "Configure and initialize the CometChat JavaScript SDK with your App ID and settings." +--- + +The `init()` method initializes the SDK and must be called before any other CometChat method. Call it once at app startup, typically in your entry file (`index.js`, `main.js`, or `App.js`). + +## Basic Initialization + + + +```typescript +let appID: string = "APP_ID"; +let region: string = "APP_REGION"; + +let appSetting: CometChat.AppSettings = new CometChat.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion(region) + .autoEstablishSocketConnection(true) + .build(); + +CometChat.init(appID, appSetting).then( + (initialized: boolean) => { + console.log("Initialization completed successfully", initialized); + }, + (error: CometChat.CometChatException) => { + console.log("Initialization failed with error:", error); + } +); +``` + + + + +```javascript +let appID = "APP_ID"; +let region = "APP_REGION"; + +let appSetting = new CometChat.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion(region) + .autoEstablishSocketConnection(true) + .build(); + +CometChat.init(appID, appSetting).then( + () => { + console.log("Initialization completed successfully"); + }, + (error) => { + console.log("Initialization failed with error:", error); + } +); +``` + + + + +```javascript +let appID = "APP_ID"; +let region = "APP_REGION"; + +let appSetting = new CometChat.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion(region) + .autoEstablishSocketConnection(true) + .build(); + +try { + await CometChat.init(appID, appSetting); + console.log("Initialization completed successfully"); +} catch (error) { + console.log("Initialization failed with error:", error); +} +``` + + + + + +Replace `APP_ID` with your CometChat App ID and `APP_REGION` with your Region from the [Dashboard](https://app.cometchat.com). + + +`CometChat.init()` must be called before any other SDK method. Calling `login()`, `sendMessage()`, or registering listeners before `init()` will fail. + + +## Parameters + +| Parameter | Type | Description | +|-----------|------|-------------| +| appID | string | Your CometChat App ID | +| appSetting | AppSettings | Configuration object built with AppSettingsBuilder | + +## AppSettings Options + +| Method | Description | Default | +|--------|-------------|---------| +| `setRegion(region)` | Region where your app was created (`us`, `eu`, `in`) | Required | +| `subscribePresenceForAllUsers()` | Subscribe to presence events for all users | — | +| `subscribePresenceForRoles(roles)` | Subscribe to presence for specific roles | — | +| `subscribePresenceForFriends()` | Subscribe to presence for friends only | — | +| `autoEstablishSocketConnection(bool)` | Let SDK manage WebSocket connections | `true` | +| `overrideAdminHost(adminHost)` | Custom admin URL (dedicated deployment) | — | +| `overrideClientHost(clientHost)` | Custom client URL (dedicated deployment) | — | +| `setStorageMode(storageMode)` | Local storage mode (`CometChat.StorageMode.SESSION` for session storage) | — | + +### Presence Subscription + +Choose how to subscribe to user presence (online/offline status): + +```javascript +// All users +new CometChat.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion(region) + .build(); + +// Specific roles +new CometChat.AppSettingsBuilder() + .subscribePresenceForRoles(["admin", "moderator"]) + .setRegion(region) + .build(); + +// Friends only +new CometChat.AppSettingsBuilder() + .subscribePresenceForFriends() + .setRegion(region) + .build(); +``` + +See [User Presence](/sdk/javascript/user-presence) for more details. + +### WebSocket Connection + +By default, the SDK manages WebSocket connections automatically. To manage them manually: + +```javascript +let appSetting = new CometChat.AppSettingsBuilder() + .setRegion(region) + .autoEstablishSocketConnection(false) + .build(); +``` + +See [Managing WebSocket Connections](/sdk/javascript/managing-web-sockets-connections-manually) for manual control. + +### Session Storage + +Use session storage instead of local storage (data clears when browser closes): + +```javascript +let appSetting = new CometChat.AppSettingsBuilder() + .setRegion(region) + .setStorageMode(CometChat.StorageMode.SESSION) + .build(); +``` + +--- + +## Next Steps + + + + Log in users with Auth Key or Auth Token + + + Setup for Next.js, Nuxt, and Ionic + + diff --git a/sdk/javascript/installation.mdx b/sdk/javascript/installation.mdx new file mode 100644 index 000000000..c0ece3d34 --- /dev/null +++ b/sdk/javascript/installation.mdx @@ -0,0 +1,59 @@ +--- +title: "Installation" +description: "Add the CometChat JavaScript SDK to your project using npm or CDN." +--- + +## Package Manager + +Install the SDK using npm: + +```bash +npm install @cometchat/chat-sdk-javascript +``` + +Then import the `CometChat` object wherever you need it: + +```javascript +import { CometChat } from "@cometchat/chat-sdk-javascript"; +``` + +## CDN + +Include the CometChat JavaScript library directly in your HTML: + +```html + +``` + +When using the CDN, `CometChat` is available as a global variable. + +## Get Your Credentials + +Before initializing the SDK, get your credentials from the [CometChat Dashboard](https://app.cometchat.com): + +1. Sign up or log in +2. Create a new app (or use an existing one) +3. Go to **API & Auth Keys** and note your: + - App ID + - Region + - Auth Key + + +**Auth Key** is for development/testing only. In production, generate **Auth Tokens** on your server using the REST API. Never expose Auth Keys in production client code. + + +--- + +## Next Steps + + + + Configure and initialize the SDK + + + Setup for Next.js, Nuxt, and Ionic + + diff --git a/sdk/javascript/interactive-messages.mdx b/sdk/javascript/interactive-messages.mdx index 2f083f734..d4efe0c47 100644 --- a/sdk/javascript/interactive-messages.mdx +++ b/sdk/javascript/interactive-messages.mdx @@ -1,203 +1,548 @@ --- title: "Interactive Messages" +sidebarTitle: "Interactive Messages" +description: "Send and receive interactive messages with embedded forms, buttons, and other UI elements using the CometChat JavaScript SDK." --- + + +| Component | Description | +| --- | --- | +| [`InteractiveMessage`](/sdk/reference/messages#interactivemessage) | Message containing interactive UI elements (forms, buttons, etc.) | +| `InteractionGoal` | Defines the desired outcome of user interactions | +| `Interaction` | Represents a single user action on an interactive element | + +```javascript +let receiverId = "UID"; +let receiverType = CometChat.RECEIVER_TYPE.USER; +let interactiveData = {}; + +// Create and send an interactive form +const interactiveMessage = new CometChat.InteractiveMessage( + receiverId, + receiverType, + "form", + interactiveData +); +await CometChat.sendInteractiveMessage(interactiveMessage); +// Listen for interactive messages +CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({ + onInteractiveMessageReceived: (message) => console.log("Received:", message), + onInteractionGoalCompleted: (receipt) => console.log("Goal completed:", receipt) +})); +``` + -An InteractiveMessage is a specialised object that encapsulates an interactive unit within a chat message, such as an embedded form that users can fill out directly within the chat interface. This enhances user engagement by making the chat experience more interactive and responsive to user input. +Interactive messages embed UI elements like forms, buttons, and dropdowns directly within chat messages. Users can interact with these elements without leaving the conversation, enabling surveys, quick actions, and data collection. ## InteractiveMessage -`InteractiveMessage` is a chat message with embedded interactive content. It can contain various properties: +The [`InteractiveMessage`](/sdk/reference/messages#interactivemessage) class represents a message with embedded interactive content. -| Parameter | Description | Required | -| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- | -| receiverId | The UID or GUID of the recipient | Yes | -| receiverType | The type of the receiver to whom the message is to be sent i.e CometChatConstants.RECEIVER\_TYPE\_USER (user) or CometChatConstants.RECEIVER\_TYPE\_GROUP (group) | Yes | -| messageType | The type of the message that needs to be sent | Yes | -| interactiveData | A JSONObject holding structured data for the interactive element. | Yes | -| allowSenderInteraction | A boolean determining whether the message sender can interact with the message by default it is set to false. | Optional (Default: false) | -| interactionGoal | An InteractionGoal object encapsulating the intended outcome of interacting with the InteractiveMessage by default it is set to none | Optional (Default: none) | +| Parameter | Type | Description | Required | +| --- | --- | --- | --- | +| `receiverId` | `string` | UID of user or GUID of group | Yes | +| `receiverType` | `string` | `CometChat.RECEIVER_TYPE.USER` or `GROUP` | Yes | +| `messageType` | `string` | Type identifier (e.g., `"form"`, `"card"`) | Yes | +| `interactiveData` | `Object` | JSON structure defining the interactive elements | Yes | +| `allowSenderInteraction` | `boolean` | Whether sender can interact with the message | No (default: `false`) | +| `interactionGoal` | `InteractionGoal` | Defines when the interaction is considered complete | No (default: `none`) | -## Interaction +## Send an Interactive Message -An `Interaction` represents a user action involved with an `InteractiveMessage`. It includes: +Use `sendInteractiveMessage()` to send an interactive message. -* `elementId`: An identifier for a specific interactive element. -* `interactedAt`: A timestamp indicating when the interaction occurred. + + +```typescript +let receiverId: string = "UID"; +let receiverType: string = CometChat.RECEIVER_TYPE.USER; + +let interactiveData: Object = { + title: "Survey", + formFields: [ + { + elementType: "textInput", + elementId: "name", + optional: false, + label: "Name", + placeholder: { text: "Enter your name" } + }, + { + elementType: "textInput", + elementId: "age", + optional: true, + label: "Age", + maxLines: 1, + placeholder: { text: "Enter your age" } + }, + { + elementType: "dropdown", + elementId: "gender", + optional: false, + label: "Gender", + defaultValue: "male", + options: [ + { label: "Male", value: "male" }, + { label: "Female", value: "female" } + ] + }, + { + elementType: "Select", + elementId: "interests", + optional: true, + label: "Interests", + defaultValue: ["tech"], + options: [ + { label: "Technology", value: "tech" }, + { label: "Sports", value: "sports" }, + { label: "Music", value: "music" } + ] + } + ], + submitElement: { + elementType: "button", + elementId: "submitButton", + buttonText: "Submit", + disableAfterInteracted: true, + action: { + actionType: "urlNavigation", + url: "https://www.cometchat.com/" + } + } +}; -## Goal Completion +let interactiveMessage: CometChat.InteractiveMessage = new CometChat.InteractiveMessage( + receiverId, + receiverType, + "form", + interactiveData +); -A key feature of `InteractiveMessage` is checking whether a user's interactions with the message meet the defined `InteractionGoal` +CometChat.sendInteractiveMessage(interactiveMessage).then( + (message: CometChat.InteractiveMessage) => { + console.log("Interactive message sent successfully", message); + }, + (error: CometChat.CometChatException) => { + console.log("Interactive message sending failed with error:", error); + } +); +``` + + +```javascript +let receiverId = "UID"; +let receiverType = CometChat.RECEIVER_TYPE.USER; + +let interactiveData = { + title: "Survey", + formFields: [ + { + elementType: "textInput", + elementId: "name", + optional: false, + label: "Name", + placeholder: { text: "Enter your name" } + }, + { + elementType: "textInput", + elementId: "age", + optional: true, + label: "Age", + maxLines: 1, + placeholder: { text: "Enter your age" } + }, + { + elementType: "dropdown", + elementId: "gender", + optional: false, + label: "Gender", + defaultValue: "male", + options: [ + { label: "Male", value: "male" }, + { label: "Female", value: "female" } + ] + }, + { + elementType: "Select", + elementId: "interests", + optional: true, + label: "Interests", + defaultValue: ["tech"], + options: [ + { label: "Technology", value: "tech" }, + { label: "Sports", value: "sports" }, + { label: "Music", value: "music" } + ] + } + ], + submitElement: { + elementType: "button", + elementId: "submitButton", + buttonText: "Submit", + disableAfterInteracted: true, + action: { + actionType: "urlNavigation", + url: "https://www.cometchat.com/" + } + } +}; -You would be tracking every interaction users perform on an `InteractiveMessage` (captured as `Interaction` objects) and comparing those with the defined `InteractionGoal`. The completion of a goal can vary depending on the goal type: +let interactiveMessage = new CometChat.InteractiveMessage( + receiverId, + receiverType, + "form", + interactiveData +); -| Goals | Description | Keys | -| -------------------------------- | ---------------------------------------------------------------------- | --------------------------------------------- | -| **Any Interaction** | The goal is considered completed if there is at least one interaction. | CometChatConstants.INTERACTION\_TYPE\_ANY | -| **Any of Specific Interactions** | The goal is achieved if any of the specified interactions occurred. | CometChatConstants.INTERACTION\_TYPE\_ANY\_OF | -| **All of Specific Interactions** | The goal is completed when all specified interactions occur. | CometChatConstants.INTERACTION\_TYPE\_ALL\_OF | -| **None** | The goal is never completed. | CometChatConstants.INTERACTION\_TYPE\_NONE | +CometChat.sendInteractiveMessage(interactiveMessage).then( + (message) => { + console.log("Interactive message sent successfully", message); + }, + (error) => { + console.log("Interactive message sending failed with error:", error); + } +); +``` + + -This user interaction tracking mechanism provides a flexible and efficient way to monitor user engagement within an interactive chat session. By defining clear interaction goals and checking user interactions against these goals, you can manage user engagement and improve the overall chat experience in your CometChat-enabled application. +The `sendInteractiveMessage()` method returns an [`InteractiveMessage`](/sdk/reference/messages#interactivemessage) object. -## InteractionGoal +## Interactive Elements -The `InteractionGoal` represents the desired outcome of an interaction with an `InteractiveMessage`. It includes: +The `interactiveData` object defines the UI elements. Common element types: -* `elementIds`: A list of identifiers for the interactive elements. -* `type`: The type of interaction goal from the `CometChatConstants`. +| Element Type | Description | +| --- | --- | +| `textInput` | Single or multi-line text field | +| `dropdown` | Single-select dropdown menu | +| `Select` | Multi-select checkbox group | +| `button` | Clickable button with action | -## Sending InteractiveMessages +### Text Input -The `InteractiveMessage` can be sent using the `sendInteractiveMessage` method of the `CometChat` class. The method requires an `InteractiveMessage` object and a `CallbackListener` for handling the response. +```javascript +{ + elementType: "textInput", + elementId: "name", + optional: false, + label: "Name", + maxLines: 1, // Optional: limit to single line + placeholder: { text: "Enter text here" } +} +``` -Here is an example of how to use it: +### Dropdown (Single Select) + +```javascript +{ + elementType: "dropdown", + elementId: "country", + optional: false, + label: "Country", + defaultValue: "us", + options: [ + { label: "United States", value: "us" }, + { label: "United Kingdom", value: "uk" }, + { label: "Canada", value: "ca" } + ] +} +``` - - -```js -const interactiveData = { -title: "Survey", -formFields: [ - { - elementType: "textInput", - elementId: "name", - optional: false, - label: "Name", - placeholder: { - text: "Enter text here" - } - }, - { - elementType: "textInput", - elementId: "age", - optional: true, - label: "Age", - maxLines: 1, - placeholder: { - text: "Enter text here" - } - }, - { - elementType: "Select", - elementId: "checkBox1", - optional: true, - label: "Check box element", - defaultValue: ["chk_option_2"], - options: [ - { - label: "Option 1", - value: "chk_option_1" - }, - { - label: "Option 2", - value: "chk_option_2" - } - ] - }, - { - elementType: "dropdown", - elementId: "gender", - optional: false, - label: "Gender", - defaultValue: "male", - options: [ - { - label: "Male", - value: "male" - }, - { - label: "Female", - value: "female" - } - ] - } -], -submitElement: { +### Checkbox (Multi Select) + +```javascript +{ + elementType: "Select", + elementId: "preferences", + optional: true, + label: "Preferences", + defaultValue: ["email"], + options: [ + { label: "Email notifications", value: "email" }, + { label: "SMS notifications", value: "sms" }, + { label: "Push notifications", value: "push" } + ] +} +``` + +### Submit Button + +```javascript +{ elementType: "button", - elementId: "submitButton", + elementId: "submitBtn", buttonText: "Submit", - disableAfterInteracted: false, + disableAfterInteracted: true, action: { actionType: "urlNavigation", - url: "https://www.cometchat.com/" + url: "https://example.com/submit" } } -}; +``` +## Interaction Goals +An `InteractionGoal` defines when the user's interaction with the message is considered complete. Use this to track engagement and trigger follow-up actions. -const interactiveMessage = new CometChat.InteractiveMessage(receiverId,receiverType,"form", interactiveData); +| Goal Type | Constant | Description | +| --- | --- | --- | +| Any Interaction | `CometChat.GoalType.ANY_ACTION` | Complete when any element is interacted with | +| Any of Specific | `CometChat.GoalType.ANY_OF` | Complete when any of the specified elements is interacted with | +| All of Specific | `CometChat.GoalType.ALL_OF` | Complete when all specified elements are interacted with | +| None | `CometChat.GoalType.NONE` | Never considered complete (default) | +### Set an Interaction Goal -CometChat.sendInteractiveMessage(interactiveMessage) - .then((message: CometChat.InteractiveMessage) => { - // This block is executed when the InteractiveMessage is sent successfully. - }) - .catch((error: CometChat.CometChatException) => { - // This block is executed if an error occurs while sending the InteractiveMessage. - }); -``` + + +```typescript +let elementIds: Array = ["name", "gender", "submitButton"]; +let interactionGoal: CometChat.InteractionGoal = new CometChat.InteractionGoal( + CometChat.GoalType.ALL_OF, + elementIds +); +let interactiveMessage: CometChat.InteractiveMessage = new CometChat.InteractiveMessage( + receiverId, + receiverType, + "form", + interactiveData +); +interactiveMessage.setInteractionGoal(interactionGoal); + +CometChat.sendInteractiveMessage(interactiveMessage).then( + (message: CometChat.InteractiveMessage) => { + console.log("Interactive message sent successfully", message); + }, + (error: CometChat.CometChatException) => { + console.log("Interactive message sending failed with error:", error); + } +); +``` + +```javascript +let elementIds = ["name", "gender", "submitButton"]; +let interactionGoal = new CometChat.InteractionGoal( + CometChat.GoalType.ALL_OF, + elementIds +); +let interactiveMessage = new CometChat.InteractiveMessage( + receiverId, + receiverType, + "form", + interactiveData +); +interactiveMessage.setInteractionGoal(interactionGoal); + +CometChat.sendInteractiveMessage(interactiveMessage).then( + (message) => { + console.log("Interactive message sent successfully", message); + }, + (error) => { + console.log("Interactive message sending failed with error:", error); + } +); +``` + -## Event Listeners +## Interactions -CometChat SDK provides event listeners to handle real-time events related to `InteractiveMessage`. +An `Interaction` represents a single user action on an interactive element. -## On InteractiveMessage Received +| Property | Type | Description | +| --- | --- | --- | +| `elementId` | `string` | Identifier of the interacted element | +| `interactedAt` | `number` | Unix timestamp of the interaction | -The `onInteractiveMessageReceived` event listener is triggered when an `InteractiveMessage` is received. +## Mark as Interacted -Here is an example: +Use `markAsInteracted()` to record when a user interacts with an element. + +```typescript +let messageId: number = 123; +let elementId: string = "submitButton"; + +CometChat.markAsInteracted(messageId, elementId).then( + (response: string) => { + console.log("Marked as interacted successfully", response); + }, + (error: CometChat.CometChatException) => { + console.log("Failed to mark as interacted:", error); + } +); +``` + -```js +```javascript +let messageId = 123; +let elementId = "submitButton"; + +CometChat.markAsInteracted(messageId, elementId).then( + (response) => { + console.log("Marked as interacted successfully", response); + }, + (error) => { + console.log("Failed to mark as interacted:", error); + } +); +``` + + + +## Real-Time Events + +Register a `MessageListener` to receive interactive message events. + +### Receive Interactive Messages + + + +```typescript +let listenerID: string = "UNIQUE_LISTENER_ID"; + CometChat.addMessageListener( - "UNIQUE_ID", + listenerID, new CometChat.MessageListener({ - onInteractiveMessageReceived: (message: CometChat.InteractiveMessage) => { - // This block is executed when an InteractiveMessage is received. - // Here you can define logic to handle the received InteractiveMessage and display it in your chat interface. - }, + onInteractiveMessageReceived: (message: CometChat.InteractiveMessage) => { + console.log("Interactive message received", message); + // Render the interactive UI based on message.getInteractiveData() + } }) ); ``` - + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; +CometChat.addMessageListener( + listenerID, + new CometChat.MessageListener({ + onInteractiveMessageReceived: (message) => { + console.log("Interactive message received", message); + // Render the interactive UI based on message.getInteractiveData() + } + }) +); +``` + -On Interaction Goal Completed - -The `onInteractionGoalCompleted` event listener is invoked when an interaction goal is achieved. +### Interaction Goal Completed -Here is an example: +Triggered when a user's interactions satisfy the defined `InteractionGoal`. + +```typescript +let listenerID: string = "UNIQUE_LISTENER_ID"; + +CometChat.addMessageListener( + listenerID, + new CometChat.MessageListener({ + onInteractionGoalCompleted: (receipt: CometChat.InteractionReceipt) => { + console.log("Interaction goal completed", receipt); + // Handle goal completion (e.g., show confirmation, trigger next step) + } + }) +); +``` + -```js +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; + CometChat.addMessageListener( - "UNIQUE_ID", + listenerID, new CometChat.MessageListener({ - onInteractionGoalCompleted: (receipt: CometChat.InteractionReceipt) => { - // This block is executed when an interaction goal is completed. - // Here you can specify the actions your application should take once an interaction goal is achieved, such as updating the UI or notifying the user. - }, + onInteractionGoalCompleted: (receipt) => { + console.log("Interaction goal completed", receipt); + // Handle goal completion (e.g., show confirmation, trigger next step) + } }) ); ``` + + + + +Always remove listeners when they're no longer needed to prevent memory leaks. + +```javascript +CometChat.removeMessageListener("UNIQUE_LISTENER_ID"); +``` + + +## Allow Sender Interaction + +By default, the sender cannot interact with their own interactive message. Enable sender interaction: + + + +```typescript +let interactiveMessage: CometChat.InteractiveMessage = new CometChat.InteractiveMessage( + receiverId, + receiverType, + "form", + interactiveData +); +interactiveMessage.setIsSenderInteractionAllowed(true); +CometChat.sendInteractiveMessage(interactiveMessage).then( + (message: CometChat.InteractiveMessage) => { + console.log("Interactive message sent successfully", message); + }, + (error: CometChat.CometChatException) => { + console.log("Interactive message sending failed with error:", error); + } +); +``` + +```javascript +let interactiveMessage = new CometChat.InteractiveMessage( + receiverId, + receiverType, + "form", + interactiveData +); +interactiveMessage.setIsSenderInteractionAllowed(true); +CometChat.sendInteractiveMessage(interactiveMessage).then( + (message) => { + console.log("Interactive message sent successfully", message); + }, + (error) => { + console.log("Interactive message sending failed with error:", error); + } +); +``` + -These event listeners offer your application a way to provide real-time updates in response to incoming interactive messages and goal completions, contributing to a more dynamic and responsive user chat experience. - -## Usage +--- -An InteractiveMessage is constructed with the receiver's UID, the receiver type, the interactive type, and interactive data as a JSONObject. Once created, the InteractiveMessage can be sent using CometChat's sendInteractiveMessage() method. Incoming InteractiveMessages can be received and processed via CometChat's message listener framework. +## Next Steps + + + + Send text, media, and custom messages + + + Listen for incoming messages in real time + + + Send ephemeral messages that aren't stored + + + Understand message types and hierarchy + + diff --git a/sdk/javascript/join-group.mdx b/sdk/javascript/join-group.mdx index 744f9e723..511a6fcee 100644 --- a/sdk/javascript/join-group.mdx +++ b/sdk/javascript/join-group.mdx @@ -1,36 +1,40 @@ --- title: "Join A Group" +sidebarTitle: "Join Group" +description: "Learn how to join public, password-protected, and private groups, and receive real-time join events using the CometChat JavaScript SDK." --- + +```javascript +// Join a public group +await CometChat.joinGroup("GUID", CometChat.GROUP_TYPE.PUBLIC, ""); -## Join a Group +// Join a password-protected group +await CometChat.joinGroup("GUID", CometChat.GROUP_TYPE.PASSWORD, "password123"); -In order to start participating in group conversations, you will have to join a group. You can do so using the `joinGroup()` method. +// Listen for member joined events +CometChat.addGroupListener("listener", new CometChat.GroupListener({ + onGroupMemberJoined: (message, joinedUser, joinedGroup) => { } +})); +``` + - - -```javascript -var GUID = "GUID"; -var password = ""; -var groupType = CometChat.GROUP_TYPE.PUBLIC; +Join a group to start sending and receiving messages in it. Public groups can be joined freely, password groups require the correct password, and private groups require an admin to add you (no direct join). -CometChat.joinGroup(GUID, groupType, password).then( -group => { - console.log("Group joined successfully:", group); -}, error => { - console.log("Group joining failed with exception:", error); -} -); -``` +## Join a Group - +Use `joinGroup()` to join a group. + ```typescript -var GUID: string = "GUID"; +const GUID: string = "GUID"; + +const password: string = ""; +const groupType: string = CometChat.GROUP_TYPE.PUBLIC; -CometChat.joinGroup(GUID, CometChat.GroupType.Public).then( +CometChat.joinGroup(GUID, groupType, password).then( (group: CometChat.Group) => { console.log("Group joined successfully:", group); }, (error: CometChat.CometChatException) => { @@ -41,35 +45,46 @@ CometChat.joinGroup(GUID, CometChat.GroupType.Public).then( - + +```javascript +const GUID = "GUID"; +const password = ""; +const groupType = CometChat.GROUP_TYPE.PUBLIC; + +CometChat.joinGroup(GUID, groupType, password).then( +group => { + console.log("Group joined successfully:", group); +}, error => { + console.log("Group joining failed with exception:", error); +} +); +``` -The `joinGroup()` method takes the below parameters + -| Parameter | Description | -| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `GUID` | The GUID of the group you would like to join. | -| `groupType` | Type of the group. CometChat provides 3 types of groups viz. 1. CometChat.GROUP\_TYPE.PUBLIC 2. CometChat.GROUP\_TYPE.PASSWORD 3. CometChats.GROUP\_TYPE.PRIVATE | -| `password` | Password is mandatory in case of a password protected group. | + -Once you have joined a group successfully, you can send and receive messages in that group. +| Parameter | Description | +| --------- | ----------- | +| `GUID` | The GUID of the group to join | +| `groupType` | `CometChat.GROUP_TYPE.PUBLIC`, `PASSWORD`, or `PRIVATE` | +| `password` | Required for password-protected groups | -CometChat keeps a track of the groups joined and you do not need to join the group every time you want to communicate in the group. +Once joined, you can send and receive messages in the group. CometChat tracks joined groups — you don't need to rejoin each session. Check `hasJoined` on the [`Group`](/sdk/reference/entities#group) object to verify membership. -You can identify if a group is joined using the `hasJoined` parameter in the `Group` object. +The method returns a [`Group`](/sdk/reference/entities#group) object with `hasJoined` set to `true`. ## Real-time Group Member Joined Events -*In other words, as a member of a group, how do I know if someone joins the group when my app is running?* - -If a user joins any group, the members of the group receive a real-time event in the `onGroupMemberJoined()` method of the `GroupListener` class. +Register a `GroupListener` to receive events when members join. - -```javascript + +```typescript CometChat.addGroupListener( - "UNIQUE_LISTNER_ID", + "UNIQUE_LISTENER_ID", new CometChat.GroupListener({ - onGroupMemberJoined: (message, joinedUser, joinedGroup) => { + onGroupMemberJoined: (message: CometChat.Action, joinedUser: CometChat.User, joinedGroup: CometChat.Group) => { console.log("User joined", { message, joinedUser, joinedGroup }); } }) @@ -78,12 +93,12 @@ CometChat.addGroupListener( - -```typescript + +```javascript CometChat.addGroupListener( - "UNIQUE_LISTNER_ID", + "UNIQUE_LISTENER_ID", new CometChat.GroupListener({ - onGroupMemberJoined: (message: CometChat.Action, joinedUser: CometChat.User, joinedGroup: CometChat.Group) => { + onGroupMemberJoined: (message, joinedUser, joinedGroup) => { console.log("User joined", { message, joinedUser, joinedGroup }); } }) @@ -94,14 +109,36 @@ CometChat.addGroupListener( -## Missed Group Member Joined Events + +Always remove group listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + +```javascript +CometChat.removeGroupListener("UNIQUE_LISTENER_ID"); +``` + -*In other words, as a member of a group, how do I know if someone joins the group when my app is not running?* +## Missed Group Member Joined Events -When you retrieve the list of previous messages if a member has joined any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. +When fetching message history, join events appear as [`Action`](/sdk/reference/messages#action) messages with: +- `action` — `"joined"` +- `actionBy` — [`User`](/sdk/reference/entities#user) who joined +- `actionFor` — [`Group`](/sdk/reference/entities#group) that was joined -For the group member joined event, in the `Action` object received, the following fields can help you get the relevant information- +--- -1. `action` - `joined` -2. `actionBy` - User object containing the details of the user who joined the group -3. `actionFor`- Group object containing the details of the group the user has joined +## Next Steps + + + + Allow members to leave a group + + + Fetch the list of members in a group + + + Send messages to group conversations + + + Programmatically add members to a group + + diff --git a/sdk/javascript/key-concepts.mdx b/sdk/javascript/key-concepts.mdx index 37e5885f1..95f260d60 100644 --- a/sdk/javascript/key-concepts.mdx +++ b/sdk/javascript/key-concepts.mdx @@ -1,127 +1,146 @@ --- title: "Key Concepts" +description: "Understand the core concepts of CometChat including users, groups, messaging categories, authentication, and member roles." --- + +Key identifiers: +- **UID** — Unique User Identifier (alphanumeric, underscore, hyphen) +- **GUID** — Group Unique Identifier (alphanumeric, underscore, hyphen) +- **Auth Key** — Development-only credential for quick testing +- **Auth Token** — Secure per-user token for production use +- **REST API Key** — Server-side credential, never expose in client code -### CometChat Dashboard +Group types: `Public` | `Password` | `Private` +Member scopes: `Admin` | `Moderator` | `Participant` +Message categories: `message` | `custom` | `action` | `call` + -The CometChat Dashboard enables you to create new apps (projects) and manage your existing apps. +This page covers the core concepts you'll encounter when building with CometChat. Read this before diving into the SDK guides — it'll make everything else click faster. - -How many apps to create? - -Ideally, you should create two apps- one for development and one for production. And you should use a single app irrespective of the number of platforms. - -Do not create separate apps for every platform; if you do, your users on different platforms will not be able to communicate with each other! +## Users - +A user is anyone who uses CometChat. Each user is uniquely identified by a UID (Unique User Identifier). -* For every app, a unique App ID is generated. This App ID will be required when integrating CometChat within your app. -* Along with the App ID, you will need to create an Auth Key (from the Dashboard) which can be used for user authentication. +- The UID is typically the primary ID of the user from your database +- UID can be alphanumeric with underscore and hyphen only — spaces, punctuation, and other special characters are not allowed -### Auth & Rest API Keys +### User Roles -You can generate two types of keys from the dashboard. +A role is a category for grouping similar users. For example, group premium users with the role "Premium" to filter users or enable/disable features conditionally. -| Type | Privileges | Recommended Use | -| ------------ | ---------------------------------------------------------------- | --------------------------------------------- | -| Auth Key | The Auth Key can be used to create & login users. | In your client side code (during development) | -| Rest API Key | The Rest API Key can be used to perform any CometChat operation. | In your server side code | +## Authentication -### Users +CometChat does not handle user registration or friends management — you handle that in your app, then log users into CometChat programmatically. -A user is anyone who uses CometChat. +### API Keys -### UID +You can generate two types of keys from the [CometChat Dashboard](https://app.cometchat.com): -* Each user is uniquely identified using UID. -* The UID is typically the primary ID of the user from your database. +| Type | Privileges | Recommended Use | +| --- | --- | --- | +| Auth Key | Create & login users | Client-side code (development only) | +| REST API Key | Perform any CometChat operation | Server-side code only | - -UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. - +Never expose your REST API Key in client-side code. Use Auth Tokens for production authentication. -### Auth Token - -* A single user can have multiple auth tokens. The auth tokens should be per user per device. -* It should be generated by API call ideally, via server to server call. The auth token should then be given to CometChat for login. -* An Auth Token can only be deleted via dashboard or using REST API. - -### Authentication - -To allow a user to use CometChat, the user must log in to CometChat. - -**CometChat does not handle user management.** You must handle user registration and login at your end. Once the user is logged into your app/site, you can log in the user to CometChat **programmatically**. So the user does not ever directly login to CometChat. +### Auth Tokens -**CometChat does not handle friends management.** If you want to associate friends with your users, you must handle friends management in your app. Once two users are friends (i.e. they have accepted each other as friends), then you can associate them as friends in CometChat. +Auth Tokens are secure, per-user credentials for production use: -### Typical Workflow +- A single user can have multiple auth tokens (one per device) +- Generate tokens server-side via the [REST API](https://api-explorer.cometchat.com/reference/create-authtoken) +- Tokens can only be deleted via the Dashboard or REST API -| Your App | Your Server | CometChat | -| ----------------------------- | ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ | -| User registers in your app | You store the user information in your database (e.g. ID, name, email, phone, location etc. in `users` table) | You add the user to CometChat (only ID & name) using the Rest API | -| User logs in to your app | You verify the credentials, login the user and retrieve the user ID | You log in the user to CometChat using the same user ID programmatically | -| User sends a friend request | You display the request to the potential friend | No action required | -| User accepts a friend request | You display the users as friends | You add both the users as friends using the Rest API | +### Authentication Flow -### User Roles - -A role is a category for a group of similar users. For example, you may want to group your premium users using the role "Premium". You then use this to filter users or enable/disable features by writing conditional code. - -### User List - -* The User List can be used to build the **Contacts** or **Who's Online** view in your app. -* The list of users can be different based on the logged-in user. - -### Groups + +Create your apps in the [CometChat Dashboard](https://app.cometchat.com) — each app gets a unique App ID required for SDK initialization. Ideally, create two apps — one for development and one for production. Use a single app regardless of the number of platforms; if you create separate apps per platform, your users won't be able to communicate across them. + -A group can be used for multiple users to communicate with each other on a particular topic/interest. +| Your App | Your Server | CometChat | +| --- | --- | --- | +| User registers | Store user info in your database | Create user via REST API (UID & name) | +| User logs in | Verify credentials, retrieve user ID | Log in user programmatically with UID | +| User sends friend request | Display request to potential friend | No action required | +| User accepts friend request | Display users as friends | Add both users as friends via REST API | -### GUID +## Groups -* Each group is uniquely identified using GUID. -* The GUID is typically the primary ID of the group from your database. If you do not store group information in your database, you can generate a random string for use as GUID. +A group enables multiple users to communicate on a particular topic or interest. Each group is uniquely identified using a GUID (Group Unique Identifier). - +- The GUID is typically the primary ID of the group from your database +- GUID can be alphanumeric with underscore and hyphen only -GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. +## Group Types - +| Type | Visibility | Participation | +| --- | --- | --- | +| Public | All users | Any user can join | +| Password | All users | Users with valid password can join | +| Private | Members only | Users must be invited (auto-joined) | -### Types +## Member Scopes -CometChat supports three different types of groups: +Once a user joins a group, they become a member with one of three scopes: -| Type | Visibility | Participation | -| -------- | ---------------------------- | ------------------------------------------------- | -| Public | All users | Any user can choose to join | -| Password | All users | Any user with a valid password can choose to join | -| Private | Only users part of the group | Invited users will be auto-joined | +| Scope | Default | Privileges | +| --- | --- | --- | +| Admin | Group creator | Full control: manage members, change scopes, kick/ban anyone, update/delete group | +| Moderator | — | Moderate: change participant scopes, kick/ban participants, update group | +| Participant | All other members | Basic: send & receive messages and calls | -### Members +## Message Categories -Once a participant joins a group, they become a member of the group. Members are part of the group indefinitely i.e. they will keep receiving messages, calls & notifications. To stop, the participant must either be kicked, banned or intentionally leave the group. +Every message belongs to one of these categories: -CometChat supports three different types of member scopes in a group: +| Category | Types | Description | +| --- | --- | --- | +| `message` | `text`, `image`, `video`, `audio`, `file` | Standard messages | +| `custom` | Developer-defined | Custom data (e.g., location, polls) | +| `action` | `groupMember`, `message` | System-generated (joins, edits, deletes) | +| `call` | `audio`, `video` | Call-related messages | -| Member | Default | Privileges | -| ----------- | -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| Admin | Group creator is assigned Admin scope | - Change scope of Group Members to admin, moderator or participant. - Can add members to a group. - Kick & Ban Participants/Moderators/Admins - Send & Receive Messages & Calls - Update group - Delete group | -| Moderator | - | - Change scope of moderator or participant. - Update group - Kick & Ban Participants - Send & Receive Messages & Calls | -| Participant | Any other user is assigned Participant scope | - Send & Receive Messages & Calls | +For more details, see the [Message Structure and Hierarchy](/sdk/javascript/message-structure-and-hierarchy) guide. -### Messaging +## Glossary -Any message in CometChat can belong to either one of the below categories +| Term | Definition | Learn More | +| --- | --- | --- | +| UID | Unique User Identifier — alphanumeric string you assign to each user | [Users Overview](/sdk/javascript/users-overview) | +| GUID | Group Unique Identifier — alphanumeric string you assign to each group | [Groups Overview](/sdk/javascript/groups-overview) | +| Auth Key | Development-only credential for quick testing. Never use in production | [Authentication](/sdk/javascript/authentication-overview) | +| Auth Token | Secure, per-user token generated via REST API. Use in production | [Authentication](/sdk/javascript/authentication-overview#login-using-auth-token) | +| REST API Key | Server-side credential for REST API calls. Never expose in client code | [CometChat Dashboard](https://app.cometchat.com) | +| Receiver Type | Specifies if a message target is a `user` or `group` | [Send Message](/sdk/javascript/send-message) | +| Scope | Group member scope: `admin`, `moderator`, or `participant` | [Change Member Scope](/sdk/javascript/group-change-member-scope) | +| Listener | Callback handler for real-time events (messages, presence, calls, groups) | [All Real-Time Listeners](/sdk/javascript/all-real-time-listeners) | +| Conversation | A chat thread between two users or within a group | [Retrieve Conversations](/sdk/javascript/retrieve-conversations) | +| Metadata | Custom JSON data attached to users, groups, or messages | [Send Message](/sdk/javascript/send-message) | +| Tags | String labels for categorizing users, groups, conversations, or messages | [Message Filtering](/sdk/javascript/message-filtering) | +| RequestBuilder | Builder pattern class for constructing filtered/paginated queries | [Message Filtering](/sdk/javascript/message-filtering) | +| AppSettings | Configuration object for initializing the SDK (App ID, Region, presence) | [Setup SDK](/sdk/javascript/setup-sdk) | +| Transient Message | Ephemeral message not stored on server (typing indicators, live reactions) | [Transient Messages](/sdk/javascript/transient-messages) | +| Interactive Message | Message with actionable UI elements (forms, cards, buttons) | [Interactive Messages](/sdk/javascript/interactive-messages) | -| Category | Description | -| -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| message | Any message belonging to the category `message` can belong to either one of the below types: 1. text 2. image 3. video 4. audio 5. file | -| custom | Custom messages are an option available for developers to send custom data across to users/groups. To send any additional data that does not fit in the default categories and types provided by CometChat, you can use the custom messages. | -| action | Action messages are system-generated messages. These can belong to either of the below types: 1. groupMember - when the action is performed on a group member 2. message - when the action is performed on a message | -| call | These are call-related messages. These can belong to either one of the two types: 1. audio 2. video | +--- -For more information, you can refer to the [Message structure and hierarchy guide](/sdk/javascript/message-structure-and-hierarchy). +## Next Steps + + + + Install and initialize the CometChat SDK + + + Log users in and manage auth tokens + + + Send your first text or media message + + + Create and manage group conversations + + diff --git a/sdk/javascript/leave-group.mdx b/sdk/javascript/leave-group.mdx index a9534b8aa..e2ca87c4b 100644 --- a/sdk/javascript/leave-group.mdx +++ b/sdk/javascript/leave-group.mdx @@ -1,32 +1,36 @@ --- title: "Leave A Group" +sidebarTitle: "Leave Group" +description: "Learn how to leave a group and receive real-time events when members leave using the CometChat JavaScript SDK." --- + +```javascript +// Leave a group +await CometChat.leaveGroup("GUID"); -## Leave a Group +// Listen for member left events +CometChat.addGroupListener("listener", new CometChat.GroupListener({ + onGroupMemberLeft: (message, leavingUser, group) => { } +})); +``` + -In order to stop receiving updates and messages for any particular joined group, you will have to leave the group using the `leaveGroup()` method. +Leave a group to stop receiving messages and updates from it. Once you leave, you'll need to rejoin to participate again. - - -```javascript -var GUID = "GUID"; + +Group owners cannot leave without first transferring ownership to another member. See [Transfer Group Ownership](/sdk/javascript/transfer-group-ownership). + -CometChat.leaveGroup(GUID).then( -hasLeft => { - console.log("Group left successfully:", hasLeft); -}, error => { - console.log("Group leaving failed with exception:", error); -} -); -``` +## Leave a Group - +Use `leaveGroup()` to leave a group. + ```typescript -var GUID: string = "GUID"; +const GUID: string = "GUID"; CometChat.leaveGroup(GUID).then( (hasLeft: boolean) => { @@ -39,27 +43,42 @@ CometChat.leaveGroup(GUID).then( + +```javascript +const GUID = "GUID"; + +CometChat.leaveGroup(GUID).then( +hasLeft => { + console.log("Group left successfully:", hasLeft); +}, error => { + console.log("Group leaving failed with exception:", error); +} +); +``` + + + | Parameter | Description | | --------- | -------------------------------------------- | -| `GUID` | The UID of the group you would like to leave | +| `GUID` | The GUID of the group you would like to leave | Once a group is left, the user will no longer receive any updates or messages pertaining to the group. -## Real-time Group Member Left Events +On success, the method resolves with `true` (boolean). -*In other words, as a member of a group, how do I know if someone has left it?* +## Real-time Group Member Left Events -If a user leaves any group, The members of the group receive a real-time event in the `onGroupMemberLeft()` method of the `GroupListener` class. +Register a `GroupListener` to receive events when members leave. - -```javascript + +```typescript CometChat.addGroupListener( "UNIQUE_LISTENER_ID", new CometChat.GroupListener({ - onGroupMemberLeft: (message, leavingUser, group) => { + onGroupMemberLeft: (message: CometChat.Action, leavingUser: CometChat.User, group: CometChat.Group) => { console.log("User left", { message, leavingUser, group }); } }) @@ -68,12 +87,12 @@ CometChat.addGroupListener( - -```typescript + +```javascript CometChat.addGroupListener( "UNIQUE_LISTENER_ID", new CometChat.GroupListener({ - onGroupMemberLeft: (message: CometChat.Action, leavingUser: CometChat.User, group: CometChat.Group) => { + onGroupMemberLeft: (message, leavingUser, group) => { console.log("User left", { message, leavingUser, group }); } }) @@ -84,14 +103,36 @@ CometChat.addGroupListener( -## Missed Group Member Left Events + +Always remove group listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. -*In other words, as a member of a group, how do I know if someone has left it when my app is not running?* +```javascript +CometChat.removeGroupListener("UNIQUE_LISTENER_ID"); +``` + -When you retrieve the list of previous messages if a member has left any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class. +## Missed Group Member Left Events -For the group member left event, in the `Action` object received, the following fields can help you get the relevant information- +When fetching message history, leave events appear as [`Action`](/sdk/reference/messages#action) messages with: +- `action` — `"left"` +- `actionBy` — [`User`](/sdk/reference/entities#user) who left +- `actionFor` — [`Group`](/sdk/reference/entities#group) that was left + +--- -1. `action` - `left` -2. `actionBy` - User object containing the details of the user who left the group -3. `actionFor` - Group object containing the details of the group the user has left +## Next Steps + + + + Join public or password-protected groups + + + Permanently delete a group + + + Remove or ban members from a group + + + Fetch and filter the list of groups + + diff --git a/sdk/javascript/login-listener.mdx b/sdk/javascript/login-listener.mdx index 4c98ff4c4..36b1c2ede 100644 --- a/sdk/javascript/login-listener.mdx +++ b/sdk/javascript/login-listener.mdx @@ -1,23 +1,63 @@ --- title: "Login Listener" +description: "Listen for real-time login and logout events using the CometChat JavaScript SDK LoginListener class." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Add login listener +CometChat.addLoginListener("LISTENER_ID", new CometChat.LoginListener({ + loginSuccess: (user) => { }, + loginFailure: (error) => { }, + logoutSuccess: () => { }, + logoutFailure: (error) => { } +})); + +// Remove login listener +CometChat.removeLoginListener("LISTENER_ID"); +``` + The CometChat SDK provides you with real-time updates for the `login` and `logout` events. This can be achieved using the `LoginListener` class provided. LoginListener consists of 4 events that can be triggered. These are as follows: | Delegate Method | Information | | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | | loginSuccess(event) | Informs you that the login was successful and provides you with a user object containing the data for the user that logged in. | -| loginFailure(event) | Informs you about the failure while logging in the user and provides you with the reason for the failure wrapped in an object of the `CometChatException` class. | +| loginFailure(event) | Informs you about the failure while logging in the user and provides you with the reason for the failure wrapped in an object of the [`CometChatException`](/sdk/reference/auxiliary#cometchatexception) class. | | logoutSuccess() | Informs you about the user being logged out successfully. | -| logoutFailure(event) | Informs you about the failure while logging out the user. The reason for the failure can be obtained from the object of the `CometChatException` class. | +| logoutFailure(event) | Informs you about the failure while logging out the user. The reason for the failure can be obtained from the object of the [`CometChatException`](/sdk/reference/auxiliary#cometchatexception) class. | To add the `LoginListener`, you need to use the `addLoginListener()` method provided by the SDK which takes a unique identifier for the listener and of the the `LoginListener` class itself. + +```typescript + const listenerID: string = "UNIQUE_LISTENER_ID"; + CometChat.addLoginListener( + listenerID, + new CometChat.LoginListener({ + loginSuccess: (user: CometChat.User) => { + console.log("LoginListener :: loginSuccess", user); + }, + loginFailure: (error: CometChat.CometChatException) => { + console.log("LoginListener :: loginFailure", error); + }, + logoutSuccess: () => { + console.log("LoginListener :: logoutSuccess"); + }, + logoutFailure: (error: CometChat.CometChatException) => { + console.log("LoginListener :: logoutFailure", error); + } + }) + ); +``` + + + -```js +```javascript let listenerID = "UNIQUE_LISTENER_ID"; CometChat.addLoginListener( listenerID, @@ -40,49 +80,122 @@ To add the `LoginListener`, you need to use the `addLoginListener()` method prov + + +### React Example + +If you're using React, register the listener inside a `useEffect` hook and clean it up on unmount: + + ```typescript - var listenerID: string = "UNIQUE_LISTENER_ID"; - CometChat.addLoginListener( +import { useEffect } from "react"; +import { CometChat } from "@cometchat/chat-sdk-javascript"; + +function useLoginListener(): void { + useEffect(() => { + const listenerID: string = "LOGIN_LISTENER"; + CometChat.addLoginListener( listenerID, new CometChat.LoginListener({ - loginSuccess: (user: CometChat.User) => { - console.log("LoginListener :: loginSuccess", user); - }, - loginFailure: (error: CometChat.CometChatException) => { - console.log("LoginListener :: loginFailure", error); - }, - logoutSuccess: () => { - console.log("LoginListener :: logoutSuccess"); - }, - logoutFailure: (error: CometChat.CometChatException) => { - console.log("LoginListener :: logoutFailure", error); - } + loginSuccess: (user: CometChat.User) => { + console.log("User logged in:", user); + }, + loginFailure: (error: CometChat.CometChatException) => { + console.log("Login failed:", error); + }, + logoutSuccess: () => { + console.log("User logged out"); + }, + logoutFailure: (error: CometChat.CometChatException) => { + console.log("Logout failed:", error); + }, }) - ); -``` + ); + return () => { + CometChat.removeLoginListener(listenerID); + }; + }, []); +} +``` + +```javascript +import { useEffect } from "react"; +import { CometChat } from "@cometchat/chat-sdk-javascript"; + +function useLoginListener() { + useEffect(() => { + const listenerID = "LOGIN_LISTENER"; + CometChat.addLoginListener( + listenerID, + new CometChat.LoginListener({ + loginSuccess: (user) => { + console.log("User logged in:", user); + }, + loginFailure: (error) => { + console.log("Login failed:", error); + }, + logoutSuccess: () => { + console.log("User logged out"); + // Redirect to login page, clear app state, etc. + }, + logoutFailure: (error) => { + console.log("Logout failed:", error); + }, + }) + ); + return () => { + CometChat.removeLoginListener(listenerID); + }; + }, []); +} +``` + In order to stop receiving events related to login and logout you need to use the removeLoginListener() method provided by the SDK and pass the ID of the listener that needs to be removed. - -```js - var listenerID = "UNIQUE_LISTENER_ID"; + +```typescript + const listenerID: string = "UNIQUE_LISTENER_ID"; CometChat.removeLoginListener(listenerID); ``` - -```typescript - var listenerID: string = "UNIQUE_LISTENER_ID"; + +```javascript + const listenerID = "UNIQUE_LISTENER_ID"; CometChat.removeLoginListener(listenerID); ``` + + +Always remove login listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +--- + +## Next Steps + + + + Learn about login methods and auth tokens + + + Monitor the SDK connection state in real time + + + Complete reference for all SDK event listeners + + + Manually manage WebSocket connections + + diff --git a/sdk/javascript/managing-web-sockets-connections-manually.mdx b/sdk/javascript/managing-web-sockets-connections-manually.mdx index 90cf107b5..c1a9ddff4 100644 --- a/sdk/javascript/managing-web-sockets-connections-manually.mdx +++ b/sdk/javascript/managing-web-sockets-connections-manually.mdx @@ -1,44 +1,55 @@ --- title: "Managing Web Sockets Connections Manually" +description: "Learn how to manually manage WebSocket connections for real-time messaging using the CometChat JavaScript SDK." --- + +```javascript +// Disable auto WebSocket connection during init +const appSettings = new CometChat.AppSettingsBuilder() + .setRegion("REGION") + .autoEstablishSocketConnection(false) + .build(); +await CometChat.init("APP_ID", appSettings); -## Default SDK behaviour on login - -When the login method of the SDK is called, the SDK performs the below operations: +// Manually connect/disconnect +CometChat.connect(); +CometChat.disconnect(); +``` + -1. Logs the user into the SDK -2. Saves the details of the logged in user locally. -3. Creates a web-socket connection for the logged in user. +By default, the SDK automatically establishes and manages the WebSocket connection — it connects on login, reconnects on `init()` when a session exists, and handles reconnection on network drops. This page covers how to disable that and manage the connection yourself. -This makes sure that the logged in user starts receiving real-time messages sent to him or any groups that he is a part of as soon as he logs in. +You'd want manual control when you need to conserve resources by connecting only when the user is actively chatting, or when you need precise control over when real-time events start flowing. -When the app is reopened, and the init() method is called, the web-socket connection to the server is established automatically. +## Default Behavior -This is the default behaviour of the CometChat SDKs. However, if you wish to take control of the web-socket connection i.e if you wish to connect and disconnect to the web-socket server manually, you can refer to the Managing Web-socket Connection section. +When `autoEstablishSocketConnection` is `true` (the default): -## Managing the Web-socket connections manually +1. `CometChat.login()` logs the user in, saves their session locally, and opens a WebSocket connection +2. On app restart, `CometChat.init()` automatically reconnects using the saved session +3. The user immediately starts receiving real-time messages, presence updates, and call events -The CometChat SDK also allows you to modify the above default behaviour of the SDK and take the control of the web-socket connection into your own hands. In order to achieve this, you need to follow the below steps: +## Manual Connection Management -1. While calling the init() function on the app startup, you need to inform the SDK that you will be managing the web socket connect. You can do so by using the `autoEstablishSocketConnection()` method provided by the `AppSettingsBuilder` class. This method takes a boolean value as an input. If set to `true` , the SDK will manage the web-socket connection internally based on the default behaviour mentioned above. If set to `false` , the web socket connection can will not be managed by the SDK and you will have to handle it manually. You can refer to the below code snippet for the same: +To take control of the WebSocket connection, set `autoEstablishSocketConnection(false)` during initialization: - -```js -let appID = "APP_ID"; -let region = "APP_REGION"; -let appSetting = new CometChat.AppSettingsBuilder() + +```typescript +let appID: string = "APP_ID"; +let region: string = "APP_REGION"; +let appSetting: CometChat.AppSettings = new CometChat.AppSettingsBuilder() .subscribePresenceForAllUsers() .setRegion(region) .autoEstablishSocketConnection(false) .build(); CometChat.init(appID, appSetting).then( - () => { + (isInitialized: boolean) => { console.log("Initialization completed successfully"); }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Initialization failed with error:", error); } ); @@ -46,20 +57,20 @@ CometChat.init(appID, appSetting).then( - -```typescript -let appID: string = "APP_ID"; -let region: string = "APP_REGION"; -let appSetting: CometChat.AppSettings = new CometChat.AppSettingsBuilder() + +```javascript +let appID = "APP_ID"; +let region = "APP_REGION"; +let appSetting = new CometChat.AppSettingsBuilder() .subscribePresenceForAllUsers() .setRegion(region) .autoEstablishSocketConnection(false) .build(); CometChat.init(appID, appSetting).then( - (isInitialized: boolean) => { + () => { console.log("Initialization completed successfully"); }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Initialization failed with error:", error); } ); @@ -69,11 +80,11 @@ CometChat.init(appID, appSetting).then( -You can manage the connection to the web-socket server using the `connect()` and `disconnect()` methods provided by the SDK. +Once initialized with manual mode, use `connect()` and `disconnect()` to control the WebSocket connection. -1. Connect to the web-socket server +### Connect -You need to use the `connect()` method provided by the `CometChat` class to establish the connection to the web-socket server. Please make sure that the user is logged in to the SDK before calling this method. You can use the CometChat.getLoggedInUser() method to check this. Once the connection is established, you will start receiving all the real-time events for the logged in user. +Establishes the WebSocket connection. The user must be logged in first (check with `CometChat.getLoggedinUser()`). Once connected, real-time events start flowing. Returns `void`. @@ -85,9 +96,9 @@ CometChat.connect(); -2. Disconnect from the web-socket server +### Disconnect -You can use the `disconnect()` method provided by the `CometChat` class to break the established connection. Once the connection is broken, you will stop receiving all the real-time events for the logged in user. +Breaks the WebSocket connection. Real-time events stop until you call `connect()` again. Returns `void`. @@ -98,3 +109,22 @@ CometChat.disconnect(); + +--- + +## Next Steps + + + + Monitor the SDK connection state in real time + + + Listen for login and logout events + + + Complete reference for all SDK event listeners + + + SDK installation and initialization guide + + diff --git a/sdk/javascript/mentions.mdx b/sdk/javascript/mentions.mdx index c3ce4742a..888dc0521 100644 --- a/sdk/javascript/mentions.mdx +++ b/sdk/javascript/mentions.mdx @@ -1,8 +1,31 @@ --- title: "Mentions" +sidebarTitle: "Mentions" +description: "Send messages with user mentions, retrieve mentioned users, and filter messages by mention metadata using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + + +```javascript +let message = {}; // obtained from MessageListener or fetchPrevious/fetchNext + +// Send a message with a mention (use <@uid:UID> format) +const msg = new CometChat.TextMessage("receiverUID", "Hello <@uid:cometchat-uid-1>", CometChat.RECEIVER_TYPE.USER); +await CometChat.sendMessage(msg); + +// Get mentioned users from a message +const mentionedUsers = message.getMentionedUsers(); + +// Fetch messages with mention tag info +const request = new CometChat.MessagesRequestBuilder() + .setUID("UID").setLimit(30).mentionsWithTagInfo(true).build(); +const messages = await request.fetchPrevious(); +``` + + + Mentions in messages enable users to refer to specific individual within a conversation. This is done by using the `<@uid:UID>` format, where `UID` represents the user’s unique identification. @@ -61,10 +84,10 @@ CometChat.sendMessage(textMessage).then( ```typescript -let receiverID: string = "UID", - messageText: string = "Hello <@uid:cometchat-uid-1>"; - receiverType: string = CometChat.RECEIVER_TYPE.USER, - textMessage: CometChat.TextMessage = new CometChat.TextMessage(receiverID, messageText, receiverType); +let receiverID: string = "UID"; +let messageText: string = "Hello <@uid:cometchat-uid-1>"; +let receiverType: string = CometChat.RECEIVER_TYPE.USER; +let textMessage: CometChat.TextMessage = new CometChat.TextMessage(receiverID, messageText, receiverType); CometChat.sendMessage(textMessage).then( (message: CometChat.TextMessage) => { @@ -127,7 +150,7 @@ To get a list of messages in a conversation where users are mentioned along with let UID = "UID"; let limit = 30; -var messagesRequest = new CometChat.MessagesRequestBuilder() +let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .mentionsWithTagInfo(true) @@ -154,7 +177,7 @@ messagesRequest.fetchPrevious().then( let GUID = "GUID"; let limit = 30; -var messagesRequest = new CometChat.MessagesRequestBuilder() +let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .mentionsWithTagInfo(true) @@ -236,17 +259,32 @@ messagesRequest.fetchPrevious().then( +Relevant fields to access on returned messages and their mentioned users: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| mentionedUsers | `getMentionedUsers()` | [`User[]`](/sdk/reference/entities#user) | Array of users mentioned in the message | +| tags (on each mentioned user) | `getTags()` | `string[]` | Tags associated with the mentioned user | + ## Mentions With Blocked Info To get a list of messages in a conversation where users are mentioned along with the blocked relationship of the mentioned users with the logged-in user. +Relevant fields to access on returned messages and their mentioned users: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| mentionedUsers | `getMentionedUsers()` | [`User[]`](/sdk/reference/entities#user) | Array of users mentioned in the message | +| blockedByMe (on each mentioned user) | `getBlockedByMe()` | `boolean` | Whether the logged-in user has blocked this mentioned user | +| hasBlockedMe (on each mentioned user) | `getHasBlockedMe()` | `boolean` | Whether this mentioned user has blocked the logged-in user | + ```javascript let UID = "UID"; let limit = 30; -var messagesRequest = new CometChat.MessagesRequestBuilder() +let messagesRequest = new CometChat.MessagesRequestBuilder() .setUID(UID) .setLimit(limit) .mentionsWithBlockedInfo(true) @@ -274,7 +312,7 @@ messagesRequest.fetchPrevious().then( let GUID = "GUID"; let limit = 30; -var messagesRequest = new CometChat.MessagesRequestBuilder() +let messagesRequest = new CometChat.MessagesRequestBuilder() .setGUID(GUID) .setLimit(limit) .mentionsWithBlockedInfo(true) @@ -366,3 +404,25 @@ To retrieve the list of users mentioned in the particular message, you can use t ```javascript message.getMentionedUsers() ``` + + +The getMentionedUsers() method returns an array of [`User`](/sdk/reference/entities#user) objects. + +--- + +## Next Steps + + + + Send text, media, and custom messages + + + Listen for incoming messages in real time + + + Add emoji reactions to messages + + + Organize conversations with message threads + + diff --git a/sdk/javascript/additional-message-filtering.mdx b/sdk/javascript/message-filtering.mdx similarity index 69% rename from sdk/javascript/additional-message-filtering.mdx rename to sdk/javascript/message-filtering.mdx index 6648ba35e..27115177f 100644 --- a/sdk/javascript/additional-message-filtering.mdx +++ b/sdk/javascript/message-filtering.mdx @@ -1,46 +1,67 @@ --- -title: "Additional Message Filtering" +title: "Message Filtering" +sidebarTitle: "Message Filtering" +description: "Advanced filtering options for fetching messages using MessagesRequestBuilder in the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +let parentId = 100; + +// Filter by category and type +let mediaRequest = new CometChat.MessagesRequestBuilder() + .setUID("UID") + .setCategories(["message"]) + .setTypes(["image", "video", "audio", "file"]) + .setLimit(50) + .build(); -The `MessagesRequest` class as you must be familiar with helps you to fetch messages based on the various parameters provided to it. This document will help you understand better the various options that are available using the `MessagesRequest` class. +// Unread messages only +let unreadRequest = new CometChat.MessagesRequestBuilder() + .setUID("UID") + .setUnread(true) + .setLimit(50) + .build(); -The `MessagesRequest` class is designed using the `Builder design pattern`. In order to obtain an object of the `MessagesRequest` class, you will have to make use of the `MessagesRequestBuilder` class in the `MessagesRequest` class. +// Threaded messages +let threadRequest = new CometChat.MessagesRequestBuilder() + .setUID("UID") + .setParentMessageId(parentId) + .setLimit(50) + .build(); -The `MessagesRequestBuilder` class allows you to set various parameters to the `MessagesRequest` class based on which the messages are fetched. +// Fetch with pagination +mediaRequest.fetchPrevious().then(messages => { }); +mediaRequest.fetchNext().then(messages => { }); +``` -Steps to generate an object of the MessagesRequest class: +**Key methods:** `setUID()`, `setGUID()`, `setLimit()`, `setCategories()`, `setTypes()`, `setTags()`, `setUnread()`, `setParentMessageId()`, `setMessageId()`, `setTimestamp()`, `hideReplies()`, `hideDeletedMessages()` + -1. Create an object of the `MessagesRequestBuilder` class. -2. Set all the parameters you wish to set. -3. Call the `build()` method of the `MessagesRequestBuilder` class to get an object of the `MessagesRequest` class. +The `MessagesRequest` class fetches messages based on various parameters. It uses the Builder design pattern via `MessagesRequestBuilder`. -Once you have an object of the `MessagesRequest` class, you can call either the `fetchNext()` method or the `fetchPrevious()` method using the object. +To fetch messages: +1. Create a `MessagesRequestBuilder` object +2. Set your desired parameters +3. Call `build()` to get a `MessagesRequest` object +4. Call `fetchNext()` or `fetchPrevious()` to retrieve messages -1. fetchNext() - Calling this method will return the messages after the specified parameters. -2. fetchPrevious() - Calling this method will give you messages before the specified parameters. +| Method | Description | +| --- | --- | +| `fetchNext()` | Returns messages after the specified parameters | +| `fetchPrevious()` | Returns messages before the specified parameters | -Since messages are obtained in a paginated manner, a `maximum of 100` messages can be pulled in a single iteration. Calling the `fetchPrevious()`/`fetchNext()` method on the same `MessagesRequest` object will get you the next set of messages. +Messages are paginated with a maximum of 100 per request. Call `fetchPrevious()`/`fetchNext()` repeatedly on the same object to get subsequent pages. -Now that you are clear how to use the `MessagesRequest` class, below are the various options available: +Both methods return an array of [`BaseMessage`](/sdk/reference/messages#basemessage) objects. Each message may be a [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), [`CustomMessage`](/sdk/reference/messages#custommessage), [`Action`](/sdk/reference/messages#action), or [`Call`](/sdk/reference/messages#call). Use `instanceof` to check the type. ## Number of messages fetched -*In other words, how do I set the number of messages fetched in a single iteration* - -To achieve this, you can use the `setLimit()` method. This method takes an integer value as the input and informs the SDK to fetch the specified number of messages in one iteration. The maximum number of messages that can be fetched in one go is `100`. +Set the number of messages to fetch per request using `setLimit()`. Maximum is 100. - -```js -let messagesRequest = new CometChat.MessagesRequestBuilder() - .setLimit(50) - .build(); -``` - - - ```typescript let messagesRequest: CometChat.MessagesRequest = @@ -49,26 +70,22 @@ let messagesRequest: CometChat.MessagesRequest = - - -## Messages for a user conversation - -*In other words, how do I fetch messages between me and any user* - -This can be achieved using the `setUID()` method. This method takes the UID of the user with whom the conversation is to be fetched. When a valid UID is passed, the SDK will return all the messages that are a part of the conversation between the logged-in user and the UID that has been specified. - - - + ```javascript -let UID = "UID"; let messagesRequest = new CometChat.MessagesRequestBuilder() - .setUID(UID) .setLimit(50) .build(); ``` + + +## Messages for a user conversation + +Use `setUID()` to fetch messages between the logged-in user and a specific user. + + ```typescript let UID = "UID"; @@ -78,26 +95,30 @@ let messagesRequest: CometChat.MessagesRequest = - - -## Messages for a group conversation - -*In other words, how do I fetch messages for any group conversation* - -You can achieve this using the `setGUID()` method. This method takes the GUID of a group for which the conversations are to be fetched. Passing a valid GUID to this method will return all the messages that are a part of the group conversation. Please note that the logged-in user must be a member of the group to fetch the messages for that group. - - - + ```javascript -let GUID = "GUID"; +let UID = "UID"; let messagesRequest = new CometChat.MessagesRequestBuilder() - .setGUID(GUID) + .setUID(UID) .setLimit(50) .build(); ``` + + +When messages are fetched successfully, the response includes an array of message objects. + + +The examples on this page use a small `setLimit()` value for brevity. In production, use a higher limit (up to 100) for efficient pagination. + + +## Messages for a group conversation + +Use `setGUID()` to fetch messages from a group. The logged-in user must be a member of the group. + + ```typescript let GUID: string = "GUID"; @@ -107,19 +128,28 @@ let messagesRequest: CometChat.MessagesRequest = - + +```javascript +let GUID = "GUID"; +let messagesRequest = new CometChat.MessagesRequestBuilder() + .setGUID(GUID) + .setLimit(50) + .build(); +``` - + -If none of the above two methods `setUID()` and `setGUID()` is used, all the messages for the logged-in user will be fetched. This means that messages from all the one-on-one and group conversations which the logged-in user is a part of will be returned.> All the parameters discussed below can be used along with the setUID() or setGUID() or without any of the two to fetch all the messages that the logged-in user is a part of. + + +When messages are fetched successfully, the response includes an array of message objects. + +If neither `setUID()` nor `setGUID()` is used, all messages for the logged-in user across all conversations will be fetched. All parameters below can be combined with `setUID()` or `setGUID()`. ## Messages before/after a message -*In other words, how do I fetch messages before or after a particular message* - -This can be achieved using the `setMessageId()` method. This method takes the message-id as input and provides messages only after/before the message-id based on if the fetchNext() or fetchPrevious() method is triggered. +Use `setMessageId()` to fetch messages before or after a specific message ID. Use `fetchNext()` to get messages after, or `fetchPrevious()` to get messages before. @@ -182,13 +212,11 @@ let GUID: string = "GUID", -This method can be used along with `setUID()` or `setGUID()` methods to fetch messages after/before any specific message-id for a particular user/group conversation. +This method can be combined with `setUID()` or `setGUID()` to fetch messages around a specific message in a conversation. ## Messages before/after a given time -*In other words, how do I fetch messages before or after a particular date or time* - -You can easily achieve this using the `setTimestamp()` method. This method takes the Unix timestamp as input and provides messages only after/before the timestamp based on if fetchNext() or fetchPrevious() method is triggered. +Use `setTimestamp()` with a Unix timestamp to fetch messages before or after a specific time. @@ -251,13 +279,11 @@ let GUID: string = "GUID", -This method can be used along with `setUID()` or `setGUID()` methods to fetch messages after/before any specific date or time for a particular user/group conversation. +This method can be combined with `setUID()` or `setGUID()` to fetch messages around a specific time in a conversation. ## Unread messages -*In other words, how do I fetch unread messages* - -This can easily be achieved using setting the unread flag to true. For this, you need to use the `setUnread()` method. This method takes a boolean value as input. If the value is set to true, the SDK will return just the unread messages. +Use `setUnread(true)` to fetch only unread messages. @@ -316,13 +342,11 @@ let GUID: string = "GUID", -This method along with `setGUID()` or `setUID()` can be used to fetch unread messages for a particular group or user conversation respectively. +Combine with `setGUID()` or `setUID()` to fetch unread messages for a specific conversation. ## Exclude messages from blocked users -*In other words, how do I fetch messages excluding the messages from the users I have blocked* - -This can be easily achieved using the `hideMessagesFromBlockedUsers()` method. This method accepts a boolean value which determines if the messages from users blocked by the logged-in user need to be a part if the fetched messages. If the value is set to true, the messages will be hidden and won't be a part of the messages fetched. The default value is false i.e if this method is not used, the messages from blocked users will be included in the fetched messages. +Use `hideMessagesFromBlockedUsers(true)` to exclude messages from users you've blocked. Default is `false`. @@ -381,13 +405,11 @@ let GUID: string = "GUID", -This method can be used to hide the messages by users blocked by logged in user in groups that both the members are a part of. +This also works in group conversations where both users are members. ## Updated and received messages -*In other words, how do I fetch messages that have been received or updated after a particular date or time* - -This method accepts a Unix timestamp value and will return all the messages that have been updated and the ones that have been sent/received after the specified time. The messages updated could mean the messages that have been marked as read/delivered or if the messages are edited or deleted. +Use `setUpdatedAfter()` with a Unix timestamp to fetch messages that were sent or updated after a specific time. Updated messages include those marked as read/delivered, edited, or deleted. @@ -450,13 +472,11 @@ let GUID: string = "GUID", -This can be useful in finding the messages that have been received or updated after a certain time. Can prove very useful if you are saving the messages locally and would like to know the messages that have been updated or received after the last message available in your local databases. +Useful for syncing messages with a local database — fetch only what's changed since your last sync. ## Updated messages only -*In other words, how do I fetch messages that have been updated after a particular date or time* - -This can be achieved easily by setting the updatesOnly parameter to true. To do so, you can use the updatesOnly() method. This method takes a boolean input and can be used with the `setUpdatedAfter()` method to get jus the updated messages and not the messages sent/received after the specified time. This method cannot be used independently and always needs to be used with the `setUpdatedAfter()` method. +Use `updatesOnly(true)` with `setUpdatedAfter()` to fetch only updated messages (not newly received ones). This method must be used together with `setUpdatedAfter()`. @@ -523,13 +543,11 @@ let GUID: string = "GUID", -## Messages for multiple categories - -*In other words, how do I fetch messages belonging to multiple categories* +When messages are fetched successfully, the response includes only messages that have been updated (edited, deleted, read/delivered status changed) after the specified timestamp. -We recommend before trying this, you refer to the [Message structure and hierarchy guide](/sdk/javascript/message-structure-and-hierarchy) to get familiar with the various categories of messages. +## Messages for multiple categories -For this, you will have to use the `setCategories()` method. This method accepts a list of categories. This tells the SDK to fetch messages only belonging to these categories. +Use `setCategories()` with an array of category names to filter by message category. See [Message structure and hierarchy](/sdk/javascript/message-structure-and-hierarchy) for available categories. @@ -592,15 +610,11 @@ let GUID: string = "GUID", -The above snippet will help you get only the messages belonging to the `message` and `custom` category. This can also be used to disable certain categories of messages like `call` and `action`. +The above snippet fetches only messages in the `message` and `custom` categories. Use this to exclude categories like `call` and `action`. ## Messages for multiple types -*In other words, how do I fetch messages belonging to multiple types* - -We recommend before trying this, you refer to the [Message structure and hierarchy guide](/sdk/javascript/message-structure-and-hierarchy) to get familiar with the various types of messages. - -This can be easily achieved using the `setTypes()` method. This method accepts a list of types. This tells the SDK to fetch messages only belonging to these types. +Use `setTypes()` with an array of type names to filter by message type. See [Message structure and hierarchy](/sdk/javascript/message-structure-and-hierarchy) for available types. @@ -671,13 +685,11 @@ let GUID: string = "GUID", -Using the above code snippet, you can fetch all the media messages. +The above snippet fetches all media messages (image, video, audio, file). ## Messages for a specific thread -*In other words, how do I fetch messages that are a part of a thread and not directly a user/group conversations* - -This can be done using the `setParentMessageId()` method. This method needs to be used when you have implemented threaded conversations in your app. This method will return the messages only belonging to the thread with the specified parent Id. +Use `setParentMessageId()` to fetch messages belonging to a specific thread. @@ -740,13 +752,11 @@ let GUID: string = "GUID", -The above code snippet returns the messages that belong to the thread with parent id 100. +The above code returns messages belonging to the thread with the specified parent message ID. ## Hide threaded messages in user/group conversations -*In other words, how do I exclude threaded messages from the normal user/group conversations* - -In order to do this, you can use the `hideReplies()` method. This method is also related to threaded conversations. This method takes boolean as input. This boolean when set to true will make sure that the messages that belong to threads are not fetched. If set to false, which is also the default value, the messages belong to the threads will also be fetched along with other messages. +Use `hideReplies(true)` to exclude threaded messages from the main conversation. Default is `false`. @@ -804,12 +814,9 @@ let GUID: string = "GUID", - ## Hide deleted messages in user/group conversations -*In other words, how do I exclude deleted messages a user/group conversations* - -In order to do this, you can use the `hideDeletedMessages()` method. This method takes boolean as input. This boolean when set to true will make sure that the deleted messages are not fetched. If set to false, which is also the default value, the deleted messages will also be fetched along with other messages. +Use `hideDeletedMessages(true)` to exclude deleted messages. Default is `false`. @@ -867,12 +874,9 @@ let GUID: string = "GUID", - ## Hide quoted messages in user/group conversations -*In other words, how do I exclude quoted messages in a user/group conversations* - -In order to do this, you can use the `hideQuotedMessages()` method. This method takes boolean as input. This boolean when set to true will make sure that the quoted messages are not fetched. If set to false, which is also the default value, the quoted messages will also be fetched along with other messages. +Use `hideQuotedMessages(true)` to exclude quoted messages. Default is `false`. @@ -930,12 +934,9 @@ let GUID: string = "GUID", - ## Messages by tags -*In other words, how do I fetch messages by tags* - -In order to do this, you can use the `setTags()` method. This method accepts a list of tags. This tells the SDK to fetch messages only belonging to these tags. +Use `setTags()` with an array of tag names to fetch only messages with those tags. @@ -997,12 +998,9 @@ let GUID: string = "GUID", - ## Messages with tags -*In other words, how do I fetch messages with the tags information* - -In order to do this, you can use the `withTags()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages along with the tags. When set to `false` , the SDK will not fetch tags associated with messages. The default value for this parameter is `false` . +Use `withTags(true)` to include tag information in the response. Default is `false`. @@ -1060,12 +1058,15 @@ let GUID: string = "GUID", +When `withTags(true)` is set, each message's `tags` field will be populated. Access tags using `getTags()`. -## Messages with links +| Additional Field | Getter | Return Type | Description | +| --- | --- | --- | --- | +| tags | `getTags()` | `string[]` | Tags associated with the message | -In other words, as a logged-in user, how do I fetch messages that contains links? +## Messages with links -In order to do this, you can use the `hasLinks()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages which have links in the text. The default value for this parameter is `false`. +Use `hasLinks(true)` to fetch only messages containing links. Default is `false`. @@ -1129,12 +1130,9 @@ let GUID: string = "GUID", - ## Messages with attachments -In other words, as a logged-in user, how do I fetch messages that contains attachments? - -In order to do this, you can use the `hasAttachments()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages which have attachments (image, audio, video or file). The default value for this parameter is `false`. +Use `hasAttachments(true)` to fetch only messages with attachments (image, audio, video, or file). Default is `false`. @@ -1199,11 +1197,11 @@ let GUID: string = "GUID", -## Messages with reactions +The response contains media message objects with attachment details including file metadata and thumbnail URLs. -In other words, as a logged-in user, how do I fetch messages that contains reactions? +## Messages with reactions -In order to do this, you can use the `hasReactions()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages which have reactions. The default value for this parameter is `false`. +Use `hasReactions(true)` to fetch only messages that have reactions. Default is `false`. @@ -1268,11 +1266,11 @@ let GUID: string = "GUID", -## Messages with mentions +The response contains message objects with reactions. Each message's `data` object includes a `reactions` array. -In other words, as a logged-in user, how do I fetch messages that contains mentions? +## Messages with mentions -In order to do this, you can use the `hasMentions()` method. This method accepts boolean as input. When set to `true` , the SDK will fetch messages which have mentions. The default value for this parameter is `false`. +Use `hasMentions(true)` to fetch only messages that contain mentions. Default is `false`. @@ -1337,11 +1335,11 @@ let GUID: string = "GUID", -## Messages with particular user mentions +The response contains text message objects with mentions. Each message has a `mentionedUsers` array, a `mentionedMe` boolean, and a `data.mentions` object. -In other words, as a logged-in user, how do I fetch messages that mentions specific users? +## Messages with particular user mentions -In order to do this, you can use the `setMentionedUIDs()` method. This method accepts an array of UIDs as input. When set, the SDK will fetch messages which have the mentions of the UIDs passed. +Use `setMentionedUIDs()` with an array of UIDs to fetch messages that mention specific users. @@ -1406,11 +1404,11 @@ let GUID: string = "GUID", -## Messages with specific attachment types +The response contains text message objects that mention the specified users. -In other words, as a logged-in user, how do I fetch messages that contain specific types of attachments? +## Messages with specific attachment types -In order to do this, you can use the `setAttachmentTypes()` method. This method accepts an array of `CometChat.AttachmentType` ENUM values as input. When provided, the SDK will fetch only those messages that include attachments of the specified types (such as image, audio, video, or file). +Use `setAttachmentTypes()` with an array of `CometChat.AttachmentType` values to fetch messages with specific attachment types. @@ -1474,3 +1472,23 @@ let GUID: string = "GUID", + +The response contains media message objects filtered to the specified attachment types. +--- + +## Next Steps + + + + Send text, media, and custom messages + + + Listen for incoming messages in real-time + + + Understand message categories, types, and hierarchy + + + Work with threaded conversations + + diff --git a/sdk/javascript/message-structure-and-hierarchy.mdx b/sdk/javascript/message-structure-and-hierarchy.mdx index 797cb91df..158717a7b 100644 --- a/sdk/javascript/message-structure-and-hierarchy.mdx +++ b/sdk/javascript/message-structure-and-hierarchy.mdx @@ -1,86 +1,187 @@ --- -title: "Message" -sidebarTitle: "Message Structure And Hierarchy" +title: "Message Structure and Hierarchy" +sidebarTitle: "Message Structure" +description: "Understand the message categories, types, and hierarchy in the CometChat JavaScript SDK including text, media, custom, action, interactive, and call messages." --- + +Message categories and types: +- **message** → `text`, `image`, `video`, `audio`, `file` +- **custom** → Developer-defined types (e.g., `location`, `poll`) +- **interactive** → `form`, `card`, `customInteractive` +- **action** → `groupMember` (joined/left/kicked/banned), `message` (edited/deleted) +- **call** → `audio`, `video` + -The below diagram helps you better understand the various message categories and types that a CometChat message can belong to. +Every message in CometChat belongs to a category (`message`, `custom`, `interactive`, `action`, `call`) and has a specific type within that category. On the SDK side, all messages extend [`BaseMessage`](/sdk/reference/messages#basemessage), with subclasses like [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), [`CustomMessage`](/sdk/reference/messages#custommessage), [`Action`](/sdk/reference/messages#action), and [`Call`](/sdk/reference/messages#call). Understanding this hierarchy helps you handle different message types correctly. + +## Message Hierarchy -As you can see in the above diagram, every message belongs to a particular category. A message can belong to either one of the 4 categories - -1. Message -2. Custom -3. Action -4. Call - -Each category can be further be classified into types. - -A message belonging to the category `message` can be classified into either 1 of the below types: - -1. text - A plain text message -2. image- An image message -3. video- A video message -4. audio- An audio message -5. file- A file message - -## Custom - -In the case of messages that belong to the `custom` category, there are no predefined types. Custom messages can be used by developers to send messages that do not fit in the default category and types provided by CometChat. For messages with the category `custom`, the developers can set their own type to uniquely identify the custom message. A very good example of a custom message would be the sharing of location co-ordinates. In this case, the developer can decide to use the custom message with type set to `location`. - -## Interactive - -An InteractiveMessage is a specialized object that encapsulates an interactive unit within a chat message, such as an embedded form that users can fill out directly within the chat interface. Messages belonging to the interactive category can further be classified into one of the below types: - -1. form- for interactive form -2. card- for interactive card -3. customInteractive- for custom interaction messages - - - -to know about Interactive messages please [click here](/sdk/javascript/interactive-messages) +## Categories Overview + +| Category | Types | Description | +| --- | --- | --- | +| `message` | `text`, `image`, `video`, `audio`, `file` | Standard user messages | +| `custom` | Developer-defined | Custom data (location, polls, etc.) | +| `interactive` | `form`, `card`, `customInteractive` | Messages with actionable UI elements | +| `action` | `groupMember`, `message` | System-generated events | +| `call` | `audio`, `video` | Call-related messages | + +## Checking Message Category and Type + +Use `getCategory()` and `getType()` to determine how to handle a received message: + + + +```typescript +const category: string = message.getCategory(); +const type: string = message.getType(); + +switch (category) { + case CometChat.CATEGORY_MESSAGE: + if (type === CometChat.MESSAGE_TYPE.TEXT) { + const textMsg = message as CometChat.TextMessage; + console.log("Text:", textMsg.getText()); + } else if (type === CometChat.MESSAGE_TYPE.IMAGE) { + const mediaMsg = message as CometChat.MediaMessage; + console.log("Image URL:", mediaMsg.getData().url); + } + break; + case CometChat.CATEGORY_CUSTOM: + const customMsg = message as CometChat.CustomMessage; + console.log("Custom type:", type, "data:", customMsg.getData()); + break; + case CometChat.CATEGORY_ACTION: + const actionMsg = message as CometChat.Action; + console.log("Action:", actionMsg.getAction()); + break; + case CometChat.CATEGORY_CALL: + const callMsg = message as CometChat.Call; + console.log("Call status:", callMsg.getStatus()); + break; +} +``` + + +```javascript +const category = message.getCategory(); +const type = message.getType(); + +switch (category) { + case CometChat.CATEGORY_MESSAGE: + if (type === CometChat.MESSAGE_TYPE.TEXT) { + console.log("Text:", message.getText()); + } else if (type === CometChat.MESSAGE_TYPE.IMAGE) { + console.log("Image URL:", message.getData().url); + } + break; + case CometChat.CATEGORY_CUSTOM: + console.log("Custom type:", type, "data:", message.getData()); + break; + case CometChat.CATEGORY_ACTION: + console.log("Action:", message.getAction()); + break; + case CometChat.CATEGORY_CALL: + console.log("Call status:", message.getStatus()); + break; +} +``` + + + +## Standard Messages (`message` Category) + +Messages with category `message` are standard user-sent messages: + +| Type | Description | +| --- | --- | +| `text` | Plain text message | +| `image` | Image attachment | +| `video` | Video attachment | +| `audio` | Audio attachment | +| `file` | File attachment | + +## Custom Messages (`custom` Category) + +Custom messages allow you to send data that doesn't fit the default categories. You define your own type to identify the message (e.g., `location`, `poll`, `sticker`). + +```javascript +// Example: Sending a location as a custom message +const customMessage = new CometChat.CustomMessage( + receiverID, + CometChat.RECEIVER_TYPE.USER, + "location", + { latitude: 37.7749, longitude: -122.4194 } +); +``` + +See [Send Message → Custom Messages](/sdk/javascript/send-message#custom-message) for details. + +## Interactive Messages (`interactive` Category) + +Interactive messages contain actionable UI elements that users can interact with directly in the chat: + +| Type | Description | +| --- | --- | +| `form` | Embedded form with input fields | +| `card` | Card with buttons and actions | +| `customInteractive` | Custom interactive elements | + +See [Interactive Messages](/sdk/javascript/interactive-messages) for implementation details. + +## Action Messages (`action` Category) + +Action messages are system-generated events. They have a `type` and an `action` property: + +**Type: `groupMember`** — Actions on group members: +- `joined` — Member joined the group +- `left` — Member left the group +- `kicked` — Member was kicked +- `banned` — Member was banned +- `unbanned` — Member was unbanned +- `added` — Member was added +- `scopeChanged` — Member's scope was changed + +**Type: `message`** — Actions on messages: +- `edited` — Message was edited +- `deleted` — Message was deleted + +## Call Messages (`call` Category) + +Call messages track call events with types `audio` or `video`. The `status` property indicates the call state: + +| Status | Description | +| --- | --- | +| `initiated` | Call started | +| `ongoing` | Call accepted and in progress | +| `canceled` | Caller canceled | +| `rejected` | Receiver rejected | +| `unanswered` | No answer | +| `busy` | Receiver on another call | +| `ended` | Call completed | + +See [Default Calling](/sdk/javascript/default-call) or [Direct Calling](/sdk/javascript/direct-call) for implementation. - - -## Action - -Action messages are system-generated messages. Messages belonging to the `action` category can further be classified into one of the below types: - -1. groupMember - action performed on a group member. -2. message - action performed on a message. - -Action messages hold another property called `action` which actually determine the action that has been performed For the type `groupMember` the action can be either one of the below: - -1. joined - when a group member joins a group -2. left - when a group member leaves a group -3. kicked - when a group member is kicked from the group -4. banned - when a group member is banned from the group -5. unbanned - when a group member is unbanned from the group -6. added - when a user is added to the group -7. scopeChanged - When the scope of a group member is changed. - -For the type `message`, the action can be either one of the below: - -1. edited - when a message is edited. -2. deleted - when a message is deleted. - -## Call - -Messages with the category `call` are Calling related messages. These can belong to either one of the 2 types - -1. audio -2. video - -The call messages have a property called status that helps you figure out the status of the call. The status can be either one of the below values: +--- -1. initiated - when a is initiated to a user/group -2. ongoing - when the receiver of the call has accepted the call -3. canceled - when the call has been canceled by the initiator of the call -4. rejected - when the call has been rejected by the receiver of the call -5. unanswered - when the call was not answered by the receiver. -6. busy - when the receiver of the call was busy on another call. -7. ended - when the call was successfully completed and ended by either the initiator or receiver. +## Next Steps + + + + Send text, media, and custom messages + + + Listen for incoming messages in real time + + + Send messages with embedded forms and buttons + + + Advanced message filtering with RequestBuilder + + diff --git a/sdk/javascript/messaging-overview.mdx b/sdk/javascript/messaging-overview.mdx index 5a8902b43..4914e383e 100644 --- a/sdk/javascript/messaging-overview.mdx +++ b/sdk/javascript/messaging-overview.mdx @@ -1,12 +1,40 @@ --- title: "Messaging" sidebarTitle: "Overview" +description: "Overview of messaging capabilities in the CometChat JavaScript SDK including sending, receiving, editing, deleting messages, and advanced features." --- +{/* TL;DR for Agents and Quick Reference */} + +Choose your path: +- **Send Messages** → [Send Message](/sdk/javascript/send-message) - Text, media, custom +- **Receive Messages** → [Receive Message](/sdk/javascript/receive-message) - Real-time listeners +- **Edit/Delete** → [Edit](/sdk/javascript/edit-message) | [Delete](/sdk/javascript/delete-message) +- **Advanced** → [Threads](/sdk/javascript/threaded-messages) | [Reactions](/sdk/javascript/reactions) | [Mentions](/sdk/javascript/mentions) + Messaging is one of the core features of CometChat. We've thoughtfully created methods to help you send, receive and fetch message history. At the minimum, you must add code for [sending messages](/sdk/javascript/send-message) and [receiving messages](/sdk/javascript/receive-message). Once you've implemented that, you can proceed to more advanced features like [typing indicators](/sdk/javascript/typing-indicators) and [delivery & read receipts](/sdk/javascript/delivery-read-receipts). + +--- + +## Next Steps + + + + Send text, media, and custom messages + + + Listen for incoming messages in real time + + + Understand message categories and types + + + Show real-time typing status in conversations + + diff --git a/sdk/javascript/overview.mdx b/sdk/javascript/overview.mdx index 05fa4c3d2..2b3cf997a 100644 --- a/sdk/javascript/overview.mdx +++ b/sdk/javascript/overview.mdx @@ -1,554 +1,135 @@ --- -title: "Overview" +title: "JavaScript SDK" +sidebarTitle: "Overview" +description: "Add real-time chat, voice, and video to your JavaScript application with the CometChat SDK." --- +{/* TL;DR for Agents and Quick Reference */} + - -This guide demonstrates how to add chat to a JavaScript application using CometChat. Before you begin, we strongly recommend you read the [Key Concepts](/sdk/javascript/key-concepts) guide. - -#### I want to integrate with my app - -1. [Get your application keys](overview#get-your-application-keys) -2. [Add the CometChat dependency](overview#add-the-cometchat-dependency) -3. [Initialize CometChat](overview#initialize-cometchat) -4. [Register and Login your user](overview#register-and-login-your-user) -5. [Integrate our UI Kits](overview#integrate-our-ui-kits) -6. [Integrate our Chat Widget](overview#integrate-our-chat-widget) - -#### I want to explore a sample app (includes UI) - -Open the app folder in your favorite code editor and follow the steps mentioned in the `README.md` file. - -[React Sample App](https://github.com/cometchat-pro/javascript-react-chat-app) - -[Angular Sample App](https://github.com/cometchat-pro/javascript-angular-chat-app) - -[Vue Sample App](https://github.com/cometchat-pro/javascript-vue-chat-app) - -### Get your Application Keys - -[Signup for CometChat](https://app.cometchat.com) and then: - -1. Create a new app -2. Head over to the **API & Auth Keys** section and note the **Auth Key**, **App ID** & **Region** - -## Add the CometChat Dependency - -### NPM - - - -```js - npm install @cometchat/chat-sdk-javascript -``` - - - - - -Then, import the `CometChat` object wherever you want to use CometChat. - - - -```js - import { CometChat } from "@cometchat/chat-sdk-javascript"; -``` - - - - - -### HTML (via CDN) - -Include the CometChat JavaScript library in your HTML code - - - -```html - -``` - - - - - -### Server Side Rendering (SSR) Compatibility - -You can use CometChat with SSR frameworks such as [Next.js](https://nextjs.org/) or [NuxtJS](https://nuxtjs.org/) by importing it dynamically on the client side. - -#### Next.js - -You need to import the CometChat SDK dynamically in the `useEffect` React Hook or `componentDidMount()` lifecycle method. - - - -```javascript -import React from "react"; -import Chat from "./Chat"; - -export default function Home() { - let [libraryImported, setLibraryImported] = React.useState(false); - - React.useEffect(() => { - window.CometChat = require("@cometchat/chat-sdk-javascript").CometChat; - setLibraryImported(true); - }); - - return libraryImported ? :

Loading....

; -} -``` - -
- - -```javascript -import React from 'react'; -import Chat from './Chat'; - -export default class Home extends React.Component { - -constructor(props) { - super(props); - this.state = { - libraryImported: false - }; -} - -componentDidMount(){ - CometChat = require("@cometchat/chat-sdk-javascript").CometChat; - this.setState({libraryImported: true}); -} - -return( - this.state.libraryImported ? :

Loading....

-) - -} -``` - -
- - -```javascript -import React, { Component } from "react"; - -import { COMETCHAT_CONSTANTS } from "./CONSTS"; - -export default class Chat extends Component { - constructor(props) { - super(props); - this.state = { - user: undefined, - }; - } - - componentDidMount() { - this.init(); - } - - init() { - CometChat.init( - COMETCHAT_CONSTANTS.APP_ID, - new CometChat.AppSettingsBuilder() - .setRegion(COMETCHAT_CONSTANTS.REGION) - .subscribePresenceForAllUsers() - .build() - ).then( - () => { - this.login(); - }, - (error) => { - console.log("Init failed with exception:", error); - } - ); - } - - login() { - let UID = "UID"; - CometChat.login(UID, COMETCHAT_CONSTANTS.AUTH_KEY).then( - (user) => { - this.setState({ user }); - }, - (error) => { - console.log("Login failed with exception:", error); - } - ); - } - - render() { - return this.state.user ? ( -
User logged in
- ) : ( -
User not logged in
- ); - } -} -``` - -
- - -```javascript -export const COMETCHAT_CONSTANTS = { - APP_ID: "APP_ID", - REGION: "REGION", - AUTH_KEY: "AUTH_KEY", -}; -``` - - - -
- -#### NuxtJS - -You need to import the CometChat SDK dynamically in the `mounted` lifecycle hook. - - - -```javascript - - - -``` - - - - -```javascript - - - +```bash +npm install @cometchat/chat-sdk-javascript ``` - - - ```javascript -module.exports = { - APP_ID: "APP_ID", - REGION: "REGION", - AUTH_KEY: "AUTH_KEY", -}; -``` - - - - - -#### Ionic/Cordova - -For Ionic and Cordova applications, you can use the JavaScript SDK directly. Import the CometChat SDK in your component or service: - - - -```typescript -import { Component, OnInit } from '@angular/core'; -import { CometChat } from '@cometchat/chat-sdk-javascript'; - -@Component({ - selector: 'app-root', - templateUrl: 'app.component.html', -}) -export class AppComponent implements OnInit { - - ngOnInit() { - this.initCometChat(); - } - - initCometChat() { - const appID = 'APP_ID'; - const region = 'APP_REGION'; - - const appSetting = new CometChat.AppSettingsBuilder() - .subscribePresenceForAllUsers() - .setRegion(region) - .autoEstablishSocketConnection(true) - .build(); - - CometChat.init(appID, appSetting).then( - () => { - console.log('CometChat initialized successfully'); - }, - (error) => { - console.log('CometChat initialization failed:', error); - } - ); - } -} -``` - - +import { CometChat } from "@cometchat/chat-sdk-javascript"; - - - - -The dedicated Ionic Cordova SDK has been deprecated. For new Ionic/Cordova applications, use the JavaScript SDK as shown above. Existing users of the Ionic SDK can refer to the [legacy documentation](/sdk/ionic-legacy/overview) for reference. - - - -## Initialize CometChat - -The `init()` method initializes the settings required for CometChat. The `init()` method takes the below parameters: - -1. appID - Your CometChat App ID -2. appSettings - An object of the AppSettings class can be created using the AppSettingsBuilder class. The region field is mandatory and can be set using the `setRegion()` method. - -The `AppSettings` class allows you to configure the following settings: - -* **Region**: The region where your app was created. -* [Presence Subscription](/sdk/javascript/user-presence): Represents the subscription type for user presence (real-time online/offline status) -* **autoEstablishSocketConnection(boolean value)**: This property takes a boolean value which when set to `true` informs the SDK to manage the web-socket connection internally. If set to `false`, it informs the SDK that the web-socket connection will be managed manually. The default value for this parameter is true. For more information on this, please check the [Managing Web-Socket connections manually](/sdk/javascript/managing-web-sockets-connections-manually) section. The default value for this property is **true.** -* **overrideAdminHost(adminHost: string)**: This method takes the admin URL as input and uses this admin URL instead of the default admin URL. This can be used in case of dedicated deployment of CometChat. -* **overrideClientHost(clientHost: string)**: This method takes the client URL as input and uses this client URL instead of the default client URL. This can be used in case of dedicated deployment of CometChat. -* **setStorageMode(storageMode)**: This method allows you to configure how CometChat stores data locally. The storageMode parameter can be set to `CometChat.StorageMode.SESSION` to use session storage, which persists data only for the current browser session, or other available storage modes for different persistence behaviors. - -You need to call `init()` before calling any other method from CometChat. We suggest you call the `init()` method on app startup, preferably in the `index.js` file. - - - -```js -let appID = "APP_ID"; -let region = "APP_REGION"; -let appSetting = new CometChat.AppSettingsBuilder() +// 1. Initialize (once at app startup) +const appSettings = new CometChat.AppSettingsBuilder() + .setRegion("APP_REGION") // e.g. "us", "eu", "in" .subscribePresenceForAllUsers() - .setRegion(region) .autoEstablishSocketConnection(true) - .setStorageMode(CometChat.StorageMode.SESSION) .build(); -CometChat.init(appID, appSetting).then( - () => { - console.log("Initialization completed successfully"); - }, - (error) => { - console.log("Initialization failed with error:", error); - } -); -``` - - - - -```typescript -let appID: string = "APP_ID", - region: string = "APP_REGION", - appSetting: CometChat.AppSettings = new CometChat.AppSettingsBuilder() - .subscribePresenceForAllUsers() - .setRegion(region) - .autoEstablishSocketConnection(true) - .setStorageMode(CometChat.StorageMode.SESSION) - .build(); -CometChat.init(appID, appSetting).then( - (initialized: boolean) => { - console.log("Initialization completed successfully", initialized); - }, (error: CometChat.CometChatException) => { - console.log("Initialization failed with error:", error); - } -); -``` - - - - - -Make sure you replace the `APP_ID` with your CometChat **App ID** and `APP_REGION` with your **App Region** in the above code. - -## Register and Login your user - -Once initialization is successful, you will need to create a user. To create users on the fly, you can use the `createUser()` method. This method takes a `User` object and the `Auth Key` as input parameters and returns the created `User` object if the request is successful. - - -```js -let authKey = "AUTH_KEY"; -var UID = "user1"; -var name = "Kevin"; +await CometChat.init("APP_ID", appSettings); -var user = new CometChat.User(UID); - -user.setName(name); - -CometChat.createUser(user, authKey).then( - (user) => { - console.log("user created", user); - }, - (error) => { - console.log("error", error); - } -); -``` - - - - -```typescript -let authKey: string = "AUTH_KEY", - UID: string = "user1", - name: string = "Kevin"; - -var user = new CometChat.User(UID); - -user.setName(name); - -CometChat.createUser(user, authKey).then( - (user: CometChat.User) => { - console.log("user created", user); - }, - (error: CometChat.CometChatException) => { - console.log("error", error); - } -); -``` - - - - - -Make sure that `UID` and `name` are specified as these are mandatory fields to create a user. - -Once you have created the user successfully, you will need to log the user into CometChat using the `login()` method. - -We recommend you call the CometChat `login()` method once your user logs into your app. The `login()` method needs to be called only once. - - - -This straightforward authentication method is ideal for proof-of-concept (POC) development or during the early stages of application development. For production environments, however, we strongly recommend using an [Auth Token](/sdk/javascript/authentication-overview#login-using-auth-token) instead of an Auth Key to ensure enhanced security. - - - - - -```js -var UID = "cometchat-uid-1"; -var authKey = "AUTH_KEY"; - -CometChat.getLoggedinUser().then( - (user) => { - if (!user) { - CometChat.login(UID, authKey).then( - (user) => { - console.log("Login Successful:", { user }); - }, - (error) => { - console.log("Login failed with exception:", { error }); - } - ); - } - }, - (error) => { - console.log("Some Error Occured", { error }); - } -); -``` - - - - -```typescript -var UID: string = "cometchat-uid-1", - authKey: string = "AUTH_KEY"; - -CometChat.getLoggedinUser().then( - (user: CometChat.User) => { - if (!user) { - CometChat.login(UID, authKey).then( - (user: CometChat.User) => { - console.log("Login Successful:", { user }); - }, - (error: CometChat.CometChatException) => { - console.log("Login failed with exception:", { error }); - } - ); - } - }, - (error: CometChat.CometChatException) => { - console.log("Some Error Occured", { error }); - } -); +// 2. Login (check session first) +const existing = await CometChat.getLoggedinUser(); +if (!existing) { + await CometChat.login("cometchat-uid-1", "AUTH_KEY"); // dev only — use Auth Token in production +} ``` - - - - -Make sure you replace the `AUTH_KEY` with your CometChat **AuthKey** in the above code. - - -Sample Users - -We have set up 5 users for testing having UIDs: `cometchat-uid-1`, `cometchat-uid-2`, `cometchat-uid-3`, `cometchat-uid-4` and `cometchat-uid-5`. - - - -The `login()` method returns the `User` object on `Promise` resolved containing all the information of the logged-in user. - - - -UID can be alphanumeric with an underscore and hyphen. Spaces, punctuation and other special characters are not allowed. - - - -## Integrate our UI Kits - -* Please refer to the section to integrate [React UI Kit](/ui-kit/react/overview) into your website. -* Please refer to the section to integrate [Angular UI Kit](/ui-kit/angular/overview) into your website. -* Please refer to the section to integrate [Vue UI Kit](/ui-kit/vue/overview) into your website. - -## Integrate our Chat Widget - -* Please refer to the section to integrate [Chat Widget](/widget/html-bootstrap-jquery) into your Website. +**Credentials:** App ID, Region, Auth Key from [CometChat Dashboard](https://app.cometchat.com) +**Test UIDs:** `cometchat-uid-1` through `cometchat-uid-5` +**SSR:** SDK requires browser APIs — initialize client-side only (`useEffect` / `mounted`) +
+ +The CometChat JavaScript SDK lets you add real-time messaging, voice, and video calling to any JavaScript application — React, Angular, Vue, Next.js, Nuxt, or vanilla JS. + +## Requirements + +| Requirement | Minimum Version | +|-------------|-----------------| +| npm | 8.x | +| Node.js | 16 | + +Works in all modern browsers (Chrome, Firefox, Safari, Edge) and SSR frameworks (Next.js, Nuxt) with [client-side initialization](/sdk/javascript/setup-sdk#ssr-compatibility). + +## Getting Started + + + + [Sign up for CometChat](https://app.cometchat.com) and create an app. Note your App ID, Region, and Auth Key from the Dashboard. + + + Add the SDK to your project and initialize it with your credentials. See [Setup SDK](/sdk/javascript/setup-sdk). + + + Log in users with Auth Key (development) or Auth Token (production). See [Authentication](/sdk/javascript/authentication-overview). + + + Send your first message, make a call, or manage users and groups. + + + +## Features + + + + 1:1 and group chat, threads, reactions, typing indicators, read receipts, file sharing, and interactive messages. + + + Ringing flows, direct call sessions, standalone calling, recording, virtual backgrounds, and screen sharing. + + + Create, retrieve, and manage users. Track online/offline presence and block/unblock users. + + + Public, private, and password-protected groups with member management, roles, and ownership transfer. + + + +## Sample Apps + +Explore working examples with full source code: + + + + React sample app + + + Angular sample app + + + Vue sample app + + + +## UI Kits + +Skip the UI work — use pre-built, customizable components: + + + + React UI Kit + + + Angular UI Kit + + + Vue UI Kit + + + +For the fastest integration, embed the [Chat Widget](/widget/html/overview) with just a few lines of code. + +## Resources + + + + UIDs, GUIDs, auth tokens, and core SDK concepts + + + Message categories, types, and hierarchy + + + Latest SDK version and release notes + + + Migration guide for V3 users + + + Common issues and solutions + + diff --git a/sdk/javascript/presenter-mode.mdx b/sdk/javascript/presenter-mode.mdx index 42b2bdc2a..882179b7f 100644 --- a/sdk/javascript/presenter-mode.mdx +++ b/sdk/javascript/presenter-mode.mdx @@ -1,45 +1,50 @@ --- title: "Presenter Mode" +sidebarTitle: "Presenter Mode" +description: "Learn how to implement Presenter Mode for webinars, keynotes, and online classes using the CometChat JavaScript Calls SDK." --- + + +```javascript +let callToken = "CALL_TOKEN"; +let htmlElement = document.getElementById("call-screen"); +let callListener = { + onCallEnded: () => console.log("Call ended"), + onUserJoined: (user) => console.log("User joined:", user), + onUserLeft: (user) => console.log("User left:", user), +}; + +// Start a presentation session +const settings = new CometChatCalls.PresenterSettingsBuilder() + .setIsPresenter(true) + .enableDefaultLayout(true) + .setCallEventListener(callListener) + .build(); + +CometChatCalls.joinPresentation(callToken, settings, htmlElement); +``` +- **Presenter** (max 5): Can share video, audio, and screen +- **Viewer** (up to 100 total): Passive consumers, no outgoing streams + -## Overview - -The Presenter Mode feature allows developers to create a calling service experience in which: - -1. There are one or more users who are presenting their video, audio and/or screen (Maximum 5) -2. Viewers who are consumers of that presentation. (They cannot send their audio, video or screen streams out). -3. The total number of presenters and viewers can go up to 100. -4. Features such as mute/unmute audio, show/hide camera capture, recording, etc. will be enabled only for the Presenter in this mode. -5. Other call participants will not get these features. Hence they act like passive viewers in the call. - -Using this feature developers can create experiences such as: - -1. All hands calls -2. Keynote speeches -3. Webinars -4. Talk shows -5. Online classes -6. and many more... - -### About this guide - -This guide demonstrates how to start a presentation into an React Native application. Before you begin, we strongly recommend you read the calling setup guide. - -Before starting a call session you have to generate a call token using the generateToken() method of the CometChatCalls SDK as mentioned [here](/sdk/javascript/direct-call#generate-call-token). +Presenter Mode enables broadcast-style calling — up to 5 presenters share content with passive viewers (up to 100 total participants). Use it for webinars, keynotes, all-hands meetings, online classes, or talk shows. -### Start Presentation Session +| Role | Capabilities | Max Count | +| ---- | ------------ | --------- | +| Presenter | Video, audio, screen sharing, mute controls, recording | 5 | +| Viewer | Watch and listen only (no outgoing streams) | Up to 100 total | -The most important class that will be used in the implementation is the `PresentationSettings` class. This class allows you to set the various parameters for the Presentation Mode. In order to set the various parameters of the `PresentationSettings` class, you need to use the `PresentationSettingsBuilder` class. Below are the various options available with the `PresentationSettings` class. +Before starting a presentation, generate a call token using `generateToken()` as described in [Call Session](/sdk/javascript/direct-call#generate-call-token). -You will need to set the User Type, There are 2 type of users in Presenter Mode, `Presenter` & `Participant` , You can set this `PresentationSettingsBuilder` by using the following method `setIsPresenter(true/false)` +## Start a Presentation Session -A basic example of how to start a Presentation: +Configure the presentation using `PresentationSettingsBuilder`. Set `setIsPresenter(true)` for presenters or `setIsPresenter(false)` for viewers. -```js +```javascript let presenterSettings = new CometChatCalls.PresenterSettingsBuilder() .setIsPresenter(isPresenter) .enableDefaultLayout(defaultLayout) @@ -58,13 +63,19 @@ CometChatCalls.joinPresentation( -## **Listeners** +## Listeners -Listeners can be added in two ways the first one is to use `.setCallEventListener(listeners : OngoingCallListener)` method in `CallSettingsBuilder` or `PresenterSettingsBuilder` class. The second way is to use `CometChatCalls.addCallEventListener(name: string, callListener: OngoingCallListener)` by this you can add multiple listeners and remove the specific listener by their name `CometChatCalls.removeCallEventListener(name: string)` +Add listeners in two ways: +1. Use `.setCallEventListener()` in `PresentationSettingsBuilder` +2. Use `CometChatCalls.addCallEventListener(name, listener)` for multiple listeners + + +Always remove listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + -```js +```javascript useEffect(() => { CometChatCalls.addCallEventListener('UNIQUE_ID', { onUserJoined: user => { @@ -85,9 +96,6 @@ useEffect(() => { onError: error => { console.log('Call Error: ', error); }, - onAudioModesUpdated: (audioModes) => { - console.log("audio modes:", audioModes); - }, onUserMuted: (event) => { console.log("user muted:", event); } @@ -100,48 +108,45 @@ useEffect(() => { -The `CometChatCallsEventsListener` listener provides you with the below callback methods: - -| Callback Method | Description | -| ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | -| onCallEnded() | This method is called when the call is successfully ended. The call details can be obtained from the `Call` object provided. | -| onCallEndButtonPressed() | This method is called when the user press end call button. | -| onUserJoined(user: RTCUser) | This method is called when any other user joins the call. The user details can be obtained from the `User` object provided. | -| onUserLeft(user: RTCUser) | This method is called when a user leaves the call. The details of the user can be obtained from the provided `User` object. | -| onUserListUpdated(users: Array\) | This method is triggered every time a participant joins or leaves the call providing the list of users active in the call | -| onAudioModesUpdated(devices: Array\) | This callback is triggered if any new audio output source is available or becomes unavailable. | -| onUserMuted(muteObj: RTCMutedUser) | This method is triggered when a user is muted in the ongoing call. | -| onRecordingStarted(user: RTCUser) | This method is triggered when a recording starts. | -| onRecordingStopped(user: RTCUser) | This method is triggered when a recording stops. | -| onError(e: CometChatException) | This method is called when there is some error in establishing the call. | +The `OngoingCallListener` provides the same callbacks as in [Call Session](/sdk/javascript/direct-call) and [Standalone Calling](/sdk/javascript/standalone-calling). For the full list of callbacks and their parameter shapes, see the [`OngoingCallListener`](/sdk/javascript/all-real-time-listeners#ongoing-call-listener-calls-sdk) reference. ## Settings -The `PresentationSettings` class is the most important class when it comes to the implementation of the Calling feature. This is the class that allows you to customize the overall calling experience. The properties for the call/conference can be set using the `PresentationSettingsBuilder` class. This will eventually give you and object of the `PresentationSettings` class which you can pass to the `joinPresentation()` method to start the call. - -The **mandatory** parameters that are required to be present for any call/conference to work are: - -1. Context - context of the activity/application -2. RelativeLayout - A RelativeLayout object in which the calling UI is loaded. +Configure the presentation experience using `PresentationSettingsBuilder`: + +| Method | Description | Default | +| ------ | ----------- | ------- | +| `setIsPresenter(isPresenter: boolean)` | If `true`, user joins as presenter. If `false`, user joins as viewer. | — | +| `enableDefaultLayout(defaultLayout: boolean)` | Show/hide the default button layout. If `false`, only the call view is displayed. | `true` | +| `showEndCallButton(showEndCallButton: boolean)` | Show/hide the end call button. | `true` | +| `showPauseVideoButton(showPauseVideoButton: boolean)` | Show/hide the pause video button. | `true` | +| `showMuteAudioButton(showMuteAudioButton: boolean)` | Show/hide the mute audio button. | `true` | +| `showSwitchCameraButton(showSwitchCameraButton: boolean)` | Show/hide the switch camera button. | `true` | +| `showAudioModeButton(showAudioModeButton: boolean)` | Show/hide the audio mode button. | `true` | +| `showRecordingButton(showRecordingButton: boolean)` | Show/hide the recording button. | `false` | +| `setIsAudioOnlyCall(audioOnly: boolean)` | If `true`, the call is audio-only. If `false`, audio-video. | `false` | +| `startWithAudioMuted(audioMuted: boolean)` | Start the call with the microphone muted. | `false` | +| `startWithVideoMuted(videoMuted: boolean)` | Start the call with the camera off. | `false` | +| `setDefaultAudioMode(audioMode: string)` | Start with a specific audio output. Values: `CometChatCalls.AUDIO_MODE.SPEAKER`, `EARPIECE`, `BLUETOOTH`, `HEADPHONES` | — | +| `setCallEventListener(listener)` | Set the event listener for call callbacks. | — | + +For custom UI, embed your own buttons and use the call control methods described in [Call Session Methods](/sdk/javascript/direct-call#methods). -The options available for customization of calls are: - -| Parameter | Description | -| ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `setIsPresenter(isPresenter: boolean)` | If set to `true` user will join as the presenter. If set to `false` user will join as the viewer. | -| `enableDefaultLayout(defaultLayout: boolean)` | If set to `true` enables the default layout for handling the call operations. If set to `false` it hides the button layout and just displays the Call View. **Default value = true** | -| `showEndCallButton(showEndCallButton: boolean)` | If set to `true` it displays the EndCallButton in Button Layout. If set to `false` it hides the EndCallButton in Button Layout. **Default value = true** | -| `showPauseVideoButton(showPauseVideoButton: boolean)` | If set to `true` it displays the PauseVideoButton in Button Layout. If set to `false` it hides the PauseVideoButton in Button Layout. **Default value = true** | -| `showMuteAudioButton`(showMuteAudioButton: boolean)\`\` | If set to `true` it displays the MuteAudioButton in Button Layout. If set to `false` it hides the MuteAudioButton in Button Layout. **Default value = true** | -| `showSwitchCameraButton`(showSwitchCameraButton: boolean)\`\` | If set to `true` it displays the SwitchCameraButton in Button Layout. If set to `false` it hides the SwitchCameraButton in Button Layout. **Default value = true** | -| `showAudioModeButton`(showAudioModeButton: boolean)\`\` | If set to `true` it displays the AudioModeButton in Button Layout. If set to `false` it hides the AudioModeButton in Button Layout. **Default value = true** | -| `setIsAudioOnlyCall(audioOnly: boolean)` | If set to `true`, the call will be strictly an audio call. If set to `false`, the call will be an audio-video call.**Default value = false** | -| `startWithAudioMuted(audioMuted: boolean)` | This ensures the call is started with the audio muted if set to true. **Default value = false** | -| `startWithVideoMuted(videoMuted: boolean)` | This ensures the call is started with the video paused if set to true. **Default value = false** | -| `startWithVideoMuted(videoMuted: boolean)` | If set to true it displays the Recording in Button Layout. if set to false it hides the Recording in Button Layout. **Default value = false** | -| `setDefaultAudioMode(audioMode: string)` | This method can be used if you wish to start the call with a specific audio mode. The available options are 1. CometChatCalls.AUDIO\_MODE.SPEAKER = "SPEAKER" 2. CometChatCalls.AUDIO\_MODE.EARPIECE = "EARPIECE" 3. CometChatCalls.AUDIO\_MODE.BLUETOOTH = "BLUETOOTH" 4. CometChatCalls.AUDIO\_MODE.HEADPHONES = "HEADPHONES" | -| `setEventListener(new CometChatCallsEventsListener())` | The `CometChatCallsEventsListener` listener provides you callbacks | - -In case you wish to achieve a completely customised UI for the Calling experience, you can do so by embedding default android buttons to the screen as per your requirement and then use the below methods to achieve different functionalities for the embedded buttons. +--- -For the use case where you wish to align your own custom buttons and not use the default layout provided by CometChat you can embed the buttons in your layout and use the below methods to perform the corresponding operations: +## Next Steps + + + + Start standard call sessions without presenter mode + + + Record call and presentation sessions + + + Add virtual backgrounds to video calls + + + Customize the video layout and containers + + diff --git a/sdk/javascript/rate-limits.mdx b/sdk/javascript/rate-limits.mdx index 17581b144..10fb9ffd7 100644 --- a/sdk/javascript/rate-limits.mdx +++ b/sdk/javascript/rate-limits.mdx @@ -1,34 +1,119 @@ --- title: "Rate Limits" +description: "Understand CometChat REST API rate limits, response headers, and how to handle rate-limited requests in your JavaScript application." --- + +- Core Operations (login, create/delete user, create/join group): `10,000` requests/min cumulative +- Standard Operations (all other): `20,000` requests/min cumulative +- Rate-limited responses return HTTP `429` with `Retry-After` and `X-Rate-Limit-Reset` headers +- Monitor usage via `X-Rate-Limit` and `X-Rate-Limit-Remaining` response headers + -### CometChat REST API Rate Limits +CometChat applies rate limits to REST API requests to ensure fair usage and platform stability. SDK methods that call the REST API under the hood (like fetching users, sending messages, or creating groups) are subject to these limits. Understanding them helps you build applications that handle high traffic gracefully. - +## Rate Limit Tiers -The rate limits below are for general applications. Rate limits can be adjusted on a per need basis, depending on your use-case and plan. The rate limits are cumulative. For example: If the rate limit for core operations is 100 requests per minute, then you can either login a user, add user to a group, remove a user from a group, etc for total 100 requests per minute. +| Operation Type | Limit | Examples | +| --- | --- | --- | +| Core Operations | 10,000 requests/min | Login, create/delete user, create/join group | +| Standard Operations | 20,000 requests/min | All other operations | + +Rate limits are cumulative within each tier. For example, if you make 5,000 login requests and 5,000 create user requests in one minute, you've hit the 10,000 core operations limit. -1. **Core Operations:** Core operations are rate limited to `10,000` requests per minute. Core operations include user login, create/delete user, create/join group cumulatively. -2. **Standard Operations:** Standard operations are rate limited to `20,000` requests per minute. Standard operations include all other operations cumulatively. - -## What happens when rate limit is reached ? - -The request isn't processed and a response is sent containing a 429 response code. Along with the response code there will be couple of headers sent which specifies the time in seconds that you must wait before you can try request again. - -`Retry-After: 15` +## Response Headers + +CometChat includes rate limit information in response headers: + +| Header | Description | +| --- | --- | +| `X-Rate-Limit` | Your current rate limit | +| `X-Rate-Limit-Remaining` | Requests remaining in current window | +| `Retry-After` | Seconds to wait before retrying (on 429) | +| `X-Rate-Limit-Reset` | Unix timestamp when limit resets (on 429) | + +## Handling Rate Limits + +When you exceed the rate limit, CometChat returns HTTP `429 Too Many Requests`. Implement exponential backoff to handle this gracefully: + + + +```typescript +async function callWithRetry( + apiCall: () => Promise, + maxRetries: number = 3 +): Promise { + for (let attempt = 0; attempt < maxRetries; attempt++) { + try { + return await apiCall(); + } catch (error: any) { + if (error.code === "TOO_MANY_REQUEST" && attempt < maxRetries - 1) { + const waitTime = Math.pow(2, attempt) * 1000; + console.log(`Rate limited. Retrying in ${waitTime / 1000}s...`); + await new Promise((resolve) => setTimeout(resolve, waitTime)); + } else { + throw error; + } + } + } + throw new Error("Max retries exceeded"); +} + +// Usage +const users: CometChat.User[] = await callWithRetry(() => + new CometChat.UsersRequestBuilder().setLimit(30).build().fetchNext() +); +``` + + +```javascript +async function callWithRetry(apiCall, maxRetries = 3) { + for (let attempt = 0; attempt < maxRetries; attempt++) { + try { + return await apiCall(); + } catch (error) { + if (error.code === "TOO_MANY_REQUEST" && attempt < maxRetries - 1) { + const waitTime = Math.pow(2, attempt) * 1000; // 1s, 2s, 4s + console.log(`Rate limited. Retrying in ${waitTime / 1000}s...`); + await new Promise((resolve) => setTimeout(resolve, waitTime)); + } else { + throw error; + } + } + } +} + +// Usage +const users = await callWithRetry(() => + new CometChat.UsersRequestBuilder().setLimit(30).build().fetchNext() +); +``` + + + +## Tips for Staying Within Limits + +- **Batch operations** — Space out bulk operations over time instead of firing all at once +- **Monitor headers** — Check `X-Rate-Limit-Remaining` to proactively slow down before hitting limits +- **Avoid frequent login/logout** — Core operations share a lower limit; minimize login cycles +- **Use pagination** — Fetch data in reasonable page sizes (30-50 items) rather than requesting everything at once -`X-Rate-Limit-Reset: 1625143246` - -## Is there any endpoint that returns rate limit of all resources ? - -No, we don't provide a rate-limit endpoint. + +Rate limits can be adjusted based on your use case and plan. Contact CometChat support if you need higher limits. + -However, we do provide the following response headers that you can use to confirm the app's current rate limit and monitor the number of requests remaining in the current minute: +--- -`X-Rate-Limit: 700` +## Next Steps -`X-Rate-Limit-Remaining: 699` + + + Install and configure the CometChat JavaScript SDK + + + Learn the core concepts behind CometChat + + diff --git a/sdk/javascript/reactions.mdx b/sdk/javascript/reactions.mdx index 8ac8f43fd..40933f8c9 100644 --- a/sdk/javascript/reactions.mdx +++ b/sdk/javascript/reactions.mdx @@ -1,31 +1,42 @@ --- title: "Reactions" +sidebarTitle: "Reactions" +description: "Add, remove, and fetch message reactions in real-time using the CometChat JavaScript SDK. Includes listener events and helper methods for updating UI." --- + +```javascript +let messageId = "MESSAGE_ID"; -Enhance user engagement in your chat application with message reactions. Users can express their emotions using reactions to messages. This feature allows users to add or remove reactions, and to fetch all reactions on a message. You can also listen to reaction events in real-time. Let's see how to work with reactions in CometChat's SDK. +// Add a reaction +await CometChat.addReaction(messageId, "😊"); -## Add a Reaction - -Users can add a reaction to a message by calling `addReaction` with the message ID and the reaction emoji. +// Remove a reaction +await CometChat.removeReaction(messageId, "😊"); - - -```js -let messageId = "1"; -let emoji = "😊"; +// Fetch reactions for a message +const request = new CometChat.ReactionRequestBuilder() + .setMessageId(messageId).setLimit(10).build(); +const reactions = await request.fetchNext(); -CometChat.addReaction(messageId, emoji) -.then((res) => { - console.log('response', res); -}).catch(err => { - console.log('err', err); -}) +// Listen for reaction events +CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({ + onMessageReactionAdded: (reaction) => {}, + onMessageReactionRemoved: (reaction) => {} +})); ``` + -
+Reactions let users respond to messages with emoji. You can add or remove reactions, fetch all reactions on a message, listen for reaction events in real time, and update your UI when reactions change. + +Reactions work on text messages, media messages, and custom messages. + +## Add a Reaction + +Call `addReaction()` with a message ID and an emoji string. + ```typescript let messageId:string = "1"; @@ -38,37 +49,28 @@ CometChat.addReaction(messageId, emoji) console.log('err', err); }) ``` - - - - - -You can react on text message, media message and custom message - - - -## Remove a Reaction - -Removing a reaction from a message can be done using the `removeReaction` method. - - -```js +```javascript let messageId = "1"; let emoji = "😊"; -CometChat.removeReaction(messageId, emoji) +CometChat.addReaction(messageId, emoji) .then((res) => { console.log('response', res); }).catch(err => { console.log('err', err); -}) +}) ``` - + + +## Remove a Reaction +Call `removeReaction()` with the same message ID and emoji. + + ```typescript let messageId:string = "1"; @@ -81,49 +83,84 @@ CometChat.removeReaction(messageId, emoji) console.log('err', err); }) ``` - - + +```javascript +let messageId = "1"; +let emoji = "😊"; -## Fetch Reactions for a Message +CometChat.removeReaction(messageId, emoji) +.then((res) => { + console.log('response', res); +}).catch(err => { + console.log('err', err); +}) +``` + +
-To get all reactions for a specific message, first create a `ReactionRequest` using `ReactionRequestBuilder`. You can specify the number of reactions to fetch with setLimit with max limit 100. For this, you will require the ID of the message. This ID needs to be passed to the `setMessageId()` method of the builder class. The `setReaction()` will allow you to fetch details for specific reaction or emoji. +Both `addReaction()` and `removeReaction()` return a [`BaseMessage`](/sdk/reference/messages#basemessage) object with the updated reactions. -| Setting | Description | -| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `setMessageId(value)` | Specifies the unique identifier of the message for which you want to fetch reactions. This parameter is mandatory as it tells the SDK which message's reactions are being requested. | -| `setReaction(value)` | Filters the reactions fetched by the specified reaction type (e.g., "😊", "😂", "👍"). When set, this method will cause the ReactionRequest to only retrieve details of the provided reaction for the given message. | +## Read Reaction Data from a Message -*** +Any [`BaseMessage`](/sdk/reference/messages#basemessage) exposes reaction data through two methods: -## Fetch Next +### Get All Reactions -The `fetchNext()` method fetches the next set of reactions for the message. +Use `getReactions()` to retrieve the list of reactions on a message. Returns an array of [`ReactionCount`](/sdk/reference/auxiliary#reactioncount) objects, or an empty array if no one has reacted. + +```typescript +message.getReactions() +``` + + -```js -let limit = 10; -let messageId = 1; +```javascript +message.getReactions() +``` + + -let reactionRequest = new CometChat.ReactionRequestBuilder() -.setMessageId(messageId) -.setLimit(limit) -.build(); +### Check if the Logged-in User Has Reacted -reactionRequest.fetchNext().then( - messages => { - console.log("list fetched:", messages); - }, - error => {a - console.log('list fetching failed with error:', error); - }, - ); +Call `getReactedByMe()` on any [`ReactionCount`](/sdk/reference/auxiliary#reactioncount) object to check whether the logged-in user has reacted with that particular emoji. Returns `true` if they have, `false` otherwise. + + + +```typescript +let reactions = message.getReactions(); +reactions.forEach((reaction) => { +reaction.getReactedByMe(); //Return true is logged-in user reacted on that message, otherwise false +}) ``` + + +```javascript +let reactions = message.getReactions(); +reactions.forEach((reaction) => { +reaction.getReactedByMe(); //Return true is logged-in user reacted on that message, otherwise false +}) +``` + + +## Fetch Reactions for a Message + +To get the full list of who reacted with what on a specific message, use `ReactionRequestBuilder`. You can paginate through results with `fetchNext()` and `fetchPrevious()` (max 100 per request). + +| Builder Method | Description | +| --- | --- | +| `setMessageId(value)` | The message ID to fetch reactions for. Required. | +| `setReaction(value)` | Filter to a specific emoji (e.g., `"😊"`). When set, only reactions matching this emoji are returned. | +| `setLimit(value)` | Number of reactions to fetch per request. Max 100. | + +### Fetch Next + ```typescript let limit:number = 10; @@ -135,24 +172,25 @@ let reactionRequest = new CometChat.ReactionRequestBuilder() .build(); reactionRequest.fetchNext().then( - (messages: MessageReaction[]) => { - console.log("list fetched:", messages); + (reactions: CometChat.Reaction[]) => { + console.log("list fetched:", reactions); }, (error: CometChat.CometChatException) => { console.log('list fetching failed with error:', error); }, ); ``` - +The `fetchNext()` method returns an array of [`Reaction`](/sdk/reference/auxiliary#reaction) objects representing individual user reactions on the message. + Fetch Previous The `fetchPrevious()` method fetches the previous set of reactions for the message. -```js +```javascript let limit = 10; let messageId = 1; @@ -161,18 +199,21 @@ let reactionRequest = new CometChat.ReactionRequestBuilder() .setLimit(limit) .build(); -reactionRequest.fetchPrevious().then( +reactionRequest.fetchNext().then( messages => { console.log("list fetched:", messages); }, - error => {a + error => { console.log('list fetching failed with error:', error); }, - ); + ); ``` - + +### Fetch Previous + + ```typescript let limit:number = 10; @@ -184,90 +225,107 @@ let reactionRequest = new CometChat.ReactionRequestBuilder() .build(); reactionRequest.fetchPrevious().then( - (messages: MessageReaction[]) => { - console.log("list fetched:", messages); + (reactions: CometChat.Reaction[]) => { + console.log("list fetched:", reactions); }, (error: CometChat.CometChatException) => { console.log('list fetching failed with error:', error); }, ); ``` - + +```javascript +let limit = 10; +let messageId = 1; + +let reactionRequest = new CometChat.ReactionRequestBuilder() +.setMessageId(messageId) +.setLimit(limit) +.build(); + +reactionRequest.fetchPrevious().then( + messages => { + console.log("list fetched:", messages); + }, + error => { + console.log('list fetching failed with error:', error); + }, + ); +``` + -## Real-time Reaction Events +## Real-Time Reaction Events -Keep the chat interactive with real-time updates for reactions. Register a listener for these events and make your UI responsive. +Register a `MessageListener` to receive reaction events as they happen. This is how you keep your UI in sync when other users add or remove reactions. - -```js -let listenerID = "UNIQUE_LISTENER_ID"; + +```typescript +let listenerID:string = "UNIQUE_LISTENER_ID"; -CometChat.addMessageListener(listenerID, { - onMessageReactionAdded:(message) => { +CometChat.addMessageListener(listenerID, new CometChat.MessageListener({ + onMessageReactionAdded:(message: Object) => { console.log("Reaction added", message); }, - onMessageReactionRemoved:(message) => { + onMessageReactionRemoved:(message: Object) => { console.log("Reaction removed", message); } - }) + })) ``` - - -```typescript -let listenerID:string = "UNIQUE_LISTENER_ID"; + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; -CometChat.addMessageListener(listenerID, { - onMessageReactionAdded:(message: Object) => { +CometChat.addMessageListener(listenerID, new CometChat.MessageListener({ + onMessageReactionAdded:(message) => { console.log("Reaction added", message); }, - onMessageReactionRemoved:(message: Object) => { + onMessageReactionRemoved:(message) => { console.log("Reaction removed", message); } - }) + })) ``` - - -## Removing a Reaction Listener +Each reaction listener callback receives a [`ReactionEvent`](/sdk/reference/auxiliary#reactionevent) object. -To stop listening for reaction events, remove the listener as follows: +### Remove the Listener - -```js -let listenerID = "UNIQUE_LISTENER_ID"; + +```typescript +let listenerID:string = "UNIQUE_LISTENER_ID"; CometChat.removeMessageListener(listenerID); ``` - - -```typescript -let listenerID:string = "UNIQUE_LISTENER_ID"; + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; -CometChat.removeMessageReactionListener(listenerID); +CometChat.removeMessageListener(listenerID); ``` - - -## Get Reactions List + +Always remove listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + -To retrieve the list of reactions reacted on particular message, you can use the `message.getReactions()` method. This method will return an array containing the reactions, or an empty array if no one reacted on the message. +## Update a Message with Reaction Info + +When you receive a real-time reaction event, you'll want to update the corresponding message object so your UI reflects the change. `updateMessageWithReactionInfo()` does this — it takes the [`BaseMessage`](/sdk/reference/messages#basemessage) instance, the [`MessageReaction`](/sdk/reference/auxiliary#messagereaction) event data, and the action type, then returns the updated message. -```js +```javascript message.getReactions() ``` @@ -282,13 +340,15 @@ message.getReactions() +The `getReactions()` method returns an array of [`ReactionCount`](/sdk/reference/auxiliary#reactioncount) objects, each containing the reaction emoji, the total count of users who reacted with it, and whether the logged-in user used that reaction. + ## Check if Logged-in User has Reacted on Message To check if the logged-in user has reacted on a particular message or not, You can use the `getReactedByMe()` method on any `ReactionCount` object instance. This method will return a boolean value, true if the logged-in user has reacted on that message, otherwise false. -```js +```javascript let reactions = message.getReactions(); reactions.forEach((reaction) => { reaction.getReactedByMe(); //Return true is logged-in user reacted on that message, otherwise false @@ -309,6 +369,8 @@ reaction.getReactedByMe(); //Return true is logged-in user reacted on that messa +The `getReactedByMe()` method on a [`ReactionCount`](/sdk/reference/auxiliary#reactioncount) object returns a `boolean` — `true` if the logged-in user has reacted with that specific emoji, `false` otherwise. + ## Update Message With Reaction Info When a user adds or removes a reaction, you will receive a real-time event. Once you receive the real time event you would want to update the message with the latest reaction information. To do so you can use the `updateMessageWithReactionInfo()` method. @@ -319,7 +381,7 @@ When you receive a real-time reaction event (`MessageReaction`), call the `updat -```js +```javascript // The message to which the reaction is related let message = ...; @@ -340,24 +402,62 @@ action ```typescript -// The message to which the reaction is related -let message: CometChat.BaseMessage = ...; +// The message to which the reaction is related (obtained from fetchPrevious/fetchNext or a listener) +let message!: CometChat.BaseMessage; -// The reaction event data received in real-time -let messageReaction: CometChat.MessageReaction = ...; +// The reaction event data received in real-time (obtained from onMessageReactionAdded/onMessageReactionRemoved) +let messageReaction!: CometChat.MessageReaction; -// The recieved reaction event real-time action type. Can be CometChatConstants.REACTION_ADDED or CometChatConstants.REACTION_REMOVED +// The received reaction event real-time action type. Can be CometChatConstants.REACTION_ADDED or CometChatConstants.REACTION_REMOVED let action: CometChat.REACTION_ACTION = CometChat.REACTION_ACTION.REACTION_ADDED; let modifiedBaseMessage = CometChat.CometChatHelper.updateMessageWithReactionInfo( -baseMessage, -messageReaction, +message, +messageReaction, action ); ``` - + +```javascript +// The message to which the reaction is related +let message = ...; + +// The reaction event data received in real-time +let messageReaction = ...; + +// The recieved reaction event real-time action type. Can be CometChatConstants.REACTION_ADDED or CometChatConstants.REACTION_REMOVED +let action = CometChat.REACTION_ACTION.REACTION_ADDED; + +let modifiedBaseMessage = CometChat.CometChatHelper.updateMessageWithReactionInfo( +message, +messageReaction, +action +); +``` + -After calling this method, the message instance's reactions are updated. You can then use message.getReactions() to get the latest reactions and refresh your UI accordingly. +On success, this method returns a [`BaseMessage`](/sdk/reference/messages#basemessage) object + +After calling this method, use modifiedBaseMessage.getReactions() to get the latest reactions and refresh your UI. + +| Parameter | Type | Description | +|-----------|------|-------------| +| `message` | [`BaseMessage`](/sdk/reference/messages#basemessage) | The message object to update | +| `messageReaction` | [`MessageReaction`](/sdk/reference/auxiliary#messagereaction) | The reaction event received from the listener | +| `action` | `REACTION_ACTION` | `CometChat.REACTION_ACTION.REACTION_ADDED` or `CometChat.REACTION_ACTION.REACTION_REMOVED` | + +--- + +## Next Steps + + + + Send text, media, and custom messages to users and groups + + + Listen for incoming messages in real-time and fetch missed messages + + diff --git a/sdk/javascript/receive-message.mdx b/sdk/javascript/receive-message.mdx index a6f9a8925..b2d307535 100644 --- a/sdk/javascript/receive-message.mdx +++ b/sdk/javascript/receive-message.mdx @@ -1,150 +1,112 @@ --- -title: "Receive A Message" +title: "Receive Messages" +sidebarTitle: "Receive Messages" +description: "Receive real-time messages, fetch missed and unread messages, retrieve message history, search messages, and get unread counts using the CometChat JavaScript SDK." --- + +| Field | Value | +| --- | --- | +| Key Classes | `CometChat.MessageListener`, `CometChat.MessagesRequestBuilder` | +| Key Methods | `addMessageListener()`, `fetchPrevious()`, `fetchNext()`, `getUnreadMessageCount()` | +| Listener Events | `onTextMessageReceived`, `onMediaMessageReceived`, `onCustomMessageReceived` | +| Prerequisites | SDK initialized, user logged in | + + Receiving messages with CometChat has two parts: -1. Adding a listener to receive [real-time messages](/sdk/javascript/receive-message#real-time-messages) when your app is running -2. Calling a method to retrieve [missed messages](/sdk/javascript/receive-message#missed-messages) when your app was not running +1. Adding a [real-time listener](#real-time-messages) to receive messages while your app is running +2. Fetching [missed](#missed-messages), [unread](#unread-messages), or [historical](#message-history) messages when your app starts up or the user scrolls back -## Real-Time Messages + +The TypeScript and JavaScript APIs are identical — the only difference is type annotations (e.g., `: string`, `: CometChat.BaseMessage[]`). The Real-Time Messages section shows both for reference. The remaining sections use TypeScript only to keep things concise — just drop the type annotations for plain JavaScript. + -*In other words, as a recipient, how do I receive messages when my app is running?* +## Real-Time Messages -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. +Register a `MessageListener` to receive incoming messages as they arrive. Each callback receives the specific message subclass — [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), or [`CustomMessage`](/sdk/reference/messages#custommessage). - -```javascript -let listenerID = "UNIQUE_LISTENER_ID"; + +```typescript +let listenerID: string = "UNIQUE_LISTENER_ID"; CometChat.addMessageListener( listenerID, new CometChat.MessageListener({ - onTextMessageReceived: (textMessage) => { + onTextMessageReceived: (textMessage: CometChat.TextMessage) => { console.log("Text message received successfully", textMessage); }, - onMediaMessageReceived: (mediaMessage) => { + onMediaMessageReceived: (mediaMessage: CometChat.MediaMessage) => { console.log("Media message received successfully", mediaMessage); }, - onCustomMessageReceived: (customMessage) => { + onCustomMessageReceived: (customMessage: CometChat.CustomMessage) => { console.log("Custom message received successfully", customMessage); }, }) ); ``` - - -```typescript -let listenerID: string = "UNIQUE_LISTENER_ID"; + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; CometChat.addMessageListener( listenerID, new CometChat.MessageListener({ - onTextMessageReceived: (textMessage: CometChat.TextMessage) => { + onTextMessageReceived: (textMessage) => { console.log("Text message received successfully", textMessage); }, - onMediaMessageReceived: (mediaMessage: CometChat.MediaMessage) => { + onMediaMessageReceived: (mediaMessage) => { console.log("Media message received successfully", mediaMessage); }, - onCustomMessageReceived: (customMessage: CometChat.CustomMessage) => { + onCustomMessageReceived: (customMessage) => { console.log("Custom message received successfully", customMessage); }, }) ); ``` - - -| Parameter | Description | -| -------------- | --------------------------------------------- | -| **listenerId** | An ID that uniquely identifies that listener. | +| Parameter | Description | +| --- | --- | +| `listenerID` | An ID that uniquely identifies that listener. Use the same ID to remove it later. | -We recommend you remove the listener once you don't want to receive a message for particular listener. +Remove the listener when you no longer need it: - - ```javascript -let listenerID = "UNIQUE_LISTENER_ID"; - -CometChat.removeMessageListener(listenerID); +CometChat.removeMessageListener("UNIQUE_LISTENER_ID"); ``` - - - -```typescript -let listenerID: string = "UNIQUE_LISTENER_ID"; - -CometChat.removeMessageListener(listenerID); -``` - - - - - +Always remove listeners when they're no longer needed (e.g., on component unmount). Failing to do so can cause memory leaks and duplicate event handling. + -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. - + +As a sender, you will not receive your own message in a real-time event. However, if a user is logged in on multiple devices, they will receive the event on the 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 were 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. - -### Fetch Missed Messages of a particular one-on-one conversation +Fetch messages that arrived while your app was offline. Use `getLastDeliveredMessageId()` to find where you left off, then call `fetchNext()` to get everything after that point. Call `fetchNext()` repeatedly on the same request object to paginate. - -```javascript -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); - } -); -``` - - - - + ```typescript -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(); +let UID: string = "UID"; +let limit: number = 30; +let latestId: number = await CometChat.getLastDeliveredMessageId(); + +let messagesRequest: CometChat.MessagesRequest = + new CometChat.MessagesRequestBuilder() + .setUID(UID) + .setMessageId(latestId) + .setLimit(limit) + .build(); messagesRequest.fetchNext().then( (messages: CometChat.BaseMessage[]) => { @@ -155,49 +117,20 @@ messagesRequest.fetchNext().then( } ); ``` - - - -### Fetch Missed Messages of a particular group conversation - - - -```javascript -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); - } -); -``` - - - - + ```typescript -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(); +let GUID: string = "GUID"; +let limit: number = 30; +let latestId: number = await CometChat.getLastDeliveredMessageId(); + +let messagesRequest: CometChat.MessagesRequest = + new CometChat.MessagesRequestBuilder() + .setGUID(GUID) + .setMessageId(latestId) + .setLimit(limit) + .build(); messagesRequest.fetchNext().then( (messages: CometChat.BaseMessage[]) => { @@ -208,52 +141,27 @@ messagesRequest.fetchNext().then( } ); ``` - - -## Unread Messages - -*In other words, as a logged-in user, how do I fetch the messages I've not read?* +The `fetchNext()` method returns an array of [`BaseMessage`](/sdk/reference/messages#basemessage) objects (which may be [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), or other subclasses). -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. +## Unread Messages -### Fetch Unread Messages of a particular one-on-one conversation +Fetch unread messages by adding `setUnread(true)` to the builder. Use `fetchPrevious()` to retrieve them. - -```javascript -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); - } -); -``` - - - - + ```typescript -let UID: string = "UID", - limit: number = 30, - messagesRequest: CometChat.MessagesRequest = - new CometChat.MessagesRequestBuilder() - .setUID(UID) - .setUnread(true) - .setLimit(limit) - .build(); +let UID: string = "UID"; +let limit: number = 30; + +let messagesRequest: CometChat.MessagesRequest = + new CometChat.MessagesRequestBuilder() + .setUID(UID) + .setUnread(true) + .setLimit(limit) + .build(); messagesRequest.fetchPrevious().then( (messages: CometChat.BaseMessage[]) => { @@ -264,46 +172,19 @@ messagesRequest.fetchPrevious().then( } ); ``` - - - - - -### Fetch Unread Messages of a particular group conversation - - - -```javascript -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); - } -); -``` - - + ```typescript -let GUID: string = "GUID", - limit: number = 30, - messagesRequest: CometChat.MessagesRequest = - new CometChat.MessagesRequestBuilder() - .setGUID(GUID) - .setUnread(true) - .setLimit(limit) - .build(); +let GUID: string = "GUID"; +let limit: number = 30; + +let messagesRequest: CometChat.MessagesRequest = + new CometChat.MessagesRequestBuilder() + .setGUID(GUID) + .setUnread(true) + .setLimit(limit) + .build(); messagesRequest.fetchPrevious().then( (messages: CometChat.BaseMessage[]) => { @@ -314,54 +195,28 @@ messagesRequest.fetchPrevious().then( } ); ``` - - -Base Message - -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. - +Results are returned as [`BaseMessage`](/sdk/reference/messages#basemessage) objects, which may be instances of [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), [`CustomMessage`](/sdk/reference/messages#custommessage), `Action`, or `Call`. Use the `instanceof` operator to check the type. ## Message History -*In other words, as a logged-in user, how do I fetch the complete message history?* - -### Fetch Message History of 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. +Fetch the full conversation history using `fetchPrevious()`. Call it repeatedly on the same request object to paginate — useful for implementing upward scrolling. - -```javascript -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); - } -); -``` - - - - + ```typescript -let UID: string = "UID", - limit: number = 30, - messagesRequest: CometChat.MessagesRequest = - new CometChat.MessagesRequestBuilder().setUID(UID).setLimit(limit).build(); +let UID: string = "UID"; +let limit: number = 30; + +let messagesRequest: CometChat.MessagesRequest = + new CometChat.MessagesRequestBuilder() + .setUID(UID) + .setLimit(limit) + .build(); messagesRequest.fetchPrevious().then( (messages: CometChat.BaseMessage[]) => { @@ -372,48 +227,18 @@ messagesRequest.fetchPrevious().then( } ); ``` - - - - - -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. - -### Fetch Message History of a particular group 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 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. - - - -```javascript -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); - } -); -``` - - + ```typescript -let GUID: string = "GUID", - limit: number = 30, - messagesRequest: CometChat.MessagesRequest = - new CometChat.MessagesRequestBuilder() - .setGUID(GUID) - .setLimit(limit) - .build(); +let GUID: string = "GUID"; +let limit: number = 30; + +let messagesRequest: CometChat.MessagesRequest = + new CometChat.MessagesRequestBuilder() + .setGUID(GUID) + .setLimit(limit) + .build(); messagesRequest.fetchPrevious().then( (messages: CometChat.BaseMessage[]) => { @@ -424,71 +249,28 @@ messagesRequest.fetchPrevious().then( } ); ``` - - -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. +The `fetchPrevious()` method returns an array of [`BaseMessage`](/sdk/reference/messages#basemessage) objects (which may be [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), or other subclasses). ## Search Messages -In other words, as a logged-in user, how do I search a message? - -In order to do this, you can use the `setSearchKeyword()` method. This method accepts string as input. When set, the SDK will fetch messages which match the `searchKeyword`. - - - -By default, the searchKey is searched only in message text. However, if you enable `Conversation & Advanced Search`, the searchKey will be searched in message text, file name, mentions & mime type of the file. - -The `Conversation & Advanced Search` is only available in `Advanced` & `Custom` [plans](https://www.cometchat.com/pricing). If you're already on one of these plans, please enable the `Conversation & Advanced Search` from [CometChat Dashboard](https://app.cometchat.com) (Open your app, navigate to Chats -> Settings -> General Configuration) - - - -| Feature | Basic Search | Advance Search | -| ---------------- | ------------ | -------------- | -| Text search | ✅ | ✅ | -| File name search | ❌ | ✅ | -| Mentions search | ❌ | ✅ | -| Mime type search | ❌ | ✅ | - -### Search Messages in a particular one-on-one conversation +Add `setSearchKeyword()` to the builder to filter messages by keyword. - -```javascript -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); - } -); -``` - - - - + ```typescript -let UID: string = "UID", - limit: number = 30, - searchKeyword: string = "Hello", - messagesRequest: CometChat.MessagesRequest = - new CometChat.MessagesRequestBuilder() - .setUID(UID) - .setLimit(limit) - .setSearchKeyword(searchKeyword) - .build(); +let UID: string = "UID"; +let limit: number = 30; +let searchKeyword: string = "Hello"; + +let messagesRequest: CometChat.MessagesRequest = + new CometChat.MessagesRequestBuilder() + .setUID(UID) + .setLimit(limit) + .setSearchKeyword(searchKeyword) + .build(); messagesRequest.fetchPrevious().then( (messages: CometChat.BaseMessage[]) => { @@ -499,48 +281,20 @@ messagesRequest.fetchPrevious().then( } ); ``` - - - - - -### Search Messages in a particular group conversation - - - -```javascript -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); - } -); -``` - - + ```typescript -let GUID: string = "GUID", - limit: number = 30, - searchKeyword: string = "Hello", - messagesRequest: CometChat.MessagesRequest = - new CometChat.MessagesRequestBuilder() - .setGUID(GUID) - .setLimit(limit) - .setSearchKeyword(searchKeyword) - .build(); +let GUID: string = "GUID"; +let limit: number = 30; +let searchKeyword: string = "Hello"; + +let messagesRequest: CometChat.MessagesRequest = + new CometChat.MessagesRequestBuilder() + .setGUID(GUID) + .setLimit(limit) + .setSearchKeyword(searchKeyword) + .build(); messagesRequest.fetchPrevious().then( (messages: CometChat.BaseMessage[]) => { @@ -551,408 +305,186 @@ messagesRequest.fetchPrevious().then( } ); ``` - - -## Unread Message Count - -*In other words, as a logged-in user, how do I find out the number of unread messages that I have?* - -### Fetch Unread Message Count of a particular one-on-one conversation +### Search Capabilities -*In other words, how do I find out the number of unread messages I have from a particular user?* +By default, search only matches message text. With `Conversation & Advanced Search` enabled, it also matches file names, mentions, and MIME types. -In order to get the unread message count for a particular user, you can use the `getUnreadMessageCountForUser()`. +| Feature | Basic Search | Advanced Search | +| ---------------- | ------------ | --------------- | +| Text search | ✅ | ✅ | +| File name search | ❌ | ✅ | +| Mentions search | ❌ | ✅ | +| MIME type search | ❌ | ✅ | -This method has the below two variants: - - - -```javascript -CometChat.getUnreadMessageCountForUser(UID); -``` + +`Conversation & Advanced Search` is available on `Advanced` and `Custom` [plans](https://www.cometchat.com/pricing). Enable it from the [CometChat Dashboard](https://app.cometchat.com) under Chats → Settings → General Configuration. + - +## Unread Message Count - -```typescript -let UID: string = "UID"; -CometChat.getUnreadMessageCountForUser(UID); -``` +CometChat provides several methods to get unread counts at different scopes. All return a `Promise` that resolves with an object mapping IDs to counts. - +Each method accepts an optional boolean parameter to exclude messages from blocked users. - +| Method | Scope | Returns | +| --- | --- | --- | +| `getUnreadMessageCountForUser(UID)` | Single user conversation | `{ [UID]: count }` | +| `getUnreadMessageCountForGroup(GUID)` | Single group conversation | `{ [GUID]: count }` | +| `getUnreadMessageCountForAllUsers()` | All user conversations | `{ [UID]: count, ... }` | +| `getUnreadMessageCountForAllGroups()` | All group conversations | `{ [GUID]: count, ... }` | +| `getUnreadMessageCount()` | Everything | `{ users: { ... }, groups: { ... } }` | -If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true: +### Single Conversation - -```javascript -CometChat.getUnreadMessageCountForUser(UID, hideMessagesFromBlockedUsers); -``` - - - - -```typescript -let UID: string = "UID", - hideMessagesFromBlockedUsers: boolean = true; -CometChat.getUnreadMessageCountForUser(UID, hideMessagesFromBlockedUsers); -``` - - - - - - - -```javascript -let UID = "UID"; - -CometChat.getUnreadMessageCountForUser(UID).then( - (unreadMessageCountObject) => { - console.log("Unread message count fetched", unreadMessageCountObject); - }, - (error) => { - console.log("Error in getting unread message count", error); - } -); -``` - - - ```typescript +// One-on-one let UID: string = "UID"; - CometChat.getUnreadMessageCountForUser(UID).then( - (unreadMessageCount: Object) => { - console.log("Unread message count fetched", unreadMessageCount); - }, - (error: CometChat.CometChatException) => { - console.log("Error in getting unread message count", error); - } + (count: Object) => console.log("Unread count:", count), + (error: CometChat.CometChatException) => console.log("Error:", error) ); -``` - - - - - -It will return an object which will contain the UID as the key and the unread message count as the value. - -### Fetch Unread Message Count of 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: - - - -```javascript -CometChat.getUnreadMessageCountForGroup(GUID); -``` - - - -```typescript +// Group 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: - - - -```javascript -CometChat.getUnreadMessageCountForGroup(GUID, hideMessagesFromBlockedUsers); -``` - - - - -```typescript -let GUID: string = "GUID", - hideMessagesFromBlockedUsers: boolean = true; -CometChat.getUnreadMessageCountForGroup(GUID, hideMessagesFromBlockedUsers); -``` - - - - - - - -```javascript -let GUID = "GUID"; - CometChat.getUnreadMessageCountForGroup(GUID).then( - (unreadMessageCountObject) => { - console.log("Unread message count fetched", unreadMessageCountObject); - }, - (error) => { - console.log("Error in getting unread message count", error); - } + (count: Object) => console.log("Unread count:", count), + (error: CometChat.CometChatException) => console.log("Error:", error) ); ``` - + +```javascript +// One-on-one +let UID = "UID"; +CometChat.getUnreadMessageCountForUser(UID).then( + (count) => console.log("Unread count:", count), + (error) => console.log("Error:", error) +); - -```typescript -let GUID: string = "GUID"; - +// Group +let GUID = "GUID"; CometChat.getUnreadMessageCountForGroup(GUID).then( - (unreadMessageCount: Object) => { - console.log("Unread message count fetched", unreadMessageCount); - }, - (error: CometChat.CometChatException) => { - console.log("Error in getting unread message count", error); - } + (count) => console.log("Unread count:", count), + (error) => console.log("Error:", error) ); ``` - - -It will return an object which will contain the GUID as the key and the unread message count as the value. - -### Fetch Unread Message Count of 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 Conversations - -```javascript -CometChat.getUnreadMessageCount(); -``` - - - ```typescript -CometChat.getUnreadMessageCount(); -``` - - - - - -If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true: - - - -```javascript -CometChat.getUnreadMessageCount(hideMessagesFromBlockedUsers); -``` +// All users and groups combined +CometChat.getUnreadMessageCount().then( + (count: Object) => console.log("Unread count:", count), + (error: CometChat.CometChatException) => console.log("Error:", error) +); - +// All user conversations only +CometChat.getUnreadMessageCountForAllUsers().then( + (count: Object) => console.log("Unread count:", count), + (error: CometChat.CometChatException) => console.log("Error:", error) +); - -```typescript -let hideMessagesFromBlockedUsers: boolean = true; -CometChat.getUnreadMessageCount(hideMessagesFromBlockedUsers); +// All group conversations only +CometChat.getUnreadMessageCountForAllGroups().then( + (count: Object) => console.log("Unread count:", count), + (error: CometChat.CometChatException) => console.log("Error:", error) +); ``` - - - - - - + ```javascript +// All users and groups combined CometChat.getUnreadMessageCount().then( - (unreadMessageCountObject) => { - console.log("Unread message count fetched", unreadMessageCountObject); - }, - (error) => { - console.log("Error in getting unread message count", error); - } + (count) => console.log("Unread count:", count), + (error) => console.log("Error:", error) ); -``` - +// All user conversations only +CometChat.getUnreadMessageCountForAllUsers().then( + (count) => console.log("Unread count:", count), + (error) => console.log("Error:", error) +); - -```typescript -CometChat.getUnreadMessageCount().then( - (unreadMessageCount: Object) => { - console.log("Unread message count fetched", unreadMessageCount); - }, - (error: CometChat.CometChatException) => { - console.log("Error in getting unread message count", error); - } +// All group conversations only +CometChat.getUnreadMessageCountForAllGroups().then( + (count) => console.log("Unread count:", count), + (error) => console.log("Error:", error) ); ``` - - -It returns an object having two keys: - -1. users - The value for this key holds another object that holds the UID as key and their corresponding unread message count as value. -2. groups - The value for this key holds another object that holds the GUID as key and their corresponding unread message count as value. +### Excluding Blocked Users -### Fetch Unread Message Count of all one-on-one conversations +Pass `true` as the last argument to any of the methods above: -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: - - - -```javascript -CometChat.getUnreadMessageCountForAllUsers(); -``` - - - - ```typescript -CometChat.getUnreadMessageCountForAllUsers(); +CometChat.getUnreadMessageCountForUser(UID, true); +CometChat.getUnreadMessageCountForGroup(GUID, true); +CometChat.getUnreadMessageCount(true); +CometChat.getUnreadMessageCountForAllUsers(true); +CometChat.getUnreadMessageCountForAllGroups(true); ``` - - - +## Get Message Details -If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true: +Use `getMessageDetails()` to fetch a specific message by its ID. Returns the full message object ([`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), [`CustomMessage`](/sdk/reference/messages#custommessage), or [`InteractiveMessage`](/sdk/reference/messages#interactivemessage)). - -```javascript -CometChat.getUnreadMessageCountForAllUsers(hideMessagesFromBlockedUsers); -``` - - - ```typescript -let hideMessagesFromBlockedUsers: boolean = true; -CometChat.getUnreadMessageCountForAllUsers(hideMessagesFromBlockedUsers); -``` - - +let messageId: string = "MESSAGE_ID"; - - - - -```javascript -CometChat.getUnreadMessageCountForAllUsers().then( - (unreadMessageCountObject) => { - console.log("Unread message count fetched", unreadMessageCountObject); - }, - (error) => { - console.log("Error in getting unread message count", error); - } -); -``` - - - - -```typescript -CometChat.getUnreadMessageCountForAllUsers().then( - (unreadMessageCount: Object) => { - console.log("Unread message count fetched", unreadMessageCount); +CometChat.getMessageDetails(messageId).then( + (message: CometChat.BaseMessage) => { + console.log("Message details:", message); }, (error: CometChat.CometChatException) => { - console.log("Error in getting unread message count", error); + console.log("Error fetching message details:", error); } ); ``` - - - - - -It returns an object which will contain the UID as the key and the unread message count as the value. - -### Fetch Unread Message Count of 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: - - - -```javascript -CometChat.getUnreadMessageCountForAllGroups(); -``` - - - -```typescript -CometChat.getUnreadMessageCountForAllGroups(); -``` - - - - - -If you wish to ignore the messages from blocked users you can use the below syntax setting the boolean parameter to true: - - - + ```javascript -CometChat.getUnreadMessageCountForAllGroups(hideMessagesFromBlockedUsers); -``` - - +let messageId = "MESSAGE_ID"; - -```typescript -let hideMessagesFromBlockedUsers: boolean = true; -CometChat.getUnreadMessageCountForAllGroups(hideMessagesFromBlockedUsers); -``` - - - - - - - -```javascript -CometChat.getUnreadMessageCountForAllGroups().then( - (unreadMessageCountObject) => { - console.log("Unread message count fetched", unreadMessageCountObject); +CometChat.getMessageDetails(messageId).then( + (message) => { + console.log("Message details:", message); }, (error) => { - console.log("Error in getting unread message count", error); + console.log("Error fetching message details:", error); } ); ``` - + - -```typescript -CometChat.getUnreadMessageCountForAllGroups().then( - (unreadMessageCount: Object) => { - console.log("Unread message count fetched", unreadMessageCount); - }, - (error: CometChat.CometChatException) => { - console.log("Error in getting unread message count", error); - } -); -``` +| Parameter | Type | Description | +|-----------|------|-------------| +| `messageId` | `string` | The ID of the message to fetch | - +Returns a [`BaseMessage`](/sdk/reference/messages#basemessage) object (which may be a [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), [`CustomMessage`](/sdk/reference/messages#custommessage), or [`InteractiveMessage`](/sdk/reference/messages#interactivemessage)). -
+--- + +## Next Steps -It returns an object which will contain the GUID as the key and the unread message count as the value. + + + Track when messages are delivered and read by recipients + + + Show real-time typing status in conversations + + diff --git a/sdk/javascript/recording.mdx b/sdk/javascript/recording.mdx index 214d56bcd..b3270d794 100644 --- a/sdk/javascript/recording.mdx +++ b/sdk/javascript/recording.mdx @@ -1,22 +1,38 @@ --- title: "Recording (Beta)" +sidebarTitle: "Recording" +description: "Implement call recording for voice and video calls using the CometChat JavaScript SDK, including start/stop controls, listeners, and accessing recordings from the Dashboard." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Start recording +CometChatCalls.startRecording(); + +// Stop recording +CometChatCalls.stopRecording(); -This section will guide you to implement call recording feature for the voice and video calls. +// Listen for recording events (in CallSettings) +const callListener = new CometChatCalls.OngoingCallListener({ + onRecordingStarted: (event) => console.log("Recording started", event.user), + onRecordingStopped: () => console.log("Recording stopped"), +}); +``` -## Implementation +**Recordings are available on the [CometChat Dashboard](https://app.cometchat.com) → Calls section.** + -Once you have decided to implement [Default Calling](/sdk/javascript/default-call) or [Direct Calling](/sdk/javascript/direct-call) calling and followed the steps to implement them. Just few additional listeners and methods will help you quickly implement call recording in your app. +Record voice and video calls for playback, compliance, or archival purposes. Recording is built on top of the [Call Session](/sdk/javascript/direct-call) — you add recording listeners to your call settings and optionally control recording programmatically. -You need to make changes in the CometChat.startCall method and add the required listeners for recording. Please make sure your callSettings is configured accordingly for [Default Calling](/sdk/javascript/default-call) or [Direct Calling](/sdk/javascript/direct-call). +## Setup -A basic example of how to make changes to implement recording for a direct call/ a default call: +Add `onRecordingStarted` and `onRecordingStopped` callbacks to your `OngoingCallListener` when building call settings. These fire for all participants when any user starts or stops recording. - -```js + +```typescript // Add listeners onRecordingStarted and onRecordingStopped to the startCall method const defaultLayout = true; const audioOnly = false; @@ -37,7 +53,30 @@ const callSettings = new CometChatCalls.CallSettingsBuilder() const htmlElement = document.getElementById("ELEMENT_ID"); CometChatCalls.startSession(callToken, callSettings, htmlElement); ``` + + + +```javascript +// Add listeners onRecordingStarted and onRecordingStopped to the startCall method +const defaultLayout = true; +const audioOnly = false; + +const callListener = new CometChatCalls.OngoingCallListener({ + onRecordingStarted: (event) => + console.log("Listener => onRecordingStarted", event.user), + onRecordingStopped: () => + console.log("Listener => onRecordingStopped"), +}); + +const callSettings = new CometChatCalls.CallSettingsBuilder() + .enableDefaultLayout(defaultLayout) + .setIsAudioOnlyCall(audioOnly) + .setCallListener(callListener) + .build(); +const htmlElement = document.getElementById("ELEMENT_ID"); +CometChatCalls.startSession(callToken, callSettings, htmlElement); +``` @@ -49,8 +88,8 @@ const audioOnly = false; const callListener = new CometChatCalls.OngoingCallListener({ onRecordingStarted: (event) => console.log("Listener => onRecordingStarted", event.user), - onRecordingStopped: (event) => - console.log("Listener => onRecordingStopped", event.user), + onRecordingStopped: () => + console.log("Listener => onRecordingStopped"), }); const callSettings = new CometChatCalls.CallSettingsBuilder() @@ -67,33 +106,41 @@ CometChatCalls.startSession(callToken, callSettings, htmlElement); -## Settings for call recording +The `onRecordingStarted` callback receives an event object — see the [`OngoingCallListener`](/sdk/javascript/all-real-time-listeners#ongoing-call-listener-calls-sdk) reference for the full shape. + +The `onRecordingStopped` callback receives no arguments. -The `CallSettings`class allows you to customise the overall calling experience. The properties for the call/conference can be set using the `CallSettingsBuilder` class. This will eventually give you and object of the `CallSettings` class which you can pass to the `startSession()` method to start the call. + +Always remove listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + -The options available for recording of calls are: +## Settings for call recording + +## CallSettings Options -| Setting | Description | -| --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `showRecordingButton(showRecordingButton: boolean)` | If set to `true` it displays the Recording button in the button Layout. if set to `false` it hides the Recording button in the button Layout. **Default value = false** | +| Setting | Description | +| ------- | ----------- | +| `showRecordingButton(showRecordingButton: boolean)` | If set to `true` it displays the Recording button in the button Layout. if set to `false` it hides the Recording button in the button Layout. **Default value = false** | | `startRecordingOnCallStart(startRecordingOnCallStart: boolean)` | If set to `true` call recording will start as soon as the call is started. if set to `false` call recording will not start as soon as the call is started. **Default value = false** | -For the use case where you wish to align your own custom buttons and not use the default layout provided by CometChat, you can embed the buttons in your layout and use the below methods to perform the corresponding operations: +## Programmatic Control + +If you're building a custom UI without the default layout, use these methods to control recording during an active call. ### Start Recording You can use the startRecording() method to start call recording. - -```javascript + +```typescript CometChatCalls.startRecording(); ``` - -```typescript + +```javascript CometChatCalls.startRecording(); ``` @@ -106,15 +153,15 @@ CometChatCalls.startRecording(); You can use the stopRecording() method to stop call recording. - -```javascript + +```typescript CometChatCalls.stopRecording(); ``` - -```typescript + +```javascript CometChatCalls.stopRecording(); ``` @@ -122,9 +169,9 @@ CometChatCalls.stopRecording(); -## Downloading Recording +## Downloading Recordings -Currently, the call recordings are available on the [CometChat Dashboard](https://app.cometchat.com/signup) under the Calls Section. Recordings can be accessed after clicking on the three dots menu icon to expand the menu and then select "View Recordings". You can refer to the below screenshot. You can refer to the below screenshot. +Call recordings are available on the [CometChat Dashboard](https://app.cometchat.com) under the Calls section. Click the three-dot menu and select "View Recordings". @@ -137,3 +184,16 @@ Currently, the call recordings are available on the [CometChat Dashboard](https: + +--- + +## Next Steps + + + + Implement ringing call flows with accept/reject functionality + + + Retrieve and display call history for users and groups + + diff --git a/sdk/javascript/resources-overview.mdx b/sdk/javascript/resources-overview.mdx index ea0d211bf..d2610212a 100644 --- a/sdk/javascript/resources-overview.mdx +++ b/sdk/javascript/resources-overview.mdx @@ -1,12 +1,32 @@ --- title: "Resources" sidebarTitle: "Overview" +description: "Access CometChat JavaScript SDK resources including real-time listeners reference, migration guides, and rate limits." --- +{/* TL;DR for Agents and Quick Reference */} + +- [All Real-Time Listeners](/sdk/javascript/all-real-time-listeners) — Complete listener reference +- [Upgrading from v3](/sdk/javascript/upgrading-from-v3) — Migration guide +- [Rate Limits](/sdk/javascript/rate-limits) — API rate limit details + We have a number of resources that will help you while integrating CometChat in your app. You can begin with the [all real-time listeners](/sdk/javascript/all-real-time-listeners) guide. If you're upgrading from v2, we recommend reading our [Upgrading from v3](/sdk/javascript/upgrading-from-v3) guide. + +--- + +## Next Steps + + + + Complete reference for all CometChat event listeners + + + Step-by-step migration guide from SDK v3 to v4 + + \ No newline at end of file diff --git a/sdk/javascript/retrieve-conversations.mdx b/sdk/javascript/retrieve-conversations.mdx index a58ef59d1..5e3dd8997 100644 --- a/sdk/javascript/retrieve-conversations.mdx +++ b/sdk/javascript/retrieve-conversations.mdx @@ -1,10 +1,31 @@ --- title: "Retrieve Conversations" +sidebarTitle: "Retrieve Conversations" +description: "Fetch, filter, tag, and search conversations using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Fetch conversations list +const request = new CometChat.ConversationsRequestBuilder() + .setLimit(30).build(); +const conversations = await request.fetchNext(); + +// Get a specific conversation +const conversation = await CometChat.getConversation("UID", "user"); + +// Tag a conversation +await CometChat.tagConversation("UID", "user", ["archived"]); -Conversations provide the last messages for every one-on-one and group conversation the logged-in user is a part of. This makes it easy for you to build a **Recent Chat** list. +// Convert message to conversation +let message = {}; // obtained from MessageListener or fetchPrevious/fetchNext +const conv = await CometChat.CometChatHelper.getConversationFromMessage(message); +``` + + +Conversations provide the last message for every one-on-one and group conversation the logged-in user is part of. Each [`Conversation`](/sdk/reference/entities#conversation) object includes the other participant (user or group), the last message, unread counts, and optional tags. Use this to build a Recent Chats list. ## Retrieve List of Conversations @@ -12,23 +33,15 @@ Conversations provide the last messages for every one-on-one and group conversat To fetch the list of conversations, you can use the `ConversationsRequest` class. To use this class i.e. to create an object of the `ConversationsRequest` class, you need to use the `ConversationsRequestBuilder` class. The `ConversationsRequestBuilder` class allows you to set the parameters based on which the conversations are to be fetched. -The `ConversationsRequestBuilder` class allows you to set the below parameters: +Fetching using this builder will return [`Conversation`](/sdk/reference/entities#conversation) objects. + +The `ConversationsRequestBuilder` to fetch conversations with various filters. ### Set Limit -This method sets the limit i.e. the number of conversations that should be fetched in a single iteration. +Set the number of conversations to fetch per request. - -```javascript -let limit = 30; -let conversationRequest = new CometChat.ConversationsRequestBuilder() - .setLimit(limit) - .build(); -``` - - - ```typescript let limit: number = 30, @@ -39,30 +52,23 @@ let limit: number = 30, - - -### Set Conversation Type - -This method can be used to fetch user or group conversations specifically. The `conversationType` variable can hold one of the below two values: - -* user - Only fetches user conversation. -* group - Only fetches group conversations. - -If none is set, the list of conversations will include both user and group conversations. - - - + ```javascript let limit = 30; -let conversationType = "group"; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .setConversationType(conversationType) .build(); ``` + + +### Set Conversation Type + +Filter by conversation type: `user` for one-on-one or `group` for group conversations. If not set, both types are returned. + + ```typescript let limit: number = 30, @@ -77,22 +83,48 @@ let limit: number = 30, + +The default value for `setLimit` is 30 and the max value is 50. + + +When conversations are fetched successfully, the response will include an array of [`Conversation`](/sdk/reference/entities#conversation) objects filtered by the specified type. + +Relevant fields to access on returned conversations: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| conversationType | `getConversationType()` | `string` | Type of conversation (`"user"` or `"group"`) | + ### With User and Group Tags This method can be used to fetch the user/group tags in the `Conversation` Object. By default the value is `false`. - + ```javascript let limit = 30; +let conversationType = "group"; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .withUserAndGroupTags(true) + .setConversationType(conversationType) .build(); ``` + + + +Default limit is 30, maximum is 50. + + +When conversations are fetched successfully, the response includes an array of [`Conversation`](/sdk/reference/entities#conversation) objects filtered by the specified type. + +### With User and Group Tags + +Use `withUserAndGroupTags(true)` to include user/group tags in the response. Default is `false`. + + ```typescript let limit: number = 30, @@ -106,23 +138,39 @@ let limit: number = 30, +When conversations are fetched successfully, the response will include `tags` arrays on the `conversationWith` objects (user or group). + +Relevant fields to access on returned conversations: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| conversationWith tags | `getConversationWith().getTags()` | `string[]` | Tags associated with the user or group in the conversation | + ### Set User Tags This method fetches user conversations that have the specified tags. -```js +```javascript let limit = 30; -let userTags = ["tag1"]; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .setUserTags(userTags) - .build(); + .withUserAndGroupTags(true) + .build(); ``` + + +When conversations are fetched successfully, the response includes `tags` arrays on the `conversationWith` objects. + +### Set User Tags + +Fetch user conversations where the user has specific tags. + + ```typescript let limit: number = 30, @@ -137,23 +185,40 @@ let limit: number = 30, +When conversations are fetched successfully, the response will include only user conversations where the user has the specified tags. + +Relevant fields to access on returned conversations: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| conversationWith tags | `getConversationWith().getTags()` | `string[]` | Tags associated with the user in the conversation | + ### Set Group Tags This method fetches group conversations that have the specified tags. -```js +```javascript let limit = 30; -let groupTags = ["tag1"]; +let userTags = ["tag1"]; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .setGroupTags(groupTags) - .build(); + .setUserTags(userTags) + .build(); ``` + + +When conversations are fetched successfully, the response includes only user conversations where the user has the specified tags. + +### Set Group Tags + +Fetch group conversations where the group has specific tags. + + ```typescript let limit: number = 30, @@ -168,22 +233,40 @@ let limit: number = 30, +When conversations are fetched successfully, the response will include only group conversations where the group has the specified tags. + +Relevant fields to access on returned conversations: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| conversationWith tags | `getConversationWith().getTags()` | `string[]` | Tags associated with the group in the conversation | + ### With Tags This method makes sure that the tags associated with the conversations are returned along with the other details of the conversations. The default value for this parameter is `false` - + ```javascript let limit = 30; +let groupTags = ["tag1"]; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .withTags(true) + .setGroupTags(groupTags) .build(); ``` + + +When conversations are fetched successfully, the response includes only group conversations where the group has the specified tags. + +### With Tags + +Use `withTags(true)` to include conversation tags in the response. Default is `false`. + + ```typescript let limit: number = 30, @@ -197,23 +280,34 @@ conversationRequest: CometChat.ConversationsRequest = new CometChat.Conversation +Relevant fields to access on returned conversations: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags associated with the conversation | + ### Set Tags This method helps you fetch the conversations based on the specified tags. - + ```javascript let limit = 30; -let tags = ["archivedChat"]; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .setTags(tags) + .withTags(true) .build(); ``` + +### Set Tags + +Fetch conversations that have specific tags. + + ```typescript let limit: number = 30, @@ -228,22 +322,35 @@ let limit: number = 30, +Relevant fields to access on returned conversations: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags associated with the conversation | + ### Include Blocked Users This method helps you fetch the conversations of users whom the logged-in user has blocked. -```js +```javascript let limit = 30; +let tags = ["archivedChat"]; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .setIncludeBlockedUsers(true) + .setTags(tags) .build(); ``` + +### Include Blocked Users + +Use `setIncludeBlockedUsers(true)` to include conversations with users you've blocked. + + ```typescript let limit: number = 30, @@ -255,24 +362,26 @@ let limit: number = 30, - - -### With Blocked Info - -This method can be used to fetch the blocked information of the blocked user in the `ConversationWith` object. - - -```js +```javascript let limit = 30; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .setWithBlockedInfo(true) + .setIncludeBlockedUsers(true) .build(); ``` + + +When conversations are fetched successfully, the response includes conversations with blocked users. To also get blocked info details (`blockedByMe`, `blockedByMeAt`, `blockedAt`), set `withBlockedInfo` to `true`. + +### With Blocked Info + +Use `setWithBlockedInfo(true)` to include blocked user information in the response. + + ```typescript let limit: number = 30, @@ -284,11 +393,31 @@ let limit: number = 30, + +```javascript +let limit = 30; +let conversationRequest = new CometChat.ConversationsRequestBuilder() + .setLimit(limit) + .setWithBlockedInfo(true) + .build(); +``` + + + +Relevant fields to access on returned conversations: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| blockedByMe | `getConversationWith().getBlockedByMe()` | `boolean` | Whether the logged-in user has blocked this user | +| hasBlockedMe | `getConversationWith().getHasBlockedMe()` | `boolean` | Whether this user has blocked the logged-in user | +| blockedByMeAt | `getConversationWith().blockedByMeAt` | `number` | Timestamp when the logged-in user blocked this user | +| blockedAt | `getConversationWith().blockedAt` | `number` | Timestamp when this user was blocked | + ### Search Conversations -This method helps you search a conversation based on User or Group name. +Use `setSearchKeyword()` to search conversations by user or group name. @@ -297,17 +426,6 @@ This feature is only available with `Conversation & Advanced Search`. The `Conve - -```js -let limit = 30; -let conversationRequest = new CometChat.ConversationsRequestBuilder() - .setLimit(limit) - .setSearchKeyword("Hiking") - .build(); -``` - - - ```typescript let limit: number = 30, @@ -319,11 +437,24 @@ let limit: number = 30, + +```javascript +let limit = 30; +let conversationRequest = new CometChat.ConversationsRequestBuilder() + .setLimit(limit) + .setSearchKeyword("Hiking") + .build(); +``` + + + +When conversations are fetched successfully, the response includes conversations where the user or group name matches the search keyword. + ### Unread Conversations -This method helps you fetch unread conversations. +Use `setUnread(true)` to fetch only conversations with unread messages. @@ -332,17 +463,6 @@ This feature is only available with `Conversation & Advanced Search`. The `Conve - -```js -let limit = 30; -let conversationRequest = new CometChat.ConversationsRequestBuilder() - .setLimit(limit) - .setUnread(true) - .build(); -``` - - - ```typescript let limit: number = 30, @@ -354,24 +474,26 @@ let limit: number = 30, - - -### Hide Agentic Conversations - -This method allows you to exclude agent conversations from the conversation list. When set to `true`, conversations with AI agents will be filtered out. - - -```js +```javascript let limit = 30; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .setHideAgentic(true) + .setUnread(true) .build(); ``` + + +When conversations are fetched successfully, the response includes only conversations with unread messages (`unreadMessageCount` > 0). + +### Hide Agentic Conversations + +Use `setHideAgentic(true)` to exclude AI agent conversations from the list. + + ```typescript let limit: number = 30, @@ -383,24 +505,23 @@ let limit: number = 30, - - -### Only Agentic Conversations - -This method allows you to fetch only agent conversations. When set to `true`, only conversations with AI agents will be returned in the list. - - -```js +```javascript let limit = 30; let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) - .setOnlyAgentic(true) + .setHideAgentic(true) .build(); ``` + +### Only Agentic Conversations + +Use `setOnlyAgentic(true)` to fetch only AI agent conversations. + + ```typescript let limit: number = 30, @@ -420,32 +541,38 @@ The `setHideAgentic()` and `setOnlyAgentic()` methods are mutually exclusive. Yo +When conversations are fetched successfully, the response will include only conversations with AI agents. Agent users have `role: "@agentic"` and include agent-specific metadata. Finally, once all the parameters are set to the builder class, you need to call the `build()` method to get the object of the `ConversationsRequest` class. -Once you have the object of the `ConversationsRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `Conversation` objects containing X number of users depending on the limit set. +Once you have the object of the `ConversationsRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of [`Conversation`](/sdk/reference/entities#conversation) objects containing X number of users depending on the limit set. A Maximum of only 50 Conversations can be fetched at once. - + ```javascript let limit = 30; -let conversationsRequest = new CometChat.ConversationsRequestBuilder() +let conversationRequest = new CometChat.ConversationsRequestBuilder() .setLimit(limit) + .setOnlyAgentic(true) .build(); - -conversationsRequest.fetchNext().then( - (conversationList) => { - console.log("Conversations list received:", conversationList); - }, - (error) => { - console.log("Conversations list fetching failed with error:", error); - } -); ``` + + + +`setHideAgentic()` and `setOnlyAgentic()` are mutually exclusive — use only one per request. + + +When conversations are fetched successfully, the response includes only AI agent conversations. Agent users have `role: "@agentic"`. + +### Fetch Conversations + +After configuring the builder, call `build()` to create the request, then `fetchNext()` to retrieve conversations. Maximum 50 per request. Call `fetchNext()` repeatedly on the same object to paginate. + + ```typescript let limit: number = 30, @@ -465,50 +592,40 @@ conversationsRequest.fetchNext().then( - - -The `Conversation` Object consists of the following fields: - -| Field | Information | -| ------------------ | ----------------------------------------------------------------- | -| conversationId | ID of the conversation. | -| conversationType | Type of conversation. (user/group) | -| lastMessage | Last message the conversation. | -| conversationWith | User or Group object containing the details of the user or group. | -| unreadMessageCount | Unread message count for the conversation. | - -## Tag Conversation - -*In other words, as a logged-in user, how do I tag a conversation?* - -To tag a specific conversation, you can use the `tagConversation()` method. The `tagConversation()` method accepts three parameters. - -1. `conversationWith`: UID/GUID of the user/group whose conversation you want to fetch. - -2. `conversationType`: The `conversationType` variable can hold one of the below two values: - - 1. user - Only fetches user conversation. - 2. group - Only fetches group conversations. - -3. `tags`: The `tags` variable will be a list of tags you want to add to a conversation. - - - + ```javascript -let tags = ["archivedChat"]; +let limit = 30; +let conversationsRequest = new CometChat.ConversationsRequestBuilder() + .setLimit(limit) + .build(); -CometChat.tagConversation("conversationWith", "conversationType", tags).then( - (conversation) => { - console.log("conversation", conversation); +conversationsRequest.fetchNext().then( + (conversationList) => { + console.log("Conversations list received:", conversationList); }, (error) => { - console.log("error while fetching a conversation", error); + console.log("Conversations list fetching failed with error:", error); } ); ``` + + +The `fetchNext()` method returns an array of [`Conversation`](/sdk/reference/entities#conversation) objects. + +## Tag Conversation + +Use `tagConversation()` to add tags to a conversation. + +| Parameter | Description | +| --- | --- | +| `conversationWith` | UID or GUID of the user/group | +| `conversationType` | `user` or `group` | +| `tags` | Array of tags to add | + + ```typescript let conversationWith: string = "UID", @@ -535,6 +652,16 @@ The tags for conversations are one-way. This means that if user A tags a convers +When the conversation is tagged successfully, the response will return a single [`Conversation`](/sdk/reference/entities#conversation) object (not an array) with the `tags` field included. + +The `tagConversation()` method returns a [`Conversation`](/sdk/reference/entities#conversation) object with the `tags` field populated. + +Relevant fields to access on returned conversation: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags applied to the conversation | + ## Retrieve Single Conversation *In other words, as a logged-in user, how do I retrieve a specific conversation?* @@ -549,9 +676,14 @@ To fetch a specific conversation, you can use the `getConversation` method. The 2. group - Only fetches group conversations. - + ```javascript -CometChat.getConversation("conversationWith", "conversationType").then( +let tags = ["archivedChat"]; + +let conversationWith = "UID"; +let conversationType = "user"; + +CometChat.tagConversation(conversationWith, conversationType, tags).then( (conversation) => { console.log("conversation", conversation); }, @@ -563,6 +695,24 @@ CometChat.getConversation("conversationWith", "conversationType").then( + + + +Conversation tags are one-way. If user A tags a conversation with user B, that tag only applies for user A. + + +When the conversation is tagged successfully, the response returns a [`Conversation`](/sdk/reference/entities#conversation) object with the `tags` field. + +## Retrieve Single Conversation + +Use `getConversation()` to fetch a specific conversation. + +| Parameter | Description | +| --- | --- | +| `conversationWith` | UID or GUID of the user/group | +| `conversationType` | `user` or `group` | + + ```typescript let conversationWith: string = "UID", @@ -582,24 +732,41 @@ CometChat.getConversation(conversationWith, conversationType).then( +When the conversation is fetched successfully, the response will return a single [`Conversation`](/sdk/reference/entities#conversation) object (not an array). + +The `getConversation()` method returns a single [`Conversation`](/sdk/reference/entities#conversation) object. + ## Convert Messages to Conversations -As per our [receive messages](/sdk/javascript/receive-message) guide, for real-time messages, you will always receive `Message` objects and not `Conversation` objects. Thus, you will need a mechanism to convert the Message object to the `Conversation` object. You can use the `getConversationFromMessage(BaseMessage message)` of the `CometChatHelper` class. +As per our [receive messages](/sdk/javascript/receive-message) guide, for real-time messages, you will always receive `Message` objects and not [`Conversation`](/sdk/reference/entities#conversation) objects. Thus, you will need a mechanism to convert the Message object to the `Conversation` object. You can use the `getConversationFromMessage(BaseMessage message)` of the `CometChatHelper` class. - + ```javascript -CometChat.CometChatHelper.getConversationFromMessage(message).then( +let conversationWith = "UID"; +let conversationType = "user"; + +CometChat.getConversation(conversationWith, conversationType).then( (conversation) => { - console.log("Conversation Object", conversation); - }, (error) => { - console.log("Error while converting message object", error); + console.log("conversation", conversation); + }, + (error) => { + console.log("error while fetching a conversation", error); } ); ``` + + +When the conversation is fetched successfully, the response returns a single [`Conversation`](/sdk/reference/entities#conversation) object. + +## Convert Messages to Conversations + +Use `CometChatHelper.getConversationFromMessage()` to convert a received message into a [`Conversation`](/sdk/reference/entities#conversation) object. Useful for updating your Recent Chats list when receiving real-time messages. + + ```typescript let message: CometChat.TextMessage | CometChat.MediaMessage | CometChat.CustomMessage; @@ -615,10 +782,36 @@ CometChat.CometChatHelper.getConversationFromMessage(message).then( + +```javascript +CometChat.CometChatHelper.getConversationFromMessage(message).then( + (conversation) => { + console.log("Conversation Object", conversation); + }, (error) => { + console.log("Error while converting message object", error); + } +); +``` + + + +When converting a message to a conversation, `unreadMessageCount` and `tags` won't be available. Manage unread counts in your client-side code. + -While converting the `Message` object to the `Conversation` object, the `unreadMessageCount` & `tags` will not be available in the `Conversation` object. The unread message count needs to be managed in your client-side code. +The `getConversationFromMessage()` method returns a [`Conversation`](/sdk/reference/entities#conversation) object. - +--- + +## Next Steps + + + + Remove conversations from the logged-in user's list + + + Show real-time typing status in conversations + + diff --git a/sdk/javascript/retrieve-group-members.mdx b/sdk/javascript/retrieve-group-members.mdx index 3e21b909d..f591d61f8 100644 --- a/sdk/javascript/retrieve-group-members.mdx +++ b/sdk/javascript/retrieve-group-members.mdx @@ -1,33 +1,40 @@ --- title: "Retrieve Group Members" +sidebarTitle: "Retrieve Members" +description: "Fetch and filter group members by scope, status, and search keyword using the CometChat JavaScript SDK with pagination support." --- + +```javascript +// Fetch group members +const request = new CometChat.GroupMembersRequestBuilder("GUID") + .setLimit(30).build(); +const members = await request.fetchNext(); + +// Filter by scope +const scopeRequest = new CometChat.GroupMembersRequestBuilder("GUID") + .setLimit(30).setScopes(["admin", "moderator"]).build(); + +// Search members +const searchRequest = new CometChat.GroupMembersRequestBuilder("GUID") + .setLimit(30).setSearchKeyword("john").build(); +``` + -## Retrieve the List of Group Members +Fetch the members of a group with filtering by scope, online status, and search keyword. Results are returned as [`GroupMember`](/sdk/reference/entities#groupmember) objects, which extend [`User`](/sdk/reference/entities#user) with group-specific fields like scope. -In order to fetch the list of groups members for a group, you can use the `GroupMembersRequest` class. To use this class i.e to create an object of the GroupMembersRequest class, you need to use the `GroupMembersRequestBuilder` class. The `GroupMembersRequestBuilder` class allows you to set the parameters based on which the groups are to be fetched. +## Retrieve the List of Group Members -The `GroupMembersRequestBuilder` class allows you to set the below parameters: +Use `GroupMembersRequestBuilder` to fetch members of a [Group](/sdk/reference/entities#group). The GUID must be specified in the constructor. -The GUID of the group for which the members are to be fetched must be specified in the constructor of the `GroupMembersRequestBuilder` class. +Fetching using this builder will return [`GroupMember`](/sdk/reference/entities#groupmember) objects. `GroupMember` extends [`User`](/sdk/reference/entities#user) and adds group-specific fields. ### Set Limit -This method sets the limit i.e. the number of members that should be fetched in a single iteration. +Sets the number of members to fetch per request. - -```javascript -let GUID = "GUID"; -let limit = 30; -let groupMembersRequest = new CometChat.GroupMembersRequestBuilder(GUID) - .setLimit(limit) - .build(); -``` - - - ```typescript let GUID: string = "GUID"; @@ -39,26 +46,24 @@ let groupMembersRequest: CometChat.GroupMembersRequest = new CometChat.GroupMemb - - -### Set Search Keyword - -This method allows you to set the search string based on which the group members are to be fetched. - - - + ```javascript let GUID = "GUID"; let limit = 30; -let searchKeyword = "super"; let groupMembersRequest = new CometChat.GroupMembersRequestBuilder(GUID) .setLimit(limit) - .setSearchKeyword(searchKeyword) - .build(); + .build(); ``` + + +### Set Search Keyword + +Filters members by a search string. + + ```typescript let GUID: string = "GUID"; @@ -72,26 +77,26 @@ let groupMembersRequest: CometChat.GroupMembersRequest = new CometChat.GroupMemb - - -### Set Scopes - -This method allows you to fetch group members based on multiple scopes. - - - + ```javascript let GUID = "GUID"; let limit = 30; -let scopes = ["admin", "moderator"]; +let searchKeyword = "super"; let groupMembersRequest = new CometChat.GroupMembersRequestBuilder(GUID) .setLimit(limit) - .setScopes(scopes) + .setSearchKeyword(searchKeyword) .build(); ``` + + +### Set Scopes + +Filters members by one or more scopes (`admin`, `moderator`, `participant`). + + ```typescript let GUID: string = "GUID"; @@ -107,48 +112,73 @@ let groupMembersRequest: CometChat.GroupMembersRequest = new CometChat.GroupMemb +Relevant fields to access on returned members: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| scope | `getScope()` | `string` | Scope of the member in the group (`"admin"`, `"moderator"`, or `"participant"`) | + ### Set Status -The status based on which the group members are to be fetched. The status parameter can contain one of the below two values: +Filters members by online status: -* CometChat.USER_STATUS.ONLINE - will return the list of only online group members. -* CometChat.USER_STATUS.OFFLINE - will return the list of only offline group members. +| Value | Description | +|-------|-------------| +| `CometChat.USER_STATUS.ONLINE` | Only online members | +| `CometChat.USER_STATUS.OFFLINE` | Only offline members | -If this parameter is not set, will return all the group members regardless of their status. +If not set, returns all members regardless of status. - + +```typescript +let GUID: string = "GUID"; +let limit: number = 30; +let groupMembersRequest: CometChat.GroupMembersRequest = new CometChat.GroupMembersRequestBuilder(GUID) + .setLimit(limit) + .setStatus(CometChat.USER_STATUS.ONLINE) + .build(); +``` + + + + ```javascript let GUID = "GUID"; let limit = 30; let groupMembersRequest = new CometChat.GroupMembersRequestBuilder(GUID) - .setLimit(limit) - .setStatus(CometChat.USER_STATUS.ONLINE) - .build(); + .setLimit(limit) + .setStatus(CometChat.USER_STATUS.ONLINE) + .build(); ``` + + +After configuring the builder, call `build()` to create the request, then `fetchNext()` to retrieve members. Call `fetchNext()` repeatedly on the same instance to paginate. + + ```typescript let GUID: string = "GUID"; let limit: number = 30; let groupMembersRequest: CometChat.GroupMembersRequest = new CometChat.GroupMembersRequestBuilder(GUID) .setLimit(limit) - .setStatus(CometChat.USER_STATUS.ONLINE) .build(); + +groupMembersRequest.fetchNext().then( + (groupMembers: CometChat.GroupMember[]) => { + console.log("Group Member list fetched successfully:", groupMembers); + }, (error: CometChat.CometChatException) => { + console.log("Group Member list fetching failed with exception:", error); + } +); ``` - - -Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the `GroupMembersRequest` class. - -Once you have the object of the `GroupMembersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `GroupMember` objects containing n number of members where n is the limit set in the builder class. - - - + ```javascript let GUID = "GUID"; let limit = 30; @@ -167,23 +197,19 @@ groupMembers => { - -```typescript -let GUID: string = "GUID"; -let limit: number = 30; -let groupMembersRequest: CometChat.GroupMembersRequest = new CometChat.GroupMembersRequestBuilder(GUID) - .setLimit(limit) - .build(); + -groupMembersRequest.fetchNext().then( - (groupMembers: CometChat.GroupMember[]) => { - console.log("Group Member list fetched successfully:", groupMembers); - }, (error: CometChat.CometChatException) => { - console.log("Group Member list fetching failed with exception:", error); - } -); -``` +The `fetchNext()` method returns an array of [`GroupMember`](/sdk/reference/entities#groupmember) objects. `GroupMember` extends [`User`](/sdk/reference/entities#user) and adds group-specific fields. - +--- - +## Next Steps + + + + Add users to a group programmatically + + + Remove or ban members from a group + + diff --git a/sdk/javascript/retrieve-groups.mdx b/sdk/javascript/retrieve-groups.mdx index 92a074c0d..aa8bba5f0 100644 --- a/sdk/javascript/retrieve-groups.mdx +++ b/sdk/javascript/retrieve-groups.mdx @@ -1,8 +1,31 @@ --- title: "Retrieve Groups" +sidebarTitle: "Retrieve Groups" +description: "Fetch, filter, and search groups using the CometChat JavaScript SDK. Includes pagination, tag-based filtering, joined-only groups, and online member counts." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Fetch groups list +const request = new CometChat.GroupsRequestBuilder() + .setLimit(30).build(); +const groups = await request.fetchNext(); + +// Get specific group details +const group = await CometChat.getGroup("GUID"); + +// Fetch only joined groups +const joinedRequest = new CometChat.GroupsRequestBuilder() + .setLimit(30).joinedOnly(true).build(); + +// Get online member count +const count = await CometChat.getOnlineGroupMemberCount(["GUID"]); +``` + + +Fetch the list of [`Group`](/sdk/reference/entities#group) objects the logged-in user can see, get details for a specific group, or check online member counts. ## Retrieve List of Groups @@ -10,15 +33,17 @@ title: "Retrieve Groups" In order to fetch the list of groups, you can use the `GroupsRequest` class. To use this class i.e to create an object of the `GroupsRequest` class, you need to use the `GroupsRequestBuilder` class. The `GroupsRequestBuilder` class allows you to set the parameters based on which the groups are to be fetched. -The `GroupsRequestBuilder` class allows you to set the below parameters: +Fetching using this builder will return [`Group`](/sdk/reference/entities#group) objects + +Use `GroupsRequestBuilder` to fetch groups with filtering, searching, and pagination. ### Set Limit -This method sets the limit i.e. the number of groups that should be fetched in a single iteration. +Sets the number of groups to fetch per request. - -```javascript + +```typescript let limit = 30; let groupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) @@ -27,8 +52,8 @@ let groupsRequest = new CometChat.GroupsRequestBuilder() - -```typescript + +```javascript let limit = 30; let groupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) @@ -41,10 +66,22 @@ let groupsRequest = new CometChat.GroupsRequestBuilder() ### Set Search Keyword -This method allows you to set the search string based on which the groups are to be fetched. +Filters groups by a search string. - + +```typescript +let limit: number = 30; +let searchKeyword: string = "group"; +let groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder() + .setLimit(limit) + .setSearchKeyword(searchKeyword) + .build(); +``` + + + + ```javascript let limit = 30; let searchKeyword = "group"; @@ -56,26 +93,25 @@ let groupsRequest = new CometChat.GroupsRequestBuilder() + + +### Joined Only + +When `true`, returns only groups the logged-in user has joined. + + ```typescript let limit: number = 30; -let searchKeyword: string = "group"; let groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) - .setSearchKeyword(searchKeyword) + .joinedOnly(true) .build(); ``` - - -### Joined Only - -This method when used, will ask the SDK to only return the groups that the user has joined or is a part of. - - - + ```javascript let limit = 30; let groupsRequest = new CometChat.GroupsRequestBuilder() @@ -86,25 +122,26 @@ let groupsRequest = new CometChat.GroupsRequestBuilder() + + +### Set Tags + +Filters groups by specified tags. + + ```typescript let limit: number = 30; +let tags: Array = ["tag1", "tag2"]; let groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) - .joinedOnly(true) + .setTags(tags) .build(); ``` - - -### Set Tags - -This method accepts a list of tags based on which the list of groups is to be fetched. The list fetched will only contain the groups that have been tagged with the specified tags. - - - + ```javascript let limit = 30; let tags = ["tag1", "tag2"]; @@ -116,13 +153,19 @@ let groupsRequest = new CometChat.GroupsRequestBuilder() + + +### With Tags + +When `true`, includes tag data in the returned group objects. + + ```typescript let limit: number = 30; -let tags: Array = ["tag1", "tag2"]; let groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) - .setTags(tags) + .withTags(true) .build(); ``` @@ -130,12 +173,20 @@ let groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder( -### With Tags +Relevant fields to access on returned groups: -This property when set to true will fetch tags data along with the list of groups. +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags associated with the group | + +Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the `GroupsRequest` class. + +Once you have the object of the `GroupsRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of [`Group`](/sdk/reference/entities#group) objects containing n number of groups where n is the limit set in the builder class. + +The list of groups fetched will only have the public and password type groups. The private groups will only be available if the user is a member of the group. - + ```javascript let limit = 30; let groupsRequest = new CometChat.GroupsRequestBuilder() @@ -146,27 +197,34 @@ let groupsRequest = new CometChat.GroupsRequestBuilder() + + +After configuring the builder, call `build()` to get the `GroupsRequest` object, then call `fetchNext()` to retrieve groups. + + +The list only includes public and password-protected groups. Private groups appear only if the user is a member. + + + ```typescript let limit: number = 30; let groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder() .setLimit(limit) - .withTags(true) .build(); + +groupsRequest.fetchNext().then( + (groupList: CometChat.Group[]) => { + console.log("Groups list fetched successfully", groupList); + }, (error: CometChat.CometChatException) => { + console.log("Groups list fetching failed with error", error); + } +); ``` - - -Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the `GroupsRequest` class. - -Once you have the object of the `GroupsRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `Group` objects containing n number of groups where n is the limit set in the builder class. - -The list of groups fetched will only have the public and password type groups. The private groups will only be available if the user is a member of the group. - - - + ```javascript let limit = 30; let groupsRequest = new CometChat.GroupsRequestBuilder() @@ -184,36 +242,32 @@ groupList => { + + +The `fetchNext()` method returns an array of [`Group`](/sdk/reference/entities#group) objects. + +## Retrieve Particular Group Details + +Use `getGroup()` to fetch a specific group's details by GUID. + + ```typescript -let limit: number = 30; -let groupsRequest: CometChat.GroupsRequest = new CometChat.GroupsRequestBuilder() - .setLimit(limit) - .build(); - -groupsRequest.fetchNext().then( - (groupList: CometChat.Group[]) => { - console.log("Groups list fetched successfully", groupList); +const GUID: string = "GUID"; +CometChat.getGroup(GUID).then( + (group: CometChat.Group) => { + console.log("Group details fetched successfully:", group); }, (error: CometChat.CometChatException) => { - console.log("Groups list fetching failed with error", error); + console.log("Group details fetching failed with exception:", error); } ); ``` - - -## Retrieve Particular Group Details - -*In other words, as a logged-in user, how do I retrieve information for a specific group?* - -To get the information of a group, you can use the `getGroup()` method. - - - + ```javascript -var GUID = "GUID"; +const GUID = "GUID"; CometChat.getGroup(GUID).then( group => { console.log("Group details fetched successfully:", group); @@ -225,34 +279,34 @@ group => { + + +| Parameter | Description | +| --------- | ------------------------------ | +| `GUID` | The GUID of the group to fetch | + +The method returns a [`Group`](/sdk/reference/entities#group) object. + +## Get Online Group Member Count + +Use `getOnlineGroupMemberCount()` to get the number of online members in specified groups. + + ```typescript -var GUID: string = "GUID"; -CometChat.getGroup(GUID).then( - (group: CometChat.Group) => { - console.log("Group details fetched successfully:", group); +let guids: String[] = ["cometchat-guid-1"]; +CometChat.getOnlineGroupMemberCount(guids).then( + (groupMemberCount: Object) => { + console.log("Total online user for specified groups:", groupMemberCount); }, (error: CometChat.CometChatException) => { - console.log("Group details fetching failed with exception:", error); + console.log("Online group member count fetching failed with error:", error); } ); ``` - - -| Parameter | Description | -| --------- | ------------------------------------------------------------ | -| `GUID` | The GUID of the group for whom the details are to be fetched | - -It returns `Group` object containing the details of the group. - -## Get online group member count - -To get the total count of online users in particular groups, you can use the `getOnlineGroupMemberCount()` method. - - - + ```javascript let guids = ["cometchat-guid-1"]; CometChat.getOnlineGroupMemberCount(guids).then( @@ -266,20 +320,21 @@ groupMemberCount => { - -```typescript -let guids: String[] = ["cometchat-guid-1"]; -CometChat.getOnlineGroupMemberCount(guids).then( - (groupMemberCount: number) => { - console.log("Total online user for specified groups:", groupMemberCount); - }, (error: CometChat.CometChatException) => { - console.log("Online group member count fetching failed with error:", error); - } -); -``` + - +Returns an object with GUIDs as keys and online member counts as values. - +`getOnlineGroupMemberCount()` resolves with a `{ guid: count }` object where each key is a group GUID and its value is the number of currently online members in that group. + +--- + +## Next Steps -This method returns a JSON Object with the GUID as the key and the online member count for that group as the value. + + + Create public, private, or password-protected groups + + + Fetch and filter members of a specific group + + diff --git a/sdk/javascript/retrieve-users.mdx b/sdk/javascript/retrieve-users.mdx index 943df6404..bccdf7f1c 100644 --- a/sdk/javascript/retrieve-users.mdx +++ b/sdk/javascript/retrieve-users.mdx @@ -1,26 +1,46 @@ --- title: "Retrieve Users" +sidebarTitle: "Retrieve Users" +description: "Fetch, filter, search, and sort users using the CometChat JavaScript SDK. Includes pagination, role-based filtering, tag support, and online user counts." --- + +```javascript +// Fetch users list +const request = new CometChat.UsersRequestBuilder() + .setLimit(30).build(); +const users = await request.fetchNext(); -## Retrieve Logged In User Details +// Get specific user details +const user = await CometChat.getUser("UID"); -You can get the details of the logged-in user using the `getLoggedInUser()` method. This method can also be used to check if the user is logged in or not. If the method returns `Promise` with reject callback, it indicates that the user is not logged in and you need to log the user into CometChat SDK. +// Get logged-in user +const me = await CometChat.getLoggedinUser(); - - -```javascript -CometChat.getLoggedinUser().then( -user => { - console.log("user details:", { user }); -}, error => { - console.log("error getting details:", { error }); -} -); +// Get online user count +const count = await CometChat.getOnlineUserCount(); ``` + - +The CometChat SDK provides methods to retrieve the logged-in user, fetch filtered user lists, look up individual users by UID, and get online user counts. All user methods return [`User`](/sdk/reference/entities#user) objects. + +### User Object Fields + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| uid | `getUid()` | `string` | Unique user ID | +| name | `getName()` | `string` | Display name of the user | +| avatar | `getAvatar()` | `string` | URL of the user's avatar image | +| status | `getStatus()` | `string` | Online status of the user | +| lastActiveAt | `getLastActiveAt()` | `number` | Timestamp when the user was last active | +| role | `getRole()` | `string` | Role assigned to the user | + +## Get the Logged-In User + +Use `getLoggedinUser()` to get the current user's details. Resolves with `null` if no user is logged in. + + ```typescript @@ -35,30 +55,36 @@ CometChat.getLoggedinUser().then( + +```javascript +CometChat.getLoggedinUser().then( +user => { + console.log("user details:", { user }); +}, error => { + console.log("error getting details:", { error }); +} +); +``` + + + -This method will return a `User` object containing all the information related to the logged-in user. +This method returns a [`User`](/sdk/reference/entities#user) object with the logged-in user's information ## Retrieve List of Users In order to fetch the list of users, you can use the `UsersRequest` class. To use this class i.e to create an object of the `UsersRequest` class, you need to use the `UsersRequestBuilder` class. The `UsersRequestBuilder` class allows you to set the parameters based on which the users are to be fetched. +Fetching using this builder will return [`User`](/sdk/reference/entities#user) objects + The `UsersRequestBuilder` class allows you to set the below parameters: ### Set Limit -This method sets the limit i.e. the number of users that should be fetched in a single iteration. +Sets the number of users to fetch per request. - -```javascript -let limit = 30; -let usersRequest = new CometChat.UsersRequestBuilder() - .setLimit(limit) - .build(); -``` - - ```typescript @@ -70,25 +96,24 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() - - -### Set Search Keyword - -This method allows you to set the search string based on which the users are to be fetched. - - - + ```javascript let limit = 30; -let searchKeyword = "super"; let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) - .setSearchKeyword(searchKeyword) .build(); ``` + + +### Set Search Keyword + +Filters users by a search string. + + + ```typescript let limit: number = 30; @@ -101,27 +126,26 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() - - -### Search In - -This method allows you to define in which user property should the searchKeyword be searched. This method only works in combination with `setSearchKeyword()`. By default the keyword is searched in both UID & Name. - - -```js +```javascript let limit = 30; let searchKeyword = "super"; -let searchIn = ["uid", "name"]; let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) .setSearchKeyword(searchKeyword) - .searchIn(searchIn) .build(); ``` + + +### Search In + +Specifies which user properties to search. Works with `setSearchKeyword()`. By default, searches both UID and name. + + + ```typescript let limit: number = 30; @@ -136,28 +160,32 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() + +```javascript +let limit = 30; +let searchKeyword = "super"; +let searchIn = ["uid", "name"]; +let usersRequest = new CometChat.UsersRequestBuilder() + .setLimit(limit) + .setSearchKeyword(searchKeyword) + .searchIn(searchIn) + .build(); +``` + + + ### Set Status -The status based on which the users are to be fetched. The status parameter can contain one of the below two values: +Filters users by online status: -* CometChat.USER\_STATUS.ONLINE - will return the list of only online users. -* CometChat.USER\_STATUS.OFFLINE - will return the list of only offline users. +- `CometChat.USER_STATUS.ONLINE` — Only online users +- `CometChat.USER_STATUS.OFFLINE` — Only offline users -If this parameter is not set, will return all the available users. +If not set, returns all users. - -```javascript -let limit = 30; -let usersRequest = new CometChat.UsersRequestBuilder() - .setLimit(limit) - .setStatus(CometChat.USER_STATUS.ONLINE) - .build() -``` - - ```typescript @@ -172,22 +200,35 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() +Relevant fields to access on returned users: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| status | `getStatus()` | `string` | Online status of the user (`"online"` or `"offline"`) | + ### Hide Blocked Users This method is used to determine if the blocked users should be returned as a part of the user list. If set to true, the user list will not contain the users blocked by the logged in user. - + ```javascript let limit = 30; let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) - .hideBlockedUsers(true) - .build(); + .setStatus(CometChat.USER_STATUS.ONLINE) + .build() ``` + + +### Hide Blocked Users + +When `true`, excludes users blocked by the logged-in user from the results. + + ```typescript let limit: number = 30; @@ -199,25 +240,24 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() - - -### Set Roles - -This method allows you to fetch the users based on multiple roles. - - - + ```javascript let limit = 30; -let roles = ["default", "dev"]; let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) - .setRoles(roles) + .hideBlockedUsers(true) .build(); ``` + + +### Set Roles + +Filters users by specified roles. + + ```typescript let limit: number = 30; @@ -232,22 +272,36 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() +Relevant fields to access on returned users: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| role | `getRole()` | `string` | Role assigned to the user | + ### Friends Only This property when set to true will return only the friends of the logged-in user. - + ```javascript let limit = 30; +let roles = ["default", "dev"]; let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) - .friendsOnly(true) + .setRoles(roles) .build(); ``` + + +### Friends Only + +When `true`, returns only friends of the logged-in user. + + ```typescript let limit: number = 30; @@ -259,25 +313,24 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() - - -### Set Tags - -This method accepts a list of tags based on which the list of users is to be fetched. The list fetched will only contain the users that have been tagged with the specified tags. - - - + ```javascript let limit = 30; -let tags = ["tag1", "tag2"]; let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) - .setTags(tags) + .friendsOnly(true) .build(); ``` + + +### Set Tags + +Filters users by specified tags. + + ```typescript let limit: number = 30; @@ -292,22 +345,36 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() +Relevant fields to access on returned users: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags associated with the user | + ### With Tags This property when set to true will fetch tags data along with the list of users. - + ```javascript let limit = 30; +let tags = ["tag1", "tag2"]; let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) - .withTags(true) + .setTags(tags) .build(); ``` + + +### With Tags + +When `true`, includes tag data in the returned user objects. + + ```typescript let limit: number = 30; @@ -321,23 +388,35 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() +Relevant fields to access on returned users: + +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags associated with the user | + ### Set UIDs This method accepts a list of UIDs based on which the list of users is fetched. A maximum of `25` users can be fetched. - + ```javascript let limit = 30; -let UIDs = ["cometchat-uid-1", "cometchat-uid-2"]; let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) - .setUIDs(UIDs) + .withTags(true) .build(); ``` + + +### Set UIDs + +Fetches specific users by their UIDs. Maximum 25 users per request. + + ```typescript let limit: number = 30; @@ -350,26 +429,25 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() - - -### Sort By - -This method allows you to sort the User List by a specific property of User. By default the User List is sorted by `status => name => UID` . If `name` is pass to the `sortBy()` method the user list will be sorted by `name => UID`. - - -```js +```javascript let limit = 30; let UIDs = ["cometchat-uid-1", "cometchat-uid-2"]; -let sortBy = "name"; let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) - .sortBy(sortBy) + .setUIDs(UIDs) .build(); ``` + + +### Sort By + +Sorts the user list by a specific property. Default sort order: `status → name → UID`. Pass `"name"` to sort by `name → UID`. + + ```typescript let limit: number = 30; @@ -382,18 +460,31 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() + +```javascript +let limit = 30; +let UIDs = ["cometchat-uid-1", "cometchat-uid-2"]; +let sortBy = "name"; +let usersRequest = new CometChat.UsersRequestBuilder() + .setLimit(limit) + .sortBy(sortBy) + .build(); +``` + + + ### Sort By Order -This method allows you to sort the User List in a specific order. By default the user list is sorted in ascending order. +Sets the sort order. Default is ascending (`"asc"`). Use `"desc"` for descending. - -```js -let limit = 30; -let sortByOrder = "desc"; -let usersReques = new CometChat.UsersRequestBuilder() + +```typescript +let limit: number = 30; +let sortOrder: string = "desc"; +let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) .sortByOrder(sortOrder) .build(); @@ -401,13 +492,13 @@ let usersReques = new CometChat.UsersRequestBuilder() - -```typescript -let limit: number = 30; -let sortOrder: string = "desc"; -let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() + +```javascript +let limit = 30; +let sortOrder = "desc"; +let usersRequest = new CometChat.UsersRequestBuilder() .setLimit(limit) - .sortOrder(sortOrder) + .sortByOrder(sortOrder) .build(); ``` @@ -415,29 +506,9 @@ let usersRequest: CometChat.UsersRequest = new CometChat.UsersRequestBuilder() -Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the UsersRequest class. - -Once you have the object of the UsersRequest class, you need to call the fetchNext() method. Calling this method will return a list of User objects containing n number of users where n is the limit set in the builder class. +After configuring the builder, call `build()` to get the `UsersRequest` object, then call `fetchNext()` to retrieve users. - -```javascript -var limit = 30; -var usersRequest = new CometChat.UsersRequestBuilder() -.setLimit(limit) -.build(); - -usersRequest.fetchNext().then( -userList => { - console.log("User list received:", userList); -}, error => { - console.log("User list fetching failed with error:", error); -} -); -``` - - - ```typescript let limit: number = 30; @@ -456,27 +527,33 @@ usersRequest.fetchNext().then( - - -## Retrieve Particular User Details - -To get the information of a user, you can use the `getUser()` method. - - - + ```javascript -let UID = "UID"; -CometChat.getUser(UID).then( -user => { - console.log("User details fetched for user:", user); +const limit = 30; +const usersRequest = new CometChat.UsersRequestBuilder() +.setLimit(limit) +.build(); + +usersRequest.fetchNext().then( +userList => { + console.log("User list received:", userList); }, error => { - console.log("User details fetching failed with error:", error); + console.log("User list fetching failed with error:", error); } ); ``` + + +The `fetchNext()` method returns an array of [`User`](/sdk/reference/entities#user) objects. + +## Retrieve Particular User Details + +Use `getUser()` to fetch a specific user's details by UID. + + ```typescript let UID: string = "UID"; @@ -491,34 +568,37 @@ CometChat.getUser(UID).then( + +```javascript +let UID = "UID"; +CometChat.getUser(UID).then( +user => { + console.log("User details fetched for user:", user); +}, error => { + console.log("User details fetching failed with error:", error); +} +); +``` + + + -The `getUser()` method takes the following parameters: +| Parameter | Description | +| --------- | ----------- | +| UID | The UID of the user to fetch | | Parameter | Description | | --------- | ---------------------------------------------------------- | | UID | The UID of the user for whom the details are to be fetched | -It returns the `User` object containing the details of the user. +It returns the [`User`](/sdk/reference/entities#user) object containing the details of the user. -## Get online user count +## Get Online User Count -To get the total count of online users for your app, you can use the `getOnlineUserCount()` method. +Use `getOnlineUserCount()` to get the total number of online users in your app. - -```javascript -CometChat.getOnlineUserCount().then( -userCount => { - console.log("Total online user count:", userCount); -}, error => { - console.log("Online user count fetching failed with error:", error); -} -); -``` - - - ```typescript CometChat.getOnlineUserCount().then( @@ -532,6 +612,34 @@ CometChat.getOnlineUserCount().then( + +```javascript +CometChat.getOnlineUserCount().then( +userCount => { + console.log("Total online user count:", userCount); +}, error => { + console.log("Online user count fetching failed with error:", error); +} +); +``` + + + -This method returns the total online user count for your app. +Returns the total online user count as a number. + +`getOnlineUserCount()` resolves with a `number` representing the total count of currently online users in your app. + +--- + +## Next Steps + + + + Track and subscribe to user online/offline status + + + Block and unblock users from your application + + diff --git a/sdk/javascript/send-message.mdx b/sdk/javascript/send-message.mdx index ae9f8e125..bdb742e3e 100644 --- a/sdk/javascript/send-message.mdx +++ b/sdk/javascript/send-message.mdx @@ -1,81 +1,34 @@ --- -title: "Send A Message" +title: "Send Messages" +sidebarTitle: "Send Messages" +description: "Send text, media, and custom messages to users and groups using the CometChat JavaScript SDK." --- + +| Field | Value | +| --- | --- | +| Key Classes | [`TextMessage`](/sdk/reference/messages#textmessage), [`MediaMessage`](/sdk/reference/messages#mediamessage), [`CustomMessage`](/sdk/reference/messages#custommessage) | +| Key Methods | `sendMessage()`, `sendMediaMessage()`, `sendCustomMessage()` | +| Receiver Types | `CometChat.RECEIVER_TYPE.USER`, `CometChat.RECEIVER_TYPE.GROUP` | +| Message Types | `TEXT`, `IMAGE`, `VIDEO`, `AUDIO`, `FILE`, `CUSTOM` | +| Prerequisites | SDK initialized, user logged in | -Using CometChat, you can send three types of messages: + -1. [Text Message](/sdk/javascript/send-message#text-message) is the most common and standard message type. -2. [Media Message](/sdk/javascript/send-message#media-message) for sending photos, videos and files. -3. [Custom Message](/sdk/javascript/send-message#custom-message), for sending completely custom data using JSON structures. -4. [Interactive Messages](/sdk/javascript/interactive-messages), for sending end-user interactive messages of type form, card and custom Interactive +CometChat supports three types of messages: -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 +| Type | Method | Use Case | +| --- | --- | --- | +| [Text](#text-message) | `sendMessage()` | Plain text messages | +| [Media](#media-message) | `sendMediaMessage()` | Images, videos, audio, files | +| [Custom](#custom-message) | `sendCustomMessage()` | Location, polls, or any JSON data | -## 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. - - - -```javascript -let metadata = { - latitude: "50.6192171633316", - longitude: "-72.68182268750002", -}; - -textMessage.setMetadata(metadata); -``` - - - - -```typescript -let metadata: Object = { - latitude: "50.6192171633316", - longitude: "-72.68182268750002", -}; - -textMessage.setMetadata(metadata); -``` - - - - +You can also send [Interactive Messages](/sdk/javascript/interactive-messages) for forms, cards, and custom UI elements. -### 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. - - - -```javascript -let tags = ["starredMessage"]; - -textMessage.setTags(tags); -``` - - - - -```typescript -let tags: Array = ["starredMessage"]; - -textMessage.setTags(tags); -``` - - - - +## Text Message -Once the text message object is ready, you need to use the `sendMessage()` method to send the text message to the recipient. +Send a text message using `CometChat.sendMessage()` with a [`TextMessage`](/sdk/reference/messages#textmessage) object. @@ -98,9 +51,7 @@ CometChat.sendMessage(textMessage).then( } ); ``` - - ```javascript let receiverID = "GUID"; @@ -121,9 +72,7 @@ CometChat.sendMessage(textMessage).then( } ); ``` - - ```typescript let receiverID: string = "UID", @@ -144,9 +93,7 @@ CometChat.sendMessage(textMessage).then( } ); ``` - - ```typescript let receiverID: string = "GUID", @@ -167,14 +114,35 @@ CometChat.sendMessage(textMessage).then( } ); ``` - + +```javascript +try { + const receiverID = "UID"; + const messageText = "Hello world!"; + const receiverType = CometChat.RECEIVER_TYPE.USER; + const textMessage = new CometChat.TextMessage( + receiverID, + messageText, + receiverType + ); + const message = await CometChat.sendMessage(textMessage); + console.log("Message sent successfully:", message); +} catch (error) { + console.log("Message sending failed with error:", error); +} +``` + -### Set Quoted Message Id +The [`TextMessage`](/sdk/reference/messages#textmessage) class constructor takes the following parameters: -To set a quoted message ID for a message, use the `setQuotedMessageId()` method of the TextMessage class. This method accepts the ID of the message to be quoted. +| Parameter | Description | Required | +| --- | --- | --- | +| `receiverID` | UID of the user or GUID of the group receiving the message | Yes | +| `messageText` | The text message content | Yes | +| `receiverType` | `CometChat.RECEIVER_TYPE.USER` or `CometChat.RECEIVER_TYPE.GROUP` | Yes | @@ -290,7 +258,7 @@ CometChat.sendMessage(textMessage).then( -The `TextMessage` class constructor takes the following parameters: +The [`TextMessage`](/sdk/reference/messages#textmessage) class constructor takes the following parameters: | Parameter | Description | | | ---------------- | -------------------------------------------------------------------------------------------- | -------- | @@ -298,131 +266,107 @@ The `TextMessage` class constructor takes the following parameters: | **messageText** | The text message | Required | | **receiverType** | The type of the receiver - `CometChat.RECEIVER_TYPE.USER` or `CometChat.RECEIVER_TYPE.GROUP` | Required | -When a text message is sent successfully, the response will include a `TextMessage` object which includes all information related to the sent message. + +On success, `sendMessage()` method returns a [`TextMessage`](/sdk/reference/messages#textmessage) | [`MediaMessage`](/sdk/reference/messages#mediamessage) | [`CustomMessage`](/sdk/reference/messages#custommessage) | [`BaseMessage`](/sdk/reference/messages#basemessage) object depending on the input message type. ## 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. +To send a media message to any user or group, you need to use the `sendMediaMessage()` method and pass a [`MediaMessage`](/sdk/reference/messages#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. +Attach custom JSON data to the message: - -```javascript -let metadata = { + +```typescript +let metadata: Object = { latitude: "50.6192171633316", longitude: "-72.68182268750002", }; -mediaMessage.setMetadata(metadata); +textMessage.setMetadata(metadata); ``` - - - -```typescript -let metadata: Object = { + +```javascript +let metadata = { latitude: "50.6192171633316", longitude: "-72.68182268750002", }; -mediaMessage.setMetadata(metadata); +textMessage.setMetadata(metadata); ``` - - -### Add Caption(Text along with Media Message) +### Add Tags -To send a caption with a media message, you can use the `setCaption` method and pass text to it. +Tag messages for easy filtering later: - -```javascript -let caption = "Random Caption"; - -mediaMessage.setCaption(caption); -``` - - - ```typescript -let caption: string = "Random Caption"; +let tags: Array = ["starredMessage"]; -mediaMessage.setCaption(caption); +textMessage.setTags(tags); ``` - - - - -### 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. - - - + ```javascript let tags = ["starredMessage"]; -mediaMessage.setTags(tags); +textMessage.setTags(tags); ``` - + + +### Quote a Message +Reply to a specific message by setting its ID: + + ```typescript -let tags: Array = ["starredMessage"]; - -mediaMessage.setTags(tags); +textMessage.setQuotedMessageId(10); +``` + + +```javascript +textMessage.setQuotedMessageId(10); ``` - - -There are 2 ways you can send Media Messages using the CometChat SDK: +## Media Message -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. +Send images, videos, audio, or files using `CometChat.sendMediaMessage()`. -Getting file Object: +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 - - -```html - - - - - - -``` +### Upload a File - +Get the file from an input element and pass it to [`MediaMessage`](/sdk/reference/messages#mediamessage): - +```html + +``` ```javascript let receiverID = "UID"; -let messageType = CometChat.MESSAGE_TYPE.FILE; +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, - "INPUT FILE OBJECT", + file, messageType, receiverType ); @@ -436,17 +380,17 @@ CometChat.sendMediaMessage(mediaMessage).then( } ); ``` - - ```javascript let receiverID = "GUID"; -let messageType = CometChat.MESSAGE_TYPE.FILE; +let messageType = CometChat.MESSAGE_TYPE.IMAGE; let receiverType = CometChat.RECEIVER_TYPE.GROUP; +let file = document.getElementById("img_file").files[0]; + let mediaMessage = new CometChat.MediaMessage( receiverID, - `INPUT FILE OBJECT`, + file, messageType, receiverType ); @@ -460,20 +404,20 @@ CometChat.sendMediaMessage(mediaMessage).then( } ); ``` - - ```typescript let receiverID: string = "UID", - messageType: string = CometChat.MESSAGE_TYPE.FILE, + messageType: string = CometChat.MESSAGE_TYPE.IMAGE, receiverType: string = CometChat.RECEIVER_TYPE.USER, - mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( - receiverID, - "INPUT FILE OBJECT", - messageType, - receiverType - ); + file: File = (document.getElementById("img_file") as HTMLInputElement).files![0]; + +let mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( + receiverID, + file, + messageType, + receiverType +); CometChat.sendMediaMessage(mediaMessage).then( (message: CometChat.MediaMessage) => { @@ -484,20 +428,20 @@ CometChat.sendMediaMessage(mediaMessage).then( } ); ``` - - ```typescript let receiverID: string = "GUID", - messageType: string = CometChat.MESSAGE_TYPE.FILE, + messageType: string = CometChat.MESSAGE_TYPE.IMAGE, receiverType: string = CometChat.RECEIVER_TYPE.GROUP, - mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( - receiverID, - "INPUT FILE OBJECT", - messageType, - receiverType - ); + file: File = (document.getElementById("img_file") as HTMLInputElement).files![0]; + +let mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( + receiverID, + file, + messageType, + receiverType +); CometChat.sendMediaMessage(mediaMessage).then( (message: CometChat.MediaMessage) => { @@ -508,72 +452,36 @@ CometChat.sendMediaMessage(mediaMessage).then( } ); ``` - - - - - -### Set Quoted Message Id - -To set a quoted message ID for a message, use the `setQuotedMessageId()` method of the MediaMessage class. This method accepts the ID of the message to be quoted. - - - -```javascript -mediaMessage.setQuotedMessageId(10); -``` - - - - -```typescript -mediaMessage.setQuotedMessageId(10); -``` - - -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 - - - - - - -``` - - +### Send a URL - +Send media hosted on your server or cloud storage using the [`Attachment`](/sdk/reference/auxiliary#attachment) class: ```javascript let receiverID = "UID"; -let messageType = CometChat.MESSAGE_TYPE.FILE; +let messageType = CometChat.MESSAGE_TYPE.IMAGE; let receiverType = CometChat.RECEIVER_TYPE.USER; let mediaMessage = new CometChat.MediaMessage( receiverID, - "INPUT FILE OBJECT", + "", 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); @@ -583,21 +491,29 @@ CometChat.sendMediaMessage(mediaMessage).then( } ); ``` - - ```javascript let receiverID = "GUID"; -let messageType = CometChat.MESSAGE_TYPE.FILE; +let messageType = CometChat.MESSAGE_TYPE.IMAGE; let receiverType = CometChat.RECEIVER_TYPE.GROUP; let mediaMessage = new CometChat.MediaMessage( receiverID, - `INPUT FILE OBJECT`, + "", 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); @@ -607,30 +523,182 @@ CometChat.sendMediaMessage(mediaMessage).then( } ); ``` - - ```typescript let receiverID: string = "UID", - messageType: string = CometChat.MESSAGE_TYPE.FILE, + messageType: string = CometChat.MESSAGE_TYPE.IMAGE, receiverType: string = CometChat.RECEIVER_TYPE.USER, mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( receiverID, - "INPUT FILE OBJECT", + "", messageType, receiverType ); -CometChat.sendMediaMessage(mediaMessage).then( - (message: CometChat.MediaMessage) => { - console.log("Media message sent successfully", message); - }, - (error: CometChat.CometChatException) => { - console.log("Media message sending failed with error", error); - } -); -``` +let file: Object = { + name: "mario", + extension: "png", + mimeType: "image/png", + url: "https://pngimg.com/uploads/mario/mario_PNG125.png", +}; + +let attachment: CometChat.Attachment = new CometChat.Attachment(file); +mediaMessage.setAttachment(attachment); + +CometChat.sendMediaMessage(mediaMessage).then( + (message: CometChat.MediaMessage) => { + console.log("Media message sent successfully", message); + }, + (error: CometChat.CometChatException) => { + console.log("Media message sending failed with error", error); + } +); +``` + + +```typescript +let receiverID: string = "GUID", + messageType: string = CometChat.MESSAGE_TYPE.IMAGE, + receiverType: string = CometChat.RECEIVER_TYPE.GROUP, + mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( + receiverID, + "", + messageType, + receiverType + ); + +let file: Object = { + name: "mario", + extension: "png", + mimeType: "image/png", + url: "https://pngimg.com/uploads/mario/mario_PNG125.png", +}; + +let attachment: CometChat.Attachment = new CometChat.Attachment(file); +mediaMessage.setAttachment(attachment); + +CometChat.sendMediaMessage(mediaMessage).then( + (message: CometChat.MediaMessage) => { + console.log("Media message sent successfully", message); + }, + (error: CometChat.CometChatException) => { + console.log("Media message sending failed with error", error); + } +); +``` + + + +The [`MediaMessage`](/sdk/reference/messages#mediamessage) class constructor takes the following parameters: + +| Parameter | Description | Required | +| --- | --- | --- | +| `receiverID` | UID of the user or GUID of the group | Yes | +| `file` | File object to upload, or empty string if using URL | Yes | +| `messageType` | `CometChat.MESSAGE_TYPE.IMAGE`, `VIDEO`, `AUDIO`, or `FILE` | Yes | +| `receiverType` | `CometChat.RECEIVER_TYPE.USER` or `GROUP` | Yes | + +On success, `sendMediaMessage()` returns a [MediaMessage](/sdk/reference/messages#mediamessage) object. + +### Add Caption + +Add text along with the media: + + + +```typescript +mediaMessage.setCaption("Check out this photo!"); +``` + + +```javascript +mediaMessage.setCaption("Check out this photo!"); +``` + + + +### Add Metadata and Tags + +Same as text messages: + +```javascript +mediaMessage.setMetadata({ location: "Paris" }); +mediaMessage.setTags(["vacation"]); +mediaMessage.setQuotedMessageId(10); +``` + +## Multiple Attachments + +Send multiple files in a single media message. + +### Upload Multiple Files + + + +```javascript +let receiverID = "UID"; +let messageType = CometChat.MESSAGE_TYPE.FILE; +let receiverType = CometChat.RECEIVER_TYPE.USER; +let files = document.getElementById("img_file").files; // FileList + +CometChat.sendMediaMessage(mediaMessage).then( + (message) => { + console.log("Media message sent successfully", message); + }, + (error) => { + console.log("Media message sending failed with error", error); + } +); +``` + + + + +```javascript +let receiverID = "GUID"; +let messageType = CometChat.MESSAGE_TYPE.FILE; +let receiverType = CometChat.RECEIVER_TYPE.GROUP; +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); + } +); +``` + + + + +```typescript +let receiverID: string = "UID", + messageType: string = CometChat.MESSAGE_TYPE.FILE, + receiverType: string = CometChat.RECEIVER_TYPE.USER, + mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( + receiverID, + "INPUT FILE OBJECT", + messageType, + receiverType + ); + +CometChat.sendMediaMessage(mediaMessage).then( + (message: CometChat.MediaMessage) => { + console.log("Media message sent successfully", message); + }, + (error: CometChat.CometChatException) => { + console.log("Media message sending failed with error", error); + } +); +``` @@ -660,7 +728,7 @@ CometChat.sendMediaMessage(mediaMessage).then( -The `MediaMessage` class constructor takes the following parameters: +The [`MediaMessage`](/sdk/reference/messages#mediamessage) class constructor takes the following parameters: | Parameter | Description | | | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | @@ -814,7 +882,8 @@ CometChat.sendMediaMessage(mediaMessage).then( -When a media message is sent successfully, the response will include a `MediaMessage` object which includes all information related to the sent message. + +The `sendMediaMessage()` method returns a [`MediaMessage`](/sdk/reference/messages#mediamessage) object. ## Multiple Attachments in a Media Message @@ -831,11 +900,11 @@ Getting files: @@ -867,39 +936,39 @@ CometChat.sendMediaMessage(mediaMessage).then( } ); ``` - + +```javascript +let receiverID = "GUID"; +let messageType = CometChat.MESSAGE_TYPE.FILE; +let receiverType = CometChat.RECEIVER_TYPE.GROUP; +let files = document.getElementById("img_file").files; - -```typescript -let receiverID: string = "UID", - messageType: string = CometChat.MESSAGE_TYPE.FILE, - receiverType: string = CometChat.RECEIVER_TYPE.USER, - mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( - receiverID, - files, - messageType, - receiverType - ); +let mediaMessage = new CometChat.MediaMessage( + receiverID, + files, + messageType, + receiverType +); CometChat.sendMediaMessage(mediaMessage).then( - (message: CometChat.MediaMessage) => { + (message) => { console.log("Media message sent successfully", message); }, - (error: CometChat.CometChatException) => { + (error) => { console.log("Media message sending failed with error", error); } ); ``` - + +```typescript +let receiverID: string = "UID", + messageType: string = CometChat.MESSAGE_TYPE.FILE, + receiverType: string = CometChat.RECEIVER_TYPE.USER, + files: FileList = (document.getElementById("img_file") as HTMLInputElement).files!; - -```javascript -let receiverID = "GUID"; -let messageType = CometChat.MESSAGE_TYPE.FILE; -let receiverType = CometChat.RECEIVER_TYPE.GROUP; -let mediaMessage = new CometChat.MediaMessage( +let mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( receiverID, files, messageType, @@ -907,28 +976,28 @@ let mediaMessage = new CometChat.MediaMessage( ); CometChat.sendMediaMessage(mediaMessage).then( - (message) => { + (message: CometChat.MediaMessage) => { console.log("Media message sent successfully", message); }, - (error) => { + (error: CometChat.CometChatException) => { console.log("Media message sending failed with error", error); } ); ``` - - ```typescript let receiverID: string = "GUID", messageType: string = CometChat.MESSAGE_TYPE.FILE, receiverType: string = CometChat.RECEIVER_TYPE.GROUP, - mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( - receiverID, - files, - messageType, - receiverType - ); + files: FileList = (document.getElementById("img_file") as HTMLInputElement).files!; + +let mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( + receiverID, + files, + messageType, + receiverType +); CometChat.sendMediaMessage(mediaMessage).then( (message: CometChat.MediaMessage) => { @@ -939,26 +1008,15 @@ CometChat.sendMediaMessage(mediaMessage).then( } ); ``` - - -The `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.USER`
`2. CometChat.RECEIVER_TYPE.GROUP` | - -2. **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: +### Send Multiple URLs ```javascript -let receiverID = "cometchat-uid-2"; +let receiverID = "UID"; let messageType = CometChat.MESSAGE_TYPE.IMAGE; let receiverType = CometChat.RECEIVER_TYPE.USER; let mediaMessage = new CometChat.MediaMessage( @@ -989,20 +1047,60 @@ attachments.push(new CometChat.Attachment(attachment2)); mediaMessage.setAttachments(attachments); CometChat.sendMediaMessage(mediaMessage).then( - (mediaMessage) => { - console.log("message", mediaMessage); + (message) => { + console.log("Media message sent successfully", message); }, (error) => { - console.log("error in sending message", error); + console.log("Media message sending failed with error", error); } ); ``` - + +```javascript +let receiverID = "GUID"; +let messageType = CometChat.MESSAGE_TYPE.IMAGE; +let receiverType = CometChat.RECEIVER_TYPE.GROUP; +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); + } +); +``` + ```typescript -let receiverID: string = "cometchat-uid-2", +let receiverID: string = "UID", messageType: string = CometChat.MESSAGE_TYPE.IMAGE, receiverType: string = CometChat.RECEIVER_TYPE.USER, mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( @@ -1033,64 +1131,18 @@ attachments.push(new CometChat.Attachment(attachment2)); mediaMessage.setAttachments(attachments); CometChat.sendMediaMessage(mediaMessage).then( - (mediaMessage: CometChat.MediaMessage) => { - console.log("message", mediaMessage); + (message: CometChat.MediaMessage) => { + console.log("Media message sent successfully", message); }, (error: CometChat.CometChatException) => { - console.log("error in sending message", error); - } -); -``` - - - - -```javascript -let receiverID = "cometchat-guid-1"; -let messageType = CometChat.MESSAGE_TYPE.IMAGE; -let receiverType = CometChat.RECEIVER_TYPE.GROUP; -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); + console.log("Media message sending failed with error", error); } ); ``` - - ```typescript -let receiverID: string = "cometchat-guid-1", +let receiverID: string = "GUID", messageType: string = CometChat.MESSAGE_TYPE.IMAGE, receiverType: string = CometChat.RECEIVER_TYPE.GROUP, mediaMessage: CometChat.MediaMessage = new CometChat.MediaMessage( @@ -1107,7 +1159,7 @@ let attachment1: Object = { url: "https://pngimg.com/uploads/mario/mario_PNG125.png", }; -let attachment1: Object = { +let attachment2: Object = { name: "jaguar", extension: "png", mimeType: "image/png", @@ -1121,97 +1173,20 @@ attachments.push(new CometChat.Attachment(attachment2)); mediaMessage.setAttachments(attachments); CometChat.sendMediaMessage(mediaMessage).then( - (mediaMessage: CometChat.MediaMessage) => { - console.log("message", mediaMessage); + (message: CometChat.MediaMessage) => { + console.log("Media message sent successfully", message); }, (error: CometChat.CometChatException) => { - console.log("error in sending message", error); + console.log("Media message sending failed with error", 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. - - - -```js -let customMessage = new CometChat.CustomMessage( - receiverID, - receiverType, - customType, - customData -); -``` - - - - -```typescript -let customMessage: CometChat.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. - - - -```javascript -let tags = ["starredMessage"]; - -customMessage.setTags(tags); -``` - - - - -```typescript -let tags: Array = ["starredMessage"]; - -customMessage.setTags(tags); -``` - - - - - -Once the object of `CustomMessage` class is ready you can send the custom message using the `sendCustomMessage()` method. +Send structured data that doesn't fit text or media categories — like location coordinates, polls, or game moves. @@ -1223,144 +1198,7 @@ let customData = { }; 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); - } -); -``` - - - - -```javascript -let receiverID = "GUID"; -let customData = { - latitude: "50.6192171633316", - longitude: "-72.68182268750002", -}; -let customType = "location"; -let receiverType = CometChat.RECEIVER_TYPE.GROUP; -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); - } -); -``` - - - - -```typescript -let receiverID: string = "UID", - customData: Object = { - latitude: "50.6192171633316", - longitude: "-72.68182268750002", - }, - customType: string = "location", - receiverType: string = CometChat.RECEIVER_TYPE.USER, - customMessage: CometChat.CustomMessage = new CometChat.CustomMessage( - receiverID, - receiverType, - customType, - customData - ); - -CometChat.sendCustomMessage(customMessage).then( - (message: CometChat.CustomMessage) => { - console.log("custom message sent successfully", message); - }, - (error: CometChat.CometChatException) => { - console.log("custom message sending failed with error", error); - } -); -``` - - - - -```typescript -let receiverID: string = "GUID", - customData: Object = { - latitude: "50.6192171633316", - longitude: "-72.68182268750002", - }, - customType: string = "location", - receiverType: string = CometChat.RECEIVER_TYPE.GROUP, - customMessage: CometChat.CustomMessage = new CometChat.CustomMessage( - receiverID, - receiverType, - customType, - customData - ); - -CometChat.sendCustomMessage(customMessage).then( - (message: CometChat.CustomMessage) => { - console.log("custom message sent successfully", message); - }, - (error: CometChat.CometChatException) => { - console.log("custom message sending failed with error", error); - } -); -``` - - - - - -### Set Quoted Message Id - -To set a quoted message ID for a message, use the `setQuotedMessageId()` method of the CustomMessage class. This method accepts the ID of the message to be quoted. - - -```javascript -customMessage.setQuotedMessageId(10); -``` - - - - -```typescript -customMessage.setQuotedMessageId(10); -``` - - - - - -Once the object of `CustomMessage` class is ready you can send the custom message using the `sendCustomMessage()` method. - - - -```javascript -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, @@ -1370,16 +1208,14 @@ let customMessage = new CometChat.CustomMessage( CometChat.sendCustomMessage(customMessage).then( (message) => { - console.log("custom message sent successfully", message); + console.log("Custom message sent successfully", message); }, (error) => { - console.log("custom message sending failed with error", error); + console.log("Custom message sending failed with error", error); } ); ``` - - ```javascript let receiverID = "GUID"; @@ -1389,6 +1225,7 @@ let customData = { }; let customType = "location"; let receiverType = CometChat.RECEIVER_TYPE.GROUP; + let customMessage = new CometChat.CustomMessage( receiverID, receiverType, @@ -1398,16 +1235,14 @@ let customMessage = new CometChat.CustomMessage( CometChat.sendCustomMessage(customMessage).then( (message) => { - console.log("custom message sent successfully", message); + console.log("Custom message sent successfully", message); }, (error) => { - console.log("custom message sending failed with error", error); + console.log("Custom message sending failed with error", error); } ); ``` - - ```typescript let receiverID: string = "UID", @@ -1426,16 +1261,14 @@ let receiverID: string = "UID", CometChat.sendCustomMessage(customMessage).then( (message: CometChat.CustomMessage) => { - console.log("custom message sent successfully", message); + console.log("Custom message sent successfully", message); }, (error: CometChat.CometChatException) => { - console.log("custom message sending failed with error", error); + console.log("Custom message sending failed with error", error); } ); ``` - - ```typescript let receiverID: string = "GUID", @@ -1454,179 +1287,61 @@ let receiverID: string = "GUID", CometChat.sendCustomMessage(customMessage).then( (message: CometChat.CustomMessage) => { - console.log("custom message sent successfully", message); + console.log("Custom message sent successfully", message); }, (error: CometChat.CometChatException) => { - console.log("custom message sending failed with error", 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. +The [`CustomMessage`](/sdk/reference/messages#custommessage) class constructor takes the following parameters: -On success, you will receive an object of the `CustomMessage` class. +| Parameter | Description | Required | +| --- | --- | --- | +| `receiverID` | UID of the user or GUID of the group | Yes | +| `receiverType` | `CometChat.RECEIVER_TYPE.USER` or `GROUP` | Yes | +| `customType` | Your custom type string (e.g., `"location"`, `"poll"`) | Yes | +| `customData` | JSON object with your custom data | Yes | -### Update Conversation +On success, `sendCustomMessage()` returns a [CustomMessage](/sdk/reference/messages#custommessage) object. -*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. +### Add Tags - - ```javascript -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); - } -); +customMessage.setTags(["starredMessage"]); ``` - +### Quote a Message - ```javascript -let receiverID = "GUID"; -let customData = { - latitude: "50.6192171633316", - longitude: "-72.68182268750002", -}; -let customType = "location"; -let receiverType = CometChat.RECEIVER_TYPE.GROUP; -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); - } -); -``` - - - - -```typescript -let receiverID: string = "UID", - customData: Object = { - latitude: "50.6192171633316", - longitude: "-72.68182268750002", - }, - customType: string = "location", - receiverType: string = CometChat.RECEIVER_TYPE.USER, - customMessage: CometChat.CustomMessage = new CometChat.CustomMessage( - receiverID, - receiverType, - customType, - customData - ); - -customMessage.shouldUpdateConversation(false); -CometChat.sendCustomMessage(customMessage).then( - (message: CometChat.CustomMessage) => { - console.log("custom message sent successfully", message); - }, - (error: CometChat.CometChatException) => { - console.log("custom message sending failed with error", error); - } -); +customMessage.setQuotedMessageId(10); ``` - +### Control Conversation Update - -```typescript -let receiverID: string = "GUID", - customData: Object = { - latitude: "50.6192171633316", - longitude: "-72.68182268750002", - }, - customType: string = "location", - receiverType: string = CometChat.RECEIVER_TYPE.GROUP, - customMessage: CometChat.CustomMessage = new CometChat.CustomMessage( - receiverID, - receiverType, - customType, - customData - ); +By default, custom messages update the conversation's last message. To prevent this: +```javascript customMessage.shouldUpdateConversation(false); -CometChat.sendCustomMessage(customMessage).then( - (message: CometChat.CustomMessage) => { - console.log("custom message sent successfully", message); - }, - (error: CometChat.CometChatException) => { - console.log("custom message sending failed with error", error); - } -); ``` - +{/* ### Control Push Notifications - +By default, custom messages don't trigger push notifications. To enable them: -### Custom Notification Body +```javascript +customMessage.shouldSendNotification(true); +``` */} -*How can i customise the notification body of custom message?* +### Custom Notification Text -To add a custom notification body for `Push, Email & SMS` notification of a custom message you can use the `setConversationText(text: string)` method of Custom Message class. +Set custom text for push, email, and SMS notifications: - - ```javascript -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.setConversationText("Custom notification body"); -CometChat.sendCustomMessage(customMessage).then( - (message) => { - console.log("custom message sent successfully", message); - }, - (error) => { - console.log("custom message sending failed with error", error); - } -); +customMessage.setConversationText("Shared a location"); ``` @@ -1720,8 +1435,28 @@ CometChat.sendCustomMessage(customMessage).then( - -It is also possible to send interactive messages from CometChat, to know more [click here](/sdk/javascript/interactive-messages) +The `sendCustomMessage()` method returns a [`CustomMessage`](/sdk/reference/messages#custommessage) object. + +You can also send interactive messages with forms, cards, and buttons. See [Interactive Messages](/sdk/javascript/interactive-messages) for details. + +--- + +## Next Steps + + + + Listen for incoming messages in real-time + + + Edit previously sent messages + + + Delete sent messages + + + Send forms, cards, and custom UI elements + + diff --git a/sdk/javascript/session-timeout.mdx b/sdk/javascript/session-timeout.mdx index 23470881f..70bd8403d 100644 --- a/sdk/javascript/session-timeout.mdx +++ b/sdk/javascript/session-timeout.mdx @@ -1,34 +1,81 @@ --- -title: "Session Timeout Flow" +title: "Session Timeout" +sidebarTitle: "Session Timeout" +description: "Handle idle session timeouts in CometChat calls, including automatic termination, user prompts, and the onSessionTimeout event." --- + +```javascript +// Set custom timeout (in seconds) +const callSettings = new CometChatCalls.CallSettingsBuilder() + .setIdleTimeoutPeriod(300) // 5 minutes + .setCallListener(new CometChatCalls.OngoingCallListener({ + onSessionTimeout: () => { + console.log("Session timed out"); + CometChatCalls.endSession(); + } + })) + .build(); +``` -Available since v4.1.0 +- Default idle timeout: 180 seconds (3 minutes) alone in a session +- Warning dialog appears 60 seconds before auto-termination +- Listen for `onSessionTimeout` to handle auto-termination +- Customize with `setIdleTimeoutPeriod(seconds)` in CallSettings (v4.1.0+) + -## Overview +*Available since v4.1.0* -CometChat Calls SDK provides a mechanism to handle session timeouts for idle participants. By default, if a participant is alone in a call session for 180 seconds (3 minutes), the following sequence is triggered: +The Calls SDK automatically terminates call sessions when a participant is alone for too long, preventing abandoned calls from consuming resources. You can customize the timeout duration and handle the termination event. -1. After 120 seconds of being alone in the session, the participant will see a dialog box +## How It Works -2. This dialog provides options to either: +When a participant is alone in a session: - * Stay in the call - * Leave immediately +1. A warning dialog appears 60 seconds before the timeout, with "Stay" or "Leave" options +2. If no action is taken, the call auto-terminates and the `onSessionTimeout` event fires -3. If no action is taken within the next 60 seconds, the call will automatically end +The default timeout is 180 seconds (3 minutes). -This feature helps manage inactive call sessions and prevents unnecessary resource usage. The idle timeout period ensures that participants don't accidentally remain in abandoned call sessions. +## Configuration -### Session Timeout Flow +Set a custom timeout period using `setIdleTimeoutPeriod()` in `CallSettingsBuilder`. The warning dialog will always appear 60 seconds before the configured timeout. - - - +```javascript +const callSettings = new CometChatCalls.CallSettingsBuilder() + .setIdleTimeoutPeriod(300) // 5 minutes + .enableDefaultLayout(true) + .setCallListener(callListener) + .build(); +``` - +## Handling the Timeout Event -The `onSessionTimeout` event is triggered when the call automatically terminates due to session timeout, as illustrated in the diagram above. +Listen for `onSessionTimeout` in your `OngoingCallListener` to clean up when the call auto-terminates: - +```javascript +const callListener = new CometChatCalls.OngoingCallListener({ + onSessionTimeout: () => { + console.log("Session timed out due to inactivity"); + CometChatCalls.endSession(); + // Close the calling screen + }, + // ... other listeners +}); +``` + +See [Call Session — Call Listeners](/sdk/javascript/direct-call#call-listeners) for the full list of events. + +--- + +## Next Steps + + + + Implement ringing call flows with accept/reject functionality + + + Implement calling without the Chat SDK + + diff --git a/sdk/javascript/setup-sdk.mdx b/sdk/javascript/setup-sdk.mdx index e9bdce872..50c5b6467 100644 --- a/sdk/javascript/setup-sdk.mdx +++ b/sdk/javascript/setup-sdk.mdx @@ -1,96 +1,114 @@ --- title: "Setup" -sidebarTitle: "Overview" +sidebarTitle: "Setup" +description: "Install, configure, and initialize the CometChat JavaScript SDK in your application." --- +{/* TL;DR for Agents and Quick Reference */} + +```bash +npm install @cometchat/chat-sdk-javascript +``` - -Migrating app version from v3 to v4 ? +```typescript +import { CometChat } from "@cometchat/chat-sdk-javascript"; -Skip the create new app step. Your existing v3 app can be migrated to v4. +const appSettings: CometChat.AppSettings = new CometChat.AppSettingsBuilder() + .setRegion("APP_REGION") + .subscribePresenceForAllUsers() + .autoEstablishSocketConnection(true) + .build(); -Follow steps mentioned in **Add the CometChat dependency** section below to upgrade to latest version of v4 +await CometChat.init("APP_ID", appSettings); +await CometChat.login("UID", "AUTH_KEY"); // dev only +``` - +**Prerequisites:** npm 8+, Node.js 16+, credentials from [CometChat Dashboard](https://app.cometchat.com) +**SSR:** SDK requires browser APIs — initialize client-side only (useEffect / mounted) + -## Get your Application Keys +## Prerequisites -[Signup for CometChat](https://app.cometchat.com) and then: +| Requirement | Version | +|-------------|---------| +| npm | 8.x or above | +| Node.js | 16 or above | -1. Create a new app -2. Head over to the **API & Auth Keys** section and note the **Auth Key**, **App ID** & **Region** +Get your credentials from the [CometChat Dashboard](https://app.cometchat.com): +- App ID +- Region +- Auth Key (for development) -## Add the CometChat Dependency + +**Auth Key** is for development/testing only. In production, generate **Auth Tokens** on your server using the REST API. Never expose Auth Keys in production client code. + -### NPM +## Installation - - -```js +### Package Manager + +```bash npm install @cometchat/chat-sdk-javascript ``` - - - - -Then, import the `CometChat` object wherever you want to use CometChat. +Then import wherever needed: - - -```js +```javascript import { CometChat } from "@cometchat/chat-sdk-javascript"; ``` - - - +### CDN -### HTML (via CDN) - -Include the CometChat JavaScript library in your HTML code. - - - -``` +```html ``` - +When using the CDN, `CometChat` is available as a global variable. - +## Initialization -## Initialize CometChat +The `init()` method initializes the SDK and must be called before any other CometChat method. Call it once at app startup, typically in your entry file (`index.js`, `main.js`, or `App.js`). -The `init()` method initialises the settings required for CometChat. The `init()` method takes the below parameters: -1. appID - You CometChat App ID -2. appSettings - An object of the AppSettings class can be created using the AppSettingsBuilder class. The region field is mandatory and can be set using the `setRegion()` method. + + +```typescript +let appID: string = "APP_ID"; +let region: string = "APP_REGION"; -The `AppSettings` class allows you to configure two settings: +let appSetting: CometChat.AppSettings = new CometChat.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion(region) + .autoEstablishSocketConnection(true) + .build(); -* **Region**: The region where you app was created. -* [Presence Subscription](/sdk/javascript/user-presence)**:** Represents the subscription type for user presence (real-time online/offline status) -* **autoEstablishSocketConnection(boolean value)**: This property takes a boolean value which when set to `true` informs the SDK to manage the web-socket connection internally. If set to `false` , it informs the SDK that the web-socket connection will be managed manually. The default value for this parameter is true. For more information on this, please check the [Managing Web-Socket connections manually](/sdk/javascript/managing-web-sockets-connections-manually) section. The default value for this property is **true.** -* **overrideAdminHost(adminHost: string)**: This method takes the admin URL as input and uses this admin URL instead of the default admin URL. This can be used in case of dedicated deployment of CometChat. -* **overrideClientHost(clientHost: string)**: This method takes the client URL as input and uses this client URL instead of the default client URL. This can be used in case of dedicated deployment of CometChat. +CometChat.init(appID, appSetting).then( + (initialized: boolean) => { + console.log("Initialization completed successfully", initialized); + }, + (error: CometChat.CometChatException) => { + console.log("Initialization failed with error:", error); + } +); +``` -You need to call `init()` before calling any other method from CometChat. We suggest you call the `init()` method on app startup, preferably in the `index.js` file. + - -```js +```javascript let appID = "APP_ID"; let region = "APP_REGION"; + let appSetting = new CometChat.AppSettingsBuilder() .subscribePresenceForAllUsers() .setRegion(region) .autoEstablishSocketConnection(true) .build(); + CometChat.init(appID, appSetting).then( () => { console.log("Initialization completed successfully"); @@ -103,32 +121,184 @@ CometChat.init(appID, appSetting).then( - + +```javascript +let appID = "APP_ID"; +let region = "APP_REGION"; + +let appSetting = new CometChat.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion(region) + .autoEstablishSocketConnection(true) + .build(); + +try { + await CometChat.init(appID, appSetting); + console.log("Initialization completed successfully"); +} catch (error) { + console.log("Initialization failed with error:", error); +} +``` + + + + + +Replace `APP_ID` and `APP_REGION` with your credentials from the [Dashboard](https://app.cometchat.com). + + +`CometChat.init()` must be called before any other SDK method. Calling `login()`, `sendMessage()`, or registering listeners before `init()` will fail. + + +### Parameters + +| Parameter | Type | Description | +|-----------|------|-------------| +| appID | string | Your CometChat App ID | +| appSetting | AppSettings | Configuration object built with AppSettingsBuilder | + +### AppSettings Options + +| Method | Description | Default | +|--------|-------------|---------| +| `setRegion(region)` | Region where your app was created (`us`, `eu`, `in`) | Required | +| `subscribePresenceForAllUsers()` | Subscribe to presence events for all users | — | +| `subscribePresenceForRoles(roles)` | Subscribe to presence for specific roles | — | +| `subscribePresenceForFriends()` | Subscribe to presence for friends only | — | +| `autoEstablishSocketConnection(bool)` | Let SDK manage WebSocket connections | `true` | +| `overrideAdminHost(adminHost)` | Custom admin URL (dedicated deployment) | — | +| `overrideClientHost(clientHost)` | Custom client URL (dedicated deployment) | — | +| `setStorageMode(storageMode)` | Local storage mode (`CometChat.StorageMode.SESSION` for session storage) | — | + +### Presence Subscription + +Choose how to subscribe to user presence (online/offline status): + +```javascript +// All users +new CometChat.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion(region) + .build(); + +// Specific roles +new CometChat.AppSettingsBuilder() + .subscribePresenceForRoles(["admin", "moderator"]) + .setRegion(region) + .build(); + +// Friends only +new CometChat.AppSettingsBuilder() + .subscribePresenceForFriends() + .setRegion(region) + .build(); +``` + +See [User Presence](/sdk/javascript/user-presence) for more details. + +### WebSocket Connection + +By default, the SDK manages WebSocket connections automatically. To manage them manually: + +```javascript +let appSetting = new CometChat.AppSettingsBuilder() + .setRegion(region) + .autoEstablishSocketConnection(false) + .build(); +``` + +See [Managing WebSocket Connections](/sdk/javascript/managing-web-sockets-connections-manually) for manual control. + +### Session Storage + +Use session storage instead of local storage (data clears when browser closes): + +```javascript +let appSetting = new CometChat.AppSettingsBuilder() + .setRegion(region) + .setStorageMode(CometChat.StorageMode.SESSION) + .build(); +``` + +## SSR Compatibility + +The CometChat SDK requires browser APIs (`window`, `WebSocket`) and cannot run on the server. For SSR frameworks, initialize the SDK only on the client side. + + + +Import the SDK dynamically in `useEffect`: + +```javascript +import React from "react"; + +export default function Home() { + let [ready, setReady] = React.useState(false); + + React.useEffect(() => { + window.CometChat = require("@cometchat/chat-sdk-javascript").CometChat; + setReady(true); + }, []); + + return ready ? :

Loading...

; +} +``` + +
+ + +Import the SDK in the `mounted` lifecycle hook: + +```javascript +export default { + data() { + return { ready: false }; + }, + mounted() { + window.CometChat = require("@cometchat/chat-sdk-javascript").CometChat; + this.ready = true; + } +}; +``` + + + + +Use the JavaScript SDK directly. Import and initialize in your root component: + ```typescript -let appID: string = "APP_ID", - region: string = "APP_REGION", - appSetting: CometChat.AppSettings = new CometChat.AppSettingsBuilder() +import { CometChat } from "@cometchat/chat-sdk-javascript"; + +ngOnInit() { + const appSetting = new CometChat.AppSettingsBuilder() .subscribePresenceForAllUsers() - .setRegion(region) + .setRegion("APP_REGION") .autoEstablishSocketConnection(true) .build(); -CometChat.init(appID, appSetting).then( - (initialized: boolean) => { - console.log("Initialization completed successfully", initialized); - }, - (error: CometChat.CometChatException) => { - console.log("Initialization failed with error:", error); - } -); + + CometChat.init("APP_ID", appSetting).then( + () => console.log("CometChat initialized successfully"), + (error) => console.log("CometChat initialization failed:", error) + ); +} ``` + +The dedicated Ionic Cordova SDK has been deprecated. For new Ionic/Cordova applications, use the JavaScript SDK as shown above. + +
-Make sure you replace the `APP_ID` with your CometChat **App ID** and `APP_REGION` with your **App Region** in the above code. +--- + +## Next Steps -| Parameter | Description | -| ---------- | ----------------------------------- | -| appID | CometChat App ID | -| appSetting | An object of the AppSettings class. | + + + Log in users with Auth Key or Auth Token + + + Send your first message + + diff --git a/sdk/javascript/ssr-compatibility.mdx b/sdk/javascript/ssr-compatibility.mdx new file mode 100644 index 000000000..a4b362c12 --- /dev/null +++ b/sdk/javascript/ssr-compatibility.mdx @@ -0,0 +1,279 @@ +--- +title: "SSR Compatibility" +description: "Set up the CometChat JavaScript SDK with Next.js, NuxtJS, Ionic, and other server-side rendering frameworks." +--- + +The CometChat SDK requires browser APIs (`window`, `WebSocket`) and cannot run on the server. For SSR frameworks, initialize the SDK only on the client side. + +## Next.js + +Import the SDK dynamically in `useEffect` (functional components) or `componentDidMount` (class components). + + + +```javascript +import React from "react"; +import Chat from "./Chat"; + +export default function Home() { + let [libraryImported, setLibraryImported] = React.useState(false); + + React.useEffect(() => { + window.CometChat = require("@cometchat/chat-sdk-javascript").CometChat; + setLibraryImported(true); + }, []); + + return libraryImported ? :

Loading....

; +} +``` + +
+ + +```javascript +import React from 'react'; +import Chat from './Chat'; + +export default class Home extends React.Component { + constructor(props) { + super(props); + this.state = { + libraryImported: false + }; + } + + componentDidMount() { + window.CometChat = require("@cometchat/chat-sdk-javascript").CometChat; + this.setState({ libraryImported: true }); + } + + render() { + return this.state.libraryImported ? :

Loading....

; + } +} +``` + +
+ + +```javascript +import React, { Component } from "react"; +import { COMETCHAT_CONSTANTS } from "./CONSTS"; + +export default class Chat extends Component { + constructor(props) { + super(props); + this.state = { + user: undefined, + }; + } + + componentDidMount() { + this.init(); + } + + init() { + CometChat.init( + COMETCHAT_CONSTANTS.APP_ID, + new CometChat.AppSettingsBuilder() + .setRegion(COMETCHAT_CONSTANTS.REGION) + .subscribePresenceForAllUsers() + .build() + ).then( + () => { + this.login(); + }, + (error) => { + console.log("Init failed with exception:", error); + } + ); + } + + login() { + let UID = "UID"; + CometChat.login(UID, COMETCHAT_CONSTANTS.AUTH_KEY).then( + (user) => { + this.setState({ user }); + }, + (error) => { + console.log("Login failed with exception:", error); + } + ); + } + + render() { + return this.state.user ? ( +
User logged in
+ ) : ( +
User not logged in
+ ); + } +} +``` + +
+ + +```javascript +export const COMETCHAT_CONSTANTS = { + APP_ID: "APP_ID", + REGION: "REGION", + AUTH_KEY: "AUTH_KEY", +}; +``` + + + +
+ +## NuxtJS + +Import the SDK dynamically in the `mounted` lifecycle hook. + + + +```javascript + + + +``` + + + + +```javascript + + + +``` + + + + +```javascript +module.exports = { + APP_ID: "APP_ID", + REGION: "REGION", + AUTH_KEY: "AUTH_KEY", +}; +``` + + + + + +## Ionic/Cordova + +For Ionic and Cordova applications, use the JavaScript SDK directly. Import and initialize in your root component: + +```typescript +import { Component, OnInit } from '@angular/core'; +import { CometChat } from '@cometchat/chat-sdk-javascript'; + +@Component({ + selector: 'app-root', + templateUrl: 'app.component.html', +}) +export class AppComponent implements OnInit { + + ngOnInit() { + this.initCometChat(); + } + + initCometChat() { + const appID = 'APP_ID'; + const region = 'APP_REGION'; + + const appSetting = new CometChat.AppSettingsBuilder() + .subscribePresenceForAllUsers() + .setRegion(region) + .autoEstablishSocketConnection(true) + .build(); + + CometChat.init(appID, appSetting).then( + () => { + console.log('CometChat initialized successfully'); + }, + (error) => { + console.log('CometChat initialization failed:', error); + } + ); + } +} +``` + + +The dedicated Ionic Cordova SDK has been deprecated. For new Ionic/Cordova applications, use the JavaScript SDK as shown above. Existing users can refer to the [legacy documentation](/sdk/ionic-legacy/overview). + + +--- + +## Next Steps + + + + Log in users with Auth Key or Auth Token + + + Send your first message + + diff --git a/sdk/javascript/standalone-calling.mdx b/sdk/javascript/standalone-calling.mdx index 09df82d0e..b7d438368 100644 --- a/sdk/javascript/standalone-calling.mdx +++ b/sdk/javascript/standalone-calling.mdx @@ -1,10 +1,31 @@ --- title: "Standalone Calling" +sidebarTitle: "Standalone Calling" +description: "Implement video and audio calling using only the CometChat Calls SDK without the Chat SDK. Covers authentication, token generation, session management, and call controls." --- -## Overview + -This section demonstrates how to implement calling functionality using only the CometChat Calls SDK, without requiring the Chat SDK. This is ideal for applications that need video/audio calling capabilities without the full chat infrastructure. +```javascript +// Generate call token (requires user auth token from REST API) +const callToken = await CometChatCalls.generateToken(sessionId, userAuthToken); + +// Start call session +const callSettings = new CometChatCalls.CallSettingsBuilder() + .enableDefaultLayout(true) + .setIsAudioOnlyCall(false) + .setCallListener(callListener) + .build(); +CometChatCalls.startSession(callToken.token, callSettings, htmlElement); + +// End session +CometChatCalls.endSession(); +``` + + +Standalone Calling lets you add voice and video calls using only the CometChat Calls SDK — no Chat SDK required. This is ideal when you already have your own messaging system and just need calling, or when you want the smallest possible SDK footprint. + +The key difference from the regular [Call Session](/sdk/javascript/direct-call) flow is authentication: instead of using `CometChat.getLoggedinUser()`, you obtain auth tokens directly from the CometChat REST API. Before you begin, ensure you have completed the [Calls SDK setup](/sdk/javascript/calling-setup). @@ -38,33 +59,33 @@ You can generate the token just before starting the call, or generate and store Use the `generateToken()` method to create a call token: - -```javascript -const sessionId = "UNIQUE_SESSION_ID"; // Generate a unique session ID -const userAuthToken = "USER_AUTH_TOKEN"; // Obtained from REST API + +```typescript +const sessionId: string = "UNIQUE_SESSION_ID"; // Generate a unique session ID +const userAuthToken: string = "USER_AUTH_TOKEN"; // Obtained from REST API CometChatCalls.generateToken(sessionId, userAuthToken).then( - (callToken) => { + (callToken: any) => { console.log("Call token generated:", callToken.token); // Use callToken to start the session }, - (error) => { + (error: any) => { console.log("Token generation failed:", error); } ); ``` - -```typescript -const sessionId: string = "UNIQUE_SESSION_ID"; // Generate a unique session ID -const userAuthToken: string = "USER_AUTH_TOKEN"; // Obtained from REST API + +```javascript +const sessionId = "UNIQUE_SESSION_ID"; // Generate a unique session ID +const userAuthToken = "USER_AUTH_TOKEN"; // Obtained from REST API CometChatCalls.generateToken(sessionId, userAuthToken).then( - (callToken: any) => { + (callToken) => { console.log("Call token generated:", callToken.token); // Use callToken to start the session }, - (error: any) => { + (error) => { console.log("Token generation failed:", error); } ); @@ -77,6 +98,8 @@ CometChatCalls.generateToken(sessionId, userAuthToken).then( | `sessionId` | A unique session ID for the call. Generate this yourself or use a shared ID for participants to join the same call. | | `userAuthToken` | The user auth token obtained from the CometChat REST API. | +The `Promise` resolves with an object containing a `token` property (string) that you pass to `startSession()`. + ## Start Call Session Use the `startSession()` method to join a call session. This method requires: @@ -85,16 +108,16 @@ Use the `startSession()` method to join a call session. This method requires: 3. An HTML element where the call UI will be rendered - -```javascript + +```typescript const callListener = new CometChatCalls.OngoingCallListener({ - onUserJoined: (user) => { + onUserJoined: (user: any) => { console.log("User joined:", user); }, - onUserLeft: (user) => { + onUserLeft: (user: any) => { console.log("User left:", user); }, - onUserListUpdated: (userList) => { + onUserListUpdated: (userList: any[]) => { console.log("User list updated:", userList); }, onCallEnded: () => { @@ -107,13 +130,13 @@ const callListener = new CometChatCalls.OngoingCallListener({ CometChatCalls.endSession(); // Close calling screen }, - onError: (error) => { + onError: (error: any) => { console.log("Call error:", error); }, - onMediaDeviceListUpdated: (deviceList) => { + onMediaDeviceListUpdated: (deviceList: any[]) => { console.log("Device list updated:", deviceList); }, - onUserMuted: (event) => { + onUserMuted: (event: any) => { console.log("User muted:", event); }, onScreenShareStarted: () => { @@ -122,7 +145,7 @@ const callListener = new CometChatCalls.OngoingCallListener({ onScreenShareStopped: () => { console.log("Screen sharing stopped"); }, - onCallSwitchedToVideo: (event) => { + onCallSwitchedToVideo: (event: any) => { console.log("Call switched to video:", event); }, onSessionTimeout: () => { @@ -136,20 +159,69 @@ const callSettings = new CometChatCalls.CallSettingsBuilder() .setCallListener(callListener) .build(); -const htmlElement = document.getElementById("call-container"); +const htmlElement = document.getElementById("call-container") as HTMLElement; CometChatCalls.startSession(callToken, callSettings, htmlElement); ``` - -```typescript + + +| Parameter | Description | +| -------------- | -------------------------------------------------------- | +| `callToken` | The token received from `generateToken()` onSuccess | +| `callSettings` | Object of `CallSettings` class configured via `CallSettingsBuilder` | +| `htmlElement` | DOM element where the call UI will be rendered | + +`startSession()` renders the call UI inside the provided HTML element and joins the user into the active call session. + +### Call Settings + +Configure the call experience using the following `CallSettingsBuilder` methods: + +| Method | Description | +| ------ | ----------- | +| `enableDefaultLayout(boolean)` | Enables or disables the default call UI layout with built-in controls. `true` shows the default layout. `false` hides the button layout. Default: `true` | +| `setIsAudioOnlyCall(boolean)` | Sets whether the call is audio-only or audio-video. `true` for audio-only, `false` for audio-video. Default: `false` | +| `setCallListener(OngoingCallListener)` | Sets the listener to receive call events. See [Call Listeners](#call-listeners). | +| `setMode(string)` | Sets the call UI layout mode. Available: `CometChat.CALL_MODE.DEFAULT`, `CometChat.CALL_MODE.SPOTLIGHT`. Default: `DEFAULT` | +| `startWithAudioMuted(boolean)` | Starts the call with the microphone muted. Default: `false` | +| `startWithVideoMuted(boolean)` | Starts the call with the camera turned off. Default: `false` | +| `showEndCallButton(boolean)` | Shows or hides the end call button in the default layout. Default: `true` | +| `showMuteAudioButton(boolean)` | Shows or hides the mute audio button. Default: `true` | +| `showPauseVideoButton(boolean)` | Shows or hides the pause video button. Default: `true` | +| `showScreenShareButton(boolean)` | Shows or hides the screen share button. Default: `true` | +| `showModeButton(boolean)` | Shows or hides the mode toggle button (switch between DEFAULT and SPOTLIGHT). Default: `true` | +| `showSwitchToVideoCallButton(boolean)` | Shows or hides the button to upgrade an audio call to video. Default: `true` | +| `setMainVideoContainerSetting(MainVideoContainerSetting)` | Customizes the main video container. See [Video View Customization](/sdk/javascript/video-view-customisation). | +| `setIdleTimeoutPeriod(number)` | Sets idle timeout in seconds. Warning appears 60 seconds before auto-termination. Default: `180` seconds. *v4.1.0+* | + +## Call Listeners + +The `OngoingCallListener` provides real-time callbacks for call session events, including participant changes, call state updates, and error conditions. + + +Always remove listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + + +You can register listeners in two ways: + +1. **Via CallSettingsBuilder:** Use `.setCallListener(listener)` when building call settings +2. **Via addCallEventListener:** Use `CometChatCalls.addCallEventListener(listenerId, listener)` to add multiple listeners + +Each listener requires a unique `listenerId` string. This ID is used to: +- **Prevent duplicate registrations** — Re-registering with the same ID replaces the existing listener +- **Enable targeted removal** — Remove specific listeners without affecting others + + + +```javascript const callListener = new CometChatCalls.OngoingCallListener({ - onUserJoined: (user: any) => { + onUserJoined: (user) => { console.log("User joined:", user); }, - onUserLeft: (user: any) => { + onUserLeft: (user) => { console.log("User left:", user); }, - onUserListUpdated: (userList: any[]) => { + onUserListUpdated: (userList) => { console.log("User list updated:", userList); }, onCallEnded: () => { @@ -162,13 +234,13 @@ const callListener = new CometChatCalls.OngoingCallListener({ CometChatCalls.endSession(); // Close calling screen }, - onError: (error: any) => { + onError: (error) => { console.log("Call error:", error); }, - onMediaDeviceListUpdated: (deviceList: any[]) => { + onMediaDeviceListUpdated: (deviceList) => { console.log("Device list updated:", deviceList); }, - onUserMuted: (event: any) => { + onUserMuted: (event) => { console.log("User muted:", event); }, onScreenShareStarted: () => { @@ -177,7 +249,7 @@ const callListener = new CometChatCalls.OngoingCallListener({ onScreenShareStopped: () => { console.log("Screen sharing stopped"); }, - onCallSwitchedToVideo: (event: any) => { + onCallSwitchedToVideo: (event) => { console.log("Call switched to video:", event); }, onSessionTimeout: () => { @@ -191,7 +263,7 @@ const callSettings = new CometChatCalls.CallSettingsBuilder() .setCallListener(callListener) .build(); -const htmlElement = document.getElementById("call-container") as HTMLElement; +const htmlElement = document.getElementById("call-container"); CometChatCalls.startSession(callToken, callSettings, htmlElement); ``` @@ -224,6 +296,29 @@ Configure the call experience using the following `CallSettingsBuilder` methods: | `setMainVideoContainerSetting(MainVideoContainerSetting)` | Customizes the main video container. See [Video View Customization](/sdk/javascript/video-view-customisation). | | `setIdleTimeoutPeriod(number)` | Sets idle timeout in seconds. Warning appears 60 seconds before auto-termination. Default: `180` seconds. *v4.1.0+* | +## End Call Session + +To end the call session and release all media resources (camera, microphone, network connections), call `CometChatCalls.endSession()` in the `onCallEndButtonPressed()` callback. + + + +```typescript +onCallEndButtonPressed: () => { + CometChatCalls.endSession(); + // Close the calling screen +} +``` + + +```javascript +onCallEndButtonPressed: () => { + CometChatCalls.endSession(); + // Close the calling screen +} +``` + + + ## Call Listeners The `OngoingCallListener` provides real-time callbacks for call session events, including participant changes, call state updates, and error conditions. @@ -237,19 +332,23 @@ Each listener requires a unique `listenerId` string. This ID is used to: - **Prevent duplicate registrations** — Re-registering with the same ID replaces the existing listener - **Enable targeted removal** — Remove specific listeners without affecting others + +Always remove listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + + - -```javascript -const listenerId = "UNIQUE_LISTENER_ID"; + +```typescript +const listenerId: string = "UNIQUE_LISTENER_ID"; CometChatCalls.addCallEventListener(listenerId, { - onUserJoined: (user) => { + onUserJoined: (user: any) => { console.log("User joined:", user); }, - onUserLeft: (user) => { + onUserLeft: (user: any) => { console.log("User left:", user); }, - onUserListUpdated: (userList) => { + onUserListUpdated: (userList: any[]) => { console.log("User list updated:", userList); }, onCallEnded: () => { @@ -262,13 +361,13 @@ CometChatCalls.addCallEventListener(listenerId, { CometChatCalls.endSession(); // Close calling screen }, - onError: (error) => { + onError: (error: any) => { console.log("Call error:", error); }, - onMediaDeviceListUpdated: (deviceList) => { + onMediaDeviceListUpdated: (deviceList: any[]) => { console.log("Device list updated:", deviceList); }, - onUserMuted: (event) => { + onUserMuted: (event: any) => { console.log("User muted:", event); }, onScreenShareStarted: () => { @@ -277,7 +376,7 @@ CometChatCalls.addCallEventListener(listenerId, { onScreenShareStopped: () => { console.log("Screen sharing stopped"); }, - onCallSwitchedToVideo: (event) => { + onCallSwitchedToVideo: (event: any) => { console.log("Call switched to video:", event); }, onSessionTimeout: () => { @@ -289,18 +388,18 @@ CometChatCalls.addCallEventListener(listenerId, { CometChatCalls.removeCallEventListener(listenerId); ``` - -```typescript -const listenerId: string = "UNIQUE_LISTENER_ID"; + +```javascript +const listenerId = "UNIQUE_LISTENER_ID"; CometChatCalls.addCallEventListener(listenerId, { - onUserJoined: (user: any) => { + onUserJoined: (user) => { console.log("User joined:", user); }, - onUserLeft: (user: any) => { + onUserLeft: (user) => { console.log("User left:", user); }, - onUserListUpdated: (userList: any[]) => { + onUserListUpdated: (userList) => { console.log("User list updated:", userList); }, onCallEnded: () => { @@ -313,13 +412,13 @@ CometChatCalls.addCallEventListener(listenerId, { CometChatCalls.endSession(); // Close calling screen }, - onError: (error: any) => { + onError: (error) => { console.log("Call error:", error); }, - onMediaDeviceListUpdated: (deviceList: any[]) => { + onMediaDeviceListUpdated: (deviceList) => { console.log("Device list updated:", deviceList); }, - onUserMuted: (event: any) => { + onUserMuted: (event) => { console.log("User muted:", event); }, onScreenShareStarted: () => { @@ -328,7 +427,7 @@ CometChatCalls.addCallEventListener(listenerId, { onScreenShareStopped: () => { console.log("Screen sharing stopped"); }, - onCallSwitchedToVideo: (event: any) => { + onCallSwitchedToVideo: (event) => { console.log("Call switched to video:", event); }, onSessionTimeout: () => { @@ -344,45 +443,9 @@ CometChatCalls.removeCallEventListener(listenerId); ### Events -| Event | Description | -| ----- | ----------- | -| `onCallEnded()` | Invoked when the call session terminates for a 1:1 call. Both participants receive this callback. Only fires for calls with exactly 2 participants. | -| `onSessionTimeout()` | Invoked when the call is auto-terminated due to inactivity (default: 180 seconds). Warning appears 60 seconds before. *v4.1.0+* | -| `onCallEndButtonPressed()` | Invoked when the local user clicks the end call button. Call `CometChatCalls.endSession()` to leave the session. | -| `onUserJoined(user)` | Invoked when a remote participant joins. The `user` contains UID, name, and avatar. | -| `onUserLeft(user)` | Invoked when a remote participant leaves the call session. | -| `onUserListUpdated(userList)` | Invoked whenever the participant list changes (join or leave events). | -| `onMediaDeviceListUpdated(deviceList)` | Invoked when available audio/video devices change (e.g., new microphone connected). | -| `onUserMuted(event)` | Invoked when a participant's mute state changes. Contains `muted` and `mutedBy` properties. | -| `onScreenShareStarted()` | Invoked when the local user starts sharing their screen. | -| `onScreenShareStopped()` | Invoked when the local user stops sharing their screen. | -| `onCallSwitchedToVideo(event)` | Invoked when an audio call is upgraded to a video call. Contains `sessionId`, `initiator`, and `responder`. | -| `onError(error)` | Invoked when an error occurs during the call session. | +For the full list of callbacks, their descriptions, and parameter shapes, see the [`OngoingCallListener`](/sdk/javascript/all-real-time-listeners#ongoing-call-listener-calls-sdk) reference. -## End Call Session - -To end the call session and release all media resources (camera, microphone, network connections), call `CometChatCalls.endSession()` in the `onCallEndButtonPressed()` callback. - - - -```javascript -onCallEndButtonPressed: () => { - CometChatCalls.endSession(); - // Close the calling screen -} -``` - - -```typescript -onCallEndButtonPressed: () => { - CometChatCalls.endSession(); - // Close the calling screen -} -``` - - - -## Methods +## In-Call Methods These methods are available for performing custom actions during an active call session. Use them to build custom UI controls or implement specific behaviors based on your use case. @@ -395,13 +458,13 @@ These methods can only be called when a call session is active. Toggles between the front and rear camera during a video call. Only supported on mobile browsers. - -```javascript + +```typescript CometChatCalls.switchCamera(); ``` - -```typescript + +```javascript CometChatCalls.switchCamera(); ``` @@ -419,13 +482,13 @@ Controls the local audio stream transmission. When muted, other participants can - `false` — Unmutes the microphone, resumes audio transmission - -```javascript + +```typescript CometChatCalls.muteAudio(true); ``` - -```typescript + +```javascript CometChatCalls.muteAudio(true); ``` @@ -439,13 +502,13 @@ Controls the local video stream transmission. When paused, other participants se - `false` — Resumes the camera, continues video transmission - -```javascript + +```typescript CometChatCalls.pauseVideo(true); ``` - -```typescript + +```javascript CometChatCalls.pauseVideo(true); ``` @@ -456,13 +519,13 @@ CometChatCalls.pauseVideo(true); Starts sharing your screen or a specific application window with other participants. - -```javascript + +```typescript CometChatCalls.startScreenShare(); ``` - -```typescript + +```javascript CometChatCalls.startScreenShare(); ``` @@ -473,13 +536,13 @@ CometChatCalls.startScreenShare(); Stops the current screen sharing session. - -```javascript + +```typescript CometChatCalls.stopScreenShare(); ``` - -```typescript + +```javascript CometChatCalls.stopScreenShare(); ``` @@ -494,13 +557,13 @@ Changes the call UI layout mode dynamically during the call. - `CometChat.CALL_MODE.SPOTLIGHT` — Focus on the active speaker - -```javascript + +```typescript CometChatCalls.setMode(CometChat.CALL_MODE.SPOTLIGHT); ``` - -```typescript + +```javascript CometChatCalls.setMode(CometChat.CALL_MODE.SPOTLIGHT); ``` @@ -508,17 +571,17 @@ CometChatCalls.setMode(CometChat.CALL_MODE.SPOTLIGHT); ### Get Audio Input Devices -Returns a list of available audio input devices (microphones). +Returns a list of available audio input devices (microphones). Each item is a [`MediaDeviceInfo`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo) object with `deviceId`, `label`, and `kind` properties. - -```javascript + +```typescript const audioInputDevices = CometChatCalls.getAudioInputDevices(); console.log("Available microphones:", audioInputDevices); ``` - -```typescript + +```javascript const audioInputDevices = CometChatCalls.getAudioInputDevices(); console.log("Available microphones:", audioInputDevices); ``` @@ -527,17 +590,17 @@ console.log("Available microphones:", audioInputDevices); ### Get Audio Output Devices -Returns a list of available audio output devices (speakers/headphones). +Returns a list of available audio output devices (speakers/headphones). Each item is a [`MediaDeviceInfo`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo) object. - -```javascript + +```typescript const audioOutputDevices = CometChatCalls.getAudioOutputDevices(); console.log("Available speakers:", audioOutputDevices); ``` - -```typescript + +```javascript const audioOutputDevices = CometChatCalls.getAudioOutputDevices(); console.log("Available speakers:", audioOutputDevices); ``` @@ -546,17 +609,17 @@ console.log("Available speakers:", audioOutputDevices); ### Get Video Input Devices -Returns a list of available video input devices (cameras). +Returns a list of available video input devices (cameras). Each item is a [`MediaDeviceInfo`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo) object. - -```javascript + +```typescript const videoInputDevices = CometChatCalls.getVideoInputDevices(); console.log("Available cameras:", videoInputDevices); ``` - -```typescript + +```javascript const videoInputDevices = CometChatCalls.getVideoInputDevices(); console.log("Available cameras:", videoInputDevices); ``` @@ -568,13 +631,13 @@ console.log("Available cameras:", videoInputDevices); Sets the active audio input device (microphone) by device ID. - -```javascript + +```typescript CometChatCalls.setAudioInputDevice(deviceId); ``` - -```typescript + +```javascript CometChatCalls.setAudioInputDevice(deviceId); ``` @@ -585,13 +648,13 @@ CometChatCalls.setAudioInputDevice(deviceId); Sets the active audio output device (speaker/headphones) by device ID. - -```javascript + +```typescript CometChatCalls.setAudioOutputDevice(deviceId); ``` - -```typescript + +```javascript CometChatCalls.setAudioOutputDevice(deviceId); ``` @@ -602,13 +665,13 @@ CometChatCalls.setAudioOutputDevice(deviceId); Sets the active video input device (camera) by device ID. - -```javascript + +```typescript CometChatCalls.setVideoInputDevice(deviceId); ``` - -```typescript + +```javascript CometChatCalls.setVideoInputDevice(deviceId); ``` @@ -619,13 +682,13 @@ CometChatCalls.setVideoInputDevice(deviceId); Upgrades an ongoing audio call to a video call. This enables the camera and starts transmitting video to other participants. The remote participant receives the `onCallSwitchedToVideo()` callback. - -```javascript + +```typescript CometChatCalls.switchToVideoCall(); ``` - -```typescript + +```javascript CometChatCalls.switchToVideoCall(); ``` @@ -636,14 +699,27 @@ CometChatCalls.switchToVideoCall(); Terminates the current call session and releases all media resources (camera, microphone, network connections). After calling this method, the call view should be closed. - -```javascript + +```typescript CometChatCalls.endSession(); ``` - -```typescript + +```javascript CometChatCalls.endSession(); ``` + +--- + +## Next Steps + + + + Implement ringing call flows using the Chat SDK + + + Add call recording to your voice and video calls + + diff --git a/sdk/javascript/threaded-messages.mdx b/sdk/javascript/threaded-messages.mdx index e2d29a55a..fd557ea0c 100644 --- a/sdk/javascript/threaded-messages.mdx +++ b/sdk/javascript/threaded-messages.mdx @@ -1,42 +1,36 @@ --- title: "Threaded Messages" +sidebarTitle: "Threaded Messages" +description: "Send, receive, and fetch threaded messages using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Send message in a thread +const msg = new CometChat.TextMessage("UID", "Reply", CometChat.RECEIVER_TYPE.USER); +msg.setParentMessageId(100); +await CometChat.sendMessage(msg); + +// Fetch thread messages +const threadRequest = new CometChat.MessagesRequestBuilder() + .setParentMessageId(100).setLimit(30).build(); +const messages = await threadRequest.fetchPrevious(); + +// Exclude thread replies from main conversation +const mainRequest = new CometChat.MessagesRequestBuilder() + .setUID("UID").setLimit(30).hideReplies(true).build(); +``` + -Messages that are started from a particular message are called Threaded messages or simply threads. Each Thread is attached to a message which is the Parent message for that thread. +Threaded messages (or threads) are messages started from a particular parent message. Each thread is attached to a parent message. ## Send Message in a Thread -As mentioned in the [Send a Message](/sdk/javascript/send-message) section. You can either send a message to a User or a Group based on the `receiverType` and the UID/GUID specified for the message. A message can belong to either of the below types: - -1. Text Message -2. Media Message -3. Custom Message. - -Any of the above messages can be sent in a thread. As mentioned, a thread is identified based on the Parent message. So while sending a message the `parentMessageId` must be set for the message to indicate that the message to be sent needs to be a part of the thread with the specified `parentMessageId`. - -This can be achieved using the `setParentMessageId()` method provided by the object of the `TextMessage`, `MediaMessage` and `CustomMessage` class. The id specified in the `setParentMessageId()` method maps the message sent to the particular thread. - -**Example to Send a Text Message in a thread in a user conversation.** +Any message type (Text, Media, or Custom) can be sent in a thread. Set the `parentMessageId` using `setParentMessageId()` to indicate which thread the message belongs to. - -```javascript -let textMessage = new CometChat.TextMessage(UID, "Hello", CometChat.RECEIVER_TYPE.USER); -textMessage.setParentMessageId(100); - -CometChat.sendMessage(textMessage).then( - message => { - console.log('Message sent successfully', message); - }, err => { - console.log('err', err); - } -) -``` - - - ```typescript let receiverId = "UID", @@ -57,52 +51,36 @@ CometChat.sendMessage(textMessage).then( - - -The above snippet shows how a message with the text "Hello" can be sent in the thread with `parentMessageId` 100. - -Similarly, using the `setParentMessageId()` method, Media and Custom Messages can be sent in threads too. - -### Receiving Real-Time Messages - -The procedure to receive real-time messages is exactly the same as mentioned in the [Receive Messages](/sdk/javascript/receive-message). This can be achieved using the `MessageListener` class provided by the SDK. - -To add a MessageListener, you can use the `addMessageListener()` method of the SDK. The only thing that needs to be checked is if the received message belongs to the active thread. This can be done using the `parentMessageId` field of the message object. - - - + ```javascript -var listenerID = "UNIQUE_LISTENER_ID"; -var activeThreadId = 100; +let UID = "UID"; +let textMessage = new CometChat.TextMessage(UID, "Hello", CometChat.RECEIVER_TYPE.USER); +textMessage.setParentMessageId(100); -CometChat.addMessageListener( -listenerID, -new CometChat.MessageListener({ - onTextMessageReceived: textMessage => { - if(textMessage.getParentMessageId() == activeThreadId) { - console.log("Text message received for active thread.", textMessage); - } - }, - onMediaMessageReceived: mediaMessage => { - if(mediaMessage.getParentMessageId() == activeThreadId) { - console.log("Media message received for active thread.", mediaMessage); - } - }, - onCustomMessageReceived: customMessage => { - if(customMessage.getParentMessageId() == activeThreadId) { - console.log("Custom message received for active thread.", customMessage); - } +CometChat.sendMessage(textMessage).then( + message => { + console.log('Message sent successfully', message); + }, err => { + console.log('err', err); } -}) -); +) ``` + + +The above snippet sends "Hello" in the thread with `parentMessageId` 100. Media and Custom messages can also be sent in threads using `setParentMessageId()`. + +### Receiving Real-Time Messages + +Use `MessageListener` to receive real-time thread messages. Check if the received message belongs to the active thread using `getParentMessageId()`. + + ```typescript -var listenerID: string = "UNIQUE_LISTENER_ID", - activeThreadId: number = 100; +const listenerID: string = "UNIQUE_LISTENER_ID"; +const activeThreadId: number = 100; CometChat.addMessageListener( listenerID, @@ -128,35 +106,42 @@ CometChat.addMessageListener( - - -### Fetch all the messages for any particular thread. - -You can fetch all the messages belonging to a particular thread by using the `MessagesRequest` class. In order to get an object of the `MessagesRequest` class, you need to use the `MessagesRequestBuilder` class. and use the `setParentMessageId()` method of the `MessagesRequestBuilder` to inform the SDK that you only need the messages belonging to the thread with the specified parentMessageId. - -Once you have the object of the `MessagesRequest` class, you need to call the `fetchPrevious()` method to get the latest messages in the thread. In one integration, a maximum of 100 messages can be fetched. If you wish to fetch the next set of messages, you need to call the `fetchPrevious()` method again on the same object. - - - + ```javascript -let limit = 30; -let parentMessageId = 1; -let messagesRequest = new CometChat.MessagesRequestBuilder() - .setLimit(limit) - .setParentMessageId(parentMessageId) - .build(); - -messagesRequest.fetchPrevious().then( - messages => { - console.log("Messages for thread fetched successfully", messages); - }, error => { - console.log("Message fetching failed with error:", error); +const listenerID = "UNIQUE_LISTENER_ID"; +const activeThreadId = 100; + +CometChat.addMessageListener( +listenerID, +new CometChat.MessageListener({ + onTextMessageReceived: textMessage => { + if(textMessage.getParentMessageId() == activeThreadId) { + console.log("Text message received for active thread.", textMessage); + } + }, + onMediaMessageReceived: mediaMessage => { + if(mediaMessage.getParentMessageId() == activeThreadId) { + console.log("Media message received for active thread.", mediaMessage); + } + }, + onCustomMessageReceived: customMessage => { + if(customMessage.getParentMessageId() == activeThreadId) { + console.log("Custom message received for active thread.", customMessage); + } } -); +}) +); ``` + + +### Fetch all the messages for any particular thread. + +Use `MessagesRequestBuilder` with `setParentMessageId()` to fetch messages belonging to a specific thread. Call `fetchPrevious()` to get messages (max 100 per request). + + ```typescript let limit: number = 30, @@ -177,11 +162,33 @@ messagesRequest.fetchPrevious().then( + +```javascript +let limit = 30; +let parentMessageId = 1; +let messagesRequest = new CometChat.MessagesRequestBuilder() + .setLimit(limit) + .setParentMessageId(parentMessageId) + .build(); + +messagesRequest.fetchPrevious().then( + messages => { + console.log("Messages for thread fetched successfully", messages); + }, error => { + console.log("Message fetching failed with error:", error); + } +); +``` + + + +The `fetchPrevious()` method returns an array of [`BaseMessage`](/sdk/reference/messages#basemessage) objects representing thread replies. + ## Avoid Threaded Messages in User/Group Conversations -While fetching messages for normal user/group conversations using the `MessagesRequest`, the threaded messages by default will be a part of the list of messages received. In order to exclude the threaded messages from the list of user/group messages, you need to use the `hideReplies()` method of the `MessagesRequestBuilder` class. This method takes a boolean argument which when set to true excludes the messages belonging to threads from the list of messages. +Use `hideReplies(true)` to exclude threaded messages when fetching messages for a conversation. @@ -270,4 +277,25 @@ messagesRequest.fetchPrevious().then( -The above snippet will return messages between the logged in user and `cometchat-uid-1` excluding all the threaded messages belonging to the same conversation. +The response is an array of [`BaseMessage`](/sdk/reference/messages#basemessage) objects, excluding any messages that are replies within a thread. Only top-level messages in the conversation are returned. + + +Always remove message listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + +```javascript +CometChat.removeMessageListener("UNIQUE_LISTENER_ID"); +``` + + +--- + +## Next Steps + + + + Send text, media, and custom messages to users and groups + + + Listen for incoming messages in real-time and fetch missed messages + + diff --git a/sdk/javascript/transfer-group-ownership.mdx b/sdk/javascript/transfer-group-ownership.mdx index 1de52a892..8fdfbc223 100644 --- a/sdk/javascript/transfer-group-ownership.mdx +++ b/sdk/javascript/transfer-group-ownership.mdx @@ -1,44 +1,74 @@ --- title: "Transfer Group Ownership" +sidebarTitle: "Transfer Ownership" +description: "Transfer ownership of a CometChat group to another member using the JavaScript SDK." --- + +```javascript +// Transfer group ownership +await CometChat.transferGroupOwnership("GUID", "NEW_OWNER_UID"); +``` -*In other words, as a logged-in user, how do I transfer the ownership of any group if I am the owner of the group?* +**Note:** Only the current group owner can transfer ownership. The owner must transfer ownership before leaving the group. + -In order to transfer the ownership of any group, the first condition is that you must be the owner of the group. In case you are the owner of the group, you can use the `transferGroupOwnership()` method provided by the `CometChat` class. +Transfer ownership of a group to another member. Only the current owner can do this, and since owners cannot leave their group, you must transfer ownership first if you want to leave. See [Leave Group](/sdk/javascript/leave-group). -This will be helpful as the owner is not allowed to leave the group. In case, you as the owner would like to leave the group, you will have to use this method and transfer your ownership first to any other member of the group and only then you will be allowed to leave the group. +## Transfer Ownership + +Use `transferGroupOwnership()` to transfer ownership to another group member. - -```javascript -let GUID = "GUID"; -let UID = "UID"; + +```typescript +let GUID: string = "GUID"; +let UID: string = "UID"; CometChat.transferGroupOwnership(GUID, UID).then( - () => { + (ownershipTransferred: string) => { console.log("Successfully transferred ownership of the group."); }, error => { console.log("Could not transfer ownership of the group: ", error); } -) +); ``` - -```typescript -let GUID: string = "GUID"; -let UID: string = "UID"; + +```javascript +let GUID = "GUID"; +let UID = "UID"; CometChat.transferGroupOwnership(GUID, UID).then( - (ownershipTransferred: string) => { + () => { console.log("Successfully transferred ownership of the group."); }, error => { console.log("Could not transfer ownership of the group: ", error); } -); +) ``` + +| Parameter | Description | +|-----------|-------------| +| `GUID` | The GUID of the group | +| `UID` | The UID of the member to become the new owner | + +On success, the method resolves with a success message string confirming the operation. The new owner gets admin scope and the previous owner becomes a participant. + +--- + +## Next Steps + + + + Leave a group after transferring ownership + + + Promote or demote group members + + diff --git a/sdk/javascript/transient-messages.mdx b/sdk/javascript/transient-messages.mdx index 9f44b2267..f4de3a5dc 100644 --- a/sdk/javascript/transient-messages.mdx +++ b/sdk/javascript/transient-messages.mdx @@ -1,14 +1,29 @@ --- title: "Transient Messages" +sidebarTitle: "Transient Messages" +description: "Send and receive ephemeral real-time messages that are not stored on the server using the CometChat JavaScript SDK. Ideal for live reactions and temporary indicators." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Send transient message to user +const msg = new CometChat.TransientMessage("UID", CometChat.RECEIVER_TYPE.USER, { LIVE_REACTION: "heart" }); +CometChat.sendTransientMessage(msg); + +// Listen for transient messages +CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({ + onTransientMessageReceived: (msg) => console.log("Transient:", msg) +})); +``` + Transient messages are messages that are sent in real-time only and are not saved or tracked anywhere. The receiver of the message will only receive the message if he is online and these messages cannot be retrieved later. ## Send a Transient Message -You can use the `sendTransientMessage()` method to send a transient message to a user or in a group. The receiver will receive this information in the `onTransientMessageReceived()` method of the `MessageListener` class. In order to send the transient message, you need to use the `TransientMessage` class. +You can use the `sendTransientMessage()` method to send a transient message to a user or in a group. The receiver will receive this information in the `onTransientMessageReceived()` method of the `MessageListener` class. In order to send the transient message, you need to use the [`TransientMessage`](/sdk/reference/auxiliary#transientmessage) class. @@ -61,32 +76,20 @@ CometChat.sendTransientMessage(transientMessage); +`sendTransientMessage()` returns `void` — the message is sent as a fire-and-forget operation with no response. + ## Real-time Transient Messages -*In other words, as a recipient, how do I know when someone sends a transient message?* + +Always remove listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. + You will receive the transient message in the `onTransientMessageReceived()` method of the registered `MessageListener` class. - -```javascript -let listenerId = "UNIQUE_LITENER_ID"; - -CometChat.addMessageListener( -listenerId, -new CometChat.MessageListener({ - onTransientMessageReceived: transientMessage => { - console.log('transient message received', transientMessage); - }, -}) -); -``` - - - ```typescript -let listenerId: string = "UNIQUE_LITENER_ID"; +let listenerId: string = "UNIQUE_LISTENER_ID"; CometChat.addMessageListener( listenerId, @@ -100,13 +103,35 @@ CometChat.addMessageListener( + +```javascript +let listenerId = "UNIQUE_LISTENER_ID"; + +CometChat.addMessageListener( +listenerId, +new CometChat.MessageListener({ + onTransientMessageReceived: transientMessage => { + console.log('transient message received', transientMessage); + }, +}) +); +``` + + + -The `TransientMessage` class consists of the below parameters: +The received object is a [`TransientMessage`](/sdk/reference/auxiliary#transientmessage). + +--- + +## Next Steps -| Parameter | Information | -| ---------------- | -------------------------------------------------------------------------------------------------------- | -| **sender** | An object of the User class holding all the information. related to the sender of the transient message. | -| **receiverId** | Unique Id of the receiver. This can be the Id of the group or the user the transient message is sent to. | -| **receiverType** | The type of the receiver - `CometChat.RECEIVER_TYPE.USER` or `CometChat.RECEIVER_TYPE.GROUP` | -| **data** | A JSONObject to provide data. | + + + Show real-time typing status in conversations + + + Send text, media, and custom messages + + diff --git a/sdk/javascript/troubleshooting.mdx b/sdk/javascript/troubleshooting.mdx new file mode 100644 index 000000000..ee5e2671e --- /dev/null +++ b/sdk/javascript/troubleshooting.mdx @@ -0,0 +1,196 @@ +--- +title: "Troubleshooting" +sidebarTitle: "Troubleshooting" +description: "Common failure modes and fixes for the CometChat JavaScript SDK." +--- + +Find solutions to common issues when building with the CometChat JavaScript SDK. + + + +| Issue | Fix | +|-------|-----| +| `init()` fails | Verify App ID and Region from [Dashboard](https://app.cometchat.com) | +| Login fails with "UID not found" | Create user via Dashboard or REST API first | +| SDK methods fail | Ensure `init()` completes before calling other methods | +| No real-time events | Check WebSocket connection, verify listeners registered | +| SSR errors | Use dynamic imports or `useEffect` for client-side only | + + + +--- + +## Initialization & Authentication + +| Symptom | Cause | Fix | +|---------|-------|-----| +| `init()` fails with "App ID not found" | Invalid App ID or Region | Verify credentials in [Dashboard](https://app.cometchat.com) → API & Auth Keys | +| `init()` fails silently | Missing credentials | Double-check App ID and Region are strings, not undefined | +| "CometChat not initialized" | `init()` not awaited | Ensure `init()` resolves before calling other methods | +| Login fails with "UID not found" | User doesn't exist | Create user via [Dashboard](https://app.cometchat.com) or [REST API](https://api-explorer.cometchat.com/reference/creates-user) | +| Login fails with "Auth Key is not valid" | Wrong Auth Key | Verify Auth Key in Dashboard. Don't confuse with REST API Key | +| `getLoggedinUser()` returns null | Session cleared or `init()` not called | Call `init()` on every app load before checking session | +| Auth Token expired | Token lifetime exceeded | Generate new token via [REST API](https://api-explorer.cometchat.com/reference/create-authtoken) | +| User appears offline after login | Presence not configured | Use `subscribePresenceForAllUsers()` in `AppSettingsBuilder` | + +--- + +## Messaging + +| Symptom | Cause | Fix | +|---------|-------|-----| +| `sendMessage()` fails | Not logged in or invalid receiver | Ensure `login()` completes. Verify receiver UID/GUID exists | +| Messages sent but not received | Listener not registered | Register `addMessageListener()` with `onTextMessageReceived` | +| Duplicate messages | Multiple listeners | Use unique listener IDs. Remove old listeners first | +| Wrong conversation | Wrong receiver type | Use `RECEIVER_TYPE.USER` for 1:1, `RECEIVER_TYPE.GROUP` for groups | +| Media upload fails | File too large or unsupported | Check limits. Supported: PNG, JPG, GIF, MP4, MP3, WAV | +| Metadata not appearing | Set after send | Call `setMetadata()` before the send method | +| Pagination not working | New request object | Reuse the same `MessagesRequest` for `fetchPrevious()`/`fetchNext()` | +| Thread replies in main chat | Missing filter | Add `.hideReplies(true)` to `MessagesRequestBuilder` | +| Deleted messages showing | Missing filter | Add `.hideDeletedMessages(true)` | + +--- + +## Groups + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Cannot join group | Invalid GUID | Verify GUID. Create group first if needed | +| Cannot send to group | Not a member | Join group first with `joinGroup()` | +| Cannot kick/ban members | Insufficient scope | Only admins and moderators can kick/ban | +| Can't join private group | Requires invite | Private groups require admin to add you | +| Owner can't leave | Ownership not transferred | Call `transferGroupOwnership()` first | +| Password join fails | Wrong password | Pass correct password as third parameter | +| `fetchNext()` returns same results | New request object | Reuse the same `GroupsRequest` instance | +| Scope filter returns nothing | Invalid strings | Use `"admin"`, `"moderator"`, `"participant"` | +| Status filter not working | Wrong constant | Use `CometChat.USER_STATUS.ONLINE`/`OFFLINE` | +| Cannot demote admin | Not owner | Only group owner can demote admins | +| Kicked user can still see group | Kick vs ban | Use `banGroupMember()` to prevent rejoining | + +--- + +## Calling + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Calls SDK not found | Not installed | Run `npm install @cometchat/calls-sdk-javascript` | +| No audio/video | Permissions denied | Check browser permissions for camera/microphone | +| Call not connecting | Session ID mismatch | Verify both participants use same session ID | +| One-way audio | Firewall blocking WebRTC | Check network config. Corporate networks may block WebRTC | +| Incoming call not showing | Listener not registered | Register `addCallListener()` at app root level | +| Call ended event not received | Wrong callback | Use `onCallEndedMessageReceived` in `CallListener` for call-end messages, `onCallEnded` in `OngoingCallListener` for session events | +| Black screen after joining | Element not visible | Ensure HTML element has proper dimensions | +| CSS changes not applying | Specificity issue | Try adding `!important` | +| Styles only work in one mode | Mode-specific classes | Test in `DEFAULT`, `TILE`, and `SPOTLIGHT` modes | + +--- + +## WebSocket & Connection + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Real-time events not received | WebSocket disconnected | Check `getConnectionStatus()`. Reconnect if needed | +| WebSocket fails | Firewall blocking | Check network config. Corporate firewalls may block WebSocket | +| Connection drops frequently | Network instability | Use `addConnectionListener()` to monitor and reconnect | +| Stuck in "connecting" | Network or config issue | Verify network, `appId`, and `region` | +| `featureThrottled` status | Feature rate-limited | Reduce frequency of the throttled feature. Listen for `onFeatureThrottled` in `ConnectionListener` | +| No events after login | Auto-connect disabled | Call `CometChat.connect()` manually if `autoEstablishSocketConnection(false)` | +| `connect()` doesn't work | Not logged in | Ensure user is logged in first | + +--- + +## Listeners + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Events not firing | Registered before init | Register after `init()` and `login()` complete | +| Duplicate events | Multiple listeners | Remove old listeners before adding new ones | +| Missing events after navigation | Listeners removed | Re-register when new component mounts | +| Receipt events not triggering | Receipts not sent | Call `markAsDelivered()`/`markAsRead()` explicitly | + +--- + +## Typing, Receipts & Reactions + +| Symptom | Cause | Fix | +|---------|-------|-----| +| Typing indicator stuck | `endTyping()` not called | Call on send, blur, or after 3-5s timeout | +| Double-tick not showing | `markAsDelivered()` not called | Call on message fetch and real-time receive | +| Group receipts missing | Feature not enabled | Enable "Enhanced Messaging Status" in Dashboard | +| `onMessagesDeliveredToAll`/`onMessagesReadByAll` not firing | Not registered | Register these callbacks in `MessageListener` for group-level receipt events | +| Reaction not appearing | UI not synced | Call `updateMessageWithReactionInfo()` on events | +| Duplicate reactions | No check before adding | Use `getReactedByMe()` first | + +--- + +## AI Features + +| Symptom | Cause | Fix | +|---------|-------|-----| +| AI features not appearing | Not enabled | Enable in [Dashboard](https://app.cometchat.com) → AI Features | +| AI Agents not responding | Not configured | Configure Agent in Dashboard. Agents only respond to text | +| `onAIAssistantEventReceived` not firing | Listener not registered | Register `AIAssistantListener` after login | +| Moderation always PENDING | Rules not configured | Configure rules in Dashboard → Moderation → Rules | +| Agentic messages not arriving | Wrong listener | Use `MessageListener` with `onAIAssistantMessageReceived` | + +--- + +## SSR / Framework-Specific + +| Symptom | Cause | Fix | +|---------|-------|-----| +| "window is not defined" | SDK accessed during SSR | Use dynamic imports or `useEffect` | +| Next.js SSR error | Server render | Use `await import('@cometchat/chat-sdk-javascript')` | +| Nuxt "document is not defined" | Server render | Import in `mounted()` lifecycle hook | +| React Native errors | Wrong SDK | Use `@cometchat/chat-sdk-react-native` | + +--- + +## Upgrading from V3 + +| Symptom | Cause | Fix | +|---------|-------|-----| +| "Module not found" | Old import paths | Replace `@cometchat-pro/chat` with `@cometchat/chat-sdk-javascript` | +| Calls SDK not working | Wrong package | Use `@cometchat/calls-sdk-javascript` | +| Both versions installed | Package conflict | Remove v3 package completely | + +--- + +## Error Codes + +For the complete SDK error code reference, see [Error Codes](/sdk/javascript/error-codes). Common errors you'll encounter: + +| Code | Description | Resolution | +|------|-------------|------------| +| `ERR_UID_NOT_FOUND` | User doesn't exist | Create user via Dashboard or REST API | +| `AUTH_ERR_AUTH_TOKEN_NOT_FOUND` | Invalid or expired auth token | Generate new token via REST API | +| `MISSING_PARAMETERS` | Required parameter not provided | Check method signature and pass all required params | +| `NOT_INITIALIZED` | `init()` not called | Call `CometChat.init()` before any other method | +| `USER_NOT_LOGGED_IN` | No active session | Call `login()` first | +| `ERR_GUID_NOT_FOUND` | Group doesn't exist | Create group or verify GUID | +| `ERR_NOT_A_MEMBER` | Not a group member | Join group first | +| `TOO_MANY_REQUEST` | Rate limit exceeded | See [Rate Limits](/sdk/javascript/rate-limits) | +| `FAILED_TO_FETCH` | Network issue | Check internet connection and API endpoint | +| `NO_WEBSOCKET_CONNECTION` | WebSocket disconnected | Check connection status, wait for reconnect | + +--- + +## Next Steps + + + + Installation and initialization guide + + + Recommended patterns and practices + + + Complete SDK error code reference + + + AI Agents, Moderation, and Copilot + + + Open a support ticket + + diff --git a/sdk/javascript/typing-indicators.mdx b/sdk/javascript/typing-indicators.mdx index 0174f5727..168e80244 100644 --- a/sdk/javascript/typing-indicators.mdx +++ b/sdk/javascript/typing-indicators.mdx @@ -1,16 +1,33 @@ --- title: "Typing Indicators" +sidebarTitle: "Typing Indicators" +description: "Send and receive real-time typing indicators using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Start typing indicator +const typing = new CometChat.TypingIndicator("UID", CometChat.RECEIVER_TYPE.USER); +CometChat.startTyping(typing); + +// Stop typing indicator +CometChat.endTyping(typing); + +// Listen for typing events +CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({ + onTypingStarted: (indicator) => console.log("Typing started:", indicator), + onTypingEnded: (indicator) => console.log("Typing ended:", indicator) +})); +``` + ## Send a Typing Indicator -*In other words, as a sender, how do I let the recipient(s) know that I'm typing?* - ### Start Typing -You can use the `startTyping()` method to inform the receiver that the logged in user has started typing. The receiver will receive this information in the `onTypingStarted()` method of the `MessageListener` class. In order to send the typing indicator, you need to use the `TypingIndicator` class. +Use `startTyping()` with a [`TypingIndicator`](/sdk/reference/auxiliary#typingindicator) object to notify the receiver that you're typing. @@ -59,9 +76,11 @@ CometChat.startTyping(typingNotification); +`startTyping()` returns `void` — the typing indicator is sent as a fire-and-forget operation. + ### Stop Typing -You can use the `endTyping()` method to inform the receiver that the logged in user has stopped typing. The receiver will receive this information in the `onTypingEnded()` method of the `MessageListener` class. In order to send the typing indicator, you need to use the `TypingIndicator` class. +Use `endTyping()` to notify the receiver that you've stopped typing. @@ -110,23 +129,39 @@ CometChat.endTyping(typingNotification); - -Custom Data - -You can use the `metadata` field of the `TypingIndicator` class to pass additional data along with the typing indicators. The metadata field is a JSONObject and can be set using the `setMetadata()` method of the `TypingIndicator` class. This data will be received at the receiver end and can be obtained using the `getMetadata()` method. +`endTyping()` returns `void` — the typing indicator is sent as a fire-and-forget operation. + +Use `setMetadata()` on [`TypingIndicator`](/sdk/reference/auxiliary#typingindicator) to pass additional custom data. Retrieve it with `getMetadata()` on the receiver side. ## Real-time Typing Indicators -*In other words, as a recipient, how do I know when someone is typing?* - -You will receive the typing indicators in the `onTypingStarted()` and the `onTypingEnded()` method of the registered `MessageListener` class. +Use `onTypingStarted` and `onTypingEnded` in `MessageListener` to receive typing events. - + +```typescript +let listenerId: string = "UNIQUE_LISTENER_ID"; + +CometChat.addMessageListener( + listenerId, + new CometChat.MessageListener({ + onTypingStarted: (typingIndicator: CometChat.TypingIndicator) => { + console.log("Typing started :", typingIndicator); + }, + onTypingEnded: (typingIndicator: CometChat.TypingIndicator) => { + console.log("Typing ended :", typingIndicator); + } + }) +); +``` + + + + ```javascript -let listenerId = "UNIQUE_LITENER_ID"; +let listenerId = "UNIQUE_LISTENER_ID"; CometChat.addMessageListener( listenerId, @@ -143,32 +178,27 @@ new CometChat.MessageListener({ - -```typescript -let listenerId: string = "UNIQUE_LITENER_ID"; + -CometChat.addMessageListener( - listenerId, - new CometChat.MessageListener({ - onTypingStarted: (typingIndicator: CometChat.TypingIndicator) => { - console.log("Typing started :", typingIndicator); - }, - onTypingEnded: (typingIndicator: CometChat.TypingIndicator) => { - console.log("Typing ended :", typingIndicator); - } - }) -); -``` +The received object is a [`TypingIndicator`](/sdk/reference/auxiliary#typingindicator). - + +Always remove message listeners when they're no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling. - +```javascript +CometChat.removeMessageListener("UNIQUE_LISTENER_ID"); +``` +
+ +--- -The `TypingIndicator` class consists of the below parameters: +## Next Steps -| Parameter | Information | -| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| **sender** | An object of the `User` class holding all the information. related to the sender of the typing indicator. | -| **receiverId** | Unique Id of the receiver. This can be the Id of the group or the user the typing indicator is sent to. | -| **receiverType** | This parameter indicates if the typing indicator is to be sent to a user or a group. The possible values are: 1. `CometChat.RECEIVER_TYPE.USER` 2. `CometChat.RECEIVER_TYPE.GROUP` | -| **metadata** | A JSONObject to provider additional data. | + + + Track when messages are delivered and read + + + Send ephemeral real-time messages like live reactions + + diff --git a/sdk/javascript/update-group.mdx b/sdk/javascript/update-group.mdx index 113b79f8b..e0e1996c9 100644 --- a/sdk/javascript/update-group.mdx +++ b/sdk/javascript/update-group.mdx @@ -1,41 +1,32 @@ --- title: "Update A Group" +sidebarTitle: "Update Group" +description: "Update group details such as name, type, icon, and description using the CometChat JavaScript SDK." --- + +```javascript +// Update group details +const group = new CometChat.Group("GUID", "New Name", CometChat.GROUP_TYPE.PUBLIC); +const updated = await CometChat.updateGroup(group); +``` + -## Update Group +Update a group's name, icon, description, or metadata. The GUID and group type cannot be changed after creation. See the [Group Class](/sdk/javascript/create-group#group-class) reference for all editable fields. -*In other words, as a group owner, how can I update the group details?* +## Update Group -You can update the existing details of the group using the `updateGroup()` method. +Use `updateGroup()` to modify group details. Pass a [`Group`](/sdk/reference/entities#group) object with the updated values. - -```javascript -var GUID = "GUID"; -var groupName = "Hello Group"; -var groupType = CometChat.GROUP_TYPE.PUBLIC; -var group = new CometChat.Group(GUID, groupName, groupType); - -CometChat.updateGroup(group).then( -group => { - console.log("Groups details updated successfully:", group); -}, error => { - console.log("Group details update failed with exception:", error); -} -); -``` - - - ```typescript -var GUID: string = "GUID"; -var groupName: string = "Hello Group!"; -var groupType: string = CometChat.GROUP_TYPE.PUBLIC; +const GUID: string = "GUID"; +const groupName: string = "Hello Group!"; +const groupType: string = CometChat.GROUP_TYPE.PUBLIC; -var group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType); +const group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType); CometChat.updateGroup(group).then( (group: CometChat.Group) => { @@ -48,18 +39,45 @@ CometChat.updateGroup(group).then( - + +```javascript +const GUID = "GUID"; +const groupName = "Hello Group"; +const groupType = CometChat.GROUP_TYPE.PUBLIC; +const group = new CometChat.Group(GUID, groupName, groupType); -This method takes an instance of the `Group` class as a parameter which should contain the data that you wish to update. +CometChat.updateGroup(group).then( +group => { + console.log("Groups details updated successfully:", group); +}, error => { + console.log("Group details update failed with exception:", error); +} +); +``` + + + +
-| Parameter | Description | -| --------- | ---------------------------- | -| `group` | an instance of class `Group` | +| Parameter | Description | +| --------- | ----------- | +| `group` | An instance of [`Group`](/sdk/reference/entities#group) class with updated values | -After a successful update of the group, you will receive an instance of `Group` class containing update information of the group. +On success, returns a [`Group`](/sdk/reference/entities#group) object with the updated details. -There is no real-time event listener available to receive updated group details when the `updateGroup()` method is called. To get the latest group information, you need to fetch the group details again using the appropriate method. +There is no real-time event listener for group updates. To get the latest group information after calling `updateGroup()`, fetch the group details again using `getGroup()`. -For more information on the `Group` class, please check [here](/sdk/javascript/create-group#create-a-group). +--- + +## Next Steps + + + + Permanently delete a group + + + Fetch and filter groups with pagination + + diff --git a/sdk/javascript/upgrading-from-v3.mdx b/sdk/javascript/upgrading-from-v3.mdx index 00cf6868a..ede97b67b 100644 --- a/sdk/javascript/upgrading-from-v3.mdx +++ b/sdk/javascript/upgrading-from-v3.mdx @@ -1,65 +1,65 @@ --- title: "Upgrading From V3" +description: "Migrate your CometChat JavaScript SDK integration from v3 to v4 with updated dependencies and import statements." --- + +Key changes from v3 to v4: +- Chat SDK: `npm i @cometchat/chat-sdk-javascript` +- Calls SDK: `npm i @cometchat/calls-sdk-javascript` +- Import: `import { CometChat } from '@cometchat/chat-sdk-javascript'` +- Import Calls: `import { CometChatCalls } from '@cometchat/calls-sdk-javascript'` + -## Upgrading From v3 +Upgrading from v3 to v4 is straightforward — the API surface is the same, only the package names and imports changed. Follow the [v4 setup instructions](/sdk/javascript/setup-sdk) first, then update your dependencies and imports as shown below. -Upgrading from v3.x to v4 is fairly simple. Below are the major changes that are released as a part of CometChat v4: +## Update Dependencies -Please follow the [setup](/sdk/javascript/setup-sdk) instructions to upgrade to latest version of V4 before proceeding further +| SDK | v3 Package | v4 Package | +| --- | --- | --- | +| Chat SDK | `@cometchat-pro/chat` | `@cometchat/chat-sdk-javascript` | +| Calls SDK | `@cometchat-pro/calls` | `@cometchat/calls-sdk-javascript` | -## Dependency Change - -## Chat SDK - - - ```bash -npm i @cometchat/chat-sdk-javascript -``` - - - - - -## Calls SDK +# Remove v3 packages +npm uninstall @cometchat-pro/chat @cometchat-pro/calls - - -```bash +# Install v4 packages +npm i @cometchat/chat-sdk-javascript npm i @cometchat/calls-sdk-javascript ``` - +## Update Import Statements - +Find and replace all import statements across your project: -## Change The Import Statements +| SDK | v3 Import | v4 Import | +| --- | --- | --- | +| Chat SDK | `import { CometChat } from '@cometchat-pro/chat'` | `import { CometChat } from '@cometchat/chat-sdk-javascript'` | +| Calls SDK | `import { CometChatCalls } from '@cometchat-pro/calls'` | `import { CometChatCalls } from '@cometchat/calls-sdk-javascript'` | -Change the import statements all around the project. +```javascript +// v4 Chat SDK +import { CometChat } from '@cometchat/chat-sdk-javascript'; -## Chat SDK - - - -```js -import {CometChat} from '@cometchat/chat-sdk-javascript'; +// v4 Calls SDK +import { CometChatCalls } from '@cometchat/calls-sdk-javascript'; ``` - - - + +The API methods, class names, and listener interfaces are unchanged between v3 and v4. Once you update the packages and imports, your existing code should work without further modifications. + -## Calls SDK - - - -```js -import {CometChatCalls} from '@cometchat/calls-sdk-javascript'; -``` +--- - +## Next Steps - + + + Install and configure the CometChat JavaScript SDK + + + Learn the core concepts behind CometChat v4 + + diff --git a/sdk/javascript/user-management.mdx b/sdk/javascript/user-management.mdx index 76ac2b585..74bf5fa38 100644 --- a/sdk/javascript/user-management.mdx +++ b/sdk/javascript/user-management.mdx @@ -1,183 +1,196 @@ --- title: "User Management" +sidebarTitle: "User Management" +description: "Create, update, and manage CometChat users programmatically using the JavaScript SDK. Includes user creation, profile updates, and the User class reference." --- + +```javascript +// Create a user +const user = new CometChat.User("user1"); +user.setName("Kevin"); +await CometChat.createUser(user, "AUTH_KEY"); -When a user logs into your app, you need to programmatically login the user into CometChat. But before you log in the user to CometChat, you need to create the user. +// Update a user +user.setName("Kevin Fernandez"); +await CometChat.updateUser(user, "AUTH_KEY"); -Summing up- - -**When a user registers in your app** - -1. You add the user details in your database -2. You create a user in CometChat +// Update logged-in user (no auth key needed) +await CometChat.updateCurrentUserDetails(user); +``` -**When a user logs into your app** +**Note:** User creation/deletion should ideally happen on your backend via the [REST API](https://api-explorer.cometchat.com). + -1. You log in the user to your app -2. You [log in the user in CometChat](/sdk/javascript/authentication-overview) (programmatically) +Users must exist in CometChat before they can log in. This page covers creating, updating, and deleting users. All methods that return user data return a [`User`](/sdk/reference/entities#user) object. -## Creating a user +The typical flow: +1. User registers in your app → Create user in CometChat +2. User logs into your app → [Log user into CometChat](/sdk/javascript/authentication-overview) -Ideally, user creation should take place at your backend. You can refer our Rest API to learn more about [creating a user](https://api-explorer.cometchat.com/reference/creates-user) and use the appropriate code sample based on your backend language. + +User deletion is only available via the [REST API](https://api-explorer.cometchat.com/reference/delete-user) — there is no client-side SDK method for it. + -However, if you wish to create users on the fly, you can use the `createUser()` method. This method takes a `User` object and the `Auth Key` as input parameters and returns the created `User` object if the request is successful. +## Creating a User - - -```js -let authKey = "AUTH_KEY"; -var uid = "user1"; -var name = "Kevin"; +User creation should ideally happen on your backend via the [REST API](https://api-explorer.cometchat.com/reference/creates-user). -var user = new CometChat.User(uid); - -user.setName(name); - -CometChat.createUser(user, authKey).then( - user => { - console.log("user created", user); - }, error => { - console.log("error", error); - } -) -``` + +**Security:** Never expose your `Auth Key` in client-side production code. User creation and updates using `Auth Key` should ideally happen on your backend server. Use client-side creation only for prototyping or development. + - +For client-side creation (development only), use `createUser()`: + ```typescript let authKey: string = "AUTH_KEY"; -var uid: string = "user1"; -var name: string = "Kevin"; +let uid: string = "user1"; +let name: string = "Kevin"; -var user: CometChat.User = new CometChat.User(uid); +let user: CometChat.User = new CometChat.User(uid); user.setName(name); CometChat.createUser(user, authKey).then( (user: CometChat.User) => { - console.log("user created", user); + console.log("user created", user); }, (error: CometChat.CometChatException) => { - console.log("error", error); + console.log("error", error); } ); ``` - - - - - - -UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. - - - -## Updating a user - -Updating a user similar to creating a user should ideally be achieved at your backend using the Restful APIs. For more information, you can check the [update a user](https://api-explorer.cometchat.com/reference/update-user) section. However, this can be achieved on the fly as well as using the `updateUser()` method. This method takes a `User` object and the `Auth Key` as inputs and returns the updated `User` object on the successful execution of the request. - - -```js +```javascript let authKey = "AUTH_KEY"; let uid = "user1"; -let name = "Kevin Fernandez"; +let name = "Kevin"; -var user = new CometChat.User(uid); +let user = new CometChat.User(uid); user.setName(name); -CometChat.updateUser(user, authKey).then( +CometChat.createUser(user, authKey).then( user => { - console.log("user updated", user); + console.log("user created", user); }, error => { - console.log("error", error); + console.log("error", error); } -) +) ``` - + + +Returns a [`User`](/sdk/reference/entities#user) object. See [User Class](#user-class) for all available fields. + + +UID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. + + + +## Updating a User + +Like creation, user updates should ideally happen on your backend via the [REST API](https://api-explorer.cometchat.com/reference/update-user). + +For client-side updates (development only), use `updateUser()`: + + ```typescript let authKey: string = "AUTH_KEY"; -var uid: string = "user1"; -var name: string = "Kevin Fernandez"; +let uid: string = "user1"; +let name: string = "Kevin Fernandez"; -var user: CometChat.User = new CometChat.User(uid); +let user: CometChat.User = new CometChat.User(uid); user.setName(name); CometChat.updateUser(user, authKey).then( (user: CometChat.User) => { - console.log("user updated", user); + console.log("user updated", user); }, (error: CometChat.CometChatException) => { - console.log("error", error); + console.log("error", error); } -) +) ``` - - - - -Please make sure the `User` object provided to the `updateUser()` method has the `UID` of the user to be updated set. - -## Updating logged-in user - -Updating a logged-in user is similar to updating a user. The only difference being this method does not require an AuthKey. This method takes a `User` object as input and returns the updated `User` object on the successful execution of the request. - - -```js +```javascript +let authKey = "AUTH_KEY"; let uid = "user1"; let name = "Kevin Fernandez"; -var user = new CometChat.User(uid); +let user = new CometChat.User(uid); user.setName(name); -CometChat.updateCurrentUserDetails(user).then( +CometChat.updateUser(user, authKey).then( user => { - console.log("user updated", user); + console.log("user updated", user); }, error => { - console.log("error", error); + console.log("error", error); } ) ``` - + +Ensure the [`User`](/sdk/reference/entities#user) object has the correct `UID` set. + +Returns a [`User`](/sdk/reference/entities#user) object. See [User Class](#user-class) for all available fields. + +## Updating Logged-in User + +Use `updateCurrentUserDetails()` to update the current user without an Auth Key. Note: You cannot update the user's role with this method. + + ```typescript -var uid: string = "user1"; -var name: string = "Kevin Fernandez"; +let uid: string = "user1"; +let name: string = "Kevin Fernandez"; -var user: CometChat.User = new CometChat.User(uid); +let user: CometChat.User = new CometChat.User(uid); user.setName(name); CometChat.updateCurrentUserDetails(user).then( (user: CometChat.User) => { - console.log("user updated", user); + console.log("user updated", user); }, (error: CometChat.CometChatException) => { - console.log("error", error); + console.log("error", error); } ); ``` - + +```javascript +let uid = "user1"; +let name = "Kevin Fernandez"; + +let user = new CometChat.User(uid); +user.setName(name); + +CometChat.updateCurrentUserDetails(user).then( + user => { + console.log("user updated", user); + }, error => { + console.log("error", error); + } +) +``` + -By using the `updateCurrentUserDetails()` method one can only update the logged-in user irrespective of the UID passed. Also, it is not possible to update the role of a logged-in user. +The method returns a [`User`](/sdk/reference/entities#user) object. ## Deleting a user -Deleting a user can only be achieved via the Restful APIs. For more information please check the [delete a user](https://api-explorer.cometchat.com/reference/delete-user) section. +User deletion is only available via the [REST API](https://api-explorer.cometchat.com/reference/delete-user). ## User Class @@ -195,3 +208,20 @@ Deleting a user can only be achieved via the Restful APIs. For more information | hasBlockedMe | No | A boolean that determines if the user has blocked the logged in user | | blockedByMe | No | A boolean that determines if the logged in user has blocked the user | | tags | Yes | A list of tags to identify specific users | + +## Next Steps + + + + Fetch and filter user lists with pagination. + + + Monitor real-time online/offline status. + + + Block and unblock users. + + + Log users into CometChat. + + diff --git a/sdk/javascript/user-presence.mdx b/sdk/javascript/user-presence.mdx index 503bc0d6f..6ccc2c11f 100644 --- a/sdk/javascript/user-presence.mdx +++ b/sdk/javascript/user-presence.mdx @@ -1,50 +1,50 @@ --- title: "User Presence" -sidebarTitle: "Overview" +sidebarTitle: "User Presence" +description: "Track real-time user online/offline status and configure presence subscriptions using the CometChat JavaScript SDK." --- +{/* TL;DR for Agents and Quick Reference */} + +```javascript +// Subscribe to presence during init +const appSettings = new CometChat.AppSettingsBuilder() + .subscribePresenceForAllUsers() // or .subscribePresenceForRoles([]) / .subscribePresenceForFriends() + .setRegion("REGION").build(); -User Presence helps us understand if a user is available to chat or not. +// Listen for presence changes +CometChat.addUserListener("LISTENER_ID", new CometChat.UserListener({ + onUserOnline: (user) => console.log("Online:", user), + onUserOffline: (user) => console.log("Offline:", user) +})); -## Real-time Presence +// Remove listener +CometChat.removeUserListener("LISTENER_ID"); +``` + -*In other words, as a logged-in user, how do I know if a user is online or offline?* +Track whether users are online or offline in real-time. -Based on the settings provided in the AppSettings class while initialising the SDK using the `init()` method, the logged-in user will receive the presence for the other users in the app. +## Real-time Presence -In the `AppSettings` class, you can set the type of Presence you wish to receive for that particular session of the app. +Configure presence subscription in `AppSettings` during SDK initialization. The `AppSettingsBuilder` provides three subscription options: -For presence subscription, the AppSettingsBuilder provides 3 methods : +| Method | Description | +| ------ | ----------- | +| `subscribePresenceForAllUsers()` | Receive presence updates for all users | +| `subscribePresenceForRoles(roles)` | Receive presence updates only for users with specified roles | +| `subscribePresenceForFriends()` | Receive presence updates only for friends | -* `subscribePresenceForAllUsers()` - this will inform the logged-in user when any user in the app comes online or goes offline -* `subscribePresenceForRoles(Array roles)` - This will inform the logged-in user, only when the users with the specified roles come online or go offline. -* `subscribePresenceForFriends()` - This will inform the logged-in user, only when either of his friends come online or go offline. +If none of these methods are called, no presence events will be delivered. -If none of the above methods are used, no presence will be sent to the logged-in user. + +You must configure presence subscription in `AppSettings` during `CometChat.init()` before any presence events will be delivered. See [Setup SDK](/sdk/javascript/setup-sdk) for details. + -You need to register the `UserListener` using the `addUserListener()` method where ever you wish to receive these events in. +Register a `UserListener` to receive presence events: - -``` -let listenerID = "UNIQUE_LISTENER_ID"; - -CometChat.addUserListener( -listenerID, -new CometChat.UserListener({ - onUserOnline: onlineUser => { - console.log("On User Online:", { onlineUser }); - }, - onUserOffline: offlineUser => { - console.log("On User Offline:", { offlineUser }); - } -}) -); -``` - - - ```typescript let listenerID: string = "UNIQUE_LISTENER_ID"; @@ -52,58 +52,96 @@ let listenerID: string = "UNIQUE_LISTENER_ID"; CometChat.addUserListener( listenerID, new CometChat.UserListener({ - onUserOnline: (onlineUser: CometChat.User) => { - console.log("On User Online:", { onlineUser }); - }, - onUserOffline: (offlineUser: CometChat.User) => { - console.log("On User Offline:", { offlineUser }); - } + onUserOnline: (onlineUser: CometChat.User) => { + console.log("On User Online:", { onlineUser }); + }, + onUserOffline: (offlineUser: CometChat.User) => { + console.log("On User Offline:", { offlineUser }); + } }) ); ``` - + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; +CometChat.addUserListener( + listenerID, + new CometChat.UserListener({ + onUserOnline: onlineUser => { + console.log("On User Online:", { onlineUser }); + }, + onUserOffline: offlineUser => { + console.log("On User Offline:", { offlineUser }); + } + }) +); +``` + -| Parameter | Description | -| ------------ | --------------------------------------------- | -| `listenerID` | An ID that uniquely identifies that listener. | +| Parameter | Description | +| --------- | ----------- | +| `listenerID` | Unique identifier for the listener | -You will receive an object of the `User` class in the listener methods. +Each callback receives a [`User`](/sdk/reference/entities#user) object with presence information. -We recommend you remove the listener once the activity or view is not in use. -We suggest adding this method when not in use. +Relevant fields to access on returned users: - - -``` -let listenerID = "UNIQUE_LISTENER_ID"; -CometChat.removeUserListener(listenerID); -``` +| Field | Getter | Return Type | Description | +|-------|--------|-------------|-------------| +| status | `getStatus()` | `string` | Online status of the user (`"online"` or `"offline"`) | +| lastActiveAt | `getLastActiveAt()` | `number` | Timestamp when the user was last active | - +Remove the listener when no longer needed: + ```typescript let listenerID: string = "UNIQUE_LISTENER_ID"; CometChat.removeUserListener(listenerID); ``` - - + +```javascript +let listenerID = "UNIQUE_LISTENER_ID"; +CometChat.removeUserListener(listenerID); +``` + -## User List Presence + +Always remove your `UserListener` when the component or view unmounts to prevent memory leaks and duplicate event handling. -*In other words, as a logged-in user, when I retrieve the user list, how do I know if a user is online/offline?* - -When you fetch the list of users, in the [User](/sdk/javascript/user-management#user-class) object, you will receive 2 fields - -1. `status` - This will hold either of the two values : +```javascript +CometChat.removeUserListener("LISTENER_ID"); +``` + -* online - This indicates that the user is currently online and available to chat. -* offline - This indicates that the user is currently offline and is not available to chat. +## User List Presence -2. `lastActiveAt` - in case the user is offline, this field holds the timestamp of the time when the user was last online. This can be used to display the Last seen of the user if need be. +When fetching users via `UsersRequest`, each [`User`](/sdk/reference/entities#user) object includes presence fields: + +| Field | Description | +| ----- | ----------- | +| `status` | `"online"` or `"offline"` | +| `lastActiveAt` | Timestamp of last activity (useful for "Last seen" display) | + +## Next Steps + + + + Fetch user lists with filtering and pagination. + + + Create and update users programmatically. + + + Monitor SDK connection to CometChat servers. + + + Overview of all available real-time listeners. + + diff --git a/sdk/javascript/users-overview.mdx b/sdk/javascript/users-overview.mdx index 2193624f4..a63e5b9f2 100644 --- a/sdk/javascript/users-overview.mdx +++ b/sdk/javascript/users-overview.mdx @@ -1,10 +1,37 @@ --- title: "Users" sidebarTitle: "Overview" +description: "Overview of CometChat user functionality including user management, retrieval, and presence tracking in the JavaScript SDK." --- + +- [User Management](/sdk/javascript/user-management) — Create and update users +- [Retrieve Users](/sdk/javascript/retrieve-users) — Fetch and filter user lists +- [User Presence](/sdk/javascript/user-presence) — Track online/offline status +- [Block Users](/sdk/javascript/block-users) — Block and unblock users + -The primary aim for our Users functionality is to allow you to quickly retrieve and add users to CometChat. +Every person who uses your app needs a corresponding user in CometChat. Once a user exists, you can manage their profile, fetch user lists for your UI, track who's online, and control communication with blocking. -You can begin with [user management](/sdk/javascript/user-management) to sync your users to CometChat. Once that is done, you can [retrieve users](/sdk/javascript/retrieve-users) and display them in your app. +- [User Management](/sdk/javascript/user-management) — Create users when they sign up, update profiles, and delete accounts +- [Retrieve Users](/sdk/javascript/retrieve-users) — Fetch and filter user lists with pagination, search, and role-based filtering +- [User Presence](/sdk/javascript/user-presence) — Monitor real-time online/offline status and subscribe to presence changes +- [Block Users](/sdk/javascript/block-users) — Block and unblock users to prevent all communication + +## Next Steps + + + + Create, update, and delete users in CometChat. + + + Fetch user lists with filtering, sorting, and pagination. + + + Monitor real-time online/offline status of users. + + + Block and unblock users to control communication. + + diff --git a/sdk/javascript/video-view-customisation.mdx b/sdk/javascript/video-view-customisation.mdx index 5e3880224..7d5c7179d 100644 --- a/sdk/javascript/video-view-customisation.mdx +++ b/sdk/javascript/video-view-customisation.mdx @@ -1,82 +1,77 @@ --- title: "Video View Customisation" +sidebarTitle: "Video View Customisation" description: "Customize the main video container in CometChat calls — aspect ratio, full screen button, name label, and network label positioning." --- - -**Quick Reference** + + - **Class:** `CometChat.MainVideoContainerSetting` - **Apply via:** `CallSettingsBuilder.setMainVideoContainerSetting(videoSettings)` - **Customizable elements:** Aspect ratio, full screen button, name label, network label - **Position constants:** `POSITION_TOP_LEFT`, `POSITION_TOP_RIGHT`, `POSITION_BOTTOM_LEFT`, `POSITION_BOTTOM_RIGHT` - **Requires:** [Default Calling](/sdk/javascript/default-call) or [Direct Calling](/sdk/javascript/direct-call) setup - + -This section will guide you to customise the main video container. +Customize the main video container in your call UI — aspect ratio, button positions, and label visibility. Create a `MainVideoContainerSetting` instance, configure it, and pass it to `CallSettingsBuilder.setMainVideoContainerSetting()`. -## Implementation +Before you begin, make sure you've set up [Ringing](/sdk/javascript/default-call) or [Call Session](/sdk/javascript/direct-call). -Once you have decided to implement [Default Calling](/sdk/javascript/default-call) or [Direct Calling](/sdk/javascript/direct-call) calling and followed the steps to implement them. Just few additional methods will help you quickly customize the main video container. +## MainVideoContainerSetting -Please make sure your callSettings is configured accordingly for [Default Calling](/sdk/javascript/default-call) or [Direct Calling](/sdk/javascript/direct-call). +| Method | Description | Default | +| ------ | ----------- | ------- | +| `setMainVideoAspectRatio(aspectRatio)` | Aspect ratio of the main video. Values: `ASPECT_RATIO_CONTAIN`, `ASPECT_RATIO_COVER` | `ASPECT_RATIO_CONTAIN` | +| `setFullScreenButtonParams(position, visibility)` | Position and visibility of the full screen button. | Bottom-right, visible | +| `setNameLabelParams(position, visibility, backgroundColor)` | Position, visibility, and background color of the participant name label. | Bottom-left, visible, `rgba(27, 27, 27, 0.4)` | +| `setNetworkLabelParams(position, visibility)` | Position and visibility of the network quality label. | Bottom-right, visible | -## Main Video Container Setting +### Position Constants -The `MainVideoContainerSetting` Class is the required in case you want to customise the main video view. You need to pass the Object of the `MainVideoContainerSetting` Class in the `setMainVideoContainerSetting()` method of the `CallSettingsBuilder`. +All position parameters accept one of these values from `CometChat.CallSettings`: -| Setting | Description | -| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `setMainVideoAspectRatio(aspectRatio: string)` | This method is used to set the aspect ratio of main video. The default value is **contain.**

Possible Values:
**1. CometChat.CallSettings. ASPECT\_RATIO\_CONTAIN\*\*\*\***
**2. CometChat.CallSettings. ASPECT\_RATIO\_COVER** | -| `setFullScreenButtonParams(position: string, visibility: boolean)` | This method is used to set the position & visibility parameter of the full screen button. By default the full screen button is visible in the **bottom-right** position.

Possible Values for **POSITION:**
1. **CometChat.CallSettings. POSITION\_TOP\_LEFT**
2. **CometChat.CallSettings. POSITION\_TOP\_RIGHT**
3. **CometChat.CallSettings. POSITION\_BOTTOM\_LEFT**
4. **CometChat.CallSettings. POSITION\_BOTTOM\_RIGHT**

Possible Values for **VISIBILITY:**
1. **true**
2. **false** | -| `setNameLabelParams(position: string, visibility: boolean, backgroundColor: string)` | This method is used to set the position, visibility & background color of the name label. By default the name label is visible in the **bottom-left** position with a background-color \*\*rgba(27, 27, 27, 0.4)\*\*

Possible Values for **POSITION:**
1. **CometChat.CallSettings. POSITION\_TOP\_LEFT**
2. **CometChat.CallSettings. POSITION\_TOP\_RIGHT**
3. **CometChat.CallSettings. POSITION\_BOTTOM\_LEFT**
4. **CometChat.CallSettings. POSITION\_BOTTOM\_RIGHT**

Possible Values for **VISIBILITY:**
1. **true**
2. **false** | -| `setNetworkLabelParams(position: string, visibility: boolean)` | This method is used to set the position, visibility of the network label. By default the network label is visible in the **bottom-right** position.

Possible Values for **POSITION:**
1. **CometChat.CallSettings. POSITION\_TOP\_LEFT**
2. **CometChat.CallSettings. POSITION\_TOP\_RIGHT**
3. **CometChat.CallSettings. POSITION\_BOTTOM\_LEFT**
4. **CometChat.CallSettings. POSITION\_BOTTOM\_RIGHT**

Possible Values for **VISIBILITY:**
1. **true**
2. **false** | +- `POSITION_TOP_LEFT` +- `POSITION_TOP_RIGHT` +- `POSITION_BOTTOM_LEFT` +- `POSITION_BOTTOM_RIGHT` -Example: +### Example - -```javascript -let videoSettings = new CometChat.MainVideoContainerSetting(); + +```typescript +let videoSettings: CometChat.MainVideoContainerSetting = new CometChat.MainVideoContainerSetting(); videoSettings.setMainVideoAspectRatio(CometChat.CallSettings.ASPECT_RATIO_CONTAIN); videoSettings.setFullScreenButtonParams(CometChat.CallSettings.POSITION_BOTTOM_RIGHT, true); videoSettings.setNameLabelParams(CometChat.CallSettings.POSITION_BOTTOM_LEFT, true, "rgba(27, 27, 27, 0.4)"); videoSettings.setNetworkLabelParams(CometChat.CallSettings.POSITION_BOTTOM_RIGHT, true); + +// Pass to CallSettingsBuilder +const callSettings = new CometChatCalls.CallSettingsBuilder() + .setMainVideoContainerSetting(videoSettings) + // ... other settings + .build(); ``` - -```typescript -let videoSettings: CometChat.MainVideoContainerSetting = new CometChat.MainVideoContainerSetting(); + +```javascript +let videoSettings = new CometChat.MainVideoContainerSetting(); videoSettings.setMainVideoAspectRatio(CometChat.CallSettings.ASPECT_RATIO_CONTAIN); videoSettings.setFullScreenButtonParams(CometChat.CallSettings.POSITION_BOTTOM_RIGHT, true); videoSettings.setNameLabelParams(CometChat.CallSettings.POSITION_BOTTOM_LEFT, true, "rgba(27, 27, 27, 0.4)"); videoSettings.setNetworkLabelParams(CometChat.CallSettings.POSITION_BOTTOM_RIGHT, true); + +// Pass to CallSettingsBuilder +const callSettings = new CometChatCalls.CallSettingsBuilder() + .setMainVideoContainerSetting(videoSettings) + // ... other settings + .build(); ``` - - - -| Practice | Details | -| --- | --- | -| Aspect ratio choice | Use `ASPECT_RATIO_CONTAIN` to show the full video without cropping; use `ASPECT_RATIO_COVER` for a full-bleed look that may crop edges | -| Label positioning | Avoid placing the name label and network label in the same corner to prevent overlap | -| Full screen button | Keep the full screen button visible for better UX; only hide it if your app provides its own full screen toggle | - - - - -| Symptom | Cause | Fix | -| --- | --- | --- | -| Video settings not applied | `setMainVideoContainerSetting()` not called on `CallSettingsBuilder` | Pass the `MainVideoContainerSetting` object to `CallSettingsBuilder.setMainVideoContainerSetting()` before calling `startCall()` | -| Labels overlapping | Multiple labels positioned in the same corner | Assign different position constants to each label | -| Full screen button missing | Visibility set to `false` | Set the second parameter of `setFullScreenButtonParams()` to `true` | - - - - ## Next Steps diff --git a/sdk/javascript/virtual-background.mdx b/sdk/javascript/virtual-background.mdx index 0879041c1..808ebb534 100644 --- a/sdk/javascript/virtual-background.mdx +++ b/sdk/javascript/virtual-background.mdx @@ -1,59 +1,54 @@ --- title: "Virtual Background" +sidebarTitle: "Virtual Background" description: "Implement virtual background features in CometChat video calls — background blur, custom images, and enforced backgrounds using the JavaScript SDK." --- - -**Quick Reference** + + - **Settings class:** `CometChat.VirtualBackground` - **Apply via:** `CallSettingsBuilder.setVirtualBackground(virtualBackground)` - **Toggle UI:** `CallSettingsBuilder.showVirtualBackgroundSetting(true|false)` - **Runtime control:** `CometChat.CallController.getInstance()` → `setBackgroundBlur()`, `setBackgroundImage()`, `openVirtualBackground()` - **Requires:** [Default Calling](/sdk/javascript/default-call) or [Direct Calling](/sdk/javascript/direct-call) setup - - -This section will guide you to implement virtual background feature in video calls. - -## Implementation - -Once you have decided to implement [Default Calling](/sdk/javascript/default-call) or [Direct Calling](/sdk/javascript/direct-call) calling and followed the steps to implement them. Just few additional methods will help you quickly implement virtual background. + -Please make sure your callSettings is configured accordingly for [Default Calling](/sdk/javascript/default-call) or [Direct Calling](/sdk/javascript/direct-call). +Virtual backgrounds let users blur their background or replace it with a custom image during video calls. You can configure defaults at build time via `CallSettingsBuilder`, and control them programmatically at runtime via `CallController`. -## Settings +Before you begin, make sure you've set up [Ringing](/sdk/javascript/default-call) or [Call Session](/sdk/javascript/direct-call). -The `CallSettings`class allows you to customise the overall calling experience. The properties for the call/conference can be set using the `CallSettingsBuilder` class. This will eventually give you and object of the `CallSettings` class which you can pass to the `startCall()` method to start the call. +## CallSettings Options -The **mandatory** parameters that are required to be present for any call/conference to work are: +Configure virtual background behavior when building your call settings: -1. sessionId - The unique session Id for the call/conference session. +| Setting | Description | +| ------- | ----------- | +| `showVirtualBackgroundSetting(showVBSettings: boolean)` | This method shows/hides the virtual background settings in the menu bar. **Default value = true** | +| `setVirtualBackground(virtualBackground: CometChat.VirtualBackground)` | This method will set the virtual background setting. This methods takes an Object of Virtual Background Class. | -The options available for virtual background are: +See [VirtualBackground Class](#virtualbackground-class) below for the full configuration options you can pass to `setVirtualBackground()`. -| Setting | Description | -| ---------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | -| `showVirtualBackgroundSetting(showVBSettings: boolean)` | This method shows/hides the virtual background settings in the menu bar. **Default value = true** | -| `setVirtualBackground(virtualBackground: CometChat.VirtualBackground)` | This method will set the virtual background setting. This methods takes an Object of Virtual Background Class. | +## Runtime Control -For the use case where you wish to align your own custom buttons and not use the default layout provided by CometChat, you can embed the buttons in your layout and use the below methods to perform the corresponding operations: +For custom UI without the default layout, use `CallController` to control virtual background during an active call. ### Open Virtual Background Setting You can use the `openVirtualBackground()` method to open the virtual background settings pop-up. - -```javascript -let callController = CometChat.CallController.getInstance(); -callController.openVirtualBackground(); -``` - ```typescript let callController: CometChat.CallController = CometChat.CallController.getInstance(); callController.openVirtualBackground(); ``` + +```javascript +let callController = CometChat.CallController.getInstance(); +callController.openVirtualBackground(); +``` + ### Set Background Blur @@ -61,13 +56,6 @@ callController.openVirtualBackground(); You can use the `setBackgroundBlur()` method to apply background blur on the video stream. This method accepts a number which decides the level of blur to be applied. - -```javascript -let callController = CometChat.CallController.getInstance(); -let blurLevel = 1; -callController.setBackgroundBlur(blurLevel); -``` - ```typescript let callController: CometChat.CallController = CometChat.CallController.getInstance(); @@ -75,6 +63,13 @@ let blurLevel: number = 1; callController.setBackgroundBlur(blurLevel); ``` + +```javascript +let callController = CometChat.CallController.getInstance(); +let blurLevel = 1; +callController.setBackgroundBlur(blurLevel); +``` + ### Set Background Image @@ -82,13 +77,6 @@ callController.setBackgroundBlur(blurLevel); You can use the `setBackgroundImage()`method to set the background image. This method takes either a URL or file Object & sets that image as the background. - -```javascript -let callController = CometChat.CallController.getInstance(); -let imageURL = "URL_OF_BACKGROUND_IMAGE"; -callController.setBackgroundImage(imageURL); -``` - ```typescript let callController: CometChat.CallController = CometChat.CallController.getInstance(); @@ -96,11 +84,18 @@ let imageURL: string = "URL_OF_BACKGROUND_IMAGE"; callController.setBackgroundImage(imageURL); ``` + +```javascript +let callController = CometChat.CallController.getInstance(); +let imageURL = "URL_OF_BACKGROUND_IMAGE"; +callController.setBackgroundImage(imageURL); +``` + -## Virtual Background Settings +## VirtualBackground Class -The `VirtualBackground` Class is the required in case you want to change how the end user can use virtual background features in a video call. You need to pass the Object of the `VirtualBackground` Class in the `setVirtualBackground()` method of the `CallSettingsBuilder`. +The `VirtualBackground` class controls how users interact with virtual background features. Pass a `VirtualBackground` instance to `setVirtualBackground()` in `CallSettingsBuilder`. | Setting | Description | | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | @@ -111,30 +106,6 @@ The `VirtualBackground` Class is the required in case you want to change how the | `enforceBackgroundBlur(enforceBackgroundBlur: number)` | This method starts the call with background blurred. To blur the background you need to pass an integer value between 1-99 which decides the blur level. **Default = 0** | | `enforceBackgroundImage(enforceBackgroundImage: string)` | This methods starts the call with the provided background image. | - - - -| Practice | Details | -| --- | --- | -| Blur level range | Use values between 1-99 for `enforceBackgroundBlur()`. Higher values produce stronger blur. A value of 0 disables blur | -| Image hosting | Host background images on a CDN for fast loading. Large images may cause lag when applied | -| Enforce vs allow | Use `enforceBackgroundBlur()` or `enforceBackgroundImage()` when you want a mandatory background (e.g., for privacy). Use `allowBackgroundBlur()` and `allowUserImages()` to let users choose | -| Custom buttons | Use `CallController` methods (`setBackgroundBlur`, `setBackgroundImage`, `openVirtualBackground`) when building a custom UI instead of the default CometChat menu | - - - - -| Symptom | Cause | Fix | -| --- | --- | --- | -| Virtual background option not visible | `showVirtualBackgroundSetting(false)` was set | Set `showVirtualBackgroundSetting(true)` in `CallSettingsBuilder` | -| Background blur not applied on call start | `enforceBackgroundBlur()` not set or set to 0 | Pass a value between 1-99 to `enforceBackgroundBlur()` | -| Custom images not appearing | `setImages()` not called or empty array passed | Pass a non-empty array of valid image URLs to `setImages()` | -| `CallController.getInstance()` returns null | Called before the call has started | Only use `CallController` methods after `startCall()` has been invoked | -| User can't upload their own images | `allowUserImages(false)` was set | Set `allowUserImages(true)` in the `VirtualBackground` configuration | - - - - ## Next Steps diff --git a/sdk/reference/auxiliary.mdx b/sdk/reference/auxiliary.mdx new file mode 100644 index 000000000..59a49e3a3 --- /dev/null +++ b/sdk/reference/auxiliary.mdx @@ -0,0 +1,136 @@ +--- +title: "Auxiliary" +sidebarTitle: "Auxiliary" +description: "Class reference for auxiliary objects returned by CometChat SDK methods. Covers MessageReceipt, Reaction, ReactionCount, ReactionEvent, TypingIndicator, TransientMessage, Attachment, and CometChatException." +--- + +This page documents the auxiliary classes used across all CometChat SDKs. These objects are returned by listener callbacks and specific SDK methods. + +All properties are accessed via getter methods. + +--- + +## MessageReceipt + +`MessageReceipt` represents a delivery or read receipt for a message. It is received via the `onMessagesDelivered` and `onMessagesRead` listener callbacks. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| messageId | `getMessageId()` | `string` | ID of the message this receipt is for | +| sender | `getSender()` | `User` | The user who triggered the receipt | +| receiver | `getReceiver()` | `string` | UID or GUID of the receiver | +| receiverType | `getReceiverType()` | `string` | Receiver type (`"user"` or `"group"`) | +| receiptType | `getReceiptType()` | `string` | Type of receipt (`"read"`, `"delivery"`, `"readByAll"`, or `"deliveredToAll"`) | +| timestamp | `getTimestamp()` | `string` | Timestamp of the receipt | +| readAt | `getReadAt()` | `number` | Timestamp when the message was read (epoch seconds) | +| deliveredAt | `getDeliveredAt()` | `number` | Timestamp when the message was delivered (epoch seconds) | + +--- + +## Reaction + +`Reaction` represents an individual user's reaction on a message. It is returned by `fetchNext()` and `fetchPrevious()` on a `ReactionsRequest` object, and is also available within a `ReactionEvent`. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| id | `getReactionId()` | `string` | Unique identifier for this reaction | +| messageId | `getMessageId()` | `string` | ID of the message this reaction belongs to | +| reaction | `getReaction()` | `string` | The reaction emoji (e.g., `"😀"`, `"👍"`) | +| uid | `getUid()` | `string` | UID of the user who reacted | +| reactedAt | `getReactedAt()` | `number` | Timestamp when the reaction was added (epoch seconds) | +| reactedBy | `getReactedBy()` | `User` | The user who added this reaction | + +--- + +## ReactionCount + +`ReactionCount` represents the count of a specific reaction on a message. It is available via `getReactions()` on a `BaseMessage` object. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| reaction | `getReaction()` | `string` | The reaction emoji (e.g., `"😀"`, `"👍"`) | +| count | `getCount()` | `number` | Number of users who reacted with this reaction | +| reactedByMe | `getReactedByMe()` | `boolean` | Whether the logged-in user reacted with this reaction | + +--- + +## ReactionEvent + +`ReactionEvent` represents a real-time reaction event. It is received via the `onMessageReactionAdded` and `onMessageReactionRemoved` listener callbacks. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| reaction | `getReaction()` | `Reaction` | The reaction details object | +| receiverId | `getReceiverId()` | `string` | UID or GUID of the receiver | +| receiverType | `getReceiverType()` | `string` | Receiver type (`"user"` or `"group"`) | +| conversationId | `getConversationId()` | `string` | ID of the conversation | +| parentMessageId | `getParentMessageId()` | `string` | ID of the parent message (if the reacted message is in a thread) | + +--- + +## TypingIndicator + +`TypingIndicator` represents a typing event. It is received via the `onTypingStarted` and `onTypingEnded` listener callbacks. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| receiverId | `getReceiverId()` | `string` | UID or GUID of the receiver | +| receiverType | `getReceiverType()` | `string` | Receiver type (`"user"` or `"group"`) | +| sender | `getSender()` | `User` | The user who is typing | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the typing indicator | + +--- + +## TransientMessage + +`TransientMessage` represents a transient (non-persistent) message. It is received via the `onTransientMessageReceived` listener callback. Transient messages are not stored on the server. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| receiverId | `getReceiverId()` | `string` | UID or GUID of the receiver | +| receiverType | `getReceiverType()` | `string` | Receiver type (`"user"` or `"group"`) | +| sender | `getSender()` | `User` | The user who sent the transient message | +| data | `getData()` | `any` | Custom data payload of the transient message | + +--- + +## Attachment + +`Attachment` represents a file attachment on a `MediaMessage`. It is available via `getAttachment()` or `getAttachments()` on a `MediaMessage` object. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| name | `getName()` | `string` | Name of the file | +| extension | `getExtension()` | `string` | File extension (e.g., `"png"`, `"pdf"`) | +| mimeType | `getMimeType()` | `string` | MIME type of the file (e.g., `"image/png"`) | +| size | `getSize()` | `number` | Size of the file in bytes | +| url | `getUrl()` | `string` | URL to download the file | + +--- + +## CometChatException + +`CometChatException` represents an error from the Chat SDK. It is received in rejection callbacks of promise-based SDK methods (e.g., `sendMessage`, `login`, `getGroup`). + +### Properties + +| Property | Type | Description | +|----------|------|-------------| +| `code` | `string` \| `number` | Error code identifying the type of error | +| `name` | `string` | Name/category of the error | +| `message` | `string` | Human-readable error description | +| `details` | `string` | Additional error details | diff --git a/sdk/reference/calls.mdx b/sdk/reference/calls.mdx new file mode 100644 index 000000000..07fbf5689 --- /dev/null +++ b/sdk/reference/calls.mdx @@ -0,0 +1,143 @@ +--- +title: "Calls" +sidebarTitle: "Calls" +description: "Class reference for objects returned by the CometChat Calls SDK. Covers CallLog, CallUser, CallGroup, Participant, Recording, OngoingCallListener, CometChatCallsException, and MediaDeviceInfo." +--- + +This page documents the classes used in the CometChat Calls SDK. These objects are returned by call log queries and call session methods. + +All properties on proper classes are accessed via getter methods. Listener event objects are plain objects accessed via dot notation. + +--- + +## CallLog + +`CallLog` represents a call history entry. It is returned by `CallLogRequestBuilder.fetchNext()` and `CallLogRequestBuilder.fetchPrevious()`. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| sessionId | `getSessionID()` | `string` | Unique session ID of the call | +| totalAudioMinutes | `getTotalAudioMinutes()` | `number` | Total audio minutes in the call | +| totalVideoMinutes | `getTotalVideoMinutes()` | `number` | Total video minutes in the call | +| totalDuration | `getTotalDuration()` | `string` | Total duration of the call as a formatted string | +| totalDurationInMinutes | `getTotalDurationInMinutes()` | `number` | Total duration in minutes | +| hasRecording | `getHasRecording()` | `boolean` | Whether the call has a recording | +| initiatedAt | `getInitiatedAt()` | `number` | Timestamp when the call was initiated | +| endedAt | `getEndedAt()` | `number` | Timestamp when the call ended | +| callCategory | `getCallCategory()` | `string` | Category of the call (`"call"` or `"meet"`) | +| type | `getType()` | `string` | Type of the call (`"audio"` or `"video"`) | +| status | `getStatus()` | `string` | Status of the call (`"ongoing"`, `"ended"`, `"cancelled"`, etc.) | +| receiverType | `getReceiverType()` | `string` | Receiver type (`"user"` or `"group"`) | +| totalParticipants | `getTotalParticipants()` | `number` | Total number of participants | +| mid | `getMid()` | `string` | Message ID associated with the call | +| initiator | `getInitiator()` | [`CallUser`](#calluser) | The user who initiated the call | +| receiver | `getReceiver()` | [`CallUser`](#calluser) \| [`CallGroup`](#callgroup) | The user or group that received the call | +| participants | `getParticipants()` | [`Participant[]`](#participant) | Array of call participants | +| recordings | `getRecordings()` | [`Recording[]`](#recording) | Array of call recordings | + +--- + +## CallUser + +`CallUser` represents a user within the Calls SDK context (used in `CallLog` as initiator/receiver). + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| uid | `getUid()` | `string` | Unique user ID | +| name | `getName()` | `string` | Display name of the user | +| avatar | `getAvatar()` | `string` | URL of the user's avatar | + +--- + +## CallGroup + +`CallGroup` represents a group within the Calls SDK context (used in `CallLog` as receiver for group calls). + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| guid | `getGuid()` | `string` | Unique group ID | +| name | `getName()` | `string` | Display name of the group | +| icon | `getIcon()` | `string` | URL of the group icon | + +--- + +## Participant + +`Participant` represents a user who participated in a call. It is available via `getParticipants()` on a [`CallLog`](#calllog) object. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| uid | `getUid()` | `string` | Unique user ID | +| name | `getName()` | `string` | Display name of the participant | +| avatar | `getAvatar()` | `string` | URL of the participant's avatar | +| totalAudioMinutes | `getTotalAudioMinutes()` | `number` | Audio minutes for this participant | +| totalVideoMinutes | `getTotalVideoMinutes()` | `number` | Video minutes for this participant | +| totalDurationInMinutes | `getTotalDurationInMinutes()` | `number` | Total duration in minutes for this participant | +| deviceID | `getDeviceID()` | `string` | Device ID of the participant | +| hasJoined | `getHasJoined()` | `boolean` | Whether the participant joined the call | +| joinedAt | `getJoinedAt()` | `number` | Timestamp when the participant joined | +| leftAt | `getLeftAt()` | `number` | Timestamp when the participant left | +| mid | `getMid()` | `string` | Media ID of the participant | +| state | `getState()` | `string` | State of the participant | + +--- + +## Recording + +`Recording` represents a call recording. It is available via `getRecordings()` on a [`CallLog`](#calllog) object. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| rid | `getRid()` | `string` | Unique recording ID | +| recordingUrl | `getRecordingURL()` | `string` | URL to download the recording | +| startTime | `getStartTime()` | `number` | Timestamp when the recording started | +| endTime | `getEndTime()` | `number` | Timestamp when the recording ended | +| duration | `getDuration()` | `number` | Duration of the recording in seconds | + +--- + +## OngoingCallListener + +For the full `OngoingCallListener` documentation including all callbacks and parameter shapes, see the [All Real-Time Listeners](/sdk/javascript/all-real-time-listeners#ongoing-call-listener-calls-sdk) page. + +--- + +## CometChatCallsException + +`CometChatCallsException` represents an error from the Calls SDK. It is received in the `onError` callback of `OngoingCallListener` and in rejection callbacks of Calls SDK promise-based methods. + +### Properties + +| Property | Type | Description | +|----------|------|-------------| +| `code` | `string` \| `number` | Error code identifying the type of error | +| `name` | `string` | Name/category of the error | +| `message` | `string` | Human-readable error description | +| `details` | `string` | Additional error details | + +--- + +## MediaDeviceInfo + +`MediaDeviceInfo` represents an audio or video device available on the user's system. It is returned by `getAudioInputDevices()`, `getAudioOutputDevices()`, and `getVideoInputDevices()` methods, and also received in the `onMediaDeviceListUpdated` callback. + +These objects follow the browser's [MediaDeviceInfo](https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo) interface shape. + +### Properties + +| Property | Type | Description | +|----------|------|-------------| +| `deviceId` | `string` | Unique device identifier | +| `kind` | `string` | Device type (`"videoinput"`, `"audioinput"`, or `"audiooutput"`) | +| `label` | `string` | Human-readable device name | +| `groupId` | `string` | Group identifier for related devices (e.g., a headset's mic and speaker share a group) | diff --git a/sdk/reference/entities.mdx b/sdk/reference/entities.mdx new file mode 100644 index 000000000..2d5588aa8 --- /dev/null +++ b/sdk/reference/entities.mdx @@ -0,0 +1,109 @@ +--- +title: "Entities" +sidebarTitle: "Entities" +description: "Class reference for entity objects returned by CometChat SDK methods. Covers User, Group, Conversation, and GroupMember." +--- + +This page documents the entity classes used across all CometChat SDKs. All entity objects share the same structure regardless of platform. + +All properties are accessed via getter methods. + +--- + +## User + +`User` represents a CometChat user. It is returned by methods like `CometChat.login()`, `CometChat.getUser()`, and user list requests. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| uid | `getUid()` | `string` | Unique user ID | +| name | `getName()` | `string` | Display name of the user | +| avatar | `getAvatar()` | `string` | URL of the user's avatar image | +| link | `getLink()` | `string` | URL link associated with the user | +| role | `getRole()` | `string` | Role assigned to the user | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the user | +| status | `getStatus()` | `string` | Online status of the user (`"online"` or `"offline"`) | +| statusMessage | `getStatusMessage()` | `string` | Custom status message set by the user | +| lastActiveAt | `getLastActiveAt()` | `number` | Timestamp when the user was last active (epoch seconds) | +| tags | `getTags()` | `string[]` | Tags associated with the user | +| deactivatedAt | `getDeactivatedAt()` | `number` | Timestamp when the user was deactivated (epoch seconds) | + +### Conditional Properties + +These properties may or may not be populated depending on the method or request configuration used. + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| hasBlockedMe | `getHasBlockedMe()` | `boolean` | Whether this user has blocked the logged-in user | +| blockedByMe | `getBlockedByMe()` | `boolean` | Whether the logged-in user has blocked this user | +| authToken | `getAuthToken()` | `string` | Auth token of the user (only available after login) | + +--- + +## Group + +`Group` represents a CometChat group. It is returned by methods like `CometChat.createGroup()`, `CometChat.getGroup()`, and group list requests. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| guid | `getGuid()` | `string` | Unique group ID | +| name | `getName()` | `string` | Display name of the group | +| type | `getType()` | `string` | Group type (`"public"`, `"private"`, or `"password"`) | +| icon | `getIcon()` | `string` | URL of the group icon | +| description | `getDescription()` | `string` | Description of the group | +| owner | `getOwner()` | `string` | UID of the group owner | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the group | +| createdAt | `getCreatedAt()` | `number` | Timestamp when the group was created (epoch seconds) | +| updatedAt | `getUpdatedAt()` | `number` | Timestamp when the group was last updated (epoch seconds) | +| hasJoined | `getHasJoined()` | `boolean` | Whether the logged-in user has joined this group | +| scope | `getScope()` | `string` | Scope of the logged-in user in the group (`"admin"`, `"moderator"`, or `"participant"`) | +| joinedAt | `getJoinedAt()` | `string` | Timestamp when the logged-in user joined the group | +| membersCount | `getMembersCount()` | `number` | Total number of members in the group | +| tags | `getTags()` | `string[]` | Tags associated with the group | + +### Conditional Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| password | `getPassword()` | `string` | Group password (only for password-protected groups, only available during creation) | +| isBanned | `isBannedFromGroup()` | `boolean` | Whether the logged-in user is banned from the group | + +--- + +## Conversation + +`Conversation` represents a chat conversation. It is returned by `CometChat.getConversation()` and conversation list requests. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| conversationId | `getConversationId()` | `string` | Unique conversation ID | +| conversationType | `getConversationType()` | `string` | Type of conversation (`"user"` or `"group"`) | +| lastMessage | `getLastMessage()` | `BaseMessage` | The last message in the conversation | +| conversationWith | `getConversationWith()` | `User` \| `Group` | The user or group this conversation is with | +| unreadMessageCount | `getUnreadMessageCount()` | `number` | Number of unread messages in the conversation | +| unreadMentionsCount | `getUnreadMentionsCount()` | `number` | Number of unread mentions in the conversation | +| lastReadMessageId | `getLastReadMessageId()` | `string` | ID of the last message read by the logged-in user | +| latestMessageId | `getLatestMessageId()` | `string` | ID of the latest message in the conversation | +| tags | `getTags()` | `string[]` | Tags associated with the conversation | + +--- + +## GroupMember + +`GroupMember` extends [User](#user) and represents a member of a CometChat group. It is returned by group member list requests. + +It inherits all properties from `User` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| scope | `getScope()` | `string` | Scope of the member in the group (`"admin"`, `"moderator"`, or `"participant"`) | +| joinedAt | `getJoinedAt()` | `number` | Timestamp when the member joined the group (epoch seconds) | +| guid | `getGuid()` | `string` | GUID of the group this member belongs to | diff --git a/sdk/reference/messages.mdx b/sdk/reference/messages.mdx new file mode 100644 index 000000000..d2331149e --- /dev/null +++ b/sdk/reference/messages.mdx @@ -0,0 +1,323 @@ +--- +title: "Messages" +sidebarTitle: "Messages" +description: "Class reference for message objects returned by CometChat SDK methods. Covers BaseMessage and its subclasses like TextMessage." +--- + +This page documents the message classes used across all CometChat SDKs. All message objects share the same structure regardless of platform. + +All properties are accessed via getter methods. + +## Class Hierarchy + +``` +BaseMessage +├── TextMessage +├── MediaMessage +├── CustomMessage +├── InteractiveMessage +└── Action +``` + +--- + +## BaseMessage + +`BaseMessage` is the base class for all message types. Every message object — whether it's a text message, media message, or custom message — extends this class. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| id | `getId()` | `number` | Unique message ID | +| conversationId | `getConversationId()` | `string` | ID of the conversation this message belongs to | +| parentMessageId | `getParentMessageId()` | `number` | ID of the parent message (for threaded messages) | +| muid | `getMuid()` | `string` | Client-generated unique message ID | +| sender | `getSender()` | `User` | Sender of the message | +| receiver | `getReceiver()` | `User` \| `Group` | Receiver of the message | +| receiverId | `getReceiverId()` | `string` | UID/GUID of the receiver | +| type | `getType()` | `string` | Message type (e.g., `"text"`, `"image"`, `"file"`, `"custom"`) | +| receiverType | `getReceiverType()` | `string` | Receiver type (`"user"` or `"group"`) | +| category | `getCategory()` | `MessageCategory` | Message category (e.g., `"message"`, `"action"`, `"call"`, `"custom"`) | +| sentAt | `getSentAt()` | `number` | Timestamp when the message was sent (epoch seconds) | +| deliveredAt | `getDeliveredAt()` | `number` | Timestamp when the message was delivered | +| readAt | `getReadAt()` | `number` | Timestamp when the message was read | +| deliveredToMeAt | `getDeliveredToMeAt()` | `number` | Timestamp when the message was delivered to the logged-in user | +| readByMeAt | `getReadByMeAt()` | `number` | Timestamp when the message was read by the logged-in user | +| editedAt | `getEditedAt()` | `number` | Timestamp when the message was edited | +| editedBy | `getEditedBy()` | `string` | UID of the user who edited the message | +| deletedAt | `getDeletedAt()` | `number` | Timestamp when the message was deleted | +| deletedBy | `getDeletedBy()` | `string` | UID of the user who deleted the message | +| replyCount | `getReplyCount()` | `number` | Number of replies to this message | +| unreadRepliesCount | `getUnreadRepliesCount()` | `number` | Number of unread replies | +| data | `getData()` | `Object` | Raw data payload of the message | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the message | +| rawMessage | `getRawMessage()` | `Object` | Raw JSON of the message as received from the server | + +### Conditional Properties + +These properties may or may not be populated depending on the method or request configuration used to fetch the message. + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| reactions | `getReactions()` | `ReactionCount[]` | Array of reaction counts on the message | +| mentionedUsers | `getMentionedUsers()` | `User[]` | Array of users mentioned in the message | +| hasMentionedMe | `hasMentionedMe()` | `boolean` | Whether the logged-in user was mentioned in the message | +| quotedMessageId | `getQuotedMessageId()` | `number` | ID of the quoted message (if this is a reply) | +| quotedMessage | `getQuotedMessage()` | `BaseMessage` | The quoted message object (if this is a reply) | + +--- + +## TextMessage + +`TextMessage` extends [BaseMessage](#basemessage) and represents a text-based chat message. + +It inherits all properties from `BaseMessage` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| text | `getText()` | `string` | The text content of the message | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the message (includes extension data like data masking, link preview, etc.) | +| data | `getData()` | `Object` | Raw data payload including `text`, `resource`, `metadata`, `moderation`, and `entities` | +| moderationStatus | `getModerationStatus()` | `ModerationStatus` | Moderation status of the message. Returns `"unmoderated"` if not moderated. | + +### Conditional Properties + +These properties may or may not be populated depending on the method or request configuration used to fetch the message. + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags associated with the message | + +--- + +## MediaMessage + +`MediaMessage` extends [BaseMessage](#basemessage) and represents a message with media attachments such as images, videos, audio files, or documents. + +It inherits all properties from `BaseMessage` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| caption | `getCaption()` | `string` | Caption text for the media message | +| attachment | `getAttachment()` | `Attachment` | The primary attachment of the media message | +| attachments | `getAttachments()` | `Attachment[]` | All attachments of the media message | +| url | `getURL()` | `string` | URL of the media file | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the message | +| data | `getData()` | `Object` | Raw data payload of the message | +| moderationStatus | `getModerationStatus()` | `ModerationStatus` | Moderation status of the message. Returns `"unmoderated"` if not moderated. | + +### Conditional Properties + +These properties may or may not be populated depending on the method or request configuration used to fetch the message. + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags associated with the message | + +--- + +## CustomMessage + +`CustomMessage` extends [BaseMessage](#basemessage) and represents a developer-defined message with a custom data payload. + +It inherits all properties from `BaseMessage` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| customData | `getCustomData()` | `Object` | The custom data payload set by the developer | +| subType | `getSubType()` | `string` | Sub type of the custom message | +| conversationText | `getConversationText()` | `string` | Preview text displayed in the conversation list | +| updateConversation | `willUpdateConversation()` | `boolean` | Whether this message updates the conversation's last message | +| sendNotification | `willSendNotification()` | `boolean` | Whether a push notification is sent for this message | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the message | +| data | `getData()` | `Object` | Raw data payload of the message | + +### Conditional Properties + +These properties may or may not be populated depending on the method or request configuration used to fetch the message. + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags associated with the message | + +--- + +## InteractiveMessage + +`InteractiveMessage` extends [BaseMessage](#basemessage) and represents a message with interactive UI elements such as forms, cards, or buttons. + +It inherits all properties from `BaseMessage` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| interactiveData | `getInteractiveData()` | `Object` | The interactive element data (form fields, buttons, etc.) | +| interactionGoal | `getInteractionGoal()` | `InteractionGoal` | The goal that defines when the interaction is considered complete | +| interactions | `getInteractions()` | `Interaction[]` | Array of interactions that have occurred on this message | +| allowSenderInteraction | `getIsSenderInteractionAllowed()` | `boolean` | Whether the sender is allowed to interact with the message | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the message | +| data | `getData()` | `Object` | Raw data payload of the message | + +### Conditional Properties + +These properties may or may not be populated depending on the method or request configuration used to fetch the message. + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| tags | `getTags()` | `string[]` | Tags associated with the message | + +--- + +## Action + +`Action` extends [BaseMessage](#basemessage) and represents a system-generated action message such as a member joining, leaving, or being banned from a group. + +It inherits all properties from `BaseMessage` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| action | `getAction()` | `string` | The action being performed (e.g., `"joined"`, `"left"`, `"kicked"`, `"banned"`) | +| message | `getMessage()` | `string` | The default human-readable action message | +| actionBy | `getActionBy()` | `User` \| `Group` \| `BaseMessage` | The entity that performed the action | +| actionOn | `getActionOn()` | `User` \| `Group` \| `BaseMessage` | The entity on which the action was performed | +| actionFor | `getActionFor()` | `User` \| `Group` \| `BaseMessage` | The entity for whom the action was performed | +| oldScope | `getOldScope()` | `string` | Previous scope of the member (for scope change actions) | +| newScope | `getNewScope()` | `string` | New scope of the member (for scope change actions) | +| rawData | `getRawData()` | `Object` | Raw JSON data of the action message | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the action message | + +--- + +## Call + +`Call` extends [BaseMessage](#basemessage) and represents a voice or video call message. + +It inherits all properties from `BaseMessage` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| sessionId | `getSessionId()` | `string` | Unique session ID of the call | +| callInitiator | `getCallInitiator()` | [`User`](/sdk/reference/entities#user) | The user who initiated the call | +| callReceiver | `getCallReceiver()` | [`User`](/sdk/reference/entities#user) \| [`Group`](/sdk/reference/entities#group) | The user or group receiving the call | +| action | `getAction()` | `string` | The call action (e.g., `"initiated"`, `"ongoing"`, `"ended"`, `"cancelled"`, `"rejected"`) | +| initiatedAt | `getInitiatedAt()` | `number` | Timestamp when the call was initiated | +| joinedAt | `getJoinedAt()` | `number` | Timestamp when the call was joined | +| rawData | `getRawData()` | `Object` | Raw JSON data of the call message | +| metadata | `getMetadata()` | `Object` | Custom metadata attached to the call message | + +--- + +## AIAssistantMessage + +`AIAssistantMessage` extends [BaseMessage](#basemessage) and represents the full assistant reply persisted after an AI Agent run completes. It is received via the `onAIAssistantMessageReceived` callback of the [MessageListener](/sdk/javascript/all-real-time-listeners#message-listener). + +It inherits all properties from `BaseMessage` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| assistantMessageData | `getAssistantMessageData()` | `AIAssistantMessageData` | The assistant message data containing runId, threadId, and text | + +### AIAssistantMessageData + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| runId | `getRunId()` | `string` | The run ID of the agent execution | +| threadId | `getThreadId()` | `string` | The thread ID of the conversation | +| text | `getText()` | `string` | The full text of the assistant's reply | + +--- + +## AIToolResultMessage + +`AIToolResultMessage` extends [BaseMessage](#basemessage) and represents the output of a tool call made during an AI Agent run. It is received via the `onAIToolResultReceived` callback of the [MessageListener](/sdk/javascript/all-real-time-listeners#message-listener). + +It inherits all properties from `BaseMessage` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| toolResultMessageData | `getToolResultMessageData()` | `AIToolResultMessageData` | The tool result data containing runId, threadId, text, and toolCallId | + +### AIToolResultMessageData + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| runId | `getRunId()` | `string` | The run ID of the agent execution | +| threadId | `getThreadId()` | `string` | The thread ID of the conversation | +| text | `getText()` | `string` | The text output of the tool | +| toolCallId | `getToolCallId()` | `string` | The ID of the tool call this result belongs to | + +--- + +## AIToolArgumentMessage + +`AIToolArgumentMessage` extends [BaseMessage](#basemessage) and represents the arguments passed to a tool during an AI Agent run. It is received via the `onAIToolArgumentsReceived` callback of the [MessageListener](/sdk/javascript/all-real-time-listeners#message-listener). + +It inherits all properties from `BaseMessage` and adds the following. + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| toolArgumentMessageData | `getToolArgumentMessageData()` | `AIToolArgumentMessageData` | The tool argument data containing runId, threadId, and toolCalls | + +### AIToolArgumentMessageData + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| runId | `getRunId()` | `string` | The run ID of the agent execution | +| threadId | `getThreadId()` | `string` | The thread ID of the conversation | +| toolCalls | `getToolCalls()` | `AIToolCall[]` | Array of tool calls with their arguments | + +### AIToolCall + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| id | `getId()` | `string` | Unique tool call ID | +| type | `getType()` | `string` | Type of the tool call | +| displayName | `getDisplayName()` | `string` | Display name of the tool | +| executionText | `getExecutionText()` | `string` | Execution text for the tool | +| function | `getFunction()` | `AIToolCallFunction` | The function details (name and arguments) | + +### AIToolCallFunction + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| name | `getName()` | `string` | Name of the function being called | +| arguments | `getArguments()` | `string` | JSON string of the arguments passed to the function | + +--- + +## AIAssistantBaseEvent + +`AIAssistantBaseEvent` is the base class for all real-time streaming events from an AI Agent run. It is received via the `onAIAssistantEventReceived` callback of the [AIAssistantListener](/sdk/javascript/all-real-time-listeners#ai-assistant-listener). + +The `type` field identifies the specific event (e.g., `"run_started"`, `"tool_call_started"`, `"text_message_content"`, `"run_finished"`). + +### Properties + +| Property | Getter | Return Type | Description | +|----------|--------|-------------|-------------| +| type | `getType()` | `string` | The event type identifier | +| conversationId | `getConversationId()` | `string` | The conversation ID associated with the event | +| messageId | `getMessageId()` | `string` | The message ID associated with the event | +| parentMessageId | `getParentMessageId()` | `string` | The parent message ID (if in a thread) | +| data | `getData()` | `object` | Additional event-specific data | +| data.timestamp | `getTimestamp()` | `number` | Timestamp of the event | +| data.runId | `getRunId()` | `string` | The run ID of the agent execution | +| data.threadId | `getThreadId()` | `string` | The thread ID of the conversation | diff --git a/ui-kit/android/v4/overview.mdx b/ui-kit/android/v4/overview.mdx index 25c5e7a82..a9f7d2e3f 100644 --- a/ui-kit/android/v4/overview.mdx +++ b/ui-kit/android/v4/overview.mdx @@ -2,18 +2,18 @@ title: "Overview" --- -With CometChat's UI Kit for Android, you can effortlessly build a chat app equipped with all the essential messaging features, along with customizable options tailored to your application requirements. This UI Kit comprises prebuilt UI components organized into smaller modules and components, each configurable to meet your specific needs. +With CometChat's **UI Kit** for Android, you can effortlessly build a chat app equipped with all the essential messaging features, along with customizable options tailored to your application requirements. This **UI Kit** comprises prebuilt UI components organized into smaller modules and components, each configurable to meet your specific needs. -[Android Sample App](https://github.com/cometchat-pro/cometchat-chat-sample-app-android-java/tree/v4) + -## Before Getting Started +#### **Before Getting Started** Before you begin, it's essential to grasp the fundamental concepts and features offered by CometChat's APIs, SDK, and UI Kit. You can find detailed information in [Key Concepts](/fundamentals/key-concepts) documentation. -The UI Kit library comprises pre-built Android Views for effortless integration and is built on top of the [Android Chat SDK](/sdk/android/overview). Installing it will also include the core Chat SDK. +The **UI Kit** library comprises pre-built Android Views for effortless integration and is built on top of the [Android Chat SDK](/sdk/android/overview). Installing it will also include the core Chat SDK. To ensure the best possible start and to familiarize yourself with our platform, we encourage you to begin by reviewing our [Getting Started](/ui-kit/android/v4/getting-started) guide. This comprehensive guide will provide you with all the essential information and steps you need to seamlessly navigate through our system.