Issue description
The original implementation of long-running Server-Sent Events (SSE) had an issue where the connection would break after a JWT token expired. Clerk.com’s default JWT tokens have a 60-second expiration. This caused a 403 Forbidden error when the backend API and LLM communication exceeded this time, leading to a permanent failure and a broken user experience.
Fix description
See file product.tsx in this folder for the fix.
This fix introduces robust token refresh and connection management logic to prevent session failures. The updated process seamlessly handles expired tokens by automatically re-establishing the SSE connection with a new JWT, ensuring an uninterrupted user experience.
Enhancements include::
- Error Handling: The code now specifically detects 403 Forbidden errors in the
onopenandonerrorcallbacks. - Automatic Reconnection: Upon a 403 error, the system automatically calls
connectWithFreshToken()to obtain a new JWT and reconnect. - Clean Connection Management: Uses
AbortControllerto gracefully terminate the existing connection before initiating a new one. - Concurrency Control:
isConnectingflag prevents multiple, simultaneous reconnection attempts. - Token Refresh: Each reconnection cycle fetches a new, valid JWT using
getToken().
Handling token expiration flow:
Token expires → 403 error → Detect in onopen/onerror → Abort current connection → Get fresh token → ReconnectThis ensures seamless user experience even during extended sessions where tokens expire.