I am trying to send an event from the server to the client and register it on their end.
my go code:
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, "id: %v
event: msg
message: %v
", id, "hello")
fmt.Printf("id: %v
event: msg
message: %v
", id, "hello")
id++
Client side JS:
function dump_event(e) {
console.log("received event")
$("#output").append("id: " + e.lastEventId + "
event: " +e.type + "
message: " + e.data + "
");
}
$(function() {
$("#listen").on('click', function (e) {
e.preventDefault()
if (source != null) {
source.close();
}
source = new EventSource($("#backend").val() + $("#channel").val());
source.addEventListener('msg', dump_event);
source.addEventListener('timeout', dump_event);
});
While fiddler does show that I actually received data, it is not console logged. I would really appreciate some help, am I doing something wrong?