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:

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
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
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:

MethodWhen to call
onRoom(_ room: RoomModel, gotNewComment comment: CommentModel)When you get a new message
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:

Swift
Copy

Subscribe Typing Event outside the ChatRoom

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

Swift
Copy

Unsubscribe Typing Event

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

Swift
Copy

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.

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

Swift
Copy

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

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