FreePBX | Register | Issues | Wiki | Portal | Support

FreePBX 13: Use existing components to send socket message to browser on AMI event


(Jerry Riggin) #1

I am building an interface between FreePBX 13 and a custom CRM. I must use 13 because HA is a critical requirement, so I have to live with PHP 5.3. When my AMI listener receives an AgentCalled event, it has to somehow send a message to the called agent’s browser to load a URL into a frame using the data from the AgentCalled event.

One problem with many of the solutions I have tried is that my client has a heartbeat process that runs every 5 seconds that confirms the extension is reachable and the browser has a current heartbeat timestamp. If either is false, the agent is automatically logged out of the queue. This is required so agents don’t get calls if their browser is hung or closed as they would not get call info in that case. A continues WHILE loop to listen for messages stops the heartbeat. Here is the heartbeat code:

<script type="text/javascript">
	var intervalID = window.setInterval(myHeartbeat, 5000);
	function myHeartbeat() {
	var ajaxurl = 'heartbeat.php',
	data =  {'action': 'heartbeat', 'exten': '<?php echo $exten; ?>'};
		 $.post(ajaxurl, data, function ( response ) {
			 var resp = response.trim();
			if(resp == 'OK')
			{
				console.log('HEARTBEAT ' + response);
			} else {
				console.log('HEARTBEAT ERROR: ' + response);
			}
		 });
	}
</script>	

When the page loads, <?php echo $exten; ?> is populated with the extension (this is index.php) and everything works great until the window.setInterval stops when a continuous loop listener is invoked in a child process to catch messages.

So, my challenge is an async heartbeat process (which seems it might defeat the purpose) OR an existing distro component that can send websocket or tpc/udp message to browser javascript at IP:port to load CRM page in iframe. I would love to see an example of this. I am experienced in PHP and Javascript, but Node JS and WebSockets, not so much.

So, I have found another tutorial https://entwickler.de/webandphp/integrating-node-js-with-php-125830.html that looks promising. It is from 2013 which puts it in the PHP 5.3 world. I will give it a shot, but I’m sure I am re-inventing the wheel here because FreePBX sends messages to web pages from PHP all over the place and there MUST be some existing component I could re-use. Can anyone offer guidance?