Description
SSE sends response only in Event struct which follows Message and Data format. Need a Event format that sends the response in the struct defined by user. Behaviour same as c.JSON()
When consumers use client sdk like OpenAI for streaming- https://github.com/openai/openai-python?tab=readme-ov-file#streaming-responses
Expectation is to receive the response in json format
{
"model": "gpt",
"text": "text response"
}
Using c.SSEvent() the response would be
message= output
data=
{
"model": "gpt",
"text": "text response"
}
Expectations
Of having an option to respond similar to c.JSON()
Comment From: flc1125
I can give it a try, just give me some time.
Comment From: flc1125
You can use the c.Stream()
.
Example:
package main
import (
"fmt"
"io"
"sync/atomic"
"time"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/ping", func(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
var counter int64 = 0
c.Stream(func(w io.Writer) bool {
_, _ = w.Write([]byte(fmt.Sprintf("data: %d\n\n", atomic.LoadInt64(&counter))))
time.Sleep(1 * time.Second)
return atomic.AddInt64(&counter, 1) <= 10
})
})
router.Run(":7777")
}
Result:
https://github.com/gin-gonic/gin/assets/14297703/c40db866-cc0e-4ad5-a93f-369f49813b3d
Comment From: Axb12
You can use:
c.SSEvent("", msg)
=
result:
But it is "data" not "data: "