Typing indicator

Layer provides a simple way to let applications send and receive typing indicator events. This lets you implement interfaces to inform users that another participant is actively engaged in communicating with them.

Sending typing indicators

Applications can broadcast typing events by calling sendTypingIndicator: on LYRConversation objects. This will send a typing indicator event on behalf of the currently authenticated user. All participants in the conversation who are currently online will receive the typing indicator. LayerKit supports three typing indicator states: LYRTypingIndicatorActionBegin, LYRTypingIndicatorActionPause, and LYRTypingIndicatorActionFinish.

// Sends a typing indicator event to the given conversation.
[conversation sendTypingIndicator:LYRTypingIndicatorActionBegin];

Receiving typing indicators

Applications are notified of typing indicator events via NSNotification objects sent via the default NSNotificationCenter. Applications should register as an observer of the LYRConversationDidReceiveTypingIndicatorNotification notification to be notified when another user is typing.

// Registers an object for typing indicator notifications.
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didReceiveTypingIndicator:)
                                             name:LYRConversationDidReceiveTypingIndicatorNotification
                                           object:nil];

Upon receipt of a typing indicator event, applications can inspect properties of the NSNotification object received to obtain information about the typing event. The object property of the notification is a reference to the LYRConversation object in which the typing indicator was sent. The userInfo property contains additional detail about the typing event. The LYRTypingIndicatorObjectUserInfoKey key contains the user identifier of the user who sent the typing indicator to the conversation.

- (void)didReceiveTypingIndicator:(NSNotification *)notification
{
    NSString *participantID = notification.userInfo[LYRTypingIndicatorParticipantUserInfoKey];
    LYRTypingIndicator *typingIndicator = notification.userInfo[LYRTypingIndicatorObjectUserInfoKey];
    
    if (typingIndicator.action == LYRTypingIndicatorActionBegin) {
        NSLog(@"Typing Started");
    } else {
        NSLog(@"Typing Stopped");
    }
}

How to use

Typing indicator events are ephemeral, which means that Layer doesn’t save them. You can call sendTypingIndicator:toConversation as often as you’d like. Layer will merge the calls internally and efficiently send typing indicator events as needed.

After calling sendTypingIndicator: with the LYRTypingIndicatorActionBegin state, if 10 seconds go by without an update, LayerKit will automatically send a LYRTypingIndicatorActionPause event. If another 10 seconds go without an update, LayerKit will send a LYRTypingIndicatorActionFinish event.

Querying Metadata