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

Proposal: add sse extension #688

Closed
li-jin-gou opened this issue Mar 24, 2023 · 7 comments · Fixed by hertz-contrib/sse#1
Closed

Proposal: add sse extension #688

li-jin-gou opened this issue Mar 24, 2023 · 7 comments · Fixed by hertz-contrib/sse#1
Assignees
Labels
good first issue Good for newcomers

Comments

@li-jin-gou
Copy link
Member

li-jin-gou commented Mar 24, 2023

Is your feature request related to a problem? Please describe.

Currently Hertz does not support sse extension

Describe the solution you'd like

Add a sse extension or add a sse example

Additional context

@li-jin-gou li-jin-gou added the good first issue Good for newcomers label Mar 24, 2023
@Haswf
Copy link
Contributor

Haswf commented Mar 26, 2023

I would like to work on this.

@li-jin-gou
Copy link
Member Author

li-jin-gou commented Mar 27, 2023

I would like to work on this.

thanks,this is a feature that has real users who are looking forward to using it. @Haswf ❤️

@Haswf
Copy link
Contributor

Haswf commented Apr 9, 2023

For anyone who are interested in this proposal, here is some update.

haswf/sse contains sse implementation for Hertz.

A few to-dos before pr:

  1. add README
  2. add a real-world example e.g. https://github.com/gin-gonic/examples/tree/master/server-sent-event
  3. upstream license

@Haswf
Copy link
Contributor

Haswf commented Apr 9, 2023

I ran into a problem that event.Render (which internally calls resp.AppendBody to write event) doesn't trigger a flush, which is required for server-sent event to work. For example, the following code only send response back to client after 10 seconds has elapled and the 2nd event has been rendered. The expected behaviour is http header is immediately returned with 1st event, then the 2nd event is sent after 10 seconds. Can someone know how Hertz response buffer work helps me out?

package main

import (
	"context"
	"time"

	"github.com/Haswf/sse"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/hlog"
)

func main() {
	h := server.Default()

	h.GET("/sse", func(ctx context.Context, c *app.RequestContext) {
		// client can tell server last event it received with Last-Event-ID header
		lastEventID := sse.GetLastEventID(c)
		hlog.CtxInfof(ctx, "last event ID: %s", lastEventID)
		// you can call Render multiple times to send events to client
		c.Render(200, sse.Event{
			Event: "message",
			Data: map[string]string{
				"content": "hello world",
			},
		})
               // this doesn't work. long sigh
		c.Flush()
		time.Sleep(10 * time.Second)
		c.Render(200, sse.Event{
			Event: "timestamp",
			Data:  time.Now().Format(time.RFC3339),
		})
	})

	h.Spin()
}

@Haswf
Copy link
Contributor

Haswf commented Apr 9, 2023

Postman privodes a reference server-sent event api, which returns immediately then event follows.

curl --location -N 'postman-echo.com/server-events/100'

image

@li-jin-gou
Copy link
Member Author

refer to https://www.cloudwego.io/docs/hertz/tutorials/framework-exten/response_writer/

@Haswf
Copy link
Contributor

Haswf commented Apr 11, 2023

refer to https://www.cloudwego.io/docs/hertz/tutorials/framework-exten/response_writer/

Thank you. This problem has been resolved by implementing a StreamBodyWriter according the document you referenced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Development

Successfully merging a pull request may close this issue.

2 participants