Skip to content

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

Receive updates from websockets

Hakan Shehu2195e7fb6197
diff --git a/desktop/src/data/account-manager.ts b/desktop/src/data/account-manager.ts
index fa22dd10..3f1a0bbf 100644
--- a/desktop/src/data/account-manager.ts
+++ b/desktop/src/data/account-manager.ts
@@ -3,16 +3,19 @@ import { Account } from '@/types/accounts';
import { Workspace } from '@/types/workspaces';
import { WorkspaceManager } from '@/data/workspace-manager';
import { SocketManager } from '@/data/socket-manager';
+import { Update } from '@/types/updates';
export class AccountManager {
private readonly accountPath: string;
private readonly socket: SocketManager;
private readonly workspaces: Map<string, WorkspaceManager>;
private interval: NodeJS.Timeout;
+ private sentTransactions: Set<string>;
constructor(account: Account, appPath: string, workspaces: Workspace[]) {
this.socket = new SocketManager(account);
this.workspaces = new Map<string, WorkspaceManager>();
+ this.sentTransactions = new Set<string>();
this.accountPath = `${appPath}/${account.id}`;
if (!fs.existsSync(this.accountPath)) {
@@ -43,6 +46,19 @@ export class AccountManager {
const workspace = this.workspaces.get(workspaceId);
await workspace?.acknowledgeTransaction(transactionId);
+ this.sentTransactions.delete(transactionId);
+ });
+
+ this.socket.on('update', async (payload) => {
+ const update = payload as Update;
+ const workspaceId = update.workspaceId;
+
+ if (!this.workspaces.has(workspaceId)) {
+ return;
+ }
+
+ const workspace = this.workspaces.get(workspaceId);
+ await workspace?.applyUpdate(update);
});
this.startEventLoop();
@@ -94,6 +110,11 @@ export class AccountManager {
}
for (const transaction of transactions) {
+ if (this.sentTransactions.has(transaction.id)) {
+ continue;
+ }
+
+ this.sentTransactions.add(transaction.id);
this.socket.send({
type: 'transaction',
payload: transaction,
diff --git a/desktop/src/data/socket-manager.ts b/desktop/src/data/socket-manager.ts
index 9bec8743..f3bf39bf 100644
--- a/desktop/src/data/socket-manager.ts
+++ b/desktop/src/data/socket-manager.ts
@@ -13,15 +13,28 @@ export class SocketManager {
private socket: WebSocket | null;
private closingCount: number;
private listeners: Map<string, ((payload: any) => void)[]>;
+ private lastInitAt: number;
constructor(account: Account) {
this.account = account;
this.socket = null;
this.closingCount = 0;
this.listeners = new Map();
+ this.lastInitAt = 0;
}
public init() {
+ if (this.isConnected()) {
+ return;
+ }
+
+ // Prevent multiple init calls in a short period
+ // init only once in 5 seconds
+ if (Date.now() - this.lastInitAt < 5000) {
+ return;
+ }
+
+ this.lastInitAt = Date.now();
this.closingCount = 0;
this.socket = new WebSocket(
`${SOCKET_URL}/v1/synapse?device_id=${this.account.deviceId}`,
diff --git a/desktop/src/data/workspace-manager.ts b/desktop/src/data/workspace-manager.ts
index 3ec55d4b..76e8debc 100644
--- a/desktop/src/data/workspace-manager.ts
+++ b/desktop/src/data/workspace-manager.ts
@@ -23,6 +23,8 @@ import {
TransactionTable,
} from '@/types/transactions';
import { eventBus } from '@/lib/event-bus';
+import { Update } from '@/types/updates';
+import { Node } from '@/types/nodes';
export class WorkspaceManager {
private readonly account: Account;
@@ -144,6 +146,68 @@ export class WorkspaceManager {
.execute();
}
+ public async applyUpdate(update: Update) {
+ if (update.type === 'node_sync') {
+ await this.handleNodeSync(update);
+ }
+ }
+
+ private async handleNodeSync(update: Update) {
+ const node = JSON.parse(JSON.parse(update.content)) as Node;
+ const existingNode = await this.database
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', '=', node.id)
+ .executeTakeFirst();
+
+ if (!existingNode) {
+ const query = this.database
+ .insertInto('nodes')
+ .values({
+ id: node.id,
+ type: node.type,
+ parent_id: node.parentId,
+ workspace_id: this.workspace.id,
+ index: node.index,
+ content: node.content as any,
+ attrs: node.attrs as any,
+ created_at: node.createdAt,
+ created_by: node.createdBy,
+ updated_at: node.updatedAt,
+ updated_by: node.updatedBy,
+ version_id: node.versionId,
+ server_created_at: node.serverCreatedAt,
+ server_updated_at: node.serverUpdatedAt,
+ server_version_id: node.serverVersionId,
+ })
+ .compile();
+ console.log('query', query);
+
+ await this.execute(query);
+ } else {
+ //TODO: check if the local version has changes before updating
+ const query = this.database
+ .updateTable('nodes')
+ .set({
+ type: node.type,
+ parent_id: node.parentId,
+ index: node.index,
+ content: node.content as any,
+ attrs: node.attrs as any,
+ updated_at: node.updatedAt,
+ updated_by: node.updatedBy,
+ version_id: node.versionId,
+ server_created_at: node.serverCreatedAt,
+ server_updated_at: node.serverUpdatedAt,
+ server_version_id: node.serverVersionId,
+ })
+ .where('id', '=', node.id)
+ .compile();
+
+ await this.execute(query);
+ }
+ }
+
private async migrate() {
const migrator = new Migrator({
db: this.database,
diff --git a/desktop/src/lib/nodes.ts b/desktop/src/lib/nodes.ts
index 67d65f04..0f921f6e 100644
--- a/desktop/src/lib/nodes.ts
+++ b/desktop/src/lib/nodes.ts
@@ -16,12 +16,15 @@ export const buildNodeWithChildren = (
};
};
-export const generateNodeIndex = (before?: string | null, after?: string | null) => {
+export const generateNodeIndex = (
+ before?: string | null,
+ after?: string | null,
+) => {
const lower = before === undefined ? null : before;
const upper = after === undefined ? null : after;
return generateKeyBetween(lower, upper);
-}
+};
export const mapNode = (row: NodesTableSchema): Node => {
return {
@@ -32,16 +35,12 @@ export const mapNode = (row: NodesTableSchema): Node => {
workspaceId: row.workspace_id,
attrs: row.attrs && JSON.parse(row.attrs),
content: row.content && JSON.parse(row.content),
- createdAt: new Date(row.created_at),
+ createdAt: row.created_at,
createdBy: row.created_by,
- updatedAt: row.updated_at ? new Date(row.updated_at) : null,
+ updatedAt: row.updated_at,
updatedBy: row.updated_by,
versionId: row.version_id,
- serverCreatedAt: row.server_created_at
- ? new Date(row.server_created_at)
- : null,
- serverUpdatedAt: row.server_updated_at
- ? new Date(row.server_updated_at)
- : null,
+ serverCreatedAt: row.server_created_at,
+ serverUpdatedAt: row.server_updated_at,
};
-}
\ No newline at end of file
+};
diff --git a/desktop/src/types/nodes.ts b/desktop/src/types/nodes.ts
index 1fe730a8..baff0517 100644
--- a/desktop/src/types/nodes.ts
+++ b/desktop/src/types/nodes.ts
@@ -6,13 +6,14 @@ export type Node = {
index?: string | null;
attrs?: Record<string, any> | null;
content?: NodeBlock[] | null;
- createdAt: Date;
+ createdAt: string;
createdBy: string;
- updatedAt?: Date | null;
+ updatedAt?: string | null;
updatedBy?: string | null;
versionId: string;
- serverCreatedAt?: Date | null;
- serverUpdatedAt?: Date | null;
+ serverCreatedAt?: string | null;
+ serverUpdatedAt?: string | null;
+ serverVersionId?: string | null;
};
export type NodeBlock = {
diff --git a/desktop/src/types/updates.ts b/desktop/src/types/updates.ts
new file mode 100644
index 00000000..5795f05a
--- /dev/null
+++ b/desktop/src/types/updates.ts
@@ -0,0 +1,8 @@
+export type Update = {
+ id: string;
+ workspaceId: string;
+ deviceId: string;
+ type: string;
+ content: string;
+ createdAt: string;
+};
diff --git a/server/src/consumers/transactions.ts b/server/src/consumers/transactions.ts
index 72c73959..20224d14 100644
--- a/server/src/consumers/transactions.ts
+++ b/server/src/consumers/transactions.ts
@@ -65,11 +65,11 @@ const handleInsertNodeTransaction = async (transaction: Transaction) => {
};
if (input.attrs !== undefined && input.attrs !== null) {
- data.attrs = input.attrs;
+ data.attrs = JSON.parse(input.attrs);
}
if (input.content !== undefined && input.content !== null) {
- data.content = input.content;
+ data.content = JSON.parse(input.content);
}
await prisma.nodes.create({
diff --git a/server/src/consumers/updates.ts b/server/src/consumers/updates.ts
index 6fba3353..f73c0e97 100644
--- a/server/src/consumers/updates.ts
+++ b/server/src/consumers/updates.ts
@@ -1,6 +1,7 @@
import { redis, CHANNEL_NAMES } from '@/data/redis';
import { sockets } from '@/lib/sockets';
import { Update } from '@/types/updates';
+import { SocketMessage } from '@/types/sockets';
export const initUpdatesSubscriber = async () => {
const subscriber = redis.duplicate();
@@ -8,12 +9,16 @@ export const initUpdatesSubscriber = async () => {
await subscriber.subscribe(CHANNEL_NAMES.UPDATES, handleMessage);
};
-const handleMessage = async (_: string, message: string) => {
+const handleMessage = async (message: string) => {
const update = JSON.parse(message) as Update;
const socket = sockets.getSocket(update.deviceId);
if (!socket) {
return;
}
- socket.send(JSON.stringify(update));
+ const socketMessage: SocketMessage = {
+ type: 'update',
+ payload: update,
+ };
+ socket.send(JSON.stringify(socketMessage));
};
diff --git a/server/src/index.ts b/server/src/index.ts
index 49f06386..9e5e40d6 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -18,7 +18,7 @@ initUpdateChangesConsumer().then(() => {
initRedis().then(() => {
console.log('Redis initialized');
- // initUpdatesSubscriber().then(() => {
- // console.log('Updates subscriber started');
- // });
+ initUpdatesSubscriber().then(() => {
+ console.log('Updates subscriber started');
+ });
});