Quick Reference — Send and listen for typing indicators:
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 thestartTyping() 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. To send the typing indicator, you need to use the TypingIndicator class.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
Stop Typing
You can use theendTyping() 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. To send the typing indicator, you need to use the TypingIndicator class.
- To User
- To Group
- TypeScript (User)
- TypeScript (Group)
Custom DataYou 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.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 theonTypingStarted() and the onTypingEnded() method of the registered MessageListener class.
- JavaScript
- TypeScript
TypingIndicator class consists of the below parameters:
| 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 provide additional data. |
Best Practices
Best Practices
- Debounce start typing calls — Avoid calling
startTyping()on every keystroke. Instead, debounce the call so it fires once when the user begins typing and doesn’t repeat until after a short pause. - Call endTyping() explicitly — Always call
endTyping()when the user clears the input field, sends a message, or navigates away from the chat screen. - Use unique listener IDs — Each screen or component that registers a
MessageListenershould use a distinctlistenerIdto avoid conflicts. - Handle metadata sparingly — Only attach
metadatato typing indicators when you have a concrete use case (e.g., indicating which thread the user is typing in).
Troubleshooting
Troubleshooting
- Typing indicator not appearing for the recipient — Verify that the recipient has registered a
MessageListenerwithonTypingStartedandonTypingEndedcallbacks before the sender starts typing. - Typing indicator stuck in “typing” state — Ensure
endTyping()is called when the user stops typing or sends a message. CometChat automatically times out typing indicators after a short period, but explicitly ending them provides a better user experience. - Listener not firing — Confirm that the
listenerIdused inaddMessageListeneris unique and that the listener has not been removed prematurely. - Indicators not working in groups — Make sure you are using
CometChat.RECEIVER_TYPE.GROUPwith the correct group ID (GUID), not a user ID.