The Typing interface provides methods for sending and receiving typing indicators in a chat room. Access it via room.typing.
1
val typing = room.typingProperties
The Typing interface has the following properties:
currentSet<String>Start typing
suspend typing.keystroke(): UnitSends a typing started event to notify other users that the current user is typing.
Events are throttled according to the heartbeatThrottle room option to prevent excessive network traffic. If called within the throttle interval, the operation becomes a no-op. Multiple rapid calls are serialized to maintain consistency.
The room must be attached and the connection must be in the Connected state.
1
2
// Call this when the user starts typing
room.typing.keystroke()Returns
Unit
This is a suspend function. It completes when the typing event has been sent, or throws a ChatException on failure.
Stop typing
suspend typing.stop(): UnitSends a typing stopped event to notify other users that the current user has stopped typing.
If the user is not currently typing, this operation is a no-op. Multiple rapid calls are serialized to maintain consistency.
The room must be attached and the connection must be in the Connected state.
1
2
// Call this when the user stops typing or clears the input
room.typing.stop()Returns
Unit
This is a suspend function. It completes when the stop event has been sent, or throws a ChatException on failure.
Subscribe to typing events
typing.subscribe(listener: TypingListener): SubscriptionSubscribes to typing events from users in the chat room. Receives updates whenever a user starts or stops typing, providing real-time feedback about who is currently composing messages.
The room must be attached to receive typing events.
1
2
3
4
5
6
val subscription = room.typing.subscribe { event ->
println("Currently typing: ${event.currentlyTyping}")
}
// To stop receiving typing events
subscription.unsubscribe()Parameters
The subscribe() method takes the following parameters:
listenerrequiredTypingSetEventReturns
Returns a Subscription object.
Unsubscribe from typing events
subscription.unsubscribe(): UnitCall unsubscribe() to stop receiving typing events.
Collect typing events as a Flow
Typing.asFlow(): Flow<TypingSetEvent>Returns a Kotlin Flow that emits typing events. This is an extension function on the Typing interface that provides a reactive alternative to listener-based subscriptions.
1
2
3
4
5
6
7
import kotlinx.coroutines.launch
launch {
room.typing.asFlow().collect { event ->
println("Currently typing: ${event.currentlyTyping}")
}
}Returns
Flow<TypingSetEvent>
Returns a Flow that emits TypingSetEvent events. The flow automatically manages the underlying subscription.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import com.ably.chat.asFlow
import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.seconds
val room = chatClient.rooms.get("my-room") {
typing {
heartbeatThrottle = 5.seconds // Throttle typing events to every 5 seconds
}
}
room.attach()
// Subscribe to typing events with a listener
val subscription = room.typing.subscribe { event ->
val typingUsers = event.currentlyTyping
when {
typingUsers.isEmpty() -> println("No one is typing")
typingUsers.size == 1 -> println("${typingUsers.first()} is typing...")
else -> println("${typingUsers.joinToString(", ")} are typing...")
}
}
// Or use Flow for reactive collection
launch {
room.typing.asFlow().collect { event ->
println("Typing users: ${event.currentlyTyping}")
}
}
// Notify that the current user is typing
room.typing.keystroke()
// Notify that the current user has stopped typing
room.typing.stop()
// Check who is currently typing
println("Currently typing: ${room.typing.current}")
// Clean up
subscription.unsubscribe()