Typing indicator
Typing indicators enable you to indicate if users are typing messages in a channel. This feature is easy to implement and can significantly improve the experience for your users.
You can trigger a typing event when a user starts typing a message. Other users in the channel receive these events in real time to show typing indicators on the screen. Applications usually display a typing indicator for a few seconds before it expires, or continue display it if the user is still typing. The app hides the indicator when it receives the actual message.
For example, you could follow a pattern like this:
- Send a
typing_on
event when the user starts typing. - Send another
typing_on
event if the user is still typing after 10 to 15 seconds. - Send a
typing_off
event if the user removes the message they were typing.
Send typing on/off events
Use the signal
method to send transient events to indicate that a user is typing a message or has stopped typing.
By default, signals are limited to a message payload size of 64 bytes. This limit applies only to the payload, and not to the URI or headers. If you require a larger payload size, contact support. For more details on working with signals, refer to Sending Signals.
- JavaScript
- Swift
- Java
- Unity
pubnub.signal({
message: 'typing_on',
channel: 'ch-1'
}, (status, response) => {
// handle status, response
});
pubnub.signal(
channel: "ch-1",
message: "typing_on"
) { result in
switch result {
case let .success(response):
print("Successful Response: \(response)")
case let .failure(error):
print("Failed Response: \(error.localizedDescription)")
}
}
pubnub.signal()
.message("typing_on")
.channel("ch-1")
.async(result -> { /* check result */ });
pubnub.Signal()
.Message("typing_on")
.Channel("ch-1")
.Execute(new PNPublishResultExt((result, status) => {
if (status.Error) {
Console.WriteLine(status.ErrorData.Information);
} else {
Console.WriteLine(result.Timetoken);
}
}));
Note that signals are transient, and are handled differently than regular messages. You can't fetch signals from Message Persistence, and you only receive the most recent signal from the buffer queue via a subscribe request.
Typing Indicator Events
Typing events are published on the same channel as the original message. To receive them, you subscribe to the channel and add a listener for signals.
{
"actualChannel": null,
"channel": "ch-1",
"message": "typing_on",
"publisher": "user-456",
"subscribedChannel": "my_channel_1",
"subscription": null,
"timetoken": "14966804541029440"
}