nextspace
    Preparing search index...

    Function useSessionJoin

    • Custom hook to handle socket initialization. _app.tsx guarantees SessionManager has completed initialization before pages render.

      Handles:

      • Initial socket creation with current access token
      • Automatic token refresh on auth-related connect_error events (via TokenManager)
      • Proactive token refresh is now managed entirely by TokenManager (expiry-based scheduling + cross-tab BroadcastChannel coordination). When TokenManager issues a new token this hook updates socket.auth so any subsequent reconnection uses the fresh token.
      • Tab visibility changes trigger TokenManager to check expiry and refresh if needed.

      Parameters

      • enableSocket: boolean = true
      • OptionalisAuthenticated: boolean
      • OptionalonSuccess: (result: { pseudonym: string; userId: string }) => void
      • OptionalonError: (error: string) => void

      Returns {
          errorMessage: string | null;
          isConnected: boolean;
          lastReconnectTime: number | null;
          pseudonym: string | null;
          socket: Socket<DefaultEventsMap, DefaultEventsMap> | null;
          userId: string | null;
      }

      Object containing socket, pseudonym, userId, connection state, and lastReconnectTime (non-null whenever the socket reconnected after a gap long enough that messages may have been missed — callers should re-fetch message history when this value changes)