Server Sent Event how to replace http.CloseNotifier by Request.Context

Dear Gophers,

I do have implemented a Server Sent Event broker that relies on http.CloseNotifier CloseNotify to close le connection. But it is deprecated!

I try to figure out how to implement the de-connection using Request.Context instead of http.CloseNotifier

My code with the CloseNotifier, simply returns on notification :

// Listen to connection close and un-register messageChan
notify := w.(http.CloseNotifier).CloseNotify()
for {
   select {
      case <-notify:
	 return
    default:
       // On message
       commands := <-messageChan

Can i simply replace the notify channel by the Done channel?

done := r.Context().Done()
for {
   select {
      case <-done:
         return
      default:
        ...

I’m new to Go Land and i’m not sure to understand completely the notion of “http.context”.

Best regards from the south of France

Benoit

2 Likes

https://www.sohamkamani.com/blog/golang/2018-06-17-golang-using-context-cancellation/

2 Likes

hmm

connection broken - disable the network card

or

disable the application - disable the connection

is it possible to catch these events?

hmm, I think there is a problem with - disable the network card on client…

because the server on linux does not break the connection.
?

2 Likes

Hello Kamil,

I ve got an implementation of a Server Sent Event Broker that works.
My question (may be naĂŻve) is :

Benoit

2 Likes

I ve been watching Fransec Campoy’s Just For Func episode on Context.

JustForFunc #9
JustForFunc #10

And the situation is clear!
I can replace w.(http.CloseNotifier).CloseNotify() channel by the done channel r.Context().Done()

Best regards.

3 Likes

Thanks, this information is also important to me. I suspected yes, but I wasn’t sure. That’s why I didn’t write anything.

2 Likes