Giter Site home page Giter Site logo

gogo-sse's Introduction

go-sse

Build Status Coverage Status Go Report Card Go Reference MIT License

This package attempts to provide a robust and reliable implementation of server-sent events. One might use this package if they were writing an application that needed to connect to an SSE server endpoint and read events in a continuous stream or provide events to a front-end service.

Features

Here's what you get with this package:

  • Complete documentation for every last type and method in the package
  • Compliancy with section 9.2 of the WHATWG HTML specification
  • Extensive test suite to ensure conformance

go-sse requires a minimum of Go 1.18.

Basic Usage

To use the package in your application, begin by importing it:

import "github.com/lampctl/go-sse"

Use as a Client

Create a client using:

c, err := sse.NewClientFromURL("http://example.com/sse")
if err != nil {
    // ...
}

You can now read events directly from the c.Events channel as they are received:

for e := range c.Events {
    fmt.Println("Event received!")
    fmt.Println(e.Data)
}

Note that if the connection is closed or interrupted, the client will attempt to reconnect as per the spec and continue returning events from where it was interrupted. This is all handled behind the scenes and won't affect the status of the event channel.

When you are done receiving events, close the client:

c.Close()

Use as a Server

The server component is provided via Handler, which implements http.Handler:

h := sse.NewHandler(nil)

// ...or if you want to customize initialization:
h := sse.NewHandler(&sse.HandlerConfig{
    NumEventsToKeep:   10,
    ChannelBufferSize: 4,
})

To send an event, simply use the Send() method:

h.Send(&sse.Event{
    Type: "alert",
    Data: "The aliens are invading!",
    ID:   "12345",
})

When you are done, use the Close() method:

h.Close()

gogo-sse's People

Contributors

nathan-osman 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.