Javascript API

Javascript functions can be used to enhance CometChat functionality.

Functions available

Function Description
chatWith Launch a chat box
getBaseData Show base data
getUnreadMessageCount Show total unread messages count
getActiveId Find out the User ID of the user being chatted with
getLanguage Can be used to retrieve the active language
getUser find out user details
ping checks cometchat has been loaded or not
getBaseUrl To get cometchat path
getname To get username using userid
scrollToTop Scroll to the top of the current page
minimizeAll To minimized open chatboxes
sendMessage To send message to particular user
reinitialize To reload CometChat

chatWith

chatWith API assists you to dynamically launch a chat box directly from your web pages.The chatWith API is used to launch a chat box. The input parameter is the user’s ID or group id with whom the conversation has to be initiated .

API:

For one-on-one chatbox

jqcc.cometchat.chatWith(USERID);

For group chatbox

jqcc.cometchat.chatWith({'groupid':groupid});

Sample:

For one-on-one chatbox

The ideal use of this function is in profile pages i.e. to add a “Chat with me” link to profiles.

<a href="javascript:void(0)" onclick="javascript:jqcc.cometchat.chatWith(1);">Chat with me<a>

For group chatbox

If ‘Hangout’ named group’s id is 2, then code will be:

<a href="javascript:void(0)" onclick="javascript:jqcc.cometchat.chatWith({'groupid':2});">Open Hangout Group<a>

getBaseData

getBaseData API assists you to get the logged-in user’s identity.

jqcc.cometchat.getBaseData();

getUnreadMessageCount

getUnreadMessageCount( ) API can be used to find out the total unread messages count of contacts and groups. By passing specific contact id or group id you can get total count of unread messages for specific id.

API:

jqcc.cometchat.getUnreadMessageCount({'contact':[],'group':[]});

For contact, groups you can pass single or multiple userid, groupdid. It will return the total count of unread messages.

Sample:

To get total unread message count:

var totalunreadmessages = jqcc.cometchat.getUnreadMessageCount();

To get total unread message count of userid 1,2,3

var userstotalunreadmessages = jqcc.cometchat.getUnreadMessageCount({'contact':[1,2,3]});

To get groups unread message count of groupids 1,2,3

var groupstotalunreadmessages = jqcc.cometchat.getUnreadMessageCount({'group':[1,2,3]});

getActiveId

getActiveId API can be used to find out the User ID of the user being chatted with. Depending on your theme, the getActiveId API will return an integer value or an array. For Standard or Lite theme, the getActiveId API will return an integer data and for Facebook or Hangout theme, the getActiveId API will return an array. If the data returned is empty then the user is currently not chatting with anyone.

API:

jqcc.cometchat.getActiveId();

Sample:

var chatters_userid = jqcc.cometchat.getActiveId();

getLanguage

getLanguage API can be used to retrieve the active language in CometChat.

API:

jqcc.cometchat.getLanguage();

Sample:

var lang = jqcc.cometchat.getLanguage();

getUser

getUser API can be used to find out if a user is online and details like profile link, avatar and status message.

API:

jqcc.cometchat.getUser('USERID','CALLBACKFN')

Sample:

  1. To check if user status
window.onload = function() {
    jqcc.cometchat.getUser('1', 'checkstatus');
}
 
function checkstatus(data) {
    console.log(data); // To see output using Firebug
    if (data.s == 'available') {
        alert('User is online');
    }
}
  1. To get all details of a user
function details(data) {
    alert(data);
}
jqcc.cometchat.getUser('1', 'details');

ping

The ping API will return true if CometChat has been loaded.

API:

jqcc.cometchat.ping()

Sample:

document.onload = function() {
    testload();
}
 
function testload(data) {
    var loaded = 0;
    try {
        loaded = jqcc.cometchat.ping();
    } catch (err) {
 
    }
 
    if (loaded) {
        alert('CometChat has loaded');
    }
}

getBaseUrl

The base url is the path to the cometchat folder from your web root. The getBaseUrl API assist you to find out the CometChat Base URL.

API:

jqcc.cometchat.getBaseUrl();

Sample:

var base = jqcc.cometchat.getBaseUrl();

scrollToTop

The scrollToTop API is used to scroll to the top of the current page.

API:

jqcc.cometchat.scrollToTop();

Sample:

jqcc.cometchat.scrollToTop();

getname

The getname API helps to get the name of user who is online by the userid.

API:

Get the name of user stored in the current session by the ‘userid’.

jqcc.cometchat.getName('USERID');

Sample:

jqcc.cometchat.getName('123');

minimizeAll

The minimizeAll API minimize one-on-one chat windows and the who’s online window.

API:

jqcc.cometchat.minimizeAll();

Sample:

<a href="javascript:void(0)" onclick="javascript:jqcc.cometchat.minimizeAll();">Minimize chat</a>

sendMessage

The sendMessage API assists you to send a message on behalf of the logged in user to another user. The input parameter is user’s ID to whom the message is to be sent along with the message.

API:

The input parameter is user’s ID to whom the message wants to be sent along with the message.

jqcc.cometchat.sendMessage('id','message');

Sample:

<a href="javascript:void(0)" onclick="javascript:jqcc.cometchat.sendMessage('44','PING!!!');">Ping This User</a>

reinitialize

The reinitialize API assists you to reload CometChat.
If you have placed your site inside an iFrame or you perform login using AJAX, then the CometChat bar will still show the user as not logged in. In order to login the user without refreshing the window, you can use this function.

API:

jqcc.cometchat.reinitialize();

Sample:

If you have placed your site inside an iFrame or you perform login using AJAX, then the CometChat bar will still show the user as not logged in. In order to login the user without refreshing the window, you can use this function.

function onlogincomplete() {
    jqcc.cometchat.reinitialize();
}
  • Was this helpful?
  • Yes   No