Skip to content

v1.0.0

Choose a tag to compare

@thomas-osgood thomas-osgood released this 05 Jul 23:09
· 5 commits to main since this release
2ff6cc5

Description

Initial release of the SSE Handler library.

This handler will add clients to the client map when they connect and remove them when they disconnect. This will automatically create a channel for each client that connects, allowing the function feeding the channels to focus on generating the information and now worry about the channel management.

Basic SSE functionality and endpoint function are included within this release.

// most likely a global variable ...
clientMap := make(ssehandler.SSEChannelMap)

// other code

hdl, err := ssehandler.NewSSEHandler(ssehandler.WithClientMap(clientMap))
if err != nil {
    // handle error
}

http.HandleFunc("/sse", hdl.EndpointFunc)

// other code
// in "worker function"

eventtype =  "message"

payload, err = json.Marshal(data)
if err != nil {
    // handle error
}

// this will transmit the data to all clients in the client map.
for _, curchan := range clientMap {
   curchan <- ssehandler.SSEMessage{Data: string(payload), Event: eventtype}
}