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(
completion: ((Swift.Result<Void, Error>) -> Void)? = nil
)
Input
This method doesn't take any parameters.
Output
Type | Description |
---|---|
Void | Success (or failure for Error ) of the operation that is reported through the completion handler. |
Basic usage
Start a typing indicator on the support
channel.
/// Assuming you have a "chat" instance available
/// Get the channel instance for "support"
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
/// Channel found, proceed to start the typing indicator
debugPrint("Fetched channel with ID: \(channel.id)")
/// Call "startTyping()" to show typing indicator in the channel
channel.startTyping { result in
switch result {
case .success:
debugPrint("Started typing indicator on the 'support' channel")
case let .failure(error):
show all 26 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:
channel.stopTyping(
completion: ((Swift.Result<Void, Error>) -> Void)? = nil
)
Input
This method doesn't take any parameters.
Output
Type | Description |
---|---|
Void | Success (or failure for Error ) of the operation that is reported through the completion handler. |
Basic usage
Stop a typing indicator on the support
channel.
/// Assuming you have a "chat" instance available
/// Get the channel instance for "support"
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
/// Channel found, proceed to stop the typing indicator
debugPrint("Fetched channel with ID: \(channel.id)")
/// Call "stopTyping()" to stop typing indicator in the channel
channel.stopTyping { result in
switch result {
case .success:
debugPrint("Stopped typing indicator on the 'support' channel")
case let .failure(error):
show all 26 linesGet 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: @escaping (([String]) -> Void)
) -> AutoCloseable
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
callback | @escaping (([String]) -> Void) | Yes | 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. |
This method returns the list of user IDs currently typing (through completion).
Basic usage
Get a list of user IDs currently typing on the support
channel.
/// Assuming you have a "chat" instance available
/// Get the channel instance for "support"
chat?.getChannel(channelId: "support") { in
switch $0 {
case let .success(channel):
if let channel = channel {
/// Channel found, proceed to get the list of typing users
debugPrint("Fetched channel with ID: \(channel.id)")
/// Call "getTyping()" to get the list of user IDs currently typing on the channel
channel.getTyping { typingUsers in
debugPrint("Typing users: \(typingUsers)")
}
} else {
show all 21 linesOther examples
Stop receiving signals on the support
channel.
/// Assuming you have a `chat` instance available
/// Variable to store the AutoCloseable reference
var typingCloseable: AutoCloseable?
/// Get the channel instance for "support"
chat?.getChannel(channelId: "support") { [weak self] result in
switch result {
case let .success(channel):
if let channel = channel {
/// Channel found, proceed to get the list of typing users
debugPrint("Fetched channel with ID: \(channel.id)")
/// Start receiving typing signals
self?.typingCloseable = channel.getTyping { typingUsers in
debugPrint("Typing users: \(typingUsers)")
show all 34 lines