8

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!

Shoan
  • 4,003
  • 1
  • 26
  • 29
Joe Torraca
  • 1,949
  • 5
  • 31
  • 50

2 Answers2

13

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.

TheOx
  • 2,208
  • 25
  • 28
  • 2
    +1 In addition, see [Pusher](http://pusher.com/), [Long Polling](https://en.wikipedia.org/wiki/Push_technology#Long_polling) – drew010 Feb 04 '12 at 04:31
  • Thank you so much! I have been trying to find this everywhere on the internet. Very thorough and great idea. Never thought of this and looking at the network requests in Facebook, thats what they do. Every 219 ms to be exact. One last question, using this method, is there any way for the server to get heavily bogged down after years of running with millions of requests on it? Thanks! – Joe Torraca Feb 04 '12 at 04:43
  • The possibility of bogging down your server is mainly related to how involved your web service is and how heavy your traffic is at any given moment. In other words if the service requires several lookups or other heavy processing and you're polling that service at a high frequency then your server could get bogged down. But if you're just doing a simple single query and returning the results then, assuming you don't have a large load on your database server, you should be ok. – TheOx Feb 04 '12 at 05:06
  • Ok thanks, I highly doubt my server will get bogged down then. – Joe Torraca Feb 04 '12 at 14:37
7

You can create a push notification service for your website utilizing websockets and graceful degradation to a long polling fallback for browsers that don't support websockets. This does require a healthy amount of technical/programming knowledge.

Some good resources for this are: http://socket.io (which uses a node.js backend for the web sockets and handles degradation) http://pusherapp.com (a commercial solution if you don't want to roll and maintain your own service)

For a list of browsers that support websockets, you can search for "caniuse" -- which provides great details to features supported by browser versions

Note: For multi-million user applications like Facebook, I would assume they've weighed the advantages of running websockets for 50+ million simultaneous users and concluded that keeping data consistent across all the nodes connected to the millions of sockets would be too much. I can imagine it would be a logic nightmare to do that on a socket system instead of a basic action sql-like infrastructure. I can't speak on their behalf though, this is simply my assumption. However, you'd be surprised how many sites have been using push and polling systems lately :)