Typing indicator

The typing indicator feature provides real-time feedback when someone is composing a message, enhancing the conversational flow. It helps in the following:

  • Active participation - within group chats or collaborative environments, a user can measure others' engagement levels based on typing indicators, encouraging active involvement and minimizing communication gaps.

  • Anticipating responses - when engaged in 1:1 conversations, a user can see if the other person is actively responding, helping manage expectations and reducing uncertainty regarding response times.

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 a typing indicator on a given channel.

The method sets a flag (typingSent) to indicate that a typing signal is in progress and adds a timer to reset the flag after a specified timeout.

The Chat SDK has a default typing timeout set to 5 seconds during the Chat SDK initialization. The startTyping() method substracts 1 second from the default timeout to cause an intentional delay between typing signals ("debounce" period).

This debounce mechanism ensures that the system responds to a user's actual typing behavior without being overly sensitive to minor fluctuations, keypresses, or temporary pauses in typing. Thanks to that, the typing indicator reflects more deliberate typing actions rather than every keystroke.

Configure default timeout

You can change the default typing timeout and set your own value during the Chat SDK configuration (init() method) using the typingTimeout parameter.

Method signature

This method has the following signature:

channel.startTyping(): PNFuture<Unit>

Input

This method doesn't take any parameters.

Output

TypeDescription
PNFuture<Unit>Function that returns an instance of PNFuture that will be completed with Unit when the startTyping operation is completed.

Basic usage

Start a typing indicator on the support channel.

// assume `chat` is an instance of your chat service or client
// get the "support" channel asynchronously
chat.getChannel("support").async { result ->
result.onSuccess { supportChannel ->
// handle success
// start the typing indicator on the "support" channel
supportChannel.startTyping().async { typingResult ->
typingResult.onSuccess {
// typing indicator started successfully
println("Typing indicator started on 'support' channel.")
}.onFailure {
// handle failure
}
}
}.onFailure {
show all 18 lines

Stop 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:

channel.stopTyping(): PNFuture<Unit>

Input

This method doesn't take any parameters.

Output

TypeDescription
PNFuture<Unit>Function that returns an instance of PNFuture that will be completed with Unit when the stopTyping operation is completed.

Basic usage

Stop a typing indicator on the support channel.

// assume `chat` is an instance of your chat service or client
// get the "support" channel asynchronously
chat.getChannel("support").async { result ->
result.onSuccess { supportChannel ->
// handle success
// stop the typing indicator on the "support" channel
supportChannel.stopTyping().async { stopTypingResult ->
stopTypingResult.onSuccess {
// typing indicator stopped successfully
println("Typing indicator stopped on 'support' channel.")
}.onFailure {
// handle failure
}
}
}.onFailure {
show all 18 lines

Get typing events

getTyping() adds a signal events listener underneath 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 a function you can invoke to stop receiving signal events and unsubscribe from the channel.

Method signature

getTyping() 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:

channel.getTyping(callback: (typingUserIds: Collection<String>) -> Unit): AutoCloseable

Input

ParameterTypeRequiredDefaultDescription
callback(typingUserIds: Collection<String>) -> UnitYesn/aCallback function passed as a parameter. It defines the custom behavior to be executed whenever a collection of user IDs starts/stops typing.

Output

TypeDescription
AutoCloseableInterface you can call to disconnect (unsubscribe) from the channel and stop receiving signal events for someone typing by invoking the close() method.

Basic usage

Get a list of user IDs currently typing on the support channel.

// assume `chat` is an instance of your chat service or client
// get the "support" channel asynchronously
chat.getChannel("support").async { result ->
result.onSuccess { supportChannel ->
// handle success
// get the list of user IDs currently typing on the "support" channel
val typingSubscription = supportChannel.getTyping { typingUserIds ->
if (typingUserIds.isNotEmpty()) {
println("Users currently typing on 'support' channel: ${typingUserIds.joinToString(", ")}")
} else {
println("No users are typing on 'support' channel.")
}
}
}.onFailure {
// handle failure
show all 17 lines

Other examples

Stop receiving signals on the support channel.

// assume `chat` is an instance of your chat service or client
// get the "support" channel asynchronously
chat.getChannel("support").async { result ->
result.onSuccess { supportChannel ->
// handle success
// get the list of user IDs currently typing on the "support" channel
val typingSubscription = supportChannel.getTyping { typingUserIds ->
if (typingUserIds.isNotEmpty()) {
println("Users currently typing on 'support' channel: ${typingUserIds.joinToString(", ")}")
} else {
println("No users are typing on 'support' channel.")
}
}

// later, when you want to stop receiving typing signals:
show all 22 lines
Last updated on