Documentation ¶
Overview ¶
Package sse implements functions to send events to a Server-Sent Events (SSE) stream. The format conforms to the ABNF specification here: https://www.w3.org/TR/eventsource/#parsing-an-event-stream.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Send ¶
Send sends the given events, in order, to a writer, returning the number of bytes written and any error.
If the writer implements http.Flusher, the Flush function is called before Send returns. An extra newline always follows the last event.
Example ¶
package main import ( "bytes" "fmt" "github.com/suiteserve/suiteserve/sse" ) func main() { var w bytes.Buffer _, err := sse.Send(&w, sse.WithComment("This is a\n comment."), sse.WithData("Hello\nthere,\r\n world\r!"), sse.WithId("123")) if err != nil { panic(err) } _, err = sse.Send(&w, sse.WithRetry(50), sse.WithRetry(100)) if err != nil { panic(err) } fmt.Println(w.String()) }
Output: :This is a : comment. data:Hello data:there, data: world data:! id:123 retry:50 retry:100
func SendWithBom ¶
SendWithBom sends a byte-order mark (BOM) followed by the given events, in order, to a writer, returning the number of bytes written and any error. A BOM must only appear at the start of an SSE stream, therefore this function should not be called after a call to Send on the same writer. It is rare to need to use this function at all.
If the writer implements http.Flusher, the Flush function is called before SendWithBom returns. An extra newline always follows the last event.
Types ¶
type Event ¶
func WithComment ¶
WithComment returns an Event that writes comment events to the stream.
func WithEventType ¶
WithEventType returns an Event that writes event type events to the stream.