Introduction

AI-powered chat applications have become a common feature in modern software. Whether you're building a customer support chatbot, an internal knowledge assistant, or an AI coding helper, users expect responses to appear instantly instead of waiting for the entire message to be generated.

This is where Server-Sent Events (SSE) become useful. Instead of waiting for the AI to finish generating a complete response, the server can stream the response to the client one piece at a time. This creates a smoother and more interactive user experience, similar to popular AI chat applications.

In this article, you'll learn what Server-Sent Events are, how they work with ASP.NET Core, and how to build a simple AI chat application that streams responses in real time.

What Are Server-Sent Events?

Server-Sent Events (SSE) is a web technology that allows a server to continuously send data to a client over a single HTTP connection.

Unlike traditional HTTP requests, where the client sends a request and waits for the complete response, SSE keeps the connection open and sends updates whenever new data becomes available.

For AI chat applications, this means users can see the response being generated word by word or token by token instead of waiting several seconds for the complete answer.

Some common use cases include:

Why Use SSE Instead of Polling?

Many applications use polling, where the client repeatedly sends requests asking if new data is available.

Polling works, but it has several disadvantages:

With SSE, the client establishes a single connection, and the server pushes updates whenever new content is available.

PollingServer-Sent Events
Multiple HTTP requestsSingle long-lived connection
Higher network overheadLower overhead
Delayed updatesNear real-time updates
More server processingMore efficient streaming

For applications where data flows from the server to the client, SSE is often a simpler and more efficient solution.

Creating an SSE Endpoint in ASP.NET Core

ASP.NET Core makes it straightforward to create an SSE endpoint.

The following example streams a few messages to the client.

app.MapGet("/chat/stream", async (HttpContext context) =>
{
    context.Response.Headers.Append("Content-Type", "text/event-stream");

    for (int i = 1; i <= 5; i++)
    {
        await context.Response.WriteAsync($"data: Message {i}\n\n");
        await context.Response.Body.FlushAsync();

        await Task.Delay(1000);
    }
});

The important parts are:

Receiving Streamed Messages in the Browser

On the client side, JavaScript provides the EventSource API for working with SSE.

const source = new EventSource("/chat/stream");

source.onmessage = function (event) {
    console.log(event.data);
};

Each time the server sends a new message, the browser receives it immediately without refreshing the page.

This creates a smooth, real-time experience for users.

Streaming AI Responses

Instead of sending fixed messages, an AI model typically generates text gradually.

The server can stream each generated chunk as soon as it becomes available.

foreach (var chunk in aiResponseChunks)
{
    await context.Response.WriteAsync($"data: {chunk}\n\n");
    await context.Response.Body.FlushAsync();
}

As each chunk arrives, the client appends it to the chat window.

This approach makes the application feel much faster because users start reading the response immediately.

Simple Chat Flow

A typical AI chat application using ASP.NET Core and SSE follows these steps:

  1. The user enters a question.

  2. The browser sends the question to the server.

  3. The server forwards the request to an AI model.

  4. The AI model generates the response in small chunks.

  5. ASP.NET Core streams each chunk through the SSE connection.

  6. The browser updates the chat window in real time.

This streaming approach improves responsiveness without requiring the client to repeatedly request updates.

Benefits of Using SSE for AI Applications

Server-Sent Events offer several advantages for AI-powered chat systems:

For applications that only need server-to-client communication, SSE is often easier to implement than more complex alternatives.

Best Practices

When building AI chat applications with ASP.NET Core and SSE, keep these recommendations in mind:

Conclusion

Server-Sent Events provide a simple and efficient way to build real-time AI chat applications with ASP.NET Core. By streaming responses as they are generated, you can create a more interactive experience that feels faster and more natural for users.

While technologies like WebSockets are useful for full-duplex communication, SSE is often the better choice for AI chat scenarios where data primarily flows from the server to the client. By combining ASP.NET Core, asynchronous programming, and streaming responses, you can build scalable chat applications that deliver a smooth and responsive user experience.