When a server side event is added to a stream, how do I make sure all clients currently connected to that stream receive the event, i.e. how do I loop through all clients and send them the message before discarding the message , is this even possible with sse and Go?
summarised pseudo code of what I want to achieve below
package main
import (
"github.com/gin-contrib/sse"
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.New()
router.Use(gin.Logger())
var events = make(chan sse.Event, 100)
router.GET("/api/addUser/event", func(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "access-control-allow-origin, access-control-allow-headers")
// if events chan has an event
// Send event to all connected clients
if( we have events then send them to all clients){
event := <-events
_ = sse.Encode(c.Writer, event)
}
})
router.POST("/api/addUser", func(c *gin.Context) {
//On user add
//Add event to events chan
events <- sse.Event{
Event: "newChiitiko",
Id: "1",
Data: "New Chiitiko Event",
}
c.JSON(http.StatusOK, "okay")
})
_ = router.Run(":5000")
}