Developer: How to detect if the chat is online/offline

Signup for a free 18-days trial today

Developer: How to detect if the chat is online/offline

In Knowledge base >

Javascript

To receive the status of your live chat through javascript, you will have to built a callback function. The script of cSupport is non-blocking and asynchronous, thus it will need to have a callback function to run when it has finished loading.

Take a look at the example below:

<script type="text/javascript">
	function chat_callback(){
		// Below is the check for offline/online
		if(typeof cs_online == 'boolean' && cs_online==true){
			alert("We are online!");
		}else{
			alert("We are offline!");
		}
	}
	(function(d,c){
		var scrs=d.getElementsByTagName("script");var scr=scrs[scrs.length-1];var e=d.createElement("script");e.async=true;e.src=("https:"==document.location.protocol?"https://":"http://")+"chat.csupporthq.com/external/chat-float-inline.js?"+scrs.length;
		if(typeof c=="function")if(e.addEventListener)e.addEventListener("load",c,false);else if(e.readyState)e.onreadystatechange=function(){if(this.readyState=="loaded")c();};
		scr.parentNode.insertBefore(e,scr);
	})(document,chat_callback);
</script>

This will popup “We are online!” whenever your chat is online and “We are offline” when it is offline. Remember that you need to put the callback javascript function before you include the tag, and then include it in the last line of the script (the last line usually only as null as it’s callback function, (document,null);, this you should replace with the actual callback function).

You will be able to do more advance things, as an example, with jquery. You can make a badge show by calling the ID:

	// Below is the check for offline/online
	function chat_callback(){
		if(typeof cs_online == 'boolean' && cs_online===true){
			$('#my_badge').fadeIn(200);
		}
	}

Server side

We do not recommend that you lookup server side, unless done carefully. If you wish to, please contact us.