import { AGENT_APPLICATION_TYPE } from '@baserow/modules/agent/constants' import { isSecureURL } from '@baserow/modules/core/utils/string' import { logoutAndRedirectToLogin } from '@baserow/modules/core/utils/auth' import { FIRST_CONNECT_CURSOR, NO_REPLAY_AVAILABLE, retryRealtimeRecovery, } from '@baserow/modules/core/plugins/realtimeProtocol' import { useRuntimeConfig } from '#imports' const RECONNECT_BASE_DELAY = 1000 const RECONNECT_MAX_DELAY = 30000 const RECONNECT_MAX_ATTEMPTS = 10 const RECONNECT_JITTER = 1000 // Force-close a socket stuck in CONNECTING so onclose can drive a reconnect. const CONNECTION_TIMEOUT = 10000 // The handshake resets ``attempts`` before the auth result arrives, so the // backoff cap can't bound an auth-rejection loop; bound the refreshes instead. const MAX_TOKEN_REFRESH_RETRIES = 1 const REPLAY_RETRY_BASE_DELAY = 1000 const REPLAY_RETRY_MAX_DELAY = 30000 const REPLAY_BUFFER_MAX_EVENTS = 1000 const REPLAY_BUFFER_MAX_BYTES = 5 * 1024 * 1024 export class RealTimeHandler { constructor(context) { this.context = context // Reconnect callbacks can run without an active Nuxt context. this.config = useRuntimeConfig() this.socket = null this.connected = false this.reconnect = false this.anonymous = false this.reconnectTimeout = null this.connectionTimeout = null this.attempts = 0 this.events = {} this.pages = [] this.subscribedToPages = true this.lastToken = null this.authenticationSuccess = true this.authResponseReceived = false this.unloading = false this.lastSeenEventId = FIRST_CONNECT_CURSOR this.replayEnabled = false this.replayRequestCursor = null this.replayInFlight = false this.replayRetryTimeout = null this.replayRetryAttempts = 0 this.replayEventBuffer = new Map() this.replayBufferBytes = 0 this.replayAbandoned = false this.recentEventIds = new Set() this.connecting = false // Set on a rejected token so the next reconnect refreshes before retrying. this.forceTokenRefresh = false this.tokenRefreshRetries = 0 this.registerCoreEvents() this._onPageHide = () => { this.unloading = true } // Immediate retry on tab refocus or network restoration. Any of these // signals means the page is alive again, so the unload latch set by // ``_onPageHide`` must be cleared here. A ``pagehide`` followed by a // ``pageshow`` (bfcache restore) would otherwise leave it stuck and // permanently suppress reconnects. this._onShouldRetryNow = () => { this.unloading = false if (!this.connected && this.reconnect) { this._retryReconnectNow() } } this._onVisibilityChange = () => { if (!this._isDocumentVisible()) { return } this._onShouldRetryNow() } if (import.meta.client) { window.addEventListener('beforeunload', this._onPageHide) window.addEventListener('pagehide', this._onPageHide) window.addEventListener('pageshow', this._onShouldRetryNow) document.addEventListener('visibilitychange', this._onVisibilityChange) window.addEventListener('online', this._onShouldRetryNow) window.addEventListener('focus', this._onShouldRetryNow) } } /** * Creates a new connection with to the web socket so that real time updates can be * received. */ async connect(reconnect = true, anonymous = false) { if (!import.meta.client) { return } this.reconnect = reconnect this.anonymous = anonymous if ( this.socket && (this.socket.readyState === WebSocket.CONNECTING || this.socket.readyState === WebSocket.OPEN) ) { return } // A backgrounded tab makes no HTTP calls, so the axios refresh interceptor // never runs and the access token silently expires; reconnecting with it // would be rejected as a permanent auth failure. Only this branch awaits, // keeping the common reconnect path synchronous. if (this._tokenRefreshNeeded(anonymous)) { if (this.connecting) { return } this.connecting = true let transientRefreshFailure = false try { await this.context.store.dispatch('auth/refresh') this.forceTokenRefresh = false } catch (error) { // A 401 means the refresh token itself expired: the session is gone // and the guards below surface it. Anything else is transient (e.g. a // network blip), so retry with backoff instead of failing, keeping // forceTokenRefresh set for the next attempt. transientRefreshFailure = error?.response?.status !== 401 } finally { this.connecting = false } if (transientRefreshFailure) { this.delayedReconnect() return } } if (this.socket) { this._interruptReplay() this.socket.onclose = null this.socket = null } const jwtToken = this.context.store.getters['auth/token'] const token = anonymous ? jwtToken || 'anonymous' : jwtToken // "Failed — refresh" is only for genuine auth problems; transient // network failures keep retrying with capped backoff. const noToken = !token const tokenAlreadyRejected = this.authResponseReceived && !this.authenticationSuccess && this.lastToken === token if (noToken || tokenAlreadyRejected) { if (!this._isDocumentHidden()) { this.context.store.dispatch('toast/setFailedConnecting', true) } this.context.store.dispatch('toast/setReconnecting', false) return } this.lastToken = token this.authResponseReceived = false // The web socket url is the same as the PUBLIC_BACKEND_URL apart from the // protocol. const rawUrl = this.config.public.publicBackendUrl const url = new URL(rawUrl) url.protocol = isSecureURL(rawUrl) ? 'wss:' : 'ws:' url.pathname = '/ws/core/' const webSocketId = this.context.store.getters['auth/webSocketId'] this.socket = new WebSocket( `${url}?jwt_token=${token}&web_socket_id=${webSocketId}` ) const socket = this.socket this._armConnectionTimeout() this.socket.onopen = () => { if (this.socket !== socket) { return } this._clearConnectionTimeout() this.connected = true this.attempts = 0 this.authenticationSuccess = true this.context.store.dispatch('toast/setFailedConnecting', false) this.context.store.dispatch('toast/setReconnecting', false) if (!this.subscribedToPages) { this.subscribeToPages() } } /** * The received messages are always JSON so we need to the parse it, extract the * type and call the correct event. */ this.socket.onmessage = (message) => { if (this.socket !== socket) { return } let data try { data = JSON.parse(message.data) } catch { return } if ( this.replayRequestCursor !== null && typeof data?._event_id === 'number' && this._bufferReplayEvent(data, message.data.length * 2) ) { return } this._dispatchEvent(data) } this.socket.onclose = () => { if (this.socket !== socket) { return } this._clearConnectionTimeout() // Keep the original cursor and buffered events: a reconnect must still // recover the gap, even if live events arrived while replay was busy. this._interruptReplay() this.connected = false this.subscribedToPages = this.pages.length === 0 this.context.store.dispatch('presence/clearAllSpaces') this.delayedReconnect() } } /** * Schedules a reconnection attempt with exponential backoff and jitter. * Bails when the tab is hidden or the navigator is offline; the * ``visibilitychange`` / ``online`` handlers resume via * ``_retryReconnectNow`` the moment either clears. */ delayedReconnect() { if (!this.reconnect || this.unloading) { return } if (this._isDocumentHidden()) { // Tab hidden — no point showing toast or retrying. The // visibilitychange handler will call _retryReconnectNow() on refocus. return } if (!this._isNavigatorOnline()) { // Offline but user is looking at the tab — show toast so they know // the connection is down. Don't schedule retries; the online event // handler will call _retryReconnectNow() when network returns. this.context.store.dispatch('toast/setReconnecting', true) return } clearTimeout(this.reconnectTimeout) this.attempts++ if (this.attempts > RECONNECT_MAX_ATTEMPTS) { // Surface the failure but keep retrying at the slowest interval: this is // the only way the connection self-heals when no visibility/focus/online // event fires (e.g. the tab stayed visible through a backend outage). this.attempts = RECONNECT_MAX_ATTEMPTS + 1 this.context.store.dispatch('toast/setReconnecting', false) this.context.store.dispatch('toast/setFailedConnecting', true) this.reconnectTimeout = setTimeout(() => { this.connect(true, this.anonymous) }, RECONNECT_MAX_DELAY) return } this.context.store.dispatch('toast/setReconnecting', true) const exponent = Math.min(this.attempts, RECONNECT_MAX_ATTEMPTS) - 1 const delay = Math.min( RECONNECT_BASE_DELAY * Math.pow(2, exponent) + Math.floor(Math.random() * RECONNECT_JITTER), RECONNECT_MAX_DELAY ) this.reconnectTimeout = setTimeout(() => { this.connect(true, this.anonymous) }, delay) } _isDocumentVisible() { return ( typeof document !== 'undefined' && document.visibilityState === 'visible' ) } _isDocumentHidden() { return ( typeof document !== 'undefined' && document.visibilityState === 'hidden' ) } _isNavigatorOnline() { // ``navigator.onLine === false`` is the only reliable signal. return typeof navigator === 'undefined' || navigator.onLine !== false } _armConnectionTimeout() { clearTimeout(this.connectionTimeout) this.connectionTimeout = setTimeout(() => { // Still mid-handshake: abandon it so onclose starts a fresh attempt. if (this.socket && this.socket.readyState === WebSocket.CONNECTING) { this.socket.close() } }, CONNECTION_TIMEOUT) } _clearConnectionTimeout() { clearTimeout(this.connectionTimeout) this.connectionTimeout = null } _tokenRefreshNeeded(anonymous) { if (anonymous) { return false } const store = this.context.store if (!store.getters['auth/isAuthenticated']) { return false } return this.forceTokenRefresh || store.getters['auth/shouldRefreshToken']() } _retryReconnectNow() { clearTimeout(this.reconnectTimeout) this.attempts = 0 this.tokenRefreshRetries = 0 this.context.store.dispatch('toast/setFailedConnecting', false) // Keep the "Reconnecting" toast up until ``onopen`` clears it; flickering // it off here just confuses the user during the retry round-trip. this.connect(true, this.anonymous) } /** * Subscribes the client to a given page. After subscribing the client will * receive updated related to that page. This is for example used when a user * opens a table page. */ subscribe(page, parameters) { const pageScope = { page, parameters, } if ( !this.pages.some( (elem) => JSON.stringify(elem) === JSON.stringify(pageScope) ) ) { this.pages.push(pageScope) // If the client is already connected we can // subscribe to updates for all pages. if (this.connected) { this.subscribeToPage(page, parameters) } else { this.subscribedToPages = false } } } /** * Unsubscribes the client from a given page. The client will * stop receiving updates related to that page. */ unsubscribe(page, parameters) { this.pages = this.pages.filter( (item) => JSON.stringify(item) !== JSON.stringify({ page, parameters }) ) if (this.connected) { this.socket.send( JSON.stringify({ remove_page: page, ...parameters, }) ) } } /* * Subscribes the client to a new page if the client is * connected. */ subscribeToPage(page, parameters) { if (this.connected) { this.socket.send( JSON.stringify({ page: page === null ? '' : page, ...parameters, }) ) } } /** * Requests real time updates for the list of pages that * have been collected by the subscribe() call. */ subscribeToPages() { if (this.subscribedToPages) { return } for (const { page, parameters } of this.pages) { this.subscribeToPage(page, parameters) } this.subscribedToPages = true } /** * Disconnects the socket and resets all the variables. The can be used when * navigating to another page that doesn't require updates. */ disconnect() { this.context.store.dispatch('presence/clearAllSpaces') if (this.socket) { this.socket.onclose = null this.socket.close() this.socket = null } this.context.store.dispatch('toast/setFailedConnecting', false) this.context.store.dispatch('toast/setReconnecting', false) this.context.store.dispatch('toast/setWorkspaceOutdated', false) clearTimeout(this.reconnectTimeout) this._clearConnectionTimeout() this.reconnect = false this.attempts = 0 this.connected = false this.connecting = false this.forceTokenRefresh = false this.tokenRefreshRetries = 0 this.lastSeenEventId = FIRST_CONNECT_CURSOR this._clearReplayRetry() this.replayRequestCursor = null this._clearReplayBuffer() this.replayAbandoned = false this.recentEventIds.clear() // Reset until the next auth message confirms replay is enabled. this.replayEnabled = false } _canReplayEvents() { return ( this.replayEnabled && !this.replayAbandoned && this.socket && this.socket.readyState === WebSocket.OPEN ) } _sendReplayEventsRequest() { if ( !this._canReplayEvents() || this.replayInFlight || this.replayRetryTimeout !== null ) { return } // Retries must use the original cursor, including FIRST_CONNECT_CURSOR. // Advancing it to a live event could silently skip missed updates. this.replayRequestCursor ??= this.lastSeenEventId this.replayInFlight = true this.socket.send( JSON.stringify({ type: 'replay_events', last_seen_id: this.replayRequestCursor, supports_retry: true, }) ) } _clearReplayRetry() { clearTimeout(this.replayRetryTimeout) this.replayRetryTimeout = null this.replayInFlight = false this.replayRetryAttempts = 0 } _interruptReplay() { this._clearReplayRetry() if (this.replayRequestCursor === FIRST_CONNECT_CURSOR) { // Losing the socket before obtaining a baseline leaves no safe cursor for // events missed while disconnected. A fresh baseline would hide that gap. this.replayRequestCursor = NO_REPLAY_AVAILABLE } } _scheduleReplayRetry(retryAfterMs) { if (!this.replayInFlight || !this._canReplayEvents()) { return } this.replayInFlight = false const minimumDelay = Math.min( Math.max( Number.isFinite(retryAfterMs) ? retryAfterMs : 0, REPLAY_RETRY_BASE_DELAY ), REPLAY_RETRY_MAX_DELAY ) const backoff = Math.min( minimumDelay * 2 ** this.replayRetryAttempts, REPLAY_RETRY_MAX_DELAY ) this.replayRetryAttempts = Math.min(this.replayRetryAttempts + 1, 5) const jitter = Math.random() * Math.min(1000, backoff / 4) const delay = Math.max( minimumDelay, Math.min(backoff, REPLAY_RETRY_MAX_DELAY - 1000) + jitter ) const socket = this.socket this.replayRetryTimeout = setTimeout(() => { this.replayRetryTimeout = null if (this.socket === socket) { this._sendReplayEventsRequest() } }, delay) } _clearReplayBuffer() { this.replayEventBuffer.clear() this.replayBufferBytes = 0 } _bufferReplayEvent(data, bytes) { if (this.recentEventIds.has(data._event_id)) { return true } const previousBytes = this.replayEventBuffer.get(data._event_id)?.bytes || 0 const nextBytes = this.replayBufferBytes - previousBytes + bytes if ( nextBytes > REPLAY_BUFFER_MAX_BYTES || (!this.replayEventBuffer.has(data._event_id) && this.replayEventBuffer.size >= REPLAY_BUFFER_MAX_EVENTS) ) { // We can no longer safely merge the missed history with live events. // This is actual loss of recoverability, rather than temporary busyness. this._clearReplayRetry() this._clearReplayBuffer() this.replayRequestCursor = null this.replayAbandoned = true this.context.store.dispatch('toast/setWorkspaceOutdated', true) return false } this.replayEventBuffer.set(data._event_id, { data, bytes }) this.replayBufferBytes = nextBytes return true } _flushReplayBuffer(cursor) { const bufferedEvents = [...this.replayEventBuffer.values()].sort( (a, b) => a.data._event_id - b.data._event_id ) this._clearReplayBuffer() for (const { data: event } of bufferedEvents) { // First connect requests only a baseline, so every buffered live event // still needs applying, even if its id precedes that baseline. if (cursor === null || event._event_id > cursor) { this._dispatchEvent(event) } } } _dispatchEvent(data) { if (typeof data?._event_id === 'number') { // The channel layer can deliver a live copy after replay has completed. // Remember a bounded window so an old duplicate cannot revert newer state. if (this.recentEventIds.has(data._event_id)) { return } this.recentEventIds.add(data._event_id) if (this.recentEventIds.size > REPLAY_BUFFER_MAX_EVENTS) { this.recentEventIds.delete(this.recentEventIds.values().next().value) } } this.updateLastSeenId(data) if ( data && Object.prototype.hasOwnProperty.call(data, 'type') && Object.prototype.hasOwnProperty.call(this.events, data.type) ) { for (const callback of this.events[data.type]) { callback(this.context, data) } } } updateLastSeenId(data) { if ( data && typeof data === 'object' && typeof data._event_id === 'number' && data._event_id > this.lastSeenEventId ) { this.lastSeenEventId = data._event_id } } _isReady() { return ( this.connected && this.socket && this.socket.readyState === WebSocket.OPEN ) } sendFocus(page, parameters, focus) { if (!this._isReady()) { return } this.socket.send( JSON.stringify({ type: 'presence.focus', page, ...parameters, focus, }) ) } /** * Registers a new event with the event registry. */ registerEvent(type, callback) { if (!this.events[type]) { this.events[type] = [] } this.events[type].push(callback) } /** * Registers all the core event handlers, which is for the workspaces and applications. */ registerCoreEvents() { this.registerEvent('authentication', ({ store }, data) => { this.authenticationSuccess = data.success this.authResponseReceived = true this.replayEnabled = data.replay_enabled === true if (!this.replayEnabled) { this._clearReplayRetry() const cursor = this.replayRequestCursor this.replayRequestCursor = null this._flushReplayBuffer(cursor) if (cursor !== null) { // A reconnect to a server without replay cannot close the pending gap. this.replayAbandoned = true store.dispatch('toast/setWorkspaceOutdated', true) } this.lastSeenEventId = NO_REPLAY_AVAILABLE } if (data.success) { this.forceTokenRefresh = false this.tokenRefreshRetries = 0 if (this._canReplayEvents()) { this._sendReplayEventsRequest() } } else if ( !this.anonymous && this.tokenRefreshRetries < MAX_TOKEN_REFRESH_RETRIES ) { // A rejected token is usually just expired: refresh and retry rather // than failing. Once the cap is hit, let connect()'s guard surface it. this.tokenRefreshRetries++ this.forceTokenRefresh = true } }) this.registerEvent('replay_events_retry', (_context, data) => { this._scheduleReplayRetry(data.retry_after_ms) }) this.registerEvent('replay_events_result', ({ store }, data) => { if (this.replayAbandoned) { return } this._clearReplayRetry() const cursor = this.replayRequestCursor this.replayRequestCursor = null this._flushReplayBuffer(cursor) const latestEventId = data.latest_event_id if (!data.force_refresh && typeof latestEventId === 'number') { // ``latest_event_id`` can be 0 when the server has no events // recorded yet; store it verbatim. this.lastSeenEventId = Math.max(latestEventId, this.lastSeenEventId) } // Later live events or reconnects cannot repair a gap declared unreplayable. this.replayAbandoned = data.force_refresh === true store.dispatch('toast/setWorkspaceOutdated', data.force_refresh === true) }) this.registerEvent('user_data_updated', ({ store }, data) => { store.dispatch('auth/forceUpdateUserData', data.user_data) }) this.registerEvent('ai_provider_updated', async ({ store }, data) => { if (data.requires_refresh === true) { const recoveries = [] if (data.refresh_workspace_availability === true) { recoveries.push( retryRealtimeRecovery(async () => { if (!store.getters['workspace/isLoaded']) { throw new Error('Workspace state is not loaded yet.') } await store.dispatch('workspace/refreshAllGenerativeAIModels', { realtimeRecovery: true, }) }) ) } const providerScopeIsActive = () => (store.getters['aiProvider/hasLoaded'] || store.getters['aiProvider/isLoading']) && store.getters['aiProvider/getWorkspaceId'] === data.workspace_id if ( data.refresh_provider_settings === true && providerScopeIsActive() ) { recoveries.push( retryRealtimeRecovery(() => { if (!providerScopeIsActive()) { return undefined } return store.dispatch('aiProvider/fetchInitial', { workspaceId: data.workspace_id, realtimeRecovery: true, }) }) ) } await Promise.all(recoveries) return } for (const [workspaceId, enabledModels] of Object.entries( data.generative_ai_models_enabled_by_workspace || {} )) { store.dispatch('workspace/forceUpdateGenerativeAIModels', { workspaceId: Number(workspaceId), generativeAIModelsEnabled: enabledModels, aiFeatures: data.ai_features_by_workspace?.[workspaceId], }) } const workspaceId = store.getters['aiProvider/getWorkspaceId'] const providers = workspaceId === null ? data.instance_ai_providers : data.ai_providers_by_workspace?.[workspaceId] const featureSettings = workspaceId === null ? data.instance_ai_provider_feature_settings : data.ai_provider_feature_settings_by_workspace?.[workspaceId] if (providers !== undefined) { store.dispatch('aiProvider/replaceFromRealtime', { workspaceId, providers, featureSettings, }) } }) this.registerEvent('user_updated', ({ store }, data) => { store.dispatch('workspace/forceUpdateWorkspaceUserAttributes', { userId: data.user.id, values: { name: data.user.first_name, }, }) }) this.registerEvent('user_deleted', ({ store }, data) => { store.dispatch('workspace/forceUpdateWorkspaceUserAttributes', { userId: data.user.id, values: { to_be_deleted: true, }, }) }) this.registerEvent('user_restored', ({ store }, data) => { store.dispatch('workspace/forceUpdateWorkspaceUserAttributes', { userId: data.user.id, values: { to_be_deleted: false, }, }) }) this.registerEvent('user_permanently_deleted', ({ store }, data) => { store.dispatch('workspace/forceDeleteUser', { userId: data.user_id, }) }) this.registerEvent('group_created', ({ store }, data) => { store.dispatch('workspace/forceCreate', data.workspace) }) this.registerEvent('group_restored', ({ store }, data) => { store.dispatch('workspace/forceCreate', data.workspace) store.dispatch('application/forceCreateAll', data.applications) }) this.registerEvent('group_updated', ({ store }, data) => { const workspace = store.getters['workspace/get'](data.workspace_id) if (workspace !== undefined) { store.dispatch('workspace/forceUpdate', { workspace, values: data.workspace, }) } }) this.registerEvent('group_deleted', ({ store }, data) => { const workspace = store.getters['workspace/get'](data.workspace_id) if (workspace !== undefined) { store.dispatch('workspace/forceDelete', workspace) } }) this.registerEvent('groups_reordered', ({ store }, data) => { store.dispatch('workspace/forceOrder', data.workspace_ids) }) this.registerEvent('group_user_added', ({ store }, data) => { store.dispatch('workspace/forceAddWorkspaceUser', { workspaceId: data.workspace_id, values: data.workspace_user, }) }) this.registerEvent('group_user_updated', ({ store }, data) => { store.dispatch('workspace/forceUpdateWorkspaceUser', { id: data.id, workspaceId: data.workspace_id, values: data.workspace_user, }) }) this.registerEvent('group_user_deleted', ({ store }, data) => { store.dispatch('workspace/forceDeleteWorkspaceUser', { id: data.id, workspaceId: data.workspace_id, values: data.workspace_user, }) }) this.registerEvent('workspace_identity_updated', ({ store }, data) => { store.commit('workspace/UPSERT_WORKSPACE_IDENTITY', { workspaceId: data.workspace_id, identity: data.identity, }) }) this.registerEvent('workspace_identity_deleted', ({ store }, data) => { store.commit('workspace/DELETE_WORKSPACE_IDENTITY', { workspaceId: data.workspace_id, identityId: data.identity_id, }) }) this.registerEvent('identity_created', ({ store }, data) => { store.dispatch('identity/forceCreate', data.identity) }) this.registerEvent('identity_updated', ({ store }, data) => { store.dispatch('identity/forceUpdate', data.identity) }) this.registerEvent('identity_deleted', ({ store }, data) => { store.dispatch('identity/forceDelete', { workspaceId: data.workspace_id, identityId: data.identity_id, }) }) this.registerEvent('agent_run_as_updated', ({ store }, data) => { const selectedAgentIds = new Set(data.selected_agent_ids) for (const agentId of data.agent_ids) { const application = store.getters['application/get'](agentId) if (application?.type !== AGENT_APPLICATION_TYPE) continue store.dispatch('application/forceUpdate', { application, data: { _: { ...application._, runAsOptionsVersion: (application._?.runAsOptionsVersion || 0) + 1, runAsIdentityVersion: (application._?.runAsIdentityVersion || 0) + Number(selectedAgentIds.has(agentId)), }, }, }) } }) this.registerEvent('application_created', ({ store }, data) => { store.dispatch('application/forceCreate', data.application) }) this.registerEvent('application_updated', ({ store }, data) => { const application = store.getters['application/get'](data.application_id) if (application !== undefined) { store.dispatch('application/forceUpdate', { application, data: data.application, viaRealtime: true, }) } }) this.registerEvent('last_viewed_updated', ({ store }, data) => { const application = store.getters['application/get'](data.application_id) if ( application !== undefined && (!application.last_viewed || Date.parse(data.last_viewed) > Date.parse(application.last_viewed)) ) { store.dispatch('application/forceUpdate', { application, data: { last_viewed: data.last_viewed }, }) } }) this.registerEvent( 'application_pending_count_updated', ({ store }, data) => { const application = store.getters['application/get']( data.application_id ) if (application !== undefined) { store.dispatch('application/forceUpdate', { application, data: { pending_count: data.pending_count }, }) } } ) this.registerEvent('application_deleted', ({ store }, data) => { const application = store.getters['application/get'](data.application_id) if (application !== undefined) { store.dispatch('application/forceDelete', application) } }) this.registerEvent('applications_reordered', ({ store }, data) => { const workspace = store.getters['workspace/get'](data.workspace_id) if (workspace !== undefined) { store.commit('application/ORDER_ITEMS', { workspace, order: data.order, isHashed: true, }) } }) // invitations this.registerEvent( 'workspace_invitation_updated_or_created', ({ store }, data) => { store.dispatch( 'auth/forceUpdateOrCreateWorkspaceInvitation', data.invitation ) } ) this.registerEvent('workspace_invitation_accepted', ({ store }, data) => { store.dispatch('auth/forceAcceptWorkspaceInvitation', data.invitation) }) this.registerEvent('workspace_invitation_rejected', ({ store }, data) => { store.dispatch('auth/forceRejectWorkspaceInvitation', data.invitation) }) // notifications this.registerEvent('notifications_created', ({ store }, data) => { store.dispatch('notification/forceCreateInBulk', { notifications: data.notifications, }) }) this.registerEvent('notifications_fetch_required', ({ store }, data) => { store.dispatch('notification/forceRefetch', { notificationsAdded: data.notifications_added, }) }) this.registerEvent('notification_marked_as_read', ({ store }, data) => { store.dispatch('notification/forceMarkAsRead', { notification: data.notification, }) }) this.registerEvent('all_notifications_marked_as_read', ({ store }) => { store.dispatch('notification/forceMarkAllAsRead') }) this.registerEvent('all_notifications_cleared', ({ store }) => { store.dispatch('notification/forceClearAll') }) this.registerEvent('presence.members', ({ store }, data) => { store.dispatch('presence/handleMembers', { space: data.space, entries: data.entries, }) }) this.registerEvent('presence.space_discard', ({ store }, data) => { store.dispatch('presence/clearSpace', { space: data.space }) }) this.registerEvent('presence.join', ({ store }, data) => { store.dispatch('presence/handleJoin', { space: data.space, presence_id: data.presence_id, user_id: data.user_id, }) }) this.registerEvent('presence.leave', ({ store }, data) => { store.dispatch('presence/handleLeave', { space: data.space, presence_id: data.presence_id, }) }) this.registerEvent('presence.focus', ({ store }, data) => { store.dispatch('presence/handleFocus', { space: data.space, presence_id: data.presence_id, focus: data.focus, }) }) this.registerEvent('presence.editors_active', ({ store }, data) => { store.dispatch('presence/handleEditorsActive', { space: data.space, active: data.active, }) }) this.registerEvent('force_disconnect', ({ store }) => { this.reconnect = false logoutAndRedirectToLogin(this.context.app.$router, store, false, true) }) this.registerEvent('job_started', ({ store }, data) => { try { store.dispatch('job/create', data.job) } catch (err) { // TODO: some job types have no frontend handlers (JobType subclasses) // registered. This will cause an error during creation. The proper fix // would be to add missing JobTypes. // Check if the error is about a missing job type in the registry const missingTypePattern = new RegExp( `^The type "${data.job.type}" is not found under namespace "job" in the registry\\.` ) if (!missingTypePattern.test(err.message)) { throw err } } }) } } export default defineNuxtPlugin({ name: 'realtime', dependsOn: ['store', 'registry'], setup(nuxtApp) { const context = { store: nuxtApp.$store, app: nuxtApp, } nuxtApp.provide('realtime', new RealTimeHandler(context)) }, })