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
//code for setDelegate qiscusRealtime / connection
QiscusCore.connect(delegate: self)
extension AppDelegate : QiscusConnectionDelegate {
func onConnected(){
}
func onReconnecting(){
}
func onDisconnected(withError err: QError?){
}
func connectionState(change state: QiscusConnectionState) {
}
}
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()
:
// re-connect once re-open the apps
extension AppDelegate {
// check whether is authenticated or not
if QiscusCore.hasSetupUser() {
// connect to realtime
QiscusCore.connect(delegate: self)
}
}
//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() {
QiscusCore.delegate = nil
}
This is example how ViewController can receive event from delegate:
extension UIChatPresenter : QiscusCoreRoomDelegate {
func onMessageUpdated(message: CommentModel) {
//
}
func onMessageReceived(message: CommentModel){
//
}
func onMessageDelivered(message : CommentModel){
//
}
func onMessageRead(message : CommentModel){
//
}
func onMessageDeleted(message: CommentModel){
//
}
func onUserTyping(userId : String, roomId : String, typing: Bool){
//
}
func onUserOnlinePresence(userId: String, isOnline: Bool, lastSeen: Date){
//
}
//this func was deprecated
func onRoom(update room: RoomModel) {
//
}
//this func was deprecated
func didComment(comment: CommentModel, changeStatus status: CommentStatus) {
//
}
}
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()
:
// re-connect once re-open the apps
extension AppDelegate {
// check whether is authenticated or not
if QiscusCore.hasSetupUser() {
// connect to realtime
QiscusCore.connect(delegate: self)
}
}
//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() {
QiscusCore.delegate = nil
}
This is example how ViewController can receive event from delegate:
extension UIChatListPresenter : QiscusCoreDelegate {
//if implement feature refreshToken
func onRefreshToken(event: QiscusRefreshTokenEvent) {
if event == .isUnauthorized {
//need to force re login
}else if event == .isTokenExpired {
//need to call api refresh token when auto refresh token from be is false, by default is true from be
}
}
func onRoomMessageUpdated(_ room: RoomModel, message: CommentModel) {
//
}
func onRoomMessageReceived(_ room: RoomModel, message: CommentModel){
//
}
func onRoomMessageDelivered(message : CommentModel){
//
}
func onRoomMessageRead(message : CommentModel){
//
}
func onChatRoomCleared(roomId : String){
//
}
func onRoomMessageDeleted(room: RoomModel, message: CommentModel) {
//
}
func gotNew(room: RoomModel) {
//
}
//this func was deprecated
func onRoom(deleted room: RoomModel) {
//
}
//this func was deprecated
func onRoom(update room: RoomModel) {
//
}
//this func was deprecated
func onRoomDidChangeComment(comment: CommentModel, changeStatus status: CommentStatus) {
//
}
}
Make sure import QiscusCore before registering the delegate
Event Delegate in Chat Room Table:
Method | When 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:
Method | When 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:
func onMessageReceived(comment: CommentModel){
//When you receive new message
}
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:
QiscusCore.shared.subscribeChatRoom(room)
Once you no need to listen these events, you can simply unsubscribe related a chat room, for example:
QiscusCore.shared.unSubcribeChatRoom(room)
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:
QiscusCore.shared.publishTyping(roomId: roomId, isTyping)
Receiving Typing Status
You can receive typing status once you have subscribed the a chat room event, for example:
func onUserTyping(userId : String, roomId : String, typing: Bool){
if let user = QiscusCore.database.member.find(byUserId : userId){
//
}
}
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:
// On chat room delegate
func onRoomMessageDelivered(message : CommentModel){
print("receive delivered message: \(message.message)")
}
func onRoomMessageRead(message : CommentModel){
print("receive read message: \(message.message)")
}
// on list chat room delegate
func onMessageDelivered(message : CommentModel){
print("receive delivered message: \(message.message)")
}
func onMessageRead(message : CommentModel){
print("receive read message: \(message.message)")
}
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:
QiscusCore.shared.subscribeUserOnlinePresence(userId: userId)
After that, you can receive the participant's online status through onUserOnlinePresence
event, for example:
func onUserOnlinePresence(userId: String, isOnline: Bool, lastSeen: Date){
if let room = self.room {
if room.type != .group {
let message = lastSeen.timeAgoSinceDate(numericDates: false)
if let user = QiscusCore.database.member.find(byUserId : userId) {
//
}
}
}
}
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:
// on chat room delegate
func onRoomMessageDeleted(message: CommentModel){
//
}
// on list chat room delegate
func onMessageDeleted(message: CommentModel){
//
}
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:
QiscusCore.shared.publishOnlinePresence(isOnline: isOnline)
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)
}