Code Monkey home page Code Monkey logo

sse's Introduction

sse

build-img pkg-img reportcard-img coverage-img

Server-Sent Events (SSE) library for Go.

See https://www.w3.org/TR/eventsource for the technical specification.

Features

  • Simple API.
  • Performant.
  • Dependency-free.
  • Low-level API to build a server.

Install

Go version 1.13

go get github.com/cristalhq/sse

Example

As a simple HTTP handler:

http.HandleFunc("/sse", func(w http.ResponseWriter, r *http.Request) {
    stream, err := sse.UpgradeHTTP(r, w)
    if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    data := struct {
        Text string `json:"text"`
    }{
        Text: "hey there",
    }
    stream.WriteJSON("123", "msg", data)
})

See example_test.go for more.

Documentation

See these docs.

License

MIT License.

sse's People

Contributors

cristaloleg avatar dependabot[bot] avatar mr-linch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mr-linch

sse's Issues

Can't close connection server-side

Hey, I want to close connection from server-side. Here is MRE:

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/cristalhq/sse"
)

func main() {
	if err := http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		stream, err := sse.UpgradeHTTP(r, w)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		defer func() { 
			if err := stream.Close(); err != nil {
				log.Printf("close stream error: %s", err)
			}
		}()

		for i := 0; i < 3; i++ {
			if err := stream.WriteEvent(
				0,
				"test",
				[]byte(fmt.Sprintf("payload %d", i)),
			); err != nil {
				log.Printf("write event error: %s", err)
			}
		}

	})); err != nil {
		log.Fatal(err)
	}
}

Output

2023/10/05 00:55:10 http: response.Write on hijacked connection from github.com/cristalhq/sse.(*Stream).Close (stream.go:31)
2023/10/05 00:55:10 close stream error: http: connection has been hijacked

I'll send out an PR soon.

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.