# 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 on the [official RFC site](https://tools.ietf.org/html/rfc6455).
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 event listener
Register the delegate for the incoming events with the `CMPComapiClient` object. The delegate should conform to the `CMPEventListener` protocol.
```objectivec
[client addEventDelegate:self];client.add(self)To receive the event, implement following method from CMPEventListener protocol:
- (void)client:(CMPComapiClient *)client didReceiveEvent:(CMPEvent *)event {
switch (event.type) {
case CMPEventTypeConversationMessageSent:
// message sent event received
break;
case CMPEventTypeConversationParticipantTypingOff:
// participant typing off event received
break;
case CMPEventTypeConversationParticipantTyping:
// participant typing event received
break;
case CMPEventTypeConversationMessageRead:
// message read event received
break;
// case .other events:
default:
// unknown event type
break;
}
}func client(_ client: ComapiClient, didReceive event: Event) {
switch event.type {
case .conversationMessageSent:
// message sent event received
case .conversationParticipantTypingOff:
// participantTypingOff event received
case .conversationParticipantTyping:
// participantTyping event received
case .conversationMessageRead:
// message read event received
// case .other events:
default:
// unknown event type
}
}You can register multiple listeners in your code, to handle events in different parts of your application. The events are broadcasted to all registered delegates.
[client removeEventDelegate:self];client.remove(self)Available events
| Event type | Event subtype | |
|---|---|---|
| profile | update | Sent when a user's profile is updated. |
| conversation | participantAdded | Sent when a participant is added to a conversation. When a conversation is created, this event also fires with the owner's profileId. |
| conversation | participantUpdated | Sent when a participant role is updated in a conversation. |
| conversation | participantRemoved | Sent when a participant is removed from a conversation. |
| conversation | delete | Sent when a conversation is deleted. |
| conversation | update | Sent when a conversation's details are updated. |
| conversation | undelete | Sent when a conversation is restored. |
| conversation | create | Sent when new conversation is created. |
| conversation | participantTyping | Sent when one of the participants is typing new message. |
| conversation | participantTypingOff | Sent when the participant stops typing. |
| conversationMessage | delivered | Sent when one of the participants updates the message status to delivered. |
| conversationMessage | read | Sent when one of the participants updates the message status to read. |
| conversationMessage | sent | Sent when a new message appears in a conversation. This event is also delivered to the message sender. |
For all events you can access id, apiSpace, name and context properties.
For other events, there are other properties you can access:
profile.profileIdConversation
Below are listed properties specific for the conversation event subtypes.
conversation.conversationId
conversation.payload.profileId
conversation.payload.roleconversation.conversationId
conversation.payload.profileId
conversation.payload.roleconversation.conversationId
conversation.payload.profileId
conversation.payload.roleconversation.conversationId
conversation.payload.dateconversation.profileId
conversation.payload.roles
conversation.payload.isPublic
conversation.payload.participantsconversation.conversationId
conversation.payload.description
conversation.payload.rolesconversation.profileId
conversation.payload.roles
conversation.payload.isPublic
conversation.payload.participantsconversation.account
conversation.payload.profileId
conversation.payload.conversationIdconversation.account
conversation.payload.profileId
conversation.payload.conversationIdConversationMessage
conversationMessage.messageId
conversationMessage.conversationId
conversationMessage.profileId
conversationMessage.timestampconversationMessage.messageId
conversationMessage.conversationId
conversationMessage.profileId
conversationMessage.timestampconversationMessage.messageId
conversationMessage.metadata
conversationMessage.parts
conversationMessage.alert