Polling a web server for latest updates
Some websites change dramatically very often. For instance, Google News and Reddit are usually loaded with recent postings the moment we refresh the page. To maintain the latest data at all times, it might be best to run an HTTP request often.
In this recipe, we poll new Reddit posts every 10 seconds as summarized in the following diagram:
How to do it…
In a new file called Main.hs
, perform the following steps:
- Import the relevant libraries:
import Network.HTTP import Control.Concurrent (threadDelay) import qualified Data.Text as T
- Define the URL to poll:
url = "http://www.reddit.com/r/pics/new.json"
- Define the function to obtain the latest data from an HTTP GET request:
latest :: IO String latest = simpleHTTP (getRequest url) >>= getResponseBody
- Polling is simply the act of recursively conducting a task after waiting for a specified amount of time. In this case, we will wait 10 seconds before asking for the latest web data:
poll ::...