Here are the beginnings of a solution: I learned how to start a background java script, and how to make a socket connection to it from a php template file. Then the script and the template can send messages back and forth over the socket. First I had to purchase a dedicated IP address from bluehost. Then I had to install node.js, socket.io, socket.io-server, socket.io-redis, and redis. Then I had bluehost open up ports 9100 and 6379. Then i had to make sure my php install is version 5.3 or greater. Then i configured ssh.
Here is the code for the background script. the file it is in is “em.js”. You can put the code in the .js file of your choosing.
// node server file emsave.js
var io= require(‘socket.io’)(9100);
io.adapter(require(‘socket.io-redis’)
({ host: ‘your dedicated IP address here’})); // supply your dedicted ip address here
console.log(‘Server is listening on 9100’);
io.on(‘connection’,function(socket){
console.log(‘connection just made’);
socket.on(‘disconnect’, function () {
console.log(‘connection just closed’);
});
socket.on(‘message’, function (msg) {
console.log(msg);
socket.send(msg);
});
});
to run the above, log in to ssh and type in the following command: node em.js
Here is the php template:
<?php
/*
Template Name: EmitterPHP
*/
/*
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
*/
?>
<?php
namespace myapp;
use SocketIO\emitter;
class OrganizationUpdates
{
public $host;
public $port = 6379;
private $_emitter;
/**
* Return redis queue
* @return emitter
*/
private function _emitter()
{
if (!$this->_emitter) {
$redis = new \Redis();
$redis->connect($this->host, $this->port);
$this->_emitter = new emitter($redis);
}
return $this->_emitter;
}
public function publishNewEvent($eventType, $data)
{
$this->_emitter()->emit(“news”, [
‘event_type’ => $eventType,
‘data’ => $data
]);
}
}
echo phpversion();
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<input type=”button” value=’Send message’ onclick=’sendMessage()’>
<input type=”text” id=”response” value=”this will change” >
<script src=”_https:_//_cdn.socket.io/socket.io-1.0.6.js”></script> // clean up this line by taking out the underscores
<script>
var socket = io(‘your website url here:9100’, { // put in your website url here
transports: [‘websocket’]
});
;
socket.on(‘message’, function (msg) {
document.getElementById(“response”).value=msg;
});
function sendMessage() {
socket.send(‘hi’);
};
</script>
</body>
</html>
Opening a page using the php file template will make a connection (see console output of the em.js file). Then, hitting the ‘Send message’ button will send a message to the em.js process. The em.js process will log the message to the console and then send it back. The php page will display the returning message in the text box.