diff --git a/src/main/realtime.ts b/src/main/realtime.ts index 880033e4c..3c24695a0 100644 --- a/src/main/realtime.ts +++ b/src/main/realtime.ts @@ -4,6 +4,9 @@ import { obtainToken } from './auth/service'; import eventBus from './event-bus'; import { broadcastToWindows } from './windows'; +type XHRRequest = { + getResponseHeader: (headerName: string) => string[] | null; +}; // REMOTE TRIGGER let socket: Socket | undefined; @@ -12,16 +15,22 @@ function cleanAndStartRemoteNotifications() { stopRemoteNotifications(); socket = io(process.env.NOTIFICATIONS_URL, { + transports: ['websocket'], auth: { token: obtainToken('bearerToken'), }, withCredentials: true, }); - socket.io.on('open', () => { + socket.on('open', () => { socket?.io.engine.transport.on('pollComplete', () => { - const request = socket?.io.engine.transport.pollXhr.xhr; - const cookieHeader = request.getResponseHeader('set-cookie'); + const xhr = ( + socket?.io.engine.transport as unknown as { + pollXhr: { xhr: XHRRequest }; + } + ).pollXhr.xhr; + + const cookieHeader = xhr.getResponseHeader('set-cookie'); if (!cookieHeader) { return; } @@ -39,15 +48,15 @@ function cleanAndStartRemoteNotifications() { }); socket.on('connect', () => { - logger.log('Remote notifications connected'); + logger.log('✅ Remote notifications connected'); }); socket.on('disconnect', (reason) => { - logger.log('Remote notifications disconnected, reason: ', reason); + logger.log('❌ Remote notifications disconnected, reason: ', reason); }); socket.on('connect_error', (error) => { - logger.error('Remote notifications connect error: ', error); + logger.error('❌ Remote notifications connect error: ', error); }); socket.on('event', (data) => { diff --git a/src/main/remote-sync/handlers.ts b/src/main/remote-sync/handlers.ts index 810264951..f81a7fa70 100644 --- a/src/main/remote-sync/handlers.ts +++ b/src/main/remote-sync/handlers.ts @@ -7,6 +7,7 @@ import { getNewTokenClient } from '../../shared/HttpClient/main-process-client'; import Logger from 'electron-log'; import { ipcMain } from 'electron'; import { reportError } from '../bug-report/service'; +import { sleep } from '../util'; import { broadcastToWindows } from '../windows'; const driveFilesCollection = new DriveFilesCollection(); @@ -63,6 +64,10 @@ ipcMain.handle('get-remote-sync-status', () => ); eventBus.on('RECEIVED_REMOTE_CHANGES', async () => { + // Wait before checking for updates, could be possible + // that we received the notification, but if we check + // for new data we don't receive it + await sleep(500); await remoteSyncManager.startRemoteSync(); }); eventBus.on('USER_LOGGED_IN', () => {