Ajax warning when a row is added to mysql database

I am trying to create a live orders page for my site. I need a page for automatic updates to show that a new order has been set and to display a warning / sound whenever a new order line is in the database.

Any ideas on how I can easily achieve this?

Thanks, -AJay

+4
source share
3 answers

You will need to use something like Comet to transfer data to the client.

Then use the MySQL trigger to somehow raise an event in your server application to open a Comet connection to push the corresponding data.

A less elegant way for many developers to use it (at least until WebSockets is popular) is to poll with AJAX for changes , but it has high bandwidth and a lot of latency.

+4
source

From an 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 ); 

I also recommended that you use this code ...

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

your UpdateFunction() should call a php or asp page that updates the order list.

+1
source

I would think that the polling approach will do you well, since server push has a lot of negative consequences for the browser.

If you go with a polling route, I would suggest that your page has a time event that will invoke the web method. Then the web method will return data (something as small as an ID) about the orders in the queue. Compare the list of identifiers with what is currently appearing on the page, and assuming that you have something in the new list that does not exist (or vice versa), call a separate method to obtain additional data to display the display of new orders from or delete old entries.

Thus, you do not need to maintain a constant stream on the server (which can block the user's browser from additional requests for content).

I hope this helps.

0
source

Source: https://habr.com/ru/post/1314234/


All Articles