Giter Site home page Giter Site logo

sse's Introduction

Server-Sent Events Writer

GoDoc Go Report Card Codecov License

sse is a lightweight Go library for writing Server-Sent Events (SSE). This library allows easy integration of SSE into web servers, providing real-time updates to connected clients.

Installation

To install the sse package, use the following command:

go get github.com/floriscornel/sse

Usage

Importing the Package

import (
    "net/http"
    "github.com/floriscornel/sse"
)

Creating a Server-Sent Events Writer

To create an SSE writer, use the NewResponseWriter function. This function requires an http.ResponseWriter and options to configure the SSE writer.

opts := sse.Options{
    ResponseStatus: http.StatusOK,
    Encoding:       sse.EncodeNone,
}

func handler(w http.ResponseWriter, r *http.Request) {
    sseWriter := sse.NewResponseWriter(w, opts)

    for {
        err := sseWriter.Write("message", map[string]string{"hello": "world"})
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        time.Sleep(2 * time.Second)
    }
}

http.HandleFunc("/events", handler)
log.Fatal(http.ListenAndServe(":8080", nil))

Encoding Options

The sse package supports various encoding options to compress the data sent to clients. Available encoding options include:

  • EncodeNone
  • EncodeDeflate
  • EncodeCompress
  • EncodeGzip
  • EncodeBrotli
  • EncodeZstd

Specify the desired encoding in the Options struct:

opts := sse.Options{
    ResponseStatus: http.StatusOK,
    Encoding:       sse.EncodeGzip,
}

Examples

You can find more examples in the examples directory. To run an example, navigate to the respective directory and execute the following command:

go run main.go

Example Structure

  • ping: A simple example of sending periodic ping messages to the client.
  • incremental-updates: Demonstrates sending incremental updates to the client with different event types.
  • number-of-listeners: Implements a counter to track the number of connected clients.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

If you have any questions, feel free to reach out:

sse's People

Contributors

floriscornel avatar dependabot[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.