Contents
What Is NodeJS?
Typically in web response paradigm, the client always initiates communication, but with the help of NodeJS 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 NodeJS, a client can send the updated data to NodeJS and the NodeJS 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 NodeJS?
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 NodeJS
Install And Setup NodeJS
- Install NodeJS and NPM
- Install socket.io using NPM
- Install Forever
Run the below notification.js file forever using the “Forever”. (it should not stop)
notification.js
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/your_project_ca-bundle.ca-bundle') }; var app = require('https').createServer(options, handler); */ var app = require('http').createServer(handler); // For non-ssl server var io = require('socket.io').listen(app); app.listen(3002); function handler (req, res) { res.writeHead(200); res.end("Welcome to socket.io."); } /** This section is for receiving and sending message **/ var Room; io.sockets.on('connection', function (socket) { socket.on('subscribeTo', function (data) { if(Room){ socket.leave(Room); } Room = data.channel; console.log('Connecting client to: '+data.channel); socket.join(data.channel); }); 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(dataJSON.channel, { message: 'A new socket added and sending message.' }); socket.broadcast.to(data.channel).emit('iotoclient', { message: data.message }); //io.sockets.in('game').emit('message', 'cool game'); console.log(data); }); });
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 NodeJS In Your Application
Use Elephant.io (a socket.io client) to send message to NodeJS.
Get the ElephantIO package from, https://github.com/Wisembly/elephant.io/tree/master/src
You need the Client.php and the AbstractPayload.php file only.
<?php use ElephantIO\Client as ElephantIOClient; include("ElephantIO/Client.php"); $elephant = new ElephantIOClient('http://www.your-node-server.com:3002', 'socket.io', 1, false, false, true); $elephant->setHandshakeTimeout(1000); $elephant->init(); $elephant->send( ElephantIOClient::TYPE_EVENT, null, null, json_encode(array('name' => 'iotoserver', 'args' => array('channel' => 'my_first_channel', 'message' => 'my message to all the online users'))) ); $elephant->close(); ?>
Put this JavaScript in the page, where you want to receive the real-time notification
<script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script> <script> try{ var client = io.connect('http://www.your-node-server.com:3002'); client.on('connect',function (data) { client.emit('subscribeTo', { channel: 'my_first_channel' }); }); client.on('iotoclient', function (data) { alert(data.message); }); } catch(e){ console.log('Socket ERROR\n'); console.log(e); } </script>
See Also: Creating a custom handler session in CakePHP 2.x
Are you looking to Install, setup and implement NodeJS in your PHP application? We are here to help you. We have experienced NodeJS developers to provide all types of JavaScript solutions. Let’s discuss
Like this blog? I’d love to hear about your thoughts on this. Thanks for sharing your comments.