Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use SSE Response with go-zero? #3967

Closed
JiehangXie opened this issue Mar 4, 2024 · 7 comments · Fixed by #4055
Closed

How to use SSE Response with go-zero? #3967

JiehangXie opened this issue Mar 4, 2024 · 7 comments · Fixed by #4055
Assignees
Labels
area/restful Categorizes issue or PR as related to restful apis. feature Marks an issue or PR as related to a new feature.

Comments

@JiehangXie
Copy link

My application requires SSE Response(text/event-stream), but I cannot find the documentation related to SSE. How should I use SSE to return with go-zero?Thanks.

@qianjiangchao1992
Copy link

me too

@kevwan
Copy link
Contributor

kevwan commented Mar 6, 2024

Respond with HTTP header Content-Type: text/event-stream.

@JiehangXie
Copy link
Author

Respond with HTTP header Content-Type: text/event-stream.

I have achieved it in handler function like:

    return func(w http.ResponseWriter, r *http.Request) {
        _, ok := w.(http.Flusher)
        w.Header().Set(`Content-Type`, `text/event-stream;charset=utf-8`)

and return streaming data with channel in golang.
But I think it is not the best way to achieve it.
Can go-zero provide other implementation methods making it simpler and more elegant?
Thanks a lot! @kevwan

@JiehangXie
Copy link
Author

JiehangXie commented Mar 6, 2024

For example, Django in Python, it provides some response objects such as StreamingHttpResponse. It makes much elegant to achieve SSE. I hope go-zero can refer to similar programming.

@sdaduanbilei
Copy link

me too

@sdaduanbilei
Copy link

sdaduanbilei commented Mar 20, 2024

func LoginQrCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.LoginQrCodeReq
		w.Header().Set(`Content-Type`, `text/event-stream;charset=utf-8`)
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := user.NewLoginQrCodeLogic(r.Context(), svcCtx, w)
		_, err := l.LoginQrCode(&req, w)
		if err != nil {
			xhttp.JsonBaseResponseCtx(r.Context(), w, err)
		} else {
			httpx.Ok(w)
		}
	
func (l *LoginQrCodeLogic) LoginQrCode(req *types.LoginQrCodeReq, w http.ResponseWriter) (resp string, err error) {
	// todo: add your logic here and delete this line
	fmt.Fprintf(w, "data: %s\n\n", fmt.Sprintf("Event %s", "3333"))
	w.(http.Flusher).Flush()
	return
}

The above code implements sse, but I need another interface. When sending data, I don't know how to call it

@kevwan kevwan self-assigned this Mar 29, 2024
@kevwan kevwan added area/restful Categorizes issue or PR as related to restful apis. feature Marks an issue or PR as related to a new feature. labels Mar 29, 2024
@kevwan kevwan linked a pull request Apr 9, 2024 that will close this issue
@JiehangXie
Copy link
Author

JiehangXie commented Apr 9, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/restful Categorizes issue or PR as related to restful apis. feature Marks an issue or PR as related to a new feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants