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 5000
milliseconds (5 seconds) during the Chat SDK initialization. The startTyping()
method substracts 1000
milliseconds (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(): Promise<any>
Input
This method doesn't take any parameters.
Output
Type | Description |
---|---|
Promise<any> | Returned object with a value of any type. |
Basic usage
Start a typing indicator on the support
channel.
// reference the channel where you want to listen to typing signals
const channel = await chat.getChannel("support")
// invoke the "startTyping()" method
await channel.startTyping()
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(): Promise<any>
Input
This method doesn't take any parameters.
Output
Type | Description |
---|---|
Promise<any> | Returned object with a value of any type. |
Basic usage
Stop a typing indicator on the support
channel.
// reference the channel where you want to listen to typing signals
const channel = await chat.getChannel("support")
// invoke the "stopTyping()" method
await channel.stopTyping()
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: string[]
) => unknown
): () => void
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
callback | n/a | Yes | n/a | Callback function passed as a parameter. It defines the custom behavior to be executed whenever someone starts/stops typing. |
→ typingUserIds | string[] | Yes | n/a | Array with a list of all currently typing user IDs (id ). |
Output
Type | Description |
---|---|
() => void | Function you can call to disconnect (unsubscribe) from the channel and stop receiving signal events. |
Basic usage
Get a list of user IDs currently typing on the support
channel.
// reference the channel where you want to listen to typing signals
const channel = await chat.getChannel("support")
// invoke the "getTyping()" method
channel.getTyping((data) => {
console.log("These channel members are currently typing: ", data)
})
Other examples
Stop receiving signals on the support
channel.
const channel = await chat.getChannel("support")
const stopUpdates = channel.getTyping(/* handle typing callback */)
// after some time...
stopUpdates()