Issue using server side events support
maggie44 opened this issue · 0 comments
maggie44 commented
When I use SSEs without compression, I get each message flushed back one at a time. When I enable the compression it seems to continue to buffer instead of flushing the data back.
Here is a small example to replicate:
curl -N http://localhost:8090
curl -N --output --compressed http://localhost:8090
package main
import (
"fmt"
"time"
"github.com/CAFxX/httpcompression"
"github.com/gin-gonic/gin"
wraphh "github.com/turtlemonvh/gin-wraphh"
)
func main() {
r := gin.Default()
compress, err := httpcompression.DefaultAdapter()
if err != nil {
panic(err)
}
r.Use(wraphh.WrapHH(compress))
r.GET("/", func(c *gin.Context) {
// Set the appropriate headers for SSE
c.Writer.Header().Set("Content-Type", "text/event-stream")
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Connection", "keep-alive")
// Create a channel to send updates to the client
updateCh := make(chan string)
defer close(updateCh)
// Start a goroutine to send updates
go sendUpdates(c.Writer, updateCh)
// Simulate sending updates every second
for {
updateCh <- "Hello, World! " + time.Now().Format("2006-01-02 15:04:05")
time.Sleep(1 * time.Second)
}
})
r.Run("0.0.0.0:8090")
}
func sendUpdates(w gin.ResponseWriter, updateCh <-chan string) {
for update := range updateCh {
// Write the update to the client
fmt.Fprintf(w, "data: %s\n\n", update)
w.Flush()
}
}