README
¶
sse
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
Documentation
¶
Index ¶
- Constants
- func LastEventID(r *http.Request) string
- type BinaryMarshaler
- type Error
- type Stream
- func (s *Stream) Close() error
- func (s *Stream) Flush() error
- func (s *Stream) SetEvent(event string) error
- func (s *Stream) SetID(id int64) error
- func (s *Stream) SetRetry(retry time.Duration) error
- func (s *Stream) WriteBinary(message BinaryMarshaler) error
- func (s *Stream) WriteBytes(data []byte) error
- func (s *Stream) WriteEvent(id int64, event string, data []byte) error
- func (s *Stream) WriteFloat(num float64) error
- func (s *Stream) WriteInt(num int64) error
- func (s *Stream) WriteJSON(v interface{}) error
- func (s *Stream) WriteString(data string) error
- func (s *Stream) WriteText(message TextMarshaler) error
- type TextMarshaler
- type Upgrader
Examples ¶
Constants ¶
View Source
const ( ErrNotFlusher = Error("sse: not an HTTP-flusher") ErrNotHijacker = Error("sse: not an HTTP-hijacker") )
Variables ¶
This section is empty.
Functions ¶
func LastEventID ¶
LastEventID returns a last ID known by user. If it's not presented - empty string will be returnes
Types ¶
type BinaryMarshaler ¶
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
func UpgradeHTTP ¶
func (*Stream) WriteBinary ¶
func (s *Stream) WriteBinary(message BinaryMarshaler) error
func (*Stream) WriteBytes ¶
func (*Stream) WriteFloat ¶
func (*Stream) WriteString ¶
func (*Stream) WriteText ¶
func (s *Stream) WriteText(message TextMarshaler) error
type TextMarshaler ¶
Click to show internal directories.
Click to hide internal directories.