Event Handler

AI Tools

Qiscus Chat SDK provides a simple way to let applications publish and listen some real time event. You can publish typing, read, user status, 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.

Event Handler in Chat Room

To get event in Chat Room, you need to register delegate in viewWillAppear:

//set your delegate in viewWillAppear func setRoomDelegage(){ if let room = self.room { room.delegate = self } }

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

//remove your delegate in viewWillDisappear func removeRoomDelegate() { if let room = self.room { room.delegate = nil } }

This is example how ViewController can receive event from delegate:

extension YourViewController : QiscusCoreRoomDelegate { // MARK: Comment Event in Room /// new comment is comming /// /// - Parameters: /// - comment: new comment object func gotNewComment(comment: CommentModel){ } /// comment status change /// /// - Parameters: /// - comment: new comment where status is change, you can compare from local data /// - status: comment status, exp: deliverd, receipt, or read. /// special case for read, for example we have message 1,2,3,4,5 then you got status change for message 5 it's mean message 1-4 has been read func didComment(comment: CommentModel, changeStatus status: CommentStatus){ } /// Deleted Comment /// /// - Parameter comment: comment deleted func didDelete(Comment comment: CommentModel){ } // MARK: User Event in Room /// User Typing Indicator /// /// - Parameters: /// - user: object user or participant /// - typing: true if user start typing and false when finish typing. typing time avarange is 5-10s, we assume user typing is finish after that func onRoom(thisParticipant user: MemberModel, isTyping typing: Bool){ } /// User Online status /// /// - Parameters: /// - user: object member /// - status: true if user login /// - time: millisecond UTC func onChangeUser(_ user: MemberModel, onlineStatus status: Bool, whenTime time: Date){ } /// Room update /// /// - Parameter room: new room object func onRoom(update room: RoomModel){ } }
Info

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:

//set your delegate in viewWillAppear private func setDelegate() { QiscusCore.delegate = self }

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

//remove your delegate in viewWillDisappear func removeRoomDelegate() { if let room = self.room { room.delegate = nil } }

This is example how ViewController can receive event from delegate:

extension YourViewController : QiscusCoreDelegate { // MARK: Event Room List /// new comment is comming /// /// - Parameters: /// - room: room where event happen /// - comment: new comment object func onRoom(_ room: RoomModel, gotNewComment comment: CommentModel){ } /// comment status change /// /// - Parameters: /// - room: room where event happen /// - comment: new comment where status is change, you can compare from local data /// - status: comment status, exp: deliverd, receipt, or read. /// special case for read, for example we have message 1,2,3,4,5 then you got status change for message 5 it's mean message 1-4 has been read func onRoom(_ room: RoomModel, didChangeComment comment: CommentModel, changeStatus status: CommentStatus){ } /// Deleted Comment /// /// - Parameter comment: comment deleted func onRoom(_ room: RoomModel, didDeleteComment comment: CommentModel){ } /// Room update /// /// - Parameter room: new room object func onRoom(update room: RoomModel){ } /// Deleted room /// /// - Parameter room: object room func onRoom(deleted room: RoomModel){ } /// Got New Room /// /// - Parameters: /// - room: object room func gotNew(room: RoomModel){ } /// Got clear all message in Room /// /// - Parameters: /// - room: object room func remove(room: RoomModel){ } }
Info

Make sure import QiscusCore before registering the delegate

Event Delegate in Chat Room Table:

Method

When to call

gotNewComment(comment: CommentModel)

When you get new message

didComment(comment: CommentModel, changeStatus status: CommentStatus)

When you get changes of message status, such as sent, delivered, read, and pending<br>

didDelete(Comment comment: CommentModel)

When you get message is deleted from yourself or came from other user

onRoom(thisParticipant user: MemberModel, isTyping typing: Bool)

When other user is typing

onChangeUser(_ user: MemberModel, onlineStatus status: Bool, whenTime time: 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:

Method

When to call

onRoom(_ room: RoomModel, gotNewComment comment: CommentModel)

When you get a new message<br>

onRoom(_ room: RoomModel, didChangeComment comment: CommentModel, changeStatus status: CommentStatus)

When you get changes of message status, such as sent, delivered, read, and pending

onRoom(_ room: RoomModel, didDeleteComment comment: 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

remove(room: RoomModel)

When you clear all messages in a room

Receiving Message

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

func gotNewComment(comment: CommentModel){ //When you get new message }

Subscribe Typing Event outside the ChatRoom

You can subscribe typing event manually, this subscribe action for getting the typing event, for example:

for room in rooms { DispatchQueue.global(qos: .background).asyncAfter(deadline: .now()+1, execute: { QiscusCore.shared.subscribeTyping(roomID: room.id) { (roomTyping) in if let room = QiscusCore.database.room.find(id: roomTyping.roomID){ //update you tableView cell } let sender = roomTyping.user // to get user data who send typing event let typingStatus = roomTyping.typing // to get typing state, whether true or false } }) }

Unsubscribe Typing Event

You can unsubscribe typing using this method, it will stop getting typing event on Chat Room List.

QiscusCore.shared.unsubscribeTyping(roomID: roomID)

Typing Status

You can have a typing status by publishing or sending the typing event. You need to pass roomId and typing status. Set true to indicate typing event is active, set false to indicate the event is inactive.

The result of typing event data will receive in onRoom(thisParticipant user: MemberModel, isTyping typing: Bool) delegate when you are in the room. And also will receive typing event data in QiscusCore.shared.subscribeTyping(roomID: room.id) when already subscribed typing event outside the chat room.

QiscusCore.shared.isTyping(true, roomID: r.id)

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 QiscusCoreRoomDelegate onChangeUser(_ user: MemberModel, onlineStatus status: Bool, whenTime time: Date) . Below are the code for publish online or offline:

QiscusCore.shared.isOnline(isOnline)

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

extension AppDelegate : QiscusConnectionDelegate { func disconnect(withError err: QError?) { } func connected() { } func connectionState(change state: QiscusConnectionState) { } }

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:

{ "sender": "John Doe", "event": "writing document...", "active": "true" }

Then you can send event using this following setEvent method:

let publish = QiscusCore.shared.publishEvent(roomID: roomId, payload: payload)

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:

{ "sender": "John Doe", "event": "writing document...", "active": "false" }

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

QiscusCore.shared.subscribeEvent(roomID: roomID) { (roomEvent) in print(roomEvent.sender) print(roomEvent.data) }