Event Handler
Qiscus Chat SDK provides a simple way to let applications publish and listen to some real-time events. You can publish typing, read, user status, and custom event so that you can handle freely in the event handler. This lets you inform users that another participant is actively engaged in communicating with them.
Receiving Message
You need to subscribe chat room listen chat room event in order to receive the message, this only for channel type. To subscribe the chat room you can follow this below code:
qiscus.subscribeChatRoom(chatRoom);
To get the chat room object you can refer to this docs chat room.
You need to subscribeChatRoom in order to receive message for a channel type, otherwise you don't receive message. Then messages can be received through onMessageReceived event
Messages can be received through a onMessageReceived
event. This event is triggered whenever users send a message. you can implement this event, for example:
qiscus.onMessageReceived().listen(message) {
print('new message $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:
qiscus.subscribeChatRoom(chatRoom);
Once you don't need to listen these events, you can simply unsubscribe related a chat room, for example:
qiscus.unsubscribeChatRoom(chatRoom);
Channel only get a message through messageReceived event after subscribe a chat room event
Sending Typing Status
You can have a typing status by publishing the typing event. You need to pass roomId
and IsTyping
status. Set true to indicate the typing
event is active, set false to indicate the event is inactive, you can use the code below:
qiscus.publishTyping(roomId: roomId, isTyping: true);
Receiving Typing Status
You can receive typing status once you have subscribed the a chat room event, for example:
qiscus.onUserTyping().listen((event) {
var userId = event.userId;
var roomId = event.roomId;
var isTyping = event.isTyping;
print("get userId ${userId}");
print("get roomId ${roomId}");
print("get typing status ${isTyping}");
});
You need to subscribe a chat room event first, otherwise you will not receive this event
Receiving Message Status (on going)
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:
// receiving read message
qiscus.onMessageRead().listen((message) {
print('get read message: ${message}');
});
// receiving delivered message
qiscus.onMessageDelivered().listen((message) {
print('get delivered message: ${message}');
});
Receiving Participant Online Status
This section suits when you want to know whether the 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 passing related userId
, for example:
qiscus.subscribeUserOnlinePresence(userId);
After that, you can create method that subscribes to event for example:
qiscus.onUserOnlinePresence().listen((event) {
var userId = event.userId;
var isOnline = event.isOnline;
var lastOnline = event.lastSeen;
print('userId: ${userId}');
print('online status: ${isOnline}');
print('last online: ${lastOnline}');
});
Once you don't need this event anymore, you need to unsubscribe the related userId
, for example:
qiscus.unsubscribeUserOnlinePresence(userId);
Receiving Deleted Message (on going)
You can receive deleted message through onMessageDeleted
. This event is triggered whenever users delete a message, you can implement this event, for example:
qiscus.onMessageDeleted().listen((message) {
print('deleted message: ${message}');
});
Customizing Real-Time Event (on going)
You can publish and listen to any events such as when participant is listening to music, writing document, and many other cases that you need to tell to other participants in a Chat Room.
First, you need to pass roomId with related ChatRoom you want to set, and the structured data to define what event that you want to send. Below is the example of structured data of writing document event:
{
"sender": "John Doe",
"event": "writing document...",
"active": "true"
}
After that, you can send event using this following method publishCustomEvent()
:
qiscus.publishCustomEvent(roomId: 1, payload: <String, Object?>{})
.catchError((Object error) {
print('error ${error}');
});
If you need to stop telling other participants that event is ended, you can send a flag to be false within your structured data, for example:
{
"sender": "John Doe",
"event": "writing document...",
"active": "false"
}
After sending an event, then you need to listen to the event with related roomId
, for example:
qiscus.subscribeCustomEvent(roomId: roomId).listen((data) {
print('get the data: $data');
});
You can unlisten the event to stop receiving the data by passing related roomId,
for example:
qiscus.unsubscribeCustomEvent(roomId: roomId);
Connection to Qiscus Server
You can have a control in your App when you are connected or disconnected from Qiscus Server. To get these event you can follow below code:
qiscus.onConnected().listen((_) {
print('connected');
});
qiscus.onDisconnected().listen((_) {
print('disconnected');
});
qiscus.onReconnecting().listen((_) {
print('reconnecting.. ');
});