Principle:A616567126 GPT WEB JAVA SSE Streaming
| Knowledge Sources | |
|---|---|
| Domains | Web_Protocols, Real_Time_Communication |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Communication pattern that enables a server to push real-time data to clients over a persistent HTTP connection using the Server-Sent Events (SSE) protocol.
Description
Server-Sent Events (SSE) is a standard web technology that allows a server to push updates to a client over a single, long-lived HTTP connection. Unlike traditional request-response HTTP, the server can send multiple events without the client issuing new requests. This addresses the limitation of polling-based approaches, which introduce latency and unnecessary network overhead.
In this repository, SSE is the primary mechanism for streaming GPT chat responses from OpenAI's API to the client. The server acts as a proxy: it opens an SSE connection to the OpenAI API using OkHttp's EventSource, receives token-by-token deltas, and forwards them to the browser via Spring's SseEmitter or to mobile clients via WebSocket.
Usage
Use this principle when designing systems that require real-time, server-pushed streaming data such as:
- LLM chat completions where tokens arrive incrementally
- Live progress updates for long-running tasks
- Any scenario where the server must push data without client polling
Prefer SSE over WebSocket for unidirectional server-to-client streaming; prefer WebSocket when the client also needs to send data on the same connection.
Theoretical Basis
SSE is built on the HTTP protocol with the text/event-stream content type. The connection remains open, and the server sends data as a series of events separated by double newlines.
Protocol Flow:
Client → Server: HTTP GET/POST with Accept: text/event-stream
Server → Client: HTTP 200, Content-Type: text/event-stream
Server → Client: data: {"choices":[{"delta":{"content":"Hello"}}]}\n\n
Server → Client: data: {"choices":[{"delta":{"content":" world"}}]}\n\n
Server → Client: data: [DONE]\n\n
Key Properties:
- One-way: Server to client only
- Persistent: Single HTTP connection reused for multiple events
- Auto-reconnect: Browsers automatically reconnect on disconnection
- Line-delimited: Events separated by double newlines
Practical Guide
In this repository, SSE streaming involves three layers:
- Upstream SSE:
ChatGPTStreamopens an OkHttpEventSourceto the OpenAI Chat Completions endpoint withstream: true - Event Processing:
AbstractStreamListenerextendsEventSourceListener, parses each SSE event as aChatCompletionResponse, extracts the delta content, and accumulates it - Downstream Delivery:
ConsoleStreamListener.onMsgdelegates toSendMessageUtil.sendMessage, which routes toSseEmitterServer(web) orWebSocketServer(mobile)
Related Pages
Implemented By
- Implementation:A616567126_GPT_WEB_JAVA_ChatGPTStream
- Implementation:A616567126_GPT_WEB_JAVA_ConsoleStreamListener
- Implementation:A616567126_GPT_WEB_JAVA_SendMessageUtil
- Implementation:A616567126_GPT_WEB_JAVA_GptController
- Implementation:A616567126_GPT_WEB_JAVA_SseController
- Implementation:A616567126_GPT_WEB_JAVA_MoonshotAiService