- Download and install it:
go get github.com/itcomusic/sse
- Import it in your code:
import "github.com/itcomusic/sse"
Field Retry
is seconds waitting reconnect client to server after
connection had lost.
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
Header: map[string]string{
"Access-Control-Allow-Origin": "*",
},
})
First argument function HandlerHTTP must be cid client's. CID must be unique. CID is key for save clients(consumers). CID can be generate with uuid packages or smt else. Identical CID is prohibited, if CID are found, new client do not connect to server.
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
http.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
handleSSE.HandlerHTTP("cid", w, r)
})
Every event with Data
has optional attribute DisabledFormatting
. If
it is enabled, Data
will be formatted.
You can disabled formatting. It is necessary to deny creating additional
fields data
. If you want send json data and it contains \n
, desirable
set DisabledFormatting = true
.
Send event all clients
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
serveSSE.SendEvent(&Event{
Data: &DataEvent{
Value: "testMessage",
},
})
Send event only to selected clients. CID are slice IDs
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
serveSSE.SendEvent(&EventOnly{
CID: []interface{}{"cid1", "cid2"},
Data: &DataEvent{
Value: "testMessageOnly",
},
})
Send event except to selected clients. CID are slice IDs
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
serveSSE.SendEvent(&EventExcept{
CID: []interface{}{"cid1", "cid2"},
Data: &DataEvent{
Value: "testMessageOnly",
},
})
Send event which it has time in seconds waitting reconnect client to server after connection had lost. Also new clients will be get this time.
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
serveSSE.SendEvent(&EventRetry{
Time: time.Second * 15,
})
Gives information about count connected clients, including inactive - client has not been removed yet from map.
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
serveSSE.CountConsumer()
Notifications inform about connected, disconnected, reconnected clients
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
serveSSE.HandlerConnectNotify(func(cid interface{}) {
// cid is id (client)consumer, when connected and ready
})
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
serveSSE.HandlerDisconnectNotify(func(cid interface{}) {
// cid is id (client)consumer, when he disconnected from server side event
})
Reconnect notify execute when client made reconnect to server side event.
Server can send events and client do not get them, because connection with server
was lost. ReconnectNotify will decide this problem. It allows
detect ID event, and you can send lost events. After sending lost events, must
close recovery channel, call StopRecovery()
import "github.com/itcomusic/sse"
handleSSE := sse.New(&sse.Config{
Retry: time.Second * 3,
})
serveSSE.HandlerReconnectNotify(func(rec *sse.Reconnect interface{}) {
// Reconnect struct shows information about client, which reconnected
// to server side event.
defer rec.StopRecovery()
})