title | keywords | description | |||
---|---|---|---|---|---|
Server-Sent Events |
|
Implementing Server-Sent Events in an application. |
This example demonstrates how to implement Server-Sent Events (SSE) in a Fiber application.
Server-Sent Events (SSE) allow servers to push updates to the client over a single HTTP connection. This is useful for real-time applications where the server needs to continuously send data to the client, such as live feeds, notifications, or real-time charts.
- Go 1.16 or higher
- Go modules
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/sse
-
Install dependencies:
go mod tidy
-
Run the application:
go run main.go
-
The server will start on
http://localhost:3000
.
- GET /: Index page
- GET /sse: SSE route
- Open your browser and navigate to
http://localhost:3000
. - The client will automatically connect to the SSE endpoint and start receiving updates from the server.
The main Go file sets up the Fiber application and handles the SSE connections. It includes the necessary configuration to send events to the client.
The HTML file provides a simple user interface to connect to the SSE endpoint and display the received messages.
Server-Sent Events (SSE) is a standard allowing servers to push data to web clients over HTTP. Unlike WebSockets, which require a full-duplex connection, SSE uses a unidirectional connection from the server to the client. This makes SSE simpler to implement and more efficient for scenarios where only the server needs to send updates.
For more information on SSE, you can refer to the following resources: