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?
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 :)
I think node.js will help a lot and will be fast. I would advice you to have a look at socket.io.
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.
精彩评论