Reputation: 591
In our web application there are data tables. Those data tables are updating automatically through ajax requests. So we are sending ajax requests each 5 seconds to server. As a result so many requests and so much bandwidth is wasted. I want to know whether there is a option that update data tables there is only change in database. When new record is added to database, automatically generate a request and update client web page. My current code is as follows. Our server side language is php and database is mysql.
<script>
var dtable= $('#ajax_load_table').DataTable( {
"ajax": '<?php echo base_url(); ?>dashboard/jobs_approved_json_refresh',
columns: [
{ data: "job_code" },
{ data: "cus_name" },
{ data: "priority" },
{ data: "department" },
{ data: {
_: "last_action.display",
sort: "last_action.s_order"
} },
{ data: "status" },
{ data: "action","width": "30%" }
],
"order": []
} );
window.setInterval(function () {
dtable.ajax.reload();
}, 5000);
</script>
Upvotes: 4
Views: 1011
Reputation: 447
(Just some basic theory here to get you started): The concept you are looking for is called Comet, more specifically the idea of the HTTP server-side push. Also, long-polling is necessary in your case (you need a server push, but in your circumstances that will not actually be possible. But, the long-polling 'technique' will serve to emulate that push you need for the server to give that warning to the client browser)
As you're running PHP, I'd say this answer gives you very appropriate guidance for your specific case:
Best of luck in finding your way
Upvotes: 2
Reputation: 838
Web Socket,
Long Polling
Server-Sent Events
As for me, I'd prefer Long Polling because it works everywhere the ajax works.
Upvotes: 3
Reputation: 982
**you can use socket.io and emit an event when updating done and get it in client side find more at http://socket.io/
Upvotes: 1