sse icon indicating copy to clipboard operation
sse copied to clipboard

onSubscribe send a message to the subscriber

Open Cyliann opened this issue 1 year ago • 0 comments

Is it possible to send a message to the subscriber as soon as it subscribes? I need to send an ID to every new client connected. Sadly, this doesn't work:

func main() {
        server := sse.New()
	mux := http.NewServeMux()
	mux.HandleFunc("/events", eventHandler)

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

func eventHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Message"))
	w.(http.Flusher).Flush()

	server.ServeHTTP(w, r)
}

I though that onSubsrcibe() can to that, but it doesn't allow passing http.ResponseWriter in any way and I can't see how it can be accomplished otherwise. I've been banging my head over the wall for the past 2 days and I can't get it to work. Could you provide any help?

Cyliann avatar Feb 05 '24 11:02 Cyliann