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

Streaming unsupported #130

Closed
mooijtech opened this issue Mar 17, 2022 · 7 comments
Closed

Streaming unsupported #130

mooijtech opened this issue Mar 17, 2022 · 7 comments

Comments

@mooijtech
Copy link

Hello,

I am trying to setup SSE for a progress bar but I am getting "Streaming unsupported!" in FireFox.

Client:

const source = new EventSource("http://localhost:1337/outlook/loading?stream=" + projectId, {withCredentials: true})
source.onmessage = e => console.log(e.data)

Server:

func (server *Server) Start() {
	server.ServerSentEvents = sse.New()

	server.Router.HandleFunc("/outlook/loading", server.ServerSentEvents.ServeHTTP)
}
go func() {
	server.ServerSentEvents.CreateStream(project.UUID)

	for percentage := range progressPercentageChannel {
		server.ServerSentEvents.Publish(project.UUID, &sse.Event{
			Data: []byte(fmt.Sprintf("%d%", percentage)),
		})
	}
}()
@mooijtech
Copy link
Author

It seems like for some reason the http.ResponseWriter has no http.Flusher?

@mooijtech
Copy link
Author

mooijtech commented Mar 17, 2022

As per the documentation:

The default HTTP/1.x and HTTP/2 ResponseWriter implementations
support Flusher, but ResponseWriter wrappers may not.

I am using a CORS wrapper:

corsHandler := cors.New(cors.Options{
	AllowedOrigins:   []string{"http://localhost:3000", "http://127.0.0.1:3000"},
	AllowedMethods:   []string{http.MethodGet, http.MethodPost, http.MethodDelete},
	AllowCredentials: true,
}).Handler(server.Router)

Logger.Fatal(http.ListenAndServe(":1337", server.SessionManager.LoadAndSave(corsHandler)))

Most likely this is the issue.

@mooijtech
Copy link
Author

I need CORS otherwise the EventSource request will be blocked by CORS.

@mooijtech
Copy link
Author

I'm still getting "Streaming unsupported!" even if I add the CORS header manually and remove the CORS library.

@mooijtech
Copy link
Author

Maybe the session manager doesn't support it?

@purehyperbole
Copy link
Member

Hey @mooijtech ,

Please could you try and run this simpler example and see if it behaves correctly? I am unable to reproduce the issue you are seeing.

package main

import (
	"time"
	"net/http"

	"github.com/r3labs/sse/v2"
)

func main() {
	s := sse.New()

	s.CreateStream("test")

	go func() {
		for {
			s.Publish("test", &sse.Event{Data: []byte("hello")})	
			time.Sleep(time.Second)	
		}
	}()

	s.Headers = map[string]string{
		"Access-Control-Allow-Origin": "*",
	}

	// Create a new Mux and set the handler
	mux := http.NewServeMux()
	mux.HandleFunc("/", s.ServeHTTP)

	http.ListenAndServe(":8080", mux)		
}

@mooijtech
Copy link
Author

Hello,

Thanks for your response.
It seems to be an issue with the session manager I'm using.
I have removed the server.SessionManager.LoadAndSave function and now it works.
It seems the session manager is wrapping http.ResponseWriter and apparently doesn't have the http.Flusher.

I will open an issue there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants