{
  "id": 22960241,
  "title": "Adding SSE Live Updates Logged Out Users With Multiple Tabs: Fixing the Refresh Token Race",
  "url": "https://urgent.news/2026/12/01/adding-sse-live-updates-logged-out-users-with-multiple-tabs-fixing",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-12-01T13:41:10.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/nazrinsuleymanli/adding-sse-live-updates-logged-out-users-with-multiple-tabs-fixing-the-refresh-token-race-5om"
  },
  "original_language": null,
  "account": "The bug report surfaced after live updates were added to the app using Server-Sent Events (SSE). Users encountered a scenario where opening the app in multiple windows resulted in both windows logging them out. The issue persisted even though no changes were made to the login code. The setup involved storing access and refresh tokens in cookies, with all tabs of the same site sharing the same cookies. When a 401 error occurred, the frontend would call POST /users/refresh, asking the backend to retrieve new tokens and set new cookies. However, the solution was complicated by the fact that Keycloak had refresh token rotation enabled, meaning each refresh returned a new refresh token, invalidating the old one. When two or more windows were open, the streams ended simultaneously due to sharing the same token, causing both windows to reconnect and call /refresh at the same time. This led to the old refresh token being reused, resulting in the session being dropped by Keycloak. The fix involved implementing a mechanism for tabs to take turns refreshing tokens, utilizing the Web Locks API to ensure only one tab refreshed at a time. A tab that waited for the lock would check if another tab had refreshed while it was waiting using localStorage to store the last refresh timestamp. If the shared token had been refreshed, the second tab would skip refreshing. This approach prevented multiple refreshes from occurring simultaneously and avoided the logout issue. The same timeline applied, with only one refresh needed instead of two. Background tabs, which were closed after 30 seconds of inactivity, remained logged in and refreshed when reopened using the same lock mechanism. The implementation kept tabs logged in and prevented logout even in hidden tabs.",
  "summary": "Everything worked until we shipped live updates We added live updates to our app with Server-Sent Events (SSE). When someone approves a request, every other user sees it right away, with no page reload. It worked well in testing. Then a bug report came in: \"I had the app open in two windows. I didn't click anything, and suddenly both windows showed the login page.\" It only happened with two or…",
  "summary_source": "publisher",
  "key_points": [
    "Users experienced logout when opening app in multiple tabs with SSE live updates.",
    "Keycloak's refresh token rotation invalidated old tokens during simultaneous refreshes.",
    "Fix implemented using Web Locks API to ensure single token refresh per tab."
  ],
  "editors_take": null,
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}