Skip to content

SSE example doesn't remove a disconnected client #193

@RizalMadani

Description

@RizalMadani

When I close one of the client browser tabs the server never log that removed client message and the len(stream.TotalClients) is the same in Event.listen()

I am new to golang so I may fail to understand the code, please cmiiw

After closing the client <-c.Writer.CloseNotify() returns true and stream.ClosedClients <- clientChan never executed.
I think the process stuck in for loop because nothing will close the clientChan except that stream.ClosedClients <- clientChan

/* serveHTTP() handler function */

clientChan := make(ClientChan)

// Send new connection to event server
stream.NewClients <- clientChan

go func() {
	<-c.Writer.CloseNotify()

	// Drain client channel so that it does not block. Server may keep sending messages to this channel
	for range clientChan {
	}
	// Send closed connection to event server
	stream.ClosedClients <- clientChan
}()
/* listen() */

select {
// Remove closed client
case client := <-stream.ClosedClients:
	delete(stream.TotalClients, client)
	close(client)
	log.Printf("Removed client. %d registered clients", len(stream.TotalClients))
}

Is this intentional, something like that channel principle when you don't close channel on receiving? why it didn't remove the client channel when closing the browser tab?

I tried breaking the loop when draining the channel and it did remove/close the client channel (after 2 next message broadcasts) but idk if this correct

for range clientChan {
	break
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions