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 function callback when you initialize Chat SDK. You can define it like this:

qiscus.init({ AppId: 'QISCUS_SDK_APP_ID', options: { commentDeletedCallback: function (data) {}, commentDeliveredCallback: function (data) {}, commentReadCallback: function (data) {}, presenceCallback: function (data, userId) {}, typingCallback: function (data) {}, onReconnectCallback: function (data) {}, newMessagesCallback: function (messages) {}, roomClearedCallback: function (data) {} } })

Receiving Message

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

newMessagesCallback: function (messages) { var message = messages[0] // Do something with message }
Info

newMessagesCallback got a parameter of array of single comment object, not an object of comment.

Typing Status

You can have a typing status by publish the typing event. You need to pass typing status. Set 1 to indicate the typing event is active, set 0 to indicate the event is inactive, you can use the code below:

qiscus.publishTyping(typing)

Customizing Real-time 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. Example of structured data of writing document event:

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

Then you can send event using this following method publishEvent:

qiscus.publishEvent(roomId, 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:

qiscus.subscribeEvent(roomId, callback)

You need unlisten the event with related roomId, for example:

qiscus.unsubscribeEvent(roomId)

Connection to Qiscus Server

You can have control in your App, when you are reconnected, by default Qiscus Chat SDK will try reconnect automatically, and you get the event once it success. Below is the code for the event:

onReconnectCallback: function (data) { // data },

On Message Status Change

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

commentDeliveredCallback: function (data) { // On comment delivered }, commentSentCallback: function (data) { // On comment has been sent }, commentReadCallback: function (data) { // On comment has been read }

This is an complete version how to use Qiscus Event Handler, for example:

qiscus.init({ AppId: 'QISCUS_SDK_APP_ID', options: { newMessagesCallback: function (messages) { // On receive a new messages }, commentDeliveredCallback: function (data) { // On comment delivered }, commentReadCallback: function (data) { // On comment has been read by user }, presenceCallback: function (data, userId) { // On user change it "online" status }, typingCallback: function (data) { // On user typing }, onReconnectCallback: function (data) { // On Chat SDK Realtime adapter successfully reconnect to server }, commentDeletedCallback: function (data) { // On comment deleted }, roomClearedCallback: function (data) { // On user successfully cleared a room messages } } })

Here's event handler table:

Method

When to call

newMessagesCallback

When a user successfully send a message

commentDeletedCallback

When a message has been deleted

commentDeliveredCallback

When a message successfully delivered to a user

commentReadCallback

When a message has been read by user

presenceCallback

When user change it online status<br>

typingCallback

When user is still typing or not<br>

onReconnectCallback

When Chat SDK realtime adapter successfully reconnected to server

roomClearedCallback

When successfully cleared a room messages<br>