Skip to content

Server-Sent Events (SSE) handler is a generic compatible and http.Handler compliant module

License

Notifications You must be signed in to change notification settings

softwarespot/sse

Repository files navigation

Server-Sent Events (SSE) handler

Go Reference Go Tests

Server-Sent Events (SSE) handler is a generic compatible and http.Handler compliant module, that implements real-time event streaming from a server to web clients using the Server-Sent Events protocol. It's a robust module for managing multiple client connections, broadcasting events, and handling client registrations and unregistrations efficiently.

Examples of using this module can be found from the ./examples directory.

Prerequisites

  • Go 1.23.0 or above

Installation

go get -u github.com/softwarespot/sse

Usage

A basic example of using SSE.

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/softwarespot/sse"
)

func main() {
    // Use the default configuration
    h := sse.New[int](nil)
    defer h.Close()

    go func() {
        var evt int
        for {
            fmt.Println("sse handler: broadcast event", h.Broadcast(evt))
            evt++
            time.Sleep(64 * time.Millisecond)
        }
    }()

    http.Handle("/events", h)
    http.ListenAndServe(":3000", nil)
}

License

The code has been licensed under the MIT license.

About

Server-Sent Events (SSE) handler is a generic compatible and http.Handler compliant module

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published