Search code examples
ajaxplayframeworkjobs

Play Framework Asynchronous css view update using Ajax


I'm currently creating an application using Play!, and i wanted to update my view according to values from my database.

I have created a monitoring job which purpose is to send http request to severals sites every 30 minutes and to record a value into database for each site : if the site has responded, it records 1, otherwise it records 0.

Beside of that, i wanted to view on my application main template little color dots for each site : if the site is responding, it will be green, and if not, it will be red. This color update must be done asynchronously. This dots will be displayed for every pages of my application.

So, i can use my job, which is already an asynchronous thing, to change dots colors when it's necessary, but i don't know if it's possible to update views from a job. Or i can create an ajax script to update colors using css but i don't know how to trigger it when my database flags values changes.

Thanks for your help !


Solution

  • You can't update views from a job. But you could update it with a WebSocket: http://www.playframework.org/documentation/1.2.4/asynchronous#UsingWebSockets But it's not supported on all browsers yet.

    I think the standard way to do this is to call a controller (which ask your db) from ajax every X seconds and update the dots colors depending on the response. Be careful in choosing your refresh rate, if you have a lot of client, you server will have a lot of requests.