views:

67

answers:

3

I am trying to create a live orders page for my website. I need the page to auto-update to show that a new order has been placed and to display an alert/sound whenever a new order row is in the database.

Any ideas on how i can easily achieve this?

Thanks, -AJay

+3  A: 

You will need to use something like Comet to be able to push data to the client.

Then, use a MySQL trigger to somehow raise an event in your server application that's holding the Comet connection open to push the appropriate data.

The less elegant way that many developers use (at least until WebSockets become popular) is to poll with AJAX for changes, but this has a high bandwidth overhead and a longer latency.

Ben S
+1 just say no to polling!
Byron Whitlock
I'd love it if everyone stopped polling, but the fact is getting long-lived HTTP connections to work seamlessly can be difficult.
Ben S
+1  A: 

From AJAX view you should use timers in javascript like this...

// First parameter is an expression and second is a interval as miliseconds.
setTimeout ( "UpdateFunction()", 2000 );

Also i recommended to you use this code...

setTimeout ( "UpdateFunction()", 5000 );

function UpdateFunction( )
{
  // (do something here)
  setTimeout ( "UpdateFunction()", 5000 );
}

your UpdateFunction() should call a php or asp page which renew list of orders.

Jalal Amini
If you don't expect new content every 5 seconds this solution leads lots of wasted bandwidth. Also, if you expect content more frequently than every 5 seconds, it would be nicer to receive the content faster. An adaptive polling timing might be better if push methods can't be used.
Ben S
You should reference UpdateFunction without the quotes and params to avoid eval. This would also work ... setTimeout(function() { alert('hi'); }, 5000);
bic72
Gmail using this technique to keep update list of mails and buzzes! I read Gmail's page's source code and setTimeOut was used for 5 times! in some cases interval was 100 or 250 ms!
Jalal Amini
A: 

I would think a polling approach would do you well, as server push has many negative implications for the browser.

If going with a polling-route, I would suggest having a timed event occur on your page that will call a web method. The web method would then return data (something small like an ID) about queued orders. Compare the list of IDs to what's currently fleshed out on the page, and assuming you have something in the newly given list that doesn't exist (or vice versa), call a separate method to retrieve the additional details to display display new orders from or delete old entries.

This way, you do not need to keep a steady stream to the server (which can block the user's browser from making additional content requests).

I hope that helped at all.

Lance May
What are the "many negative implications for the browser"?
Ben S
@Ben: Taken from your wikipedia link: "In particular, the HTTP 1.1 specification states that a browser should not have more than 2 simultaneous connections with a web server. However, holding one connection open for real-time events has a negative impact on browser usability. The browser may be blocked from sending a new request while it still loads, for example, a series of images."
Lance May
Which is why you only open the Comet connection after page load. Also note that when navigating away from the page the connection is reset as well. The connection shouldn't ever block loads. Also IE8+, Firefox, Chrome and Safari all allow for 6 connections max or more by default, only IE6/7 for HTTP 1.1 servers default to max 2 connections. To mitigate this further, have your Comet connection on a different sub-domain (i.e.: comet.example.com)
Ben S
@Ben: Yes, no arguments here. There are many workarounds to try and get the process to accommodate all scenarios. I suggested polling (like 90% of standard sites do) because he said the magic word "easily". You said yourself that "getting long-lived HTTP connections to work seamlessly can be difficult".
Lance May