I am trying my hand at server-sent events, but I cannot get it to work within an MVC project (not WebAPI). I haven't found any good samples online.
This is the server-side code I tried (including several failed attempts from various posts):
Function GetRows() as ActionResult
Dim ret = New HttpResponseMessage
' ret.Content.w
' Return ret
Response.ContentType = "text/event-stream"
Response.Write("data: " & "aaaa")
Dim flag = False
If flag Then
For i = 0 To 100
Response.Write("data: " & i)
Next
End If
Response.Flush()
'Return Content("Abv")
'Return Content(("data: {0}\n\n" & DateTime.Now.ToString(), "text/event-stream")
End Function
and here is the Javascript
var source = new EventSource("/Tool/GetRows");
source.onmessage = function (event) {
document.getElementById("messages").innerHTML += event.data + "<br>";
};
source.onerror = function (e) {
console.log(e);
};
For some reason it's always going into onerror
, and there is no information there what type of error it might be.
What am I doing wrong?
BTW, I don't think this action should really return anything, since my understanding is it should only be writing to the stream string by string.
EventSource expects a specific format, and will raise
onerror
if the stream doesn't match that format -- a set of lines of field/value pairs separated by a:
, with each line ending in a newline character:field
can be one ofdata
,event
,id
,retry
, or empty for a comment (will be ignored by EventSource).data
can span multiple lines, but each of those lines must start withdata:
Each event trigger has to end with a double newline
Note: If you are writing in VB.NET, you can't use the
\n
escape sequence to write newlines; you have to usevbLf
orChr(10)
.As an aside, EventSource is supposed to hold an open connection to the server. From MDN (emphasis mine):
Once control exits from an MVC controller method, the result will be packaged up and sent to the client, and the connection will be closed. Part of EventSource is that the client will try reopening the connection, which will once again be immediately closed by the server; the resulting close -> reopen cycle can also be seen here.
Instead of exiting from the method, the method should have some sort of loop that will be continuously writing to the
Response
stream.Example in VB.NET
Example in C#
Client-side:
Server-side: