Skip to content

Commit

Permalink
Merge pull request #275 from internxt/fix/remote-notifications-connec…
Browse files Browse the repository at this point in the history
…tion

[_]:(fix) Connect correctly to notifications websocket
  • Loading branch information
PixoDev authored Jun 23, 2023
2 parents eff5539 + ad0e209 commit 87f293d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/main/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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) => {
Expand Down
5 changes: 5 additions & 0 deletions src/main/remote-sync/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 87f293d

Please sign in to comment.