• AJAX From MySQL
    7 replies, posted
I need a function that does something on the page or reloads the page the minute something changes or comes in a mysql table. So if the table 'cars' got a new row the javascript immediatly loads something. I can do the what it does part later, I just need something to get me started.
Have the page do an AJAX GET request with the time it was last checked. Have the serverside code return what was changed since then (Basically you'll need a column with a last updated timestamp)
[QUOTE=compwhizii;22169458]Have the page do an AJAX GET request with the time it was last checked. Have the serverside code return what was changed since then (Basically you'll need a column with a last updated timestamp)[/QUOTE] So a repeating timer that pulls from a page? Sounds worth trying!
If you have any control over the server it might be worth looking into [url=http://en.wikipedia.org/wiki/Web_Sockets]WebSockets[/url]. Obviously its a bunch of new stuff to learn, but basically it opens a fully duplex connection to the web server and means that you don't have to constantly poll the server, the server will just tell the client when it has new data for it.
[QUOTE=snake logan;22174370]If you have any control over the server it might be worth looking into [url=http://en.wikipedia.org/wiki/Web_Sockets]WebSockets[/url]. Obviously its a bunch of new stuff to learn, but basically it opens a fully duplex connection to the web server and means that you don't have to constantly poll the server, the server will just tell the client when it has new data for it.[/QUOTE] Cool, but it seems really complicated.
[QUOTE=nivek;22174413]Cool, but it seems really complicated.[/QUOTE] Yeah, maybe something just to keep in mind for the future :)
This is how I do it on AnyHub: [code] var unixtime = 0; function updateFiles() { $.getJSON("/api/publicsince?t=" + unixtime, function(data) { unixtime = data.servertime; $.each(data.files, function(index, obj) { //alert(obj.name); $("#fresh").prepend( $('<li><a href="' + obj.url + '">' + obj.name + '</a><a href="' + obj.shortlink + '" class="act short" title="Shortlink">Shortlink</a></li>') .hide().fadeIn()); }); setTimeout(updateFiles, 5000); }); } updateFiles(); [/code] The server's JSON response looks like this: [code] { "servertime": 1234567890, "files": [ { "id": 42, "filename": "Cat", "url": "http://anyhub.net/file/Cat", "shortlink": "http://ahb.me/abc" }, { "id": 43, "filename": "Dog", "url": "http://anyhub.net/file/Dog", "shortlink": "http://ahb.me/def" } ] } [/code] [editline]02:32PM[/editline] It's fairly important that the server responds with it's current time, otherwise you may hit synchronization issues with timezones. Even if both sides use UTC, the two clocks may be off by a minute or so which could screw things up.
Thank you.
Sorry, you need to Log In to post a reply to this thread.