Skip to content

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

Improve socket manager and send pending mutation on device connect

Hakan Shehu1c5ea74b1a9f
diff --git a/desktop/src/data/workspace-manager.ts b/desktop/src/data/workspace-manager.ts
index 53ee4f16..43d3c97a 100644
--- a/desktop/src/data/workspace-manager.ts
+++ b/desktop/src/data/workspace-manager.ts
@@ -174,14 +174,19 @@ export class WorkspaceManager {
public async executeServerMutation(mutation: ServerMutation): Promise<void> {
if (mutation.table === 'nodes') {
- if (mutation.action === 'insert' || mutation.action === 'update') {
- const node = JSON.parse(mutation.after) as ServerNode;
- await this.syncNodeFromServer(node);
- } else if (mutation.action === 'delete') {
- const node = JSON.parse(mutation.before) as ServerNode;
+ if (
+ mutation.after &&
+ (mutation.action === 'insert' || mutation.action === 'update')
+ ) {
+ await this.syncNodeFromServer(mutation.after);
+ } else if (
+ mutation.before &&
+ mutation.action === 'delete' &&
+ mutation.before.id
+ ) {
await this.database
.deleteFrom('nodes')
- .where('id', '=', node.id)
+ .where('id', '=', mutation.before.id)
.execute();
this.debouncedNotifyQuerySubscribers(['nodes']);
@@ -217,7 +222,6 @@ export class WorkspaceManager {
}
public async syncNodeFromServer(node: ServerNode): Promise<void> {
- console.log('Syncing node from server:', node);
const existingNode = await this.database
.selectFrom('nodes')
.selectAll()
diff --git a/server/src/api.ts b/server/src/api.ts
index b6ddab6e..68d0f8e5 100644
--- a/server/src/api.ts
+++ b/server/src/api.ts
@@ -7,11 +7,8 @@ import { accountsRouter } from '@/routes/accounts';
import { workspacesRouter } from '@/routes/workspaces';
import { mutationsRouter } from '@/routes/mutations';
import { authMiddleware } from '@/middlewares/auth';
-import { sockets } from '@/lib/sockets';
-import { SocketMessage } from '@/types/sockets';
+import { synapse } from '@/synapse';
import { syncRouter } from '@/routes/sync';
-import { database } from '@/data/database';
-import { sql } from 'kysely';
export const initApi = () => {
const app = express();
@@ -36,37 +33,8 @@ export const initApi = () => {
path: '/v1/synapse',
});
- wss.on('connection', (socket, req) => {
- console.log('New connection', req.url);
- const deviceId = req.url?.split('device_id=')[1];
- if (!deviceId) {
- socket.close();
- return;
- }
-
- sockets.addSocket(deviceId, socket);
-
- socket.on('message', async (message) => {
- const socketMessage: SocketMessage = JSON.parse(message.toString());
- if (socketMessage.type === 'mutation_ack') {
- const mutationId = socketMessage.payload.id;
- if (!mutationId) {
- return;
- }
-
- await database
- .updateTable('mutations')
- .set({
- device_ids: sql`array_remove(device_ids, ${deviceId})`,
- })
- .where('id', '=', mutationId)
- .execute();
- }
- });
-
- socket.on('close', () => {
- sockets.removeSocket(deviceId);
- });
+ wss.on('connection', async (socket, req) => {
+ await synapse.addConnection(socket, req);
});
server.listen(port, () => {
diff --git a/server/src/consumers/mutations.ts b/server/src/consumers/mutations.ts
index 61a58a14..3b10d09f 100644
--- a/server/src/consumers/mutations.ts
+++ b/server/src/consumers/mutations.ts
@@ -1,6 +1,5 @@
import { redis, CHANNEL_NAMES } from '@/data/redis';
-import { sockets } from '@/lib/sockets';
-import { SocketMessage } from '@/types/sockets';
+import { synapse } from '@/synapse';
import { ServerMutation } from '@/types/mutations';
import { MutationChangeData } from '@/types/changes';
@@ -21,20 +20,14 @@ const handleMessage = async (message: string) => {
action: mutationData.action as 'insert' | 'update' | 'delete',
table: mutationData.table,
workspaceId: mutationData.workspace_id,
- before: mutationData.before,
- after: mutationData.after,
+ before: mutationData.before ? JSON.parse(mutationData.before) : null,
+ after: mutationData.after ? JSON.parse(mutationData.after) : null,
};
for (const deviceId of mutationData.device_ids) {
- const socket = sockets.getSocket(deviceId);
- if (!socket) {
- continue;
- }
-
- const socketMessage: SocketMessage = {
+ synapse.send(deviceId, {
type: 'mutation',
payload: serverMutation,
- };
- socket.send(JSON.stringify(socketMessage));
+ });
}
};
diff --git a/server/src/lib/sockets.ts b/server/src/lib/sockets.ts
deleted file mode 100644
index 31cdeaee..00000000
--- a/server/src/lib/sockets.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import { WebSocket } from 'ws';
-
-class SocketConnections {
- private sockets: Map<string, WebSocket> = new Map();
-
- public addSocket(deviceId: string, ws: WebSocket) {
- this.sockets.set(deviceId, ws);
- }
-
- public removeSocket(deviceId: string) {
- this.sockets.delete(deviceId);
- }
-
- public getSocket(deviceId: string): WebSocket | undefined {
- return this.sockets.get(deviceId);
- }
-}
-
-export const sockets = new SocketConnections();
diff --git a/server/src/synapse.ts b/server/src/synapse.ts
new file mode 100644
index 00000000..833f1903
--- /dev/null
+++ b/server/src/synapse.ts
@@ -0,0 +1,100 @@
+import { WebSocket } from 'ws';
+import { IncomingMessage } from 'http';
+import { SocketMessage } from '@/types/sockets';
+import { database } from './data/database';
+import { sql } from 'kysely';
+import { SelectMutation } from '@/data/schema';
+import { ServerMutation } from '@/types/mutations';
+
+class SynapseManager {
+ private readonly sockets: Map<string, WebSocket> = new Map();
+
+ public async addConnection(socket: WebSocket, req: IncomingMessage) {
+ const deviceId = req.url?.split('device_id=')[1];
+ if (!deviceId) {
+ socket.close();
+ return;
+ }
+
+ socket.on('message', (message) => {
+ this.handleMessage(deviceId, message.toString());
+ });
+
+ socket.on('close', () => {
+ this.sockets.delete(deviceId);
+ });
+
+ this.sockets.set(deviceId, socket);
+ await this.sendPendingMutations(deviceId);
+ }
+
+ public send(deviceId: string, message: SocketMessage) {
+ const socket = this.sockets.get(deviceId);
+ if (!socket) {
+ return;
+ }
+
+ socket.send(JSON.stringify(message));
+ }
+
+ private async handleMessage(
+ deviceId: string,
+ message: string,
+ ): Promise<void> {
+ const socketMessage: SocketMessage = JSON.parse(message);
+ if (socketMessage.type === 'mutation_ack') {
+ await this.handleMutationAck(deviceId, socketMessage);
+ }
+ }
+
+ private async handleMutationAck(deviceId: string, message: SocketMessage) {
+ const mutationId = message.payload.id;
+ if (!mutationId) {
+ return;
+ }
+
+ await database
+ .updateTable('mutations')
+ .set({
+ device_ids: sql`array_remove(device_ids, ${deviceId})`,
+ })
+ .where('id', '=', mutationId)
+ .execute();
+ }
+
+ private async sendPendingMutations(deviceId: string) {
+ let lastId = '0';
+ let hasMore = true;
+
+ while (hasMore) {
+ const pendingMutations = await sql<SelectMutation>`
+ SELECT *
+ FROM mutations
+ WHERE ${deviceId} = ANY(device_ids)
+ AND id > ${lastId}
+ ORDER BY id ASC
+ LIMIT 50
+ `.execute(database);
+
+ for (const mutation of pendingMutations.rows) {
+ const serverMutation: ServerMutation = {
+ id: mutation.id,
+ action: mutation.action as 'insert' | 'update' | 'delete',
+ table: mutation.table,
+ workspaceId: mutation.workspace_id,
+ before: mutation.before,
+ after: mutation.after,
+ };
+ this.send(deviceId, {
+ type: 'mutation',
+ payload: serverMutation,
+ });
+ lastId = mutation.id;
+ }
+
+ hasMore = pendingMutations.rows.length === 50;
+ }
+ }
+}
+
+export const synapse = new SynapseManager();