Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Add an event on socket connection open

Hakan Shehucf484b5536a1
diff --git a/apps/desktop/src/main/services/socket-connection.ts b/apps/desktop/src/main/services/socket-connection.ts
index 38f2e959..9aa5a891 100644
--- a/apps/desktop/src/main/services/socket-connection.ts
+++ b/apps/desktop/src/main/services/socket-connection.ts
@@ -3,6 +3,7 @@ import { BackoffCalculator } from '@/shared/lib/backoff-calculator';
import { Message } from '@colanode/core';
import { SelectAccount } from '@/main/data/app/schema';
import { syncService } from '@/main/services/sync-service';
+import { eventBus } from '@/shared/lib/event-bus';
export class SocketConnection {
private readonly synapseUrl: string;
@@ -55,6 +56,10 @@ export class SocketConnection {
this.socket.onopen = () => {
this.backoffCalculator.reset();
+ eventBus.publish({
+ type: 'socket_connection_opened',
+ accountId: this.account.id,
+ });
};
this.socket.onerror = () => {
@@ -71,7 +76,7 @@ export class SocketConnection {
}
public sendMessage(message: Message): void {
- if (this.socket) {
+ if (this.socket && this.isConnected()) {
this.socket.send(JSON.stringify(message));
}
}
diff --git a/apps/desktop/src/main/services/sync-service.ts b/apps/desktop/src/main/services/sync-service.ts
index 9ec2e426..ed843b10 100644
--- a/apps/desktop/src/main/services/sync-service.ts
+++ b/apps/desktop/src/main/services/sync-service.ts
@@ -34,6 +34,9 @@ class SyncService {
this.syncLocalTransactions(event.userId);
} else if (event.type === 'workspace_created') {
this.requireNodeTransactions(event.workspace.userId);
+ } else if (event.type === 'socket_connection_opened') {
+ this.requireNodeTransactions(event.accountId);
+ this.requireCollaborations(event.accountId);
}
});
}
diff --git a/apps/desktop/src/shared/types/events.ts b/apps/desktop/src/shared/types/events.ts
index 14457274..d01838a9 100644
--- a/apps/desktop/src/shared/types/events.ts
+++ b/apps/desktop/src/shared/types/events.ts
@@ -132,6 +132,11 @@ export type ServerAvailabilityChangedEvent = {
isAvailable: boolean;
};
+export type SocketConnectionOpenedEvent = {
+ type: 'socket_connection_opened';
+ accountId: string;
+};
+
export type Event =
| NodeCreatedEvent
| NodeUpdatedEvent
@@ -155,4 +160,5 @@ export type Event =
| QueryResultUpdatedEvent
| RadarDataUpdatedEvent
| NodeTransactionCreatedEvent
- | ServerAvailabilityChangedEvent;
+ | ServerAvailabilityChangedEvent
+ | SocketConnectionOpenedEvent;