I would like to build a realtime dashboard where as soon as there is any update, the script could poll and push the updates to the dashboard. Can anybody tell what would be the best way to do this. Will node.js help?
3 Answers
Update As of 8-10-2013 : The point of library and support lacking for node.js has mostly disappeared infact due to libs like mean.io you can actually get yourself one up and running in matter or hours.
Anush as told in the above two replies which were really informative you have 2 solutions for the server side.
1. Node.js Based 'Event based' server
2. PHP/ASP.net based polling/comet/forever script based server
to the best of my knowledge and experiences Node.js and Socket.io has a very big upperhand because of the simplicity and there purpose of development for realtime web - applications , But then "Php is stable powerful" so here are a few of the benifits and drawbacks of both the above mentioned frameworks.
PHP: Benifits:
Stability
Power
Ease of programming if you are familier with C++ like backend
A huge online library for support (php.net)
Mechanisms on PHP for RealTime API's with drawbacks DrawBacks
Generally when using Long Polling method your server has to process n requests from same user per second. this 1 request contains 8KB of request data + cookies to be sent to the server for fetching information. Which increases server overhead , and you might also want to authorise the client everytime (for better security) hence 1 function of authorisation will be called n times aswell which actually increases server load. with lower n you lose your epic realtime. with higher n you create a million requests with 21 people in 4 hours.
While using Comet you will have to keep requests open for sometime and continously poll the database or your server API for changes in your dashboard . Which is blocking the whole script and increases the server load rebellously because of the no of APACHE / IIS threads open. (Comet relies on Keep Alive Server), Its exceptional solution if used with a powerful server so depending on your server make a wise choice.
When using a forever open request by AJAX . where you open 1 request continously and keep it open forever to listen for server side push you extremely overload the server again as in case 2 . I would totally not recommend this i wrote a applet like this on http://chats.ws/ feel free to see the source code and inspect it in console /firebug. but the server crashes when there are like 8 people on the client.
Php Sockets (this is a 1 shot BSD Socket Solution in built in php), the problem here is you will be bound to a few hundred users at most. This is the best solution otherwise.as this gives you the "power" and "stability" of php with ease of use with web-sockets. For client you can use the free distributed XMLsocket API .But such styles sockets are better written on Java or C++ instead.
PHP is good choice in case you require not very awesome realtime api's and its the best language to write dynamic web-pages but when it comes to writing a bit faster applications PHP falls back as it blocks the server and eats up more resources.
Now coming the the second solution.
Node.js Node is an event based server software meaning it acts only when an event is happening say for instance when Apache or PHP starts they come and tell the host OS that they will handle requests on a specific port say port 80 now they continuously stay in memory and eat resources even when not used. Like in comet/Forever Open Connections the internal poll to fetch data from database keeps an open APACHE thread forever eating up your resources, but in case of Node the application starts node.js comes in action and it tells the OS to let it known when a request is made on a specific port then it goes to sleep(ie does not does anything and leaves the OS to handle it) when a request comes node.js completes the request and again goes to sleep meaning it works when its required and on other cases the memory stays free and resources like CPU usage are low .
Node.js is fairly new and quite unstable sometimes but if written nicely it boosts the performance of your application amazingly. It has a good support and you might want to visit the javascript chatroom on chat.stackoverflow.com to get help among node.js with socket.io as backend.
What socket.io does is that it allows the programmer to write only his/her application and NOT the basic things required for communication it automatically handles the transportation methods in the Order
- Web Socket
- Flash Socket.
- JsonP Polling
- XHR POLLING
doing this it makes sure that your program runs with the same speed and quality on all the web-browsers and Node.js as the server.
When you learn node.js you will understand how easy it is to write things which were extremely hard on php. for instance say on php forever open the code to send user the data when availibe from database (say mysql for now)
<?php
ob_implicit_flush(true);
set_timeout(0);
$T = 0; // for the first run send all the events
while(true){
$query = mysql_query("select * from database where TimeStamp > ".$T);
if(mysql_num_rows(query)>0){ // That is omg we have an updated thing
$T = microtime(true); // this gives the variable the value of current time stamp
while ($row = mysql_fetch_assoc($query))
{ $result = process($row) // say u have to do some processing on the row
echo json_encode($result); // this will send the JSON formation to your client for faster processing
}
}
}
?>
Now you will also need something to manage the input to database and so on so 1 more file in .php format to take input.
Instead of this the node.js code written with socket.io will look like
// Assume all functions are declared.
var io = require("socket.io").listen(82);
io.sockets.on('connection',function(client){
client.on('authorise',function(info){
var signin = Authorise(info); // say the info packet contains information about client user pwd and the Authorise function checks for the data to be true or false and then acts accordingly returns a bool status which is true on success with autorisation_id(a member variablE)for the client and false on failure with the reason on failure in the reason member variable inside the signin object.
socket.emit('authorised',signin);
});
client.on('request',function(data){
var result = process(data);
client.emit('reply',result); // yes you can also send straight javascript objects
});
client.on('some_other_event', function(ev){ // SOmething happend such as some long task completed or some other client send a message that can be sent this way.
client.emit('event',ev);
});
.
.
// and so on
});
On client side a
<script src="server:82/socket.io/socket.io.js"></scirpt>
<script>
var connection = io.connect("server:82");
connection.on('event',function(data){
Process_Event(data); // some function which processes event data
});
connection.on('authorised',function(data){
Auth(data); // Some function which tells client hey you are authroised or not and why not
});
connection.on('reply',function(data){
parse_reply(data); // some function that tells the client what is the reply of the request by server and what it has to do
});
function Authorise(){ // Lets assume its called by some html event
username = document.getElementById('username').value;
password = document.getElementById('password').value;
socket.emit('authorise',{usr:username,pass:password}); // sends the request to server
}
function Request(req) {} // this is a function which is called by some script gives it the request as the parameter when calling
{
socket.emit('request',req); // sends request to server
}
</script>
If you have NOT noted already there is NO for/while(true) loop in the latter and hence it doesnt blocks server in any way this isthe basic upperhand of using node.js with socket.io over php Polling , Comet , or BSD Sockets.
Now well choice is yours! Choose wise :) as your application depends on it feel free to ask more doubts on chat.stackoverflow.com or on here :)

