How to Install, setup and implement Node.js in your PHP application

How to Install, setup and implement Node.js in your PHP application

2 Minutes Read


What is Node.js?
Typically in web response paradigm, the client always initiates communication, but with the help of Node.js now both the client and server can initiate communication, allowing them to exchange data freely.

In simple words, after a database update the client needs to request the server to get the latest data. But using Node.js, a client can send the updated data to Node.js and the Node.js will distribute the data across all the clients. So, no need to initiate request by each client to the server to get the latest data.

Where we can use Node.js?
Below are the few examples where you can get the best out of Node.js,

  • A counter which updates after a record insert into database.
  • Show real-time activity like Twitter or Facebook.
  • Implement Desktop notification to notify users about what others are doing.
  • You can build a chatting tool using the non-blocking, event-driven I/O paradigm of Node.js

Install and setup Node.js

  1. Install NodeJS and NPM
  2. Install using NPM
  3. Install Forever

Run the below notification.js file forever using the “Forever”. (it should not stop)


var fs = require('fs');
/* If you are using SSL, uncomment the below lines*/
var options = {
    key: fs.readFileSync('/var/www/html/YOUR_CERTIFICATE_FOLDER/your_project_key.key'),
    cert: fs.readFileSync('/var/www/html/YOUR_CERTIFICATE_FOLDER/your_project_crt.crt'),
    ca: fs.readFileSync('/var/www/html/YOUR_CERTIFICATE_FOLDER/')
var app = require('https').createServer(options, handler);

var app = require('http').createServer(handler); // For non-ssl server
var io = require('').listen(app);
function handler (req, res) {
	res.end("Welcome to");
/** This section is for receiving and sending message **/
var Room;
io.sockets.on('connection', function (socket) {
	socket.on('subscribeTo', function (data) {
		Room =;
		console.log('Connecting client to: ';
	socket.on('iotoserver', function (data) {
		console.log('here we are in action event and data is: \n-----------------------------------------------------');
		//var dataJSON = JSON.parse(data);
		//socket.broadcast.emit(, { message: 'A new socket added and sending message.' });'iotoclient', { message: data.message });
		//'game').emit('message', 'cool game');

NOTE: Your Node.js should run with the port “3002”. Try to use another sub-domain/domain to call your Node.js server. Another server is always a good option.

Implement Node.js in your application

Use (a client) to send message to Node.js.
Get the ElephantIO package from,
You need the Client.php and the AbstractPayload.php file only.

use ElephantIO\Client as ElephantIOClient;

$elephant = new ElephantIOClient('', '', 1, false, false, true);
ElephantIOClient::TYPE_EVENT, null, null, json_encode(array('name' => 'iotoserver', 'args' => array('channel' => 'my_first_channel', 'message' => 'my message to all the online users')))

Put this JavaScript in the page, where you want to receive the real-time notification

<script src=""></script>
  var client = io.connect('');
  client.on('connect',function (data) {
  	client.emit('subscribeTo', { channel: 'my_first_channel' });
  client.on('iotoclient', function (data) {
} catch(e){
	console.log('Socket ERROR\n');

See Also: Creating a custom handler session in CakePHP 2.x

Like this blog? I’d love to hear about your thoughts on this. Thanks for sharing your comments.

Prakash S. Satpathy
1 Comment
  • Avatar
    hello world
    Posted at 20:47h, 06 June Reply

    how the file runs forever you stupid gay?

Post A Comment

Exit pop up

Sad to see you leaving early...

From "Aha" to "Oh shit" we are sharing everything on our journey.
Enter your email address to stay up to date with the latest news.
Holler Box