PHP is acronym for hypertxt preprocessor which is the most widely used severside scripting language in the world, even scratch.MIT.edu uses PHP for outputting the website and punBB our beloved Forum is yet another example of epicness of php,
but theres much more u actually require in these days of web 2.0 which is the accessiblity of the user to change the of data / browsing the web in realtime.
Well lemme show u the diffence b/w realtime and non realtime web .
Think of scratch Home page now if u open the page once and goto drink a coffee/or take a dinner , u come back and see the same page now in that meantime there must be like 10/20 new projects uploaded to the MIT Server ( actually more then what i am typing as u scratcth is so loved) but u dont see the change in the server page u will still see the copy like a while ago.This is a non - realtime web -app example.
now consider facebook you open your homepage over facebook and goto drink a coffee if som1 sends a msg / writes on your wall / comments/tags u on a pic. you will be notified as soon as he does so . this is a realtime application.
These days i am developing a lots and lots of realtime applications so i am going to discuss some of my UGLY experiences and how to NOT fall in these..
1. LONG POLLING : the best alternative and rather simplest solution for pseudo realtime application is refreshing the page in every n seconds ( can be any number even a 0.0002)
and asking data from the server a thousand times.
I wrote a chat application using that and heres the data i got
day 1 Users ; 40 PageHits : over a million Data Transfer was over a Gigabyte just on day 1
extremely unacceptable as my aim of providing multiple chats will literally kill my bandwidth and bankcurropt my host. For low options thats extremely easy to do and for websites which does NOT require extreme fast update the solutin is to making n>1second
but what i was making required much more then what we had.
Option 2: Comet : Comet is a commercial solution for this problem the aim of comet is open a request to the server untill unless it gets the response for the client. But again this is extremely overheading to the server as it has to process the Handshake everytime the client reconnects to it. Its a gr8 solution for websites like scratch if it goes realtime ever. but when we consider what i was working on as HTML based MMORPG realism the model is extremely slow!
Option 3 : XHR streaming : consider watching a youtube video the data is sent to you as a stream and is sent to u continously untill unless the whole video is sent over to you . Sounds intresting and is rather easy . all you have to do is open a request to server and flush data from the server accordingly until the client disconnects
something like
<?php
// DO LOGIN JOB
ignore_user_abort(); // this will make it run the whole script even if the client closes
while(!client_aborted()){
// Do something like send messages etc. blah blah blah
echo "<br> <br>"; // this magical line shows ur output on Gchrome which doesnt works
//else
flush();ob_flush(); //to make sure it works on all browsers
}
// DO LOGOFF JOB
?>This also requires another PHP script to input to the server (ah thats easy if u know php)
the problem i had with this model was that when opening multiple requests over APACHE webserver the number of open threads of apache literally killed my server and i had to reboot it in order to make a simple change ,, that madness with just 8 users on the chat client (thank god we didnt had 800 or i would have literally had to call my host to plug the server out just to reboot it)
4:: Final Solution :: (old is gold). php_socket();
A socket is something like an open connection from the client - server through which full duplex data transmission is possible and PHP incredibly supports the socket as a native of it . Sounds easy Aye ? not really while working with sockets u had to actually deal with the data in a differnt way as POST/GET methods of seeking data wont work u have to make the server deal with every single thing and even might have to create ur own methods for data input and output, but the the benefit of using stream is that u can detect when a client closes the connection without further overhead . BEST sOLUTION
Last edited by fanofcena (2011-07-06 03:02:14)
Offline
Why am i posting this here ? .????
Well with the modifications and addons of scratch 1.4 which can do XHR requests and the upcoming 2.0 (flash has native support for streams)
its possible to actually make realtime RICH applications i mean MMO's using scratch!!!!!!!!
Offline