feat: add connection status indicator with SSE heartbeat
- 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
This commit is contained in:
28
sse/signals.go
Normal file
28
sse/signals.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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)
|
||||
}
|
||||
Reference in New Issue
Block a user