PHP with

I desired to construct a real-time notification system using sockets without using any third-party services, such as Pusher, etc. I simply desired the ability to send alerts from PHP to the client and display them quickly on the web application, similar to Facebook notifications. First I came across the ratchet library, which worked well, but a problem arose when I attempted to set it up on a secure connection (https) on my host (siteground). I tried apache proxy module and everything else suggested on the internet, but to no avail (it appears siteground has a problem with ratchet on https), so I ultimately had to abandon ratchet. Then I considered utilizing with Node+PHP, and in my study I came across the library (albeit it hasn't been updated in a while), which worked marvelously well on both non-secure and secure protocols, enabling us to send and receive messages from PHP with a node-based server. Here are the procedures I took to get my notification system to function properly.

php coding
javascript with php


Install via composer for your PHP application:

composer require wisembly/

Install Node Dependencies

Create a directory at the root of your project and create a package.json file with the following contents:

    "name": "elephantIO_example",
    "version": "3.0.0",
    "main": "server.js",

    "scripts": {
        "start": "supervisor --debug server.js"

    "dependencies": {
        "": "~1",
        "winston": "*"

Execute npm install —save on the newly created directory. This will install the logger and libraries.

Create a file server js containing the following code in the same freshly created directory:

var server = require('http').createServer(),
  io = require('')(server),
  logger = require('winston'),
  port = 3001;

// Logger config
logger.add(logger.transports.Console, { colorize: true, timestamp: true });'SocketIO > listening on port ' + port);

io.on('connection', function (socket){
  var nb = 0;'SocketIO > Connected socket ' +;

  socket.on('broadcast', function (message) {
      ++nb;'Elephant.IO broadcast > ' + JSON.stringify(message));

      // send to all connected clients
      io.sockets.emit("broadcast", message);

  socket.on('disconnect', function () {'SocketIO : Received ' + nb + ' messages');'SocketIO > Disconnected socket ' +;


If you execute server.js through node by typing node server.js, you should receive a message indicating that the server has started on the chosen port.

Client Side

Insert the following javascript code into your application's page/footer:

<script src="" 
    integrity="sha384-7EyYLQZgWBi67fBtVxw60/OWl1kjsfrPFcaU0pp0nAh+i8FD068QogUvg85Ewy1k" crossorigin="anonymous"></script>

var socket = io.connect('//');

socket.on('connect', function () {

    socket.on('broadcast', function (data) {
        //socket.emit("broadcast", data);

    socket.on('disconnect', function () {

Transmitting Notification from PHP

Here's how to communicate with all linked clients:

require __DIR__. '/vendor/autoload.php';

use ElephantIOClient;
use ElephantIOEngineSocketIOVersion1X;

$client = new Client(new Version1X('//'));

// send message to connected clients
$client->emit('broadcast', ['type' => 'notification', 'text' => 'Hello There!']);

And that concludes the discussion.

Installation and Operation of Node on Production Site

I was running CentOSv6 and installed node using this procedure. Then I built a simple php file that will be executed by cron so that the node server is automatically started or restarted if it is not running:

$nodePath = 'your node binary path here';
$filePath = 'your server.js file path';
shell_exec($nodePath . ' ' . $filePath);

and then set that file in cron to run at your desired intervals.

Important Notes

  • I was experiencing some difficulty obtaining the node path from the PHP script where it was initially installed, so I transferred the nodejs folder to my public html folder (the root of the application).
  • Instead of var socket = io.connect('/');, it must be var socket = io.connect('/YOUR SERVER IP:3001'); on the production server.
  • Check browse the bugs area if you are having trouble getting it to work over https.
  • Examine the examples folder on to learn how to send and receive messages using both PHP and Javascript.