fix: unreliable websocket functionality (#144)

* increases timeout

* refactor: reuse constants for redis db

* avoid closing connection on pong error

* fix: prevent electron from pausing timer executions

Our pings could be paused when our app is backgrounded.

* refactor: remove unnecessary refs

* fix: presence unreliable within same pod

It was broadcasting presence changes to other pods, but not notifying
clients connected to the same pod
This commit was merged in pull request #144.
This commit is contained in:
Arjun Patel
2026-04-12 10:00:11 -07:00
committed by GitHub
parent 4cb562e1ea
commit 2bc4ce7060
11 changed files with 64 additions and 35 deletions
+9 -3
View File
@@ -15,14 +15,17 @@ import (
type Server struct {
pbpusher.UnimplementedPusherServiceServer
ctx context.Context // server-scoped context for graceful shutdown
hub *Hub
bridge *RedisBridge
authSvc auth.AuthService
}
// NewServer creates a new pusher server.
func NewServer(hub *Hub, bridge *RedisBridge, authSvc auth.AuthService) *Server {
// NewServer creates a new pusher server. The ctx controls the lifetime of all
// WebSocket connections — when cancelled, all connections are closed gracefully.
func NewServer(ctx context.Context, hub *Hub, bridge *RedisBridge, authSvc auth.AuthService) *Server {
return &Server{
ctx: ctx,
hub: hub,
bridge: bridge,
authSvc: authSvc,
@@ -59,7 +62,10 @@ func (s *Server) HandleWebSocket(w http.ResponseWriter, r *http.Request) {
slog.Info("websocket connected", "connId", connID, "humanId", session.HumanId)
ctx, cancel := context.WithCancel(r.Context())
// Use server context, NOT r.Context(). After WebSocket upgrade, the HTTP
// request context can be cancelled by load balancers or Go's HTTP server,
// and nhooyr/websocket permanently closes the conn on any context error.
ctx, cancel := context.WithCancel(s.ctx)
defer cancel()
// Auto-subscribe to presence channel so this user appears online