Event Handler

Qiscus Chat SDK provides a simple way to let applications publish and listen some real time event. You can publish typing, read, user status, and custom event, and you can handle freely in event handler. This lets you inform users that another participant is actively engaged in communicating with them.

Qiscus Chat SDK is using delegate for broadcasting event to entire application. What you need to do is registering the object which will receive event from delegate.

Connection to Qiscus Server

You can have a control in your App when you are connected or disconnected from Qiscus Server. QiscusConnectionState consist 3 states, there are connected, connecting, and disconnected state, for example

Swift
Copy

You need register QiscusCore.connect() when everytime re-open the app

Event Handler in Chat Room

To get event in chat room, you need to register delegate in viewWillAppear() :

Swift
Copy

You need unregister the receiver after you don't need to listen event anymore by calling this method:

Swift
Copy

This is example how ViewController can receive event from delegate:

Swift
Copy

Make sure import QiscusCore before registering the delegate

Event Handler in List of Chat Rooms

To get event list of chat rooms, you need to register delegate in viewWillAppear() :

Swift
Copy

You need unregister the receiver after you don't need to listen event anymore by calling this method:

Swift
Copy

This is example how ViewController can receive event from delegate:

Swift
Copy

Make sure import QiscusCore before registering the delegate

Event Delegate in Chat Room Table:

MethodWhen to call
onMessageReceived(comment: CommentModel)When you get new messages from yourself or from other participants
onMessageDelivered(comment: CommentModel)When you get delivered message status
onMessageRead(message : CommentModel)When a participant is mark as read your message
onMessageDeleted(message: CommentModel)When you get message is deleted from yourself or came from other user
onUserTyping(userId : String, roomId : String, typing: Bool)When other user is typing
onUserOnlinePresence(userId: String, isOnline: Bool, lastSeen: Date)When other user is online or offline
onRoom(update room: RoomModel)When there's any update chat room from yourself or came from other user

Event Delegate in List of Chat Rooms Table:

MethodWhen to call
onRoomMessageReceived(_ room: RoomModel, message: CommentModel)When you get new messages from yourself or from other participants
onRoomMessageDelivered(message : CommentModel)When you get delivered message status
onRoomMessageRead(message : CommentModel)When a participant is mark as read your message
onRoomMessageDeleted(room: RoomModel, message: CommentModel)When you get a message is deleted from yourself or came from other user
onRoom(update room: RoomModel)When there's any update chat room from yourself or came from other user
gotNew(room: RoomModel)When you get a new chat room
onChatRoomCleared(roomId : String)When you clear all messages in a chat room

Receiving Message

Messages can be received through a onMessageReceived event. This event is triggered whenever users send a message, you can implement this event, for example:

Swift
Copy

Listening Chat Room Event

Chat room event consist of 3 events that happen in a chat room, such as typing, delivered, and read. These events only work for 1-on-1 chat and group chat, for channel you need to subscribe in order to receive a message. To able receive these events you need to subscribe a chat room, for example:

Swift
Copy

Once you no need to listen these events, you can simply unsubscribe related a chat room, for example:

Swift
Copy

Channel only get a message through messageReceived event after subscribe a chat room event and register the delegate

Sending Typing Status

You can have a typing status by publishing the typing event. You need to pass roomId and typing status. Set true to indicate the typing event is active, set false to indicate the event is inactive, you can use the code below:

Swift
Copy

Receiving Typing Status

You can receive typing status once you have subscribed the a chat room event, for example:

Swift
Copy

You need to subscribe a chat room event first, otherwise you will not receive this event

Receiving Message Status

After you listen some of events in a chat room, You can receive the real time message status which defined by the event, such as delivered, and read, for example:

Swift
Copy

You need to subscribe a chat room event first, otherwise you will not receive this event

Receiving Participant Online Status

Participant online status means you can know whether a participant is online or not (last active). This case is only for 1-on-1 chat room. You can get participant's online status by subscribe related userId, for example:

Swift
Copy

After that, you can receive the participant's online status through onUserOnlinePresence event, for example:

Swift
Copy

Receiving Deleted Message

You can receive deleted message through onMessageDeleted. This event is triggered whenever users delete a message, you can implement this event, for example:

Swift
Copy

Start and Stop Online Status

You can set online or offline by passing isOnline status. Set true to indicate user is online, and set false to indicate that user is offline. The result will be on onUserOnlinePresence(userId: String, isOnline: Bool, lastSeen: Date) . Below are the code for publish online or offline:

Swift
Copy

Custom Realtime Event

You can publish and listen any events such as when participant is listening music, writing document, and many other case, that you need to tell an event to other participant in a Chat Room.

Firstly you need passing roomId which ChatRoom you want to set, and the structured data for defining what event you want to send. This is example a structured data of writing document event:

JSON
Copy

Then you can send event using this following setEvent method:

Swift
Copy

If you need to stop telling other participant that event is ended, you can send a flag to be false inside your structured data, for example:

JSON
Copy

After sending an event, then you need to listen the event with related roomId, for example:

Swift
Copy
Type to search, ESC to discard
Type to search, ESC to discard
Type to search, ESC to discard