DEV Community

Aayush
Aayush

Posted on

5

Using server sent events in ASP.NET

I am assuming you already have your ASP.NET project ready, I will just straight into implementing SSEs for sending realtime updates to client.

1. Create a Controller for sending SSEs.

public class EventsController : ControllerBase
    {
        private static readonly List<StreamWriter> _clients = [];

        [HttpGet]
        public async Task Subscribe()
        {
            Response.Headers.Append("Content-Type", "text/event-stream");
            Response.Headers.Append("Cache-Control", "no-cache");
            Response.Headers.Append("Connection", "keep-alive");

            var streamWriter = new StreamWriter(Response.Body);
            _clients.Add(streamWriter);

            try
            {
                while (!HttpContext.RequestAborted.IsCancellationRequested)
                {
                    await Task.Delay(TimeSpan.FromSeconds(1), HttpContext.RequestAborted);
                }
            }
            finally
            {
                _clients.Remove(streamWriter);
            }
        }

        public static async Task NotifyClients(string data)
        {
            List<StreamWriter> deadClients = [];

            foreach (var client in _clients)
            {
                try
                {
                    await client.WriteAsync($"data: {data}\n\n");
                    await client.FlushAsync();
                }
                catch
                {
                    deadClients.Add(client);
                }
            }

            foreach (var deadClient in deadClients)
            {
                _clients.Remove(deadClient);
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

2. Use the NotifyClients method to send SSEs
Where ever in your code you want to send an event just call the NotifyClients methods with any data. It will be sent to all subscribed clients.

[HttpPost]
public async Task<IActionResult> CreatePost(CreatePostDto createPostDto)
{
    // save new post to database

    // send SSE to all the clients
    await EventsController.NotifyClients("post created");

    return Ok("Post created successfully");
}
Enter fullscreen mode Exit fullscreen mode

3. Usage in client-side

const connectToSSE = () => {
    const eventSource = new EventSource('/api/events');

    eventSource.onmessage = (event) => {
        console.log('New data received:', event.data);
        // Do something
    };

    eventSource.onerror = (error) => {
        console.error('SSE error:', error);
        eventSource.close();
    };
};
Enter fullscreen mode Exit fullscreen mode

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong ยท web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video ๐ŸŒถ๏ธ๐Ÿ”ฅ

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

๐Ÿ‘‹ Kindness is contagious

Please show some love โค๏ธ or share a kind word in the comments if you found this useful!

Got it!