Event-driven .NET: How to consume an SSE endpoint with HttpClient

Server-Sent Events (SSE) allow a client to subscribe to messages from the server. It creates a one-way stream from the server to the client. When the server has new messages for the client, it writes them to the stream. This is an alternative to the client polling the server for updates.

Use the following to consume an SSE endpoint with HttpClient:

using (var streamReader = new StreamReader(await httpClient.GetStreamAsync(url))) { while (!streamReader.EndOfStream) { var message = await streamReader.ReadLineAsync(); Console.WriteLine($"Received message: {message}"); } }

Take a look at the example console app below to see this used in context.

Example – Simple SSE client console app

When I’m developing an SSE endpoint, I like to use the following simple console app for consuming the endpoint.

static async Task Main(string[] args) { HttpClient client = new HttpClient(); client.Timeout = TimeSpan.FromSeconds(5); string stockSymbol = "VTSAX"; string url = $"http://localhost:9000/stockpriceupdates/{stockSymbol}"; while (true) { try { Console.WriteLine("Establishing connection"); using (var streamReader = new StreamReader(await client.GetStreamAsync(url))) { while (!streamReader.EndOfStream) { var message = await streamReader.ReadLineAsync(); Console.WriteLine($"Received price update: {message}"); } } } catch(Exception ex) { //Here you can check for //specific types of errors before continuing //Since this is a simple example, i'm always going to retry Console.WriteLine($"Error: {ex.Message}"); Console.WriteLine("Retrying in 5 seconds"); await Task.Delay(TimeSpan.FromSeconds(5)); } } }

Running the console app

The console app will stay running all day. When there’s an error, it simply retries in 5 seconds.

In the following example I didn’t have the SSE endpoint running at first, which is why it’s getting a connection error. Then I started it up and the client received multiple updates. Then I stopped the SSE endpoint, which is why it’s getting an error about the connection closing. Then I restarted it and it started receiving updates again.

Leave a Comment