Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - AJAX jQuery Server "Push" System

Tags:

jquery

ajax

php

I am creating a web app that requires a live notification system. How would I set up my server to pull data from a mySQL database and then push it to the browser. I have absolutely NO idea how to do this. If anybody can help, it would be much appreciated! Thank you so much!

EDIT: I should probably be more specific, I am pulling data as in XYZ recently created an account, XZY recently ... Thanks so much!

like image 535
Joe Torraca Avatar asked Feb 04 '12 04:02

Joe Torraca


Video Answer


1 Answers

You cannot push data to a browser, but what you can do is set up your webpage to poll your server every few seconds for updates. An example setup would be:

From within your website, have a javascript function that runs on a timer every few seconds (or whatever interval works best for your situation).

Start that timer on page load.

That javascript function invokes an AJAX call to a web service on your web server (more on that in a second).

On the server side you'll need some sort of system that tracks these events and stores them somewhere such as in a database table with a timestamp. So for example when XYZ creates an account, that would be logged in this "event" table in the db.

The web service called by the AJAX call will then run a query on that table and retrieve all entries since the last time it was called. Then just update the webpage with those results.

It's obviously not 100% "live" as there will be a small delay depending on what time interval you set in the JS timer but it's pretty close.

like image 139
TheOx Avatar answered Oct 11 '22 02:10

TheOx