SoFunction
Updated on 2025-03-04

Python implements streaming mode based on SSE

introduction

In modern web application development, optimization of user experience is a very important goal, especially in scenarios involving real-time data updates. Streaming Mode is an efficient data transmission method, allowing users to obtain streaming return effects in the form of typewriter output. This method is implemented through Server-Sent Events (SSE) technology, bringing a unique user experience.

What is streaming mode?

Streaming mode, as the name suggests, is to continuously send data through streaming rather than return all at once. Unlike the traditional HTTP request mode, the feature of the streaming mode is that the server can continuously send data to the client after the connection is opened. This real-time transmission method can not only speed up the response speed of data, but also reduce bandwidth usage, making the application run smoother.

Taking the typewriter output as an example, suppose we enter a query request on the web page. In traditional mode, the page will wait for the server to return the complete result before it will be displayed. In streaming mode, the server will gradually send data, and the client can immediately present the received data to the user, creating a "typewriter" output effect. This method significantly improves the user's waiting experience and makes the application performance more dynamic and vital.

SSE: The core technology of streaming mode

The implementation of streaming mode is inseparable from the support of Server-Sent Events (SSE) technology. SSE is a technology that pushes real-time updates from servers to clients in web pages, and is one of the HTML5 standards. It allows the server to continuously send data to the client without the client making continuous requests. In streaming mode, SSE is widely used to implement streaming return effects of typewriter output.

How SSE works

SSE is based on the HTTP protocol and can enable a persistent connection through simple GET requests. The server uses Content-Type: text/event-stream to mark the returned data stream, and can then keep the connection by sending data regularly. When the data reaches the client, the browser will automatically trigger the message event for processing. The data stream format supported by SSE is relatively simple, and each piece of data is sent in the form of event blocks and ends with double line breaks.

The basic event block format of SSE is as follows:

event: Event name
data: Data content

Each event consists of two fields: event and data, where data is the actual transmission content, and event is used to mark the type of event. The client can perform different processing according to the type of event, such as displaying different content or triggering specific interaction effects.

Advantages of SSE

SSE and WebSocket are both technologies commonly used for real-time data push, but compared to WebSocket, SSE's advantage is that it achieves simple, has more stable data flow control and has an automatic reconnection mechanism. For scenarios where one-way data streams are required (i.e., server pushes them to clients), SSE is a lightweight and efficient choice. In addition, SSE also has good compatibility and can run well in mainstream browsers.

Implement SSE-based streaming mode

In streaming mode-based web applications, we can use JavaScript and server-side SSE support to achieve typewriter output effects. The following is a simple implementation example showing how to implement streaming data reception and presentation on the client via SSE.

Implementation of the server

Let's assume that the server uses the FastAPI framework, and the following is a simple server-side code example:

from fastapi import FastAPI
from  import StreamingResponse
import time

app = FastAPI()

def generate_stream():
    for i in range(1, 11):
        yield f"data: Message {i}\n\n"
        (1)

@("/stream")
async def stream():
    return StreamingResponse(generate_stream(), media_type="text/event-stream")

In this example, we define a generate_stream function to generate data streams, using yield to send message content step by step. The StreamingResponse class is used to send a data stream to the client as a response and specifies media_type="text/event-stream" to identify that this is an SSE stream.

Client implementation

On the client side, we can use JavaScript's EventSource API to receive data streams and display them in real time:

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="UTF-8" />
    <title>SSE Streaming mode example</title>
  </head>
  <body>
    <h1>Real-time message flow</h1>
    <div ></div>

    <script>
      const messagesDiv = ("messages");
      const eventSource = new EventSource("/stream");

       = (event) => {
        const newMessage = ("p");
         = ;
        (newMessage);
      };

       = () => {
        ("There is an error in connection, reconnecting...");
      };
    </script>
  </body>
</html>

In the client HTML page, we create an EventSource object to connect to the server's SSE stream. The onmessage event handler adds content to the page when each message is received, resulting in real-time updates. This implementation method is simple and efficient, allowing users to experience a line-by-line display effect similar to a typewriter.

Advantages of application scenarios and streaming modes

Streaming mode has a wide range of applications, especially suitable for applications that require fast response and real-time updates:

  1. Real-time data monitoring: In scenarios such as financial transactions, sensor data monitoring, etc., the streaming mode allows users to obtain data in real time and respond quickly.

  2. Chat app: In instant messaging applications, streaming mode allows chat messages to arrive instantly, improving the chat experience.

  3. News push: For news websites or information flow applications, SSE can instantly push the latest content to users to improve user stickiness.

  4. Streaming Q&A System: In the intelligent question and answer system, streaming mode can be used to simulate the process of answer generation to improve the authenticity and nature of the interaction.

Comparison of streaming mode and WebSocket

Despite the many advantages of streaming mode and SSE, WebSocket may be a better choice in some scenarios where bidirectional communication needs are high. Unlike SSE's one-way communication, WebSocket supports two-way communication between clients and servers, so it is more suitable for some applications such as chat systems or online games that require two-way interaction. However, SSE is lighter and more efficient for pure server-to-client data push requirements.

Future Outlook of Streaming Model

With the development of front-end technology and the increasing demand for real-time experience for users, the application of streaming mode will become more and more extensive. SSE provides a stable data transmission method while ensuring simple implementation, and is expected to be adopted in more scenarios in the future. In addition, with the gradual popularity of the HTTP/3 protocol, lower latency and higher transmission efficiency will also bring new opportunities for further optimization of streaming modes.

The above is the detailed content of Python's streaming mode based on SSE. For more information about Python SSE streaming mode, please follow my other related articles!