sse

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2021 License: BSD-2-Clause Imports: 5 Imported by: 1

README

SSE Upgrade

This library exports a func that upgrades http requests to an event streaming connection by sending a Content-Type: text/event-stream header to the client.

http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
    var connection *sse.Connection
    connection, _ = sse.Upgrade(w, r)
    connection.SendString("thanks for connecting. here's some data.")
})

Connection's SendBytes([]byte), SendString(string) and SendJson(interface{}) funcs will format and send data to the client.

Use Close() when you're done streaming event data.

A connection's BuildMessage() func can be used to send a payload with the id and event attributes.

connection.BuildMessage().WithId("id").WithEvent("event").SendString("data")

This message will be sent to the client:

id: id
event: event
data: data

Example

package main

import (
	"github.com/eighty4/sse"
	"log"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	connection, err := sse.Upgrade(w, r)
	if err != nil {
		w.WriteHeader(500)
		log.Println("sse upgrade error", err.Error())
		return
	}
	connection.BuildMessage().WithEvent("auth").SendString("token")
	connection.Close()
}

func main() {
	http.HandleFunc("/", handler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}
Original Repository

I found originating source for sse.go on GitHub a couple of years ago, but I couldn't find the repository to reference when publishing updates.

Documentation

Overview

Package sse provides a utility for upgrading http requests to event streaming connections with server sent events (SSE).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection provides channels for sending event messages, closing the connection and receiving errors from writing to the http response

func Upgrade

func Upgrade(writer http.ResponseWriter, request *http.Request) (*Connection, error)

Upgrade sends headers to client to upgrade the request to an SSE connection and returns a Connection handle for sending messages.

func (*Connection) BuildMessage

func (connection *Connection) BuildMessage() *MessageBuilder

BuildMessage returns a MessageBuilder, a fluent-style builder api for sending events

func (*Connection) Close

func (connection *Connection) Close()

Close sends a shutdown signal to close the connection for streaming data

func (*Connection) IsOpen

func (connection *Connection) IsOpen() bool

IsOpen returns whether connection is still open for sending event data

func (*Connection) SendBytes

func (connection *Connection) SendBytes(data []byte) error

SendBytes sends a series of bytes for an event's data without an id or event field

func (*Connection) SendJson

func (connection *Connection) SendJson(data interface{}) error

SendJson marshals data into a json string for an event's data and sends it without an id or event field

func (*Connection) SendString

func (connection *Connection) SendString(data string) error

SendString sends a string for an event's data without an id or event field

type Message

type Message struct {
	// contains filtered or unexported fields
}

Message contains id, event and data attributes of an event message

type MessageBuilder

type MessageBuilder struct {
	// contains filtered or unexported fields
}

MessageBuilder is a fluent-style builder api for sending events

func (*MessageBuilder) SendBytes

func (messageBuilder *MessageBuilder) SendBytes(data []byte) error

SendBytes sends a series of bytes with the specified id and event attributes

func (*MessageBuilder) SendJson

func (messageBuilder *MessageBuilder) SendJson(data interface{}) error

SendJson marshals data into a json string and sends it without an id or event field

func (*MessageBuilder) SendString

func (messageBuilder *MessageBuilder) SendString(data string) error

SendBytes sends a string with the specified id and event attributes

func (*MessageBuilder) WithEvent

func (messageBuilder *MessageBuilder) WithEvent(event string) *MessageBuilder

WithEvent adds an event attribute to event data

func (*MessageBuilder) WithId

func (messageBuilder *MessageBuilder) WithId(id string) *MessageBuilder

WithId adds an id attribute to event data

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL