Skip to content

Commit 8c54378

Browse files
committed
fix: notify about number of listeners immediately
1 parent 0b1cce8 commit 8c54378

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

internal/http/handlers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request) {
4444
close(eventChan)
4545
}()
4646

47+
// Send current number of listeners immediately
48+
countEvent := s.countListeners()
49+
fmt.Fprint(w, countEvent.Stringify())
50+
w.(http.Flusher).Flush()
51+
4752
for {
4853
event, isOpen := <-eventChan
4954
if !isOpen {

internal/http/server.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,20 @@ func (s *Server) registerMP2TMimeType() {
114114
}
115115
}
116116

117+
func (s *Server) countListeners() *sse.Event {
118+
count := s.eventsEmitter.CountSubscribers()
119+
return sse.NewEvent(eventCountListeners, strconv.Itoa(count))
120+
}
121+
117122
func (s *Server) listenEvents() {
118123
countConnectionTicker := time.Tick(5 * time.Second)
119124

120125
// TODO: add context for gracefull shutdown
121126

122127
go func() {
123128
for range countConnectionTicker {
124-
count := s.eventsEmitter.CountSubscribers()
125-
s.eventsEmitter.RegisterEvent(eventCountListeners, strconv.Itoa(count))
129+
event := s.countListeners()
130+
s.eventsEmitter.RegisterEvent(event.Name, event.Data)
126131
}
127132
}()
128133

0 commit comments

Comments
 (0)