Typing indicator
Typing indicators show users when someone is composing a message. This feature:
- Increases engagement - Users see activity in group chats
- Sets expectations - Users know when to expect a response in 1:1 conversations
Not available for public chats
Typing indicator is disabled in public chats. If you try implementing this feature in a public channel type, you'll get the Typing indicators are not supported in Public chats error.
Start typing
startTyping() activates the typing indicator on a channel.
The method uses a debounce mechanism: signals are sent at intervals rather than on every keystroke. The default timeout is 5000 ms (5 seconds), with a 1000 ms buffer to prevent rapid re-triggering.
Custom timeout
Set a custom timeout with the typingTimeout parameter during initialization.
Method signature
This method has the following signature:
1channel.startTyping(): PNFuture<PNPublishResult?>
Input
This method doesn't take any parameters.
Output
| Type | Description |
|---|---|
PNFuture<PNPublishResult?> | Function that returns an instance of PNFuture that will be completed with a PNPublishResult when the startTyping operation is completed, or null if no signal was sent (e.g., the typing indicator was already active). |
Sample code
Start a typing indicator on the support channel.
1// assume `chat` is an instance of your chat service or client
2// get the "support" channel asynchronously
3chat.getChannel("support").async { result ->
4 result.onSuccess { supportChannel ->
5 // handle success
6 // start the typing indicator on the "support" channel
7 supportChannel.startTyping().async { typingResult ->
8 typingResult.onSuccess {
9 // typing indicator started successfully
10 println("Typing indicator started on 'support' channel.")
11 }.onFailure {
12 // handle failure
13 }
14 }
15 }.onFailure {
show all 18 linesStop typing
stopTyping() deactivates a typing indicator on a given channel.
You can use this method in cases when you want to disable the typing indicator immediately - for example, when a user deletes a previously drafted message - without waiting for the typingTimeout to end.
Method signature
This method has the following signature:
1channel.stopTyping(): PNFuture<PNPublishResult?>
Input
This method doesn't take any parameters.
Output
| Type | Description |
|---|---|
PNFuture<PNPublishResult?> | Function that returns an instance of PNFuture that will be completed with a PNPublishResult when the stopTyping operation is completed, or null if no signal was sent (e.g., the typing indicator was already inactive). |
Sample code
Stop a typing indicator on the support channel.
1// assume `chat` is an instance of your chat service or client
2// get the "support" channel asynchronously
3chat.getChannel("support").async { result ->
4 result.onSuccess { supportChannel ->
5 // handle success
6 // stop the typing indicator on the "support" channel
7 supportChannel.stopTyping().async { stopTypingResult ->
8 stopTypingResult.onSuccess {
9 // typing indicator stopped successfully
10 println("Typing indicator stopped on 'support' channel.")
11 }.onFailure {
12 // handle failure
13 }
14 }
15 }.onFailure {
show all 18 linesGet typing events
onTypingChanged() adds a signal events listener to get all events of type typing. Run it once on a given channel to start listening to typing signals. You can also use it to get a list of typing user IDs. This method also returns an AutoCloseable you can invoke to stop receiving signal events and unsubscribe from the channel.
Deprecated method
getTyping() is deprecated. Use onTypingChanged() instead. Both methods have the same signature and behavior.
Method signature
onTypingChanged() accepts a callback function as an argument. The Chat SDK invokes this callback whenever someone starts/stops typing. This function takes a single argument of the list of currently typing user IDs.
This method takes the following parameters:
1channel.onTypingChanged(callback: (typingUserIds: Collection<String>) -> Unit): AutoCloseable
Input
| Parameter | Description |
|---|---|
callback *Type: (typingUserIds: Collection<String>) -> UnitDefault: n/a | Callback function passed as a parameter. It defines the custom behavior to be executed whenever a collection of user IDs starts/stops typing. |
Output
| Type | Description |
|---|---|
AutoCloseable | Interface you can call to disconnect (unsubscribe) from the channel and stop receiving signal events for someone typing by invoking the close() method. |
Sample code
Get a list of user IDs currently typing on the support channel.
1// assume `chat` is an instance of your chat service or client
2// get the "support" channel asynchronously
3chat.getChannel("support").async { result ->
4 result.onSuccess { supportChannel ->
5 // handle success
6 // get the list of user IDs currently typing on the "support" channel
7 val typingSubscription = supportChannel.onTypingChanged { typingUserIds ->
8 if (typingUserIds.isNotEmpty()) {
9 println("Users currently typing on 'support' channel: ${typingUserIds.joinToString(", ")}")
10 } else {
11 println("No users are typing on 'support' channel.")
12 }
13 }
14 }.onFailure {
15 // handle failure
show all 17 linesOther examples
Stop receiving signals on the support channel.
1// assume `chat` is an instance of your chat service or client
2// get the "support" channel asynchronously
3chat.getChannel("support").async { result ->
4 result.onSuccess { supportChannel ->
5 // handle success
6 // get the list of user IDs currently typing on the "support" channel
7 val typingSubscription = supportChannel.onTypingChanged { typingUserIds ->
8 if (typingUserIds.isNotEmpty()) {
9 println("Users currently typing on 'support' channel: ${typingUserIds.joinToString(", ")}")
10 } else {
11 println("No users are typing on 'support' channel.")
12 }
13 }
14
15 // later, when you want to stop receiving typing signals:
show all 22 lines