Support » Everything else WordPress » webpage communication using websockets

  • Hi, I am using wordpress at bluehost.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. In order for the pages to work with the most browsers, I would like to use websockets to perform the communications. As a test, I have set up a couple wordpress webpages where one is a websockets server, and one is a websockets client. I am using PHP and HTML code which I have gotten off of the web. The problem I am having is that I have not been able to make a client-server connection between the two pages. I don’t know if I have not been able to construct the right websockets address, or if there is some other problem. I have used combinations of the permalinks and server-webpage IP address to no avail. Does anyone know how to make such a connection, or where there might be an example which shows how to accomplish this connection between word-press pages? Thanks, Phil Marks Sr.

Viewing 5 replies - 1 through 5 (of 5 total)
  • I have been watching your question since you posted it, in the hope that someone would answer it.

    I have not done websockets myself, I can see how it would permit true peer-peer communications between clients on web pages. What I have done is much more server centric, I used AJAX to stream updates to/from web forms back to the server and database. There were different pages for different roles. The ajax code basically polls the server/database, the poll frequency can be dynamically adapted.
    You mention having connections between WordPress pages, instead I see the issue of having connections between browsers viewing pages.
    I am interested in how this works out for you.

    I just watched an online video 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. So i installed node.js. socket.io, and socket.io redis. I bought a dedicated ip, and got tech support to open a port of my choosing for socket.io, and also port 6379 for redis. I have been able to communicate between two js scripts running on ssh. 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 on a bluehost site? Thanks, phil marks sr.

    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:

    [email redacted] [~/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. (/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)
    [email redacted] [~/public_html/EmitterPHP]#

    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.

    i almost forgot. For the above to work, you must start the Redis server using ssh, before running the em.js file:

    ./redis-server

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘webpage communication using websockets’ is closed to new replies.