gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
79.01k stars 8.03k forks source link

SSE example not working on live #3658

Open icobani opened 1 year ago

icobani commented 1 year ago

Description

SSE example not working on live

I'm working on my localhost, everything is fine, but I'm broadcasting on the live server SSE is not working. Does anyone have any suggestions for checkpoints.

I see on server logs ------0*0a0*0***** lines.

I used nginx to my server.

func GETAssetSSE(c *gin.Context) {
    v, ok := c.Get("clientChan")
    if !ok {
        fmt.Println("clientChan not found 1")
        return
    }
    clientChan, ok := v.(dbconfig.ClientChan)
    if !ok {
        fmt.Println("clientChan not found 2")
        return
    }
    c.Stream(func(w io.Writer) bool {
        // Stream message to client from message channel
        if msg, ok := <-clientChan; ok {
            fmt.Println("msg", msg)
            c.SSEvent("message", msg)
                fmt.Println("------0**0*a0*0*********")
                fmt.Println("------0**0*a0*0*********")
                fmt.Println("------0**0*a0*0*********")
                fmt.Println("------0**0*a0*0*********")
                fmt.Println("------0**0*a0*0*********")
                fmt.Println("------0**0*a0*0*********")
                fmt.Println("------0**0*a0*0*********")
                fmt.Println("------0**0*a0*0*********")

            return true
        }
        return false
    })
}

Environment

icobani commented 1 year ago

Done :)))))


func HeadersMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Writer.Header().Set("Content-Type", "text/event-stream")
        c.Writer.Header().Set("Cache-Control", "no-cache")
        c.Writer.Header().Set("Connection", "keep-alive")
        //c.Writer.Header().Set("Transfer-Encoding", "chunked")

        c.Writer.Header().Set("X-Accel-Buffering", "no")
        c.Next()
    }
}