My preference for something like this would be to use Java as I find it easier to manage background tasks like what you describe.
Backend
My approach would be to use Java EE to create a singleton startup thread that implements a scheduler service. The reason for using a singleton startup thread is so your job can run as a background process and is thus non-blocking, freeing up resources for the rest of your application. Also the thread can be accessed using a simply invoking a method on the class. You can schedule the task to read your file every 'n' seconds/minutes etc. for any updates and these can then be made available to your frontend.
Basic Example:
@Singleton
@Startup
public class Scheduler {
private static int count = 0;
private Weather weather;
public Weather getWeather() {
return weather;
}
@PostConstruct
public void onStartup() {
System.out.println("Initialization success.");
}
@Schedule(second="*/10", minute="*", hour="*")
public void execute() {
byte[] encoded = Files.readAllBytes(Paths.get("weather_updates.txt"));
String weatherUpdateStr = encoding.decode(ByteBuffer.wrap(encoded)).toString();
weather = new Weather();
weather.parse(weatherUpdateStr);
// Possible addition of logic for push to web socket
}
}
This basic example creates a sigleton thread as your web application container (I would recommend using JBoss 7) starts up. It then creates a scheduled task which executes every 10 seconds. The code provided does a basic Java 7 file read to a string and the weather.parse() should contain some logic to convert the string into a Weather object. The weather object is then ready to either be pushed via a web socket or polled via some AJAX request on the frontend.
Frontend
There are two possible approaches I would suggest here:
- Web sockets using HTML5
- AJAX calls
1. Web sockets
Web sockets were introduced into HTML5 as a way of providing dynamic content on a page without the need to refresh or use AJAX calls. Here is a great intoduction to websockets in HTML5. Here is another great example of how to set up HTML5 websockets with Java.
2. AJAX calls
jQuery provides a great API for AJAX. You can implement a Timeout task in jQuery that will intermittently execute some functionality. The functionality you will want to implement is and AJAX Get request.
Basic example:
$.ajax({
url: "getWeatherUpdate.html",
error: function(){
// will fire when timeout is reached
},
success: function(){
// Update your webpage with weather info
},
timeout: 3000 // sets timeout to 3 seconds
});