Streaming OpenAI API responses with Server-Side Events by using iris framework Golang

package main

import (
	"time"

	"github.com/gin-contrib/sse"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

func main() {
	app := iris.New()

	app.Get("/test", func(ctx iris.Context) {
		ctx.ServeFile("./templates/index.html")
	})
	Init(app)
	app.Listen(":4000")
}

func Init(app *iris.Application) {
	mvc.Configure(app.Party("/"), func(m *mvc.Application) {
		// m.Router.Use(NewBroker().ServeHTTP)
		m.Handle(NewSSEController())
	})
}

type SSEController struct {
	Ctx iris.Context
}

func NewSSEController() *SSEController {
	return &SSEController{}
}

func (t *SSEController) GetChannel() {
	t.Ctx.ContentType("application/json, text/event-stream")
	t.Ctx.Header("Cache-Control", "no-cache")
	t.Ctx.Header("Connection", "keep-alive")

	flusher, ok := t.Ctx.ResponseWriter().Flusher()
	if !ok {
		t.Ctx.StopWithText(iris.StatusHTTPVersionNotSupported, "Streaming unsupported!")
		return
	}

	i := 0
	for {
		if i > 5 {
			return
		}
		time.Sleep(500 * time.Millisecond)
		sse.Encode(t.Ctx.ResponseWriter(), sse.Event{
			Id:    "124",
			Event: "message",
			Data: map[string]interface{}{
				"user":    "manu",
				"date":    time.Now().Unix(),
				"content": t.Ctx.Request().UserAgent(),
			},
		})
		flusher.Flush()
		i = i + 1
	}
}

You May Also Like

More From Author

+ There are no comments

Add yours