andrewstuart/sparknode — pull request #16 by nicjansma (2014-06-30 00:39:43+00:00)
Title: If the events HTTPS request stops, restart it

I'm using sparknode to listen long-term for an event from my core, which is hooked up to a water sensor. The event is basically `omg-your-house-is-filling-with-water`, so I want to ensure that my app is listening for the event reliably.

Currently, my app will quit every hour or so. Tracing the cause, it appears that the spark HTTPS event stream drops the connection after X minutes of inactivity, even with Keep Alive on. Once the HTTPS stream ends, there are no outstanding events for Node.js to wait for, so my app exits.

This change listens for an `end` event to fire from the `res`ponse. If we see an `end` event, we simply immediately request a new connection. To do this, the HTTP setup functionality was moved into a new `listenForSparkEvents()` function.

This change has been tested on my core. It picks up dropped connections and initiates a new connection successfully.

Note, a few additional lines with trailing spaces were changed as well.