As promised here is my attempt at a tutorial for anyone interested in adding this to their projects. The key feature here is this tiny bit of javascript:
var source = new EventSource("/link");
Normally the client (web browser) sends a request to a server, the server sends data back, then a connection is closed. This feature of HTML5 creates a connection that is going to stay open, and even if something closes it the default is to retry the connection after 3 seconds.
While that connection is open the browser is going to listen for events from the server, which we can use to execute a function:
source.addEventListener("event_name", function(e) {
// do something, in my case we update the webpage with the data
document.getElementById("pin9").innerHTML = e.data;
}, false);
That's really it for the client side. Once the page loads it will start listening for data sent from the server, and like I said if something goes wrong it will close the connection, wait 3 seconds, then try again.
The real challenge is dealing with the server side of things and by that I mean our little Digispark Pro. Normally this stuff is handled by a server specific scripting language like php that does a lot of the wrapping and packaging. The browser is going to send a GET request to the "link" you provided. If you are using the
wifiserver example that will show up in the
path variable. In my example the EventSource asks for "/stream" which is handled by a simple
if statement.
if(path == F("/stream")){
// this section will process and send the stream data
}
Now for the first of two major challenges, the server needs to respond with a very specific header that should look something like:
Serial.println(F(" HTTP/1.1 200 OK "));
Serial.println(F(" Content-Type: text/event-stream "));
Serial.println(F(" Cache-Control: no-cache "));
Serial.println(F(" Connection: keep-alive "));
The second line is crucial, and what tells the browser to stay connected and listen for data. I should point out that I don't really know what I'm doing, but this chunk seemed to work. If we were using PHP most of that would happen for us.
The next part is a massive pain in the ass. The browser is waiting to hear a VERY specific message, sent in a very specific way:
event: name_of_event \n
data: data_to_send \n\n
The colons, spacing, and line returns all have to line up precisely or the browser will reject everything. For our purposes I set up a function that will stitch it all together and send it. I then followed it all with the standard termination "0" and blank line but I don't know if that's needed.
void sendStream(String event, String text) {
//sends the data of the stream response
String msg = "event: " + event + "\ndata: " + text + "\n\n";
Serial.print(msg);
}
So the browser sends a request, the spark then sends the special
text/event-stream header, and now the browser is listening for data. If you write "event: pin5 \ndata: Pin 5 is 12 \n\n" the browser will trigger the function associated with "pin5" and pass a variable
event.data="Pin 5 is 12". You can then update your browser to display that info.
The browser is still listening and will do so for about 2min. If you don't send anything else it will close the connection, wait 3 seconds, and then launch "/stream" again. What you'll want to do is set up a loop in your sketch that sends as much data as fast as you want. In my case I have it looping 60 times, with a 1 sec delay. My next goal will be to make the loop continuous and have something trigger it to break. Might be a physical push button, but more likely it will be a button on the browser that sends a "quit" message.
This is about all I know on the subject. If you read through the references they talk about attaching an id to each message, which is nice if timing is important, or you worry about missing bits of data and want things to line up. There is also a slightly easier way to do this but limits you to just one event called "message."
References:
http://www.w3schools.com/html/html5_serversentevents.asphttp://www.developerdrive.com/2012/03/pushing-updates-to-the-web-page-with-html5-server-sent-events/http://www.w3.org/TR/2012/WD-eventsource-20120426/