Click here to Skip to main content
15,868,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am using wordpress at bluehost.com . My domain is "mysimplesurvey.com". My aim is to set up some webpages which have multiple users logged on and communicating with the same MySql database. When one user changes certain things in the database, I would like triggers to cause certain information to be automatically pushed to other logged-on users.
I just watched an online video, just recently created by Guillermo Rauch called "Realtime Communication with Socket.IO and WordPress". This has convinced me to use node.js, socket.io and socket.io-redis to accomplish my aims for bi-directional communications between browsers and server js script. So i installed node.js. socket.io, and socket.io redis. I bought a dedicated ip, and got tech support to open a port 9100 for socket.io, and also port 6379 for redis.
So far I have been able to communicate between two js scripts running on ssh using socket.io. However i have yet to accomplish bidirectional communication between php (on a wordpress page) and a node.js socket.io server, using redis. From what i have seen on-line, I need to get a socket.io-redis sever up and running as one of the main steps. The demo by Guillermo is straight forward. But i have not been able to figure out how to get that redis server going. Does anyone know how to get a socket.io-redis server started through using ssh on a bluehost site?
The following is the simple server-side js test code which should run without errors if the redis sever were up and running.

// node server file emsave.js
var io= require('socket.io')(9100);
io.adapter(require('socket.io-redis')
({ host: 'http://www.mysimplesurvey.com'}));
io.on('connection',function(){
console.log('this should work');
});

Right now trying to run this code on my server's ssh returns the following error:

mysimpp7@mysimplesurvey.com [~/public_html/EmitterPHP]# node emsave.js
events.js:85
throw er; // Unhandled 'error' event
^
Error: Redis connection to http://www.mysimplesurvey.com:6379 failed - getaddrinfo ENOTFOUND http://www.mysimplesurvey.com
at RedisClient.flush_and_error (/home2/mysimpp7/public_html/EmitterPHP/node_modules/socket.io-redis/node_modules/redis/index.js:142:13)
at RedisClient.on_error (/home2/mysimpp7/public_html/EmitterPHP/node_modules/socket.io-redis/node_modules/redis/index.js:180:10)
at Socket.<anonymous> (/home2/mysimpp7/public_html/EmitterPHP/node_modules/socket.io-redis/node_modules/redis/index.js:95:14)
at Socket.emit (events.js:107:17)
at net.js:915:16
at process._tickCallback (node.js:343:11)
mysimpp7@mysimplesurvey.com [~/public_html/EmitterPHP]#

On the web, there doesn't seem to be a whole lot of people who have done something like this, and i may be the first one on bluehost to try it. I have similar questions posed at bluehost.com forums, wordpress.org forums, and wordpress.com forums. I have generated some interest, but no solutions so far.
Thanks,
Phil Marks Sr.
Posted

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.
HTML
// 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:

/*
Template Name: EmitterPHP
*/
/*
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
*/
?>
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>
</title></head>
<body>
<input type="button" value="Send message" >
<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>
</script></input></input></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.
 
Share this answer
 
v2
i almost forgot. For the above to work, you must install start the Redis server using ssh, before running the em.js file:

./redis-server
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900