Skip to content

AdminXVII/go-libsse

Repository files navigation

go-libsse Go Report CardBuild Status GoDoc

Server-Sent Events for Go

Inspired from alexandrevincenzi's work

About

Server-sent events is a method of continuously sending data from a server to the browser, rather than repeatedly requesting it, replacing the "long polling way".

go-libsse is a small, thread-safe library to create a Server-Sent Events server in Go.

Features

  • Fully thread-safe
  • Custom initialization after connection from browser (for example to patch the gap in last-event-id)
  • Custom headers (useful for CORS)
  • Last-Event-ID support (resend lost messages)
  • Follow SSE specification

Getting Started

package main

import (
    "log"
    "net/http"
    "strconv"
    "time"

    "github.com/AdminXVII/go-libsse"
)

func main() {
    // Create the server.
    s := sse.NewServer(nil)

    // Register with /events endpoint.
    http.Handle("/events/", s)

    // Dispatch messages to channel-1.
    go func () {
        for {
            s.SendMessage(sse.Message{Data: time.Now().String()})
            time.Sleep(5 * time.Second)
        }
    }()

    http.ListenAndServe(":3000", nil)
}

Connecting to our server from JavaScript:

e1 = new EventSource('/events/');
e1.onmessage = function(event) {
    // do something...
};

About

A simple thread-safe library for Server-Sent Events (SSE) using Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages