- Add ConnectionIndicator component showing green/red dot - Send lastPing signal every 15 seconds via SSE - Indicator turns red if no ping received in 20 seconds - Gives users confidence the live connection is active
29 lines
704 B
Go
29 lines
704 B
Go
// Package sse provides helpers for SSE signal handling.
|
|
package sse
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/starfederation/datastar-go/datastar"
|
|
)
|
|
|
|
// Signals holds client-side state managed via SSE.
|
|
type Signals struct {
|
|
LastPing int64 `json:"lastPing,omitempty"`
|
|
}
|
|
|
|
// SendPing sends a heartbeat signal with the current timestamp.
|
|
func SendPing(sse *datastar.ServerSentEventGenerator) error {
|
|
return PatchSignals(sse, Signals{LastPing: time.Now().UnixMilli()})
|
|
}
|
|
|
|
// PatchSignals sends a signals patch to the client.
|
|
func PatchSignals(sse *datastar.ServerSentEventGenerator, s Signals) error {
|
|
data, err := json.Marshal(s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return sse.PatchSignals(data)
|
|
}
|