Listen to events

WebSocket

Whenever ComapiClient is configured and authenticated, a new session is started which opens a WebSocket connection. This allows for two-way real-time communication between the client and the server.

Learn more about WebSockets.

When open, the socket provides live updates for the profile and conversations which you are a participant of. You can subscribe for these updates to update your views and models.


Add profile changes listener

Register the delegate for the incoming profile-related events with the CMPComapiChatClient object. The delegate should conform to the CMPProfileDelegate protocol.

[client addProfileDelegate:self];

To receive the event, implement the following method from CMPProfileDelegate protocol:

- (void) didUpdateProfile:(CMPProfileEventUpdate *)event {
  	// event sent when a user profile has been updated.
}

You can register multiple listeners in your code, to handle events in different parts of your application. The events are broadcast to all registered delegates.

To remove a delegate:

[client removeProfileDelegate:self];

Add conversation participants delegate

Register the delegate to listen to conversation participants added and removed events with the CMPComapiChatClient object. The delegate should conform to the CMPParticipantDelegate protocol.

// Add delegate
[client addParticipantDelegate:self];

// removed delegate
[client removeParticipantDelegate:self];

To receive the event, implement following method from CMPParticipantDelegate protocol:

- (void)didAddParticipant:(CMPConversationEventParticipantAdded *)event {
  // event sent when participant joined a conversation
}

- (void)didRemoveParicipant:(CMPConversationEventParticipantRemoved *)event {
  // event sent when participant left a conversation
}

- (void)didUpdateParticipant:(CMPConversationEventParticipantUpdated *)event {
  // event sent when participant roles change in a conversation
}