Skip to content

Commit

Permalink
Merge pull request #33 from StreamPets/BE32-sse-timeout
Browse files Browse the repository at this point in the history
Fixes Cloudflare's SSE timeout after 100s
  • Loading branch information
louisheal authored Jan 8, 2025
2 parents 155b33c + 61099ee commit fc01239
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions controllers/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"io"
"time"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
Expand Down Expand Up @@ -68,11 +69,20 @@ func (c *OverlayController) HandleListen(ctx *gin.Context) {
c.Clients.RemoveClient(client)
}()

ticker := time.NewTicker(60 * time.Second)
defer ticker.Stop()

ctx.Stream(func(w io.Writer) bool {
if event, ok := <-client.Stream; ok {
ctx.SSEvent(event.Event, event.Message)
select {
case event, ok := <-client.Stream:
if ok {
ctx.SSEvent(event.Event, event.Message)
return true
}
return false
case <-ticker.C:
ctx.SSEvent("heartbeat", "ping")
return true
}
return false
})
}

0 comments on commit fc01239

Please sign in to comment.