├── README.md ├── client ├── client.js └── index.html └── server ├── data.txt └── server.php /README.md: -------------------------------------------------------------------------------- 1 | # php-long-polling 2 | 3 | A very simple demonstration of long-polling with AJAX (jQuery) and PHP. Long-polling makes near "real-time" 4 | applications possible. The client does not request new data every X seconds/minutes, the client gets new data 5 | delivered when there is new data (push-notification style). This is an improved, cleaned and documented 6 | fork of https://github.com/lincolnbrito/php-ajax-long-polling ! Big thanks, man. 7 | 8 | ## What is long-polling (and short polling) ? 9 | 10 | #### Short-polling 11 | 12 | Send a request to the server, get an instant answer. Do this every x seconds, minutes etc. to keep your application 13 | up-to-date. But: This costs a lot of requests. 14 | 15 | #### Long-polling 16 | 17 | Send a request to the server, keep the connection open, get an answer when there's "data" for you. This will cost you 18 | only one request (per user), but the request keeps a permanent connection between client and server up. 19 | 20 | ## How to use 21 | 22 | To test, simply change the URL in client/client.js to the location of your server.php file, for local testing 23 | `url: 'http://127.0.0.1/php-long-polling/server/server.php'` will do the job. Open the `client/index.html` to simulate 24 | the client. 25 | 26 | While having the index.html opened in your browser, edit the data.txt on the server (and save it). You'll see index.html 27 | instantly (!) getting the new content. Voila! 28 | 29 | You should get a good idea how everything works by looking into the code, I think it's self-explaining. 30 | 31 | ## In a real-world application ... 32 | 33 | This would work perfectly in a real-world application, BUT 34 | 35 | 1. There are better tools for this, like node.js, which can handle MUCH more concurrent connections and serve 36 | data faster, with much less memory usage and afaik while using only ONE thread. 37 | 38 | 2. Apache2 uses 18MB afaik per thread by default, so having a "permanent connection" with 100 clients will use a lot 39 | of memory. I'm currently experimenting with lighttpd, NGINX and appserver.io to find a better solution. 40 | 41 | ## You like that ? 42 | 43 | :) Then have a look at my blog [DEV METAL](http://www.dev-metal.com), at [my other repos](https://github.com/panique) or support this and other projects by renting a server at [DigitalOcean](https://www.digitalocean.com/?refcode=40d978532a20) or tip a coffee at BuyMeACoffee.com. Thanks! :) 44 | 45 | Buy Me A Coffee 46 | -------------------------------------------------------------------------------- /client/client.js: -------------------------------------------------------------------------------- 1 | /** 2 | * AJAX long-polling 3 | * 4 | * 1. sends a request to the server (without a timestamp parameter) 5 | * 2. waits for an answer from server.php (which can take forever) 6 | * 3. if server.php responds (whenever), put data_from_file into #response 7 | * 4. and call the function again 8 | * 9 | * @param timestamp 10 | */ 11 | function getContent(timestamp) 12 | { 13 | var queryString = {'timestamp' : timestamp}; 14 | 15 | $.ajax( 16 | { 17 | type: 'GET', 18 | url: 'http://127.0.0.1/php-long-polling/server/server.php', 19 | data: queryString, 20 | success: function(data){ 21 | // put result data into "obj" 22 | var obj = jQuery.parseJSON(data); 23 | // put the data_from_file into #response 24 | $('#response').html(obj.data_from_file); 25 | // call the function again, this time with the timestamp we just got from server.php 26 | getContent(obj.timestamp); 27 | } 28 | } 29 | ); 30 | } 31 | 32 | // initialize jQuery 33 | $(function() { 34 | getContent(); 35 | }); 36 | -------------------------------------------------------------------------------- /client/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |

Response from server:

8 |
9 | 10 | 11 | -------------------------------------------------------------------------------- /server/data.txt: -------------------------------------------------------------------------------- 1 | This is demo data from file data.txt ! Change (and save the file) to see the changed content INSTANTLY on your site ! 2 | -------------------------------------------------------------------------------- /server/server.php: -------------------------------------------------------------------------------- 1 | $last_ajax_call) { 34 | 35 | // get content of data.txt 36 | $data = file_get_contents($data_source_file); 37 | 38 | // put data.txt's content and timestamp of last data.txt change into array 39 | $result = array( 40 | 'data_from_file' => $data, 41 | 'timestamp' => $last_change_in_data_file 42 | ); 43 | 44 | // encode to JSON, render the result (for AJAX) 45 | $json = json_encode($result); 46 | echo $json; 47 | 48 | // leave this loop step 49 | break; 50 | 51 | } else { 52 | // wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes) 53 | sleep( 1 ); 54 | continue; 55 | } 56 | } 57 | --------------------------------------------------------------------------------