Skip to content

onSubscribe send a message to the subscriber #177

@Cyliann

Description

@Cyliann

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions