Javascript SDK

CallBack Subscribe API

The API is used to register Callbacks at various different events of CometChat Application. These callbacks are processed when particular events of CometChat are triggered.

Syntax for API

jqcc.cometchat.subscribe(registerCallback);

Params

Json Object of Callback.

registercallback = { 'key' : [function(data){}]} 

Register Callback key available

Register Callback key Description
gotProfileInfo This callback triggers on receiving the logged-in users profile information
gotOnlineList This callback triggers on receiving contact list
onMessageReceived This callback triggers on receiving text chat message from a user
gotAnnouncement This callback triggers on receiving an announcement from CometChat Admin
gotGroupList This callback triggers on receiving group list
onGroupMessageReceived This callback triggers on receiving text chat message from a user
onLeaveGroup This callback triggers when logged-in user leaves the group
gotRecentChatsList This callback triggers on receiving recent chat list

gotProfileInfo

gotProfileInfo triggers on receiving the logged-in users profile information.

var fucntion1 = function(data){console.log('gotProfileInfo',data)};
        registerCallback = { 'gotProfileInfo':[fucntion1,function(data){ console.log(data) }]}
    var setTime = setInterval(function(){
    if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){
            jqcc.cometchat.subscribe(registerCallback);
            clearInterval(setTime);
        }
    },3000);

gotOnlineList

gotOnlineList triggers on receiving contact list.

var fucntion1 = function(data){console.log('gotOnlineList',data)};
        registerCallback = { 'gotOnlineList':[fucntion1,function(data){ console.log(data) }]}
    var setTime = setInterval(function(){
    if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){
            jqcc.cometchat.subscribe(registerCallback);
            clearInterval(setTime);
        }
    },3000);        

onMessageReceived

onMessageReceived triggers when receiving text chat message.

var fucntion1 = function(data){console.log('onMessageReceived',data)};
        registerCallback = { 'onMessageReceived':[fucntion1,function(data){ console.log(data) }]}
    var setTime = setInterval(function(){
    if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){
            jqcc.cometchat.subscribe(registerCallback);
            clearInterval(setTime);
        }
    },3000);        

gotAnnouncement

gotAnnouncement triggers when receiving an announcement from CometChat Admin.

var fucntion1 = function(data){console.log('gotAnnouncement',data)};
        registerCallback = { 'gotAnnouncement':[fucntion1,function(data){ console.log(data) }]}
    var setTime = setInterval(function(){
    if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){
            jqcc.cometchat.subscribe(registerCallback);
            clearInterval(setTime);
        }
    },3000);        

gotGroupList

gotGroupList triggers on receiving group list

var fucntion1 = function(data){console.log('gotGroupList',data)};
        registerCallback = { 'gotGroupList':[fucntion1,function(data){ console.log(data) }]}
    var setTime = setInterval(function(){
    if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){
            jqcc.cometchat.subscribe(registerCallback);
            clearInterval(setTime);
        }
    },3000);        

onGroupMessageReceived

onGroupMessageReceived triggers on receiving text chat message from a user

var fucntion1 = function(data){console.log('onGroupMessageReceived',data)};
        registerCallback = { 'onGroupMessageReceived':[fucntion1,function(data){ console.log(data) }]}
    var setTime = setInterval(function(){
    if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){
            jqcc.cometchat.subscribe(registerCallback);
            clearInterval(setTime);
        }
    },3000);        

onLeaveGroup

onLeaveGroup triggers when logged-in user leaves the group

var fucntion1 = function(data){console.log('onLeaveGroup',data)};
        registerCallback = { 'onLeaveGroup':[fucntion1,function(data){ console.log(data) }]}
    var setTime = setInterval(function(){
    if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){
            jqcc.cometchat.subscribe(registerCallback);
            clearInterval(setTime);
        }
    },3000);        

gotRecentChatsList

gotRecentChatsList triggers on receiving recent chat list

var fucntion1 = function(data){console.log('gotRecentChatsList',data)};
        registerCallback = { 'gotRecentChatsList':[fucntion1,function(data){ console.log(data) }]}
    var setTime = setInterval(function(){
    if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){
            jqcc.cometchat.subscribe(registerCallback);
            clearInterval(setTime);
        }
    },3000);        

Sample Code for Register Callback

var fucntion1 = function(data){console.log('gotProfileInfo',data)};
    
        registerCallback = { 
                'gotProfileInfo':[fucntion1,function(data){
                    console.log('gotProfileInfo1',data)
                    }],
                'gotOnlineList':[function(data){    
                                        console.log('gotBuddyInfo',data)
                }],
                'onMessageReceived':[function(data){                                                             
                                       console.log('onMessageReceived',data)                             
                                }],
                'onGroupMessageReceived':[function(data){
                    console.log('onGroupMessageReceived',data)
                }],
                          }

    var setTime = setInterval(function(){
    if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){
            jqcc.cometchat.subscribe(registerCallback);
            clearInterval(setTime);
        }
    },3000);

How to get Logged in users Profile Details & List All Group Details.

Just follow below steps for getting logged in users profile details.

Step 1

Create "demo.html" file in your root directory.

Step 2

Copy below code and paste it in "demo.html" file.

<link type="text/css" rel="stylesheet" media="all" href="css.php" />
<script type="text/javascript" src="js.php" charset="utf-8"></script>

Step 3

Create html div tag for profile detail & Group List. In ‘demo_profile_data’ div we will append Logged in user details & in ‘group_list’ div we will append Group details. Copy below code and paste it before </body> Tag.

<!-- Profile Data append to below div -->
<div id="demo_profile_data"></div>
<div id="group_list"></div>

Step 4

For fetching profile data & Group Details using JavaScript Callback Function copy below code and just paste it in your file.

<script type="text/javascript">
    var registerCallback = {};
    var inc = 0;
       
        
    var fucntion1 = function(data){console.log('gotProfileInfo',data)};
    registerCallback = {    'gotProfileInfo':[fucntion1,function(data){
                    var str = "Avatar URL : " + data.a + " User Name : " + data.n + " User Status : " + data.s;
                    $("#demo_profile_data").append(str);
                }],
                'gotGroupList':[function(data){
                    var strGroupList = "";
                    $.each(data, function( index, value ) {
                          strGroupList += value.name + '
'; }); $("#group_list").append(strGroupList); }] }; var setTime = setInterval(function(){ if(typeof CometChathasBeenRun !== undefined && CometChathasBeenRun == true){ jqcc.cometchat.subscribe(registerCallback); clearInterval(setTime); } },100); </script>
  • Was this helpful?
  • Yes   No