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 subtracts 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() async throws -> Timetoken
Input
This method doesn't take any parameters.
Output
Parameter | Description |
---|---|
Timetoken | A Timetoken value indicating the action timestamp. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Start a typing indicator on the support
channel.
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
try await channel.startTyping()
} else {
debugPrint("Channel 'support' not found")
}
}
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() async throws -> Timetoken
Input
This method doesn't take any parameters.
Output
Parameter | Description |
---|---|
Timetoken | A Timetoken value indicating the action timestamp. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Stop a typing indicator on the support
channel.
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
try await channel.stopTyping()
} else {
debugPrint("Stopped typing indicator on the 'support' channel")
}
}
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()
returns an asynchronous stream which produces a new event whenever someone starts/stops typing.
This method takes the following parameters:
channel.getTyping() -> AsyncStream<[String]>
Input
This method doesn't take any parameters.
Output
Parameter | Description |
---|---|
AsyncStream<[String]> | An asynchronous stream that emits an array of users currently typing. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
- AsyncStream
- Closure
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
for await typingUsers in channel.getTyping() {
debugPrint("Typing users: \(typingUsers)")
}
} else {
debugPrint("Channel 'support' not found")
}
}
// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
// to receive updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
// and no further items will be produced. You can also stop receiving updates manually
// by calling the "close()" method on the "AutoCloseable" object.
// Assuming you have a reference of type "ChannelImpl" named "channel"
autoCloseable = channel.getTyping { typingUsers in
debugPrint("Typing users: \(typingUsers)")
}