Skip to main content
Version: 2.8.x(Latest)

Server-Sent Events(SSE)

Github Source: https://github.com/gogf/examples/tree/main/httpserver/sse

Description

This example demonstrates how to implement Server-Sent Events (SSE) using GoFrame. Server-Sent Events is a technology that allows a server to push updates to a client via HTTP connection. Unlike WebSocket, SSE is unidirectional - only the server can send data to the client.

The example implements a simple SSE server that streams responses character by character, simulating a real-time typing effect commonly seen in modern AI chat applications.

Requirements

Structure

- go.mod: The Go module file.
- main.go: The main application entry point with SSE implementation.

Features

  • Server-Sent Events implementation
  • Character-by-character streaming response for simulated AI chat
  • Proper HTTP headers for SSE

Setup

  1. Clone the repository:

    git clone https://github.com/gogf/examples.git
    cd examples/httpserver/sse
  2. Install the dependencies:

    go mod tidy
  3. Run the application:

    go run main.go

Usage

  1. Run the example:

    go run main.go
  2. The server will start at: http://127.0.0.1:8000

  3. Access the SSE endpoint:

Implementation Details

The SSE implementation includes:

  • Setting the correct HTTP headers for SSE:

    • Content-Type: text/event-stream
    • Cache-Control: no-cache
    • Connection: keep-alive
  • Streaming response with proper flushing

  • Simulated typing delay using time.Sleep

  • Simple HTTP routing with GoFrame's router

Notes

  • SSE connections remain open until explicitly closed by the client
  • The server must flush the response buffer after each message
  • For production use, consider implementing reconnection logic and event IDs
  • SSE works over regular HTTP/HTTPS and supports automatic reconnection in browsers