Javascript Events
Use MQTT chat javascript events in your code.
In spite of you do not have direct access to the source code and to work around the problem of cross-origin we used a framework called easyXDM for sharing javascript events between your site and our servers through socket messages exchange. So you have the possibility to capture some MQTT chat events in your JavaScript code.

Events

Description

Below list of events that you can use : data of each event are encapsulated in mqttchat_data variable.
Name
Description
mqttchat-load-complete
Event fires when chat iframe is completely loaded.
mqttchat-incoming-message
Event fires when currently user receives a message from another user.
mqttchat-not-read-messages-count-update
Event fires when the number of unread messages changes.
mqttchat-error
Event fire when an error has occurred

Integration

mqttchat-load-complete
1
telifounJQ(document).ready(function() {
2
telifounJQ(document).on("mqttchat-load-complete",function(e){
3
console.log("chat load is complete");
4
})
5
});
Copied!
No data returned.
mqttchat-incoming-message
1
telifounJQ(document).ready(function() {
2
telifounJQ(document).on("mqttchat-incoming-message",function(e){
3
console.log(e.mqttchat_data);
4
});
5
});
Copied!
mqttchat_data contains the userid of user who sent the message and the the received message.
1
{
2
fromUserid: "1",
3
message: {"cid":"85", //channel id
4
"id":138729, //message id
5
"m":"hi", //message content
6
"p1":"undefined", // parameter 1
7
"p2":"undefined", // parameter 2
8
"p3":"undefined", // parameter 3
9
"t":1601749085, //message timestamp
10
"type":0}//message type
11
}
Copied!
mqttchat-not-read-messages-count-update
1
telifounJQ(document).ready(function() {
2
telifounJQ(document).on("mqttchat-not-read-messages-count-update",function(e){
3
console.log(e.mqttchat_data);
4
});
5
});
Copied!
mqttchat_data contains the number of unread message .
1
{n: 1}
Copied!
mqttchat-error
1
telifounJQ(document).on("mqttchat-error",function(e){
2
console.log(e.mqttchat_data);
3
});
4
Copied!
mqttchat_data contains error message.
1
{error: "A name at least is required to add user!"}
Copied!

Start chat with specific userid

If you want to start conversation with a specific user automatically just after chat load you can use __initChatWithUser() function like this:
1
telifounJQ(document).ready(function() {
2
telifounJQ(document).on("mqttchat-load-complete",function(e){
3
telifounJQ.MQTTchat_Cloud.__initChatWithUser(122);
4
});
5
});
Copied!
Be carfeful, __initChatWithUser() must be called after MQTTChat script is completely loaded otherwise an error is triggered.
🧐
If contact exists in user's contact list or friend's list then chat window will be just opened . If not and if use friends option is enabled, contact is automatically added to the user's friends list before that the chat window will be opened.
Last modified 5mo ago