• Stars
    star
    425
  • Rank 102,094 (Top 3 %)
  • Language
    PHP
  • Created about 11 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An extremely simple example of a "real-time" self-updating page using long-polling.

php-long-polling

A very simple demonstration of long-polling with AJAX (jQuery) and PHP. Long-polling makes near "real-time" applications possible. The client does not request new data every X seconds/minutes, the client gets new data delivered when there is new data (push-notification style). This is an improved, cleaned and documented fork of https://github.com/lincolnbrito/php-ajax-long-polling ! Big thanks, man.

What is long-polling (and short polling) ?

Short-polling

Send a request to the server, get an instant answer. Do this every x seconds, minutes etc. to keep your application up-to-date. But: This costs a lot of requests.

Long-polling

Send a request to the server, keep the connection open, get an answer when there's "data" for you. This will cost you only one request (per user), but the request keeps a permanent connection between client and server up.

How to use

To test, simply change the URL in client/client.js to the location of your server.php file, for local testing url: 'http://127.0.0.1/php-long-polling/server/server.php' will do the job. Open the client/index.html to simulate the client.

While having the index.html opened in your browser, edit the data.txt on the server (and save it). You'll see index.html instantly (!) getting the new content. Voila!

You should get a good idea how everything works by looking into the code, I think it's self-explaining.

In a real-world application ...

This would work perfectly in a real-world application, BUT

  1. There are better tools for this, like node.js, which can handle MUCH more concurrent connections and serve data faster, with much less memory usage and afaik while using only ONE thread.

  2. Apache2 uses 18MB afaik per thread by default, so having a "permanent connection" with 100 clients will use a lot of memory. I'm currently experimenting with lighttpd, NGINX and appserver.io to find a better solution.

You like that ?

:) Then have a look at my blog DEV METAL, at my other repos or support this and other projects by renting a server at DigitalOcean or tip a coffee at BuyMeACoffee.com. Thanks! :)

Buy Me A Coffee

More Repositories

1

huge

Simple user-authentication solution, embedded into a small framework.
PHP
2,142
star
2

mini

Just an extremely simple naked PHP application, useful for small projects and quick prototypes. Some might call it a micro framework :)
PHP
1,342
star
3

php-login-minimal

An extremely minimal login / register script in pure PHP.
PHP
601
star
4

mini2

Just an extremely simple naked PHP application, useful for small projects and quick prototypes.
PHP
414
star
5

mini3

Just an extremely simple naked PHP application, useful for small projects and quick prototypes.
PHP
273
star
6

php-login-one-file

PHP
219
star
7

vagrant-lamp-bootstrap

A super-simple Vagrantfile / bootstrap.sh to setup a LAMP stack inside Vagrant 100% automatically
Shell
132
star
8

php-sass

Compiles SASS to CSS automatically with pure PHP. No ruby used. One line of code. Currently uses latest SCSS 3.2 syntax, imports and mixins. Compass can also be used.
PHP
124
star
9

tiny

TINY is an extremely simple naked demo PHP application that runs nearly config-free everywhere.
PHP
80
star
10

laravel-sass

Automatic SASS-to-CSS compiling for Laravel 4 (and any other framework too), config-free, in pure PHP, works with latest SASS 3.2 .scss syntax, imports and mixins
PHP
71
star
11

pdo-debug

Emulates the SQL query constructed by PDO. The magic behind: A simple function that combines your parameters and the raw query. Not perfect, but does the job.
PHP
69
star
12

html5-video

A simple copy&paste solution for crossbrowser-safe HTML5 video with a flash fallback. 100% free and open-source. Works on all browsers. All.
HTML
58
star
13

php-login-styles

THIS PROJECT IS DEPRECATED AND HAS REACHED END OF LIFE
44
star
14

php-sass-watcher

One simple PHP file that "watches" your SASS file folder and compiles all .scss automatically every X seconds. Sounds weird, but works perfectly. Automatic SASS compiling with 100% PHP. No Ruby used.
PHP
28
star
15

simple-php-api

An extremely simple API (with authentication) example, written in PHP (server) and JS (client), using JSON
PHP
24
star
16

phpstorm-theme-github-remixed

Get GitHub's syntax highlighting / colours and fonts within PHPStorm
19
star
17

simple-php-api-client

A PHP client for using a JSON based API
Shell
13
star
18

phpstorm-theme-github-remixed-2

Get GitHub's 2014 style syntax highlighting (colours and fonts) within PHPStorm
13
star
19

php-logo

This repo tries to professionalize the current PHP logo, offering versions that fit print / web / broadcast standards, containing vector-, pixel- and css-based versions.
11
star