Blazor and SSE (Server-Sent Events)

Viewed 541

is possible to implement use Server-Sent Events in blazor apps? Or there is other method to listening for request/response to API?

<script>
    source = new EventSource('/Home/Messaggio');
    source.onopen = function (ddd) {
        console.log("aperta");
    };
    source.onerror = function (ddd) {
        console.log("errore");
        console.log(ddd.data);
        source.close();
    };

    source.onmessage = function (ddd) {
        $("#Messaggio").text(ddd.data);
        console.log(ddd.data);
    };
</script>

Is method to get data in javascript code and add the data to C# blazor variable List responseObject?

1 Answers

.Net (and therefore Blazor) provides a standard library called SignalR that abstracts the main "bidirectional communication technologies": Long polling, SSE, Websocket...etc. That means you don't need to deal with SSE, Websockets...etc. Just use SignalR

Update:

Since your server does not use SignalR, I suggest you write (or import) a Javascript SSE client to communicate with the server, then use JSInterop to pass the results to your C# code.

Related