- 6,795
- 3
- 37
- 48
-
Very well put together, you make a good point. When I said he didn't need node.js, I meant exactly that, he didn't need it, this makes a good case of why he would want it though. Oh and you mis-pelled an end script tag (was kinda bothering me). – Jess Aug 10 '11 at 18:57
-
And I hope you don't mind me linking to your answer from mine – Jess Aug 10 '11 at 19:03
-
1no problem .. i made this a community wiki ;) so you guys can edit and give your inputs aswell – ShrekOverflow Aug 11 '11 at 04:27
-
It is well put together, I hadn't known much about node.js (mainly because I have a distaste for javascript), but it has some very interesting features, I may have to give it another look – Jess Aug 11 '11 at 04:45
-
Node.js was not too tasty for me too in the beginning as coming from a solid C++ / PHP background i was quite unfamiliar with the ease of connection it provides the first class objects might be confusing but you should be always able to find help on the JavaScript chat on chat.stackoverflow.com , btw Node.js worths learning (if you do not plan to write the whole server on C++ ) – ShrekOverflow Aug 12 '11 at 09:36
I think node.js will help a lot and will be fast. I would advice you to have a look at socket.io.

- 60,935
- 33
- 147
- 186
-
I told him the difference between node and php :-) with my biiiiig answer , probably he might get it (if he reads it though) Feel free to edit my reply sir if you find any faults or mis-concepts in it – ShrekOverflow Aug 10 '11 at 10:55
I'd look at ajax, it's a system that uses javascript & css to retrieve information from a back end (php, asp.net, etc) while not reloading the entire page.
Then there is reverse ajax, which basically opens a connection to the server, but the server won't return anything keeping the connection open. This keeps the client from having to poll the server (repeatably ask the server) for information which cuts down on bandwidth costs for both the user and server. The client can just open a connection, and the server can send information when it's available (replicates pushing information to the client, even though the client started the connection).
Also, here is an article going more indepth about reverse-ajax, and the different implementations: http://gmapsdotnetcontrol.blogspot.com/2006/08/exploring-reverse-ajax-ajax.html (thanks jacob)
node.js is a server side implementation of JavaScript (to my knowledge). This isn't needed, a back end can be written in php, asp.net, or really anything. Though you should read Darkyen's answer, he brings up some great points on why node.js is a better backend then say php for this type of work. What you will want though is a library to make ajax easy. I use jquery, but there are plenty of great libraries to choose from, including prototype, Mootools, and YUI.
I hope this helps you.
-
1The technique in the second paragraph is usually called "Comet" or "Long Polling." Searching for more information using those terms will help. – Jacob Aug 04 '11 at 04:28
-
Thanks for the heads up, I found a good article on the different types, and put a link in – Jess Aug 04 '11 at 04:33
-
@mazzzzz Node.js is not just a server side implementation of javascript its actually an event based server scripting language which is written mostly on C for speed, the actual functionality of node.js is that it supports very very realtime applications Comet and LongPolling both has huge problems comet increases server load rebellously , long polling increases the overhead required to process the information. Now node.js only uses resources when they are needed instead of long polling and comet this actually runs faster and eats less resource . so i think node is a better solution – ShrekOverflow Aug 10 '11 at 09:58
-
& reverse ajax might kill the server with 100 clients as 1 user will be 1 open thread and on PHP/Apache based server thats rebellously overloading your server – ShrekOverflow Aug 10 '11 at 11:10
-
NODE.js does taste good wont be a good reason to not using it. It's perfect for a chat application as you dont need to pull from server, but server push infos to the client when needed. And that means much lower bandwidth needed. -1 – Alireza Jul 15 '12 at 08:25
-
If you read my answer I simply stated it isn't the only solution, not that it "didn't taste good." If you wanted real performance you'd write your own REST or SOAP implementation in C++. My point was simply you didn't have to only use Node, because any language can do it; PHP even has an extension to add event handling. Though I guess I shouldn't be trying to explain this to what sounds like one of the sheep following the newest, shiniest technologies (which, oddly enough, are commonly also the least reliable solutions). – Jess Jul 15 '12 at 18:01