Skip to content

Commit e751ef1

Browse files
committed
clean up code
1 parent 7e38172 commit e751ef1

File tree

10 files changed

+61
-310
lines changed

10 files changed

+61
-310
lines changed

backend/collaboration/src/config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ export const UI_HOST = process.env.PEERPREP_UI_HOST!;
44

55
export const EXPRESS_PORT = process.env.EXPRESS_PORT;
66

7-
export const WEBSOCKET_PORT = process.env.WEBSOCKET_PORT;
7+
export const dbConfig = {
8+
host: process.env.EXPRESS_DB_HOST!,
9+
port: Number.parseInt(process.env.EXPRESS_DB_PORT!),
10+
database: process.env.POSTGRES_DB!,
11+
user: process.env.POSTGRES_USER,
12+
password: process.env.POSTGRES_PASSWORD,
13+
};

backend/collaboration/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { EXPRESS_PORT } from '@/config';
22
import { logger } from '@/lib/utils/logger';
33
import { dbHealthCheck } from '@/server';
4-
import server from '@/websocket';
4+
import server from '@/server';
55

66
const port = Number.parseInt(EXPRESS_PORT || '8001');
7-
const wsPort = Number.parseInt(WEBSOCKET_PORT || '8002');
87

98
const listenMessage = `App listening on port: ${port}`;
109
server.listen(port, () => {
1110
void dbHealthCheck();
1211
logger.info(listenMessage);
1312
});
14-
server.listen(wsPort, () => logger.info(`WebSocket listening on port: ${wsPort}`));

backend/collaboration/src/server.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import { config, db } from '@/lib/db';
1010
import { logger } from '@/lib/utils';
1111
import roomRoutes from '@/routes/room';
1212

13+
import { WebSocketServer } from 'ws';
14+
import { createServer } from 'http';
15+
import { setupWSConnection } from './y-postgresql-util/utils';
16+
import { setUpPersistence } from './y-postgresql-util/persistence';
1317
const app = express();
18+
const server = createServer(app);
19+
1420
app.use(pino());
1521
app.use(json());
1622
app.use(
@@ -25,6 +31,10 @@ app.use('/room', roomRoutes);
2531
// Health Check for Docker
2632
app.get('/health', (_req, res) => res.status(StatusCodes.OK).send('OK'));
2733

34+
// y-websocket server
35+
const wss = new WebSocketServer({ server });
36+
wss.on('connection', setupWSConnection);
37+
setUpPersistence();
2838
export const dbHealthCheck = async () => {
2939
try {
3040
await db`SELECT 1`;
@@ -42,4 +52,5 @@ app.get('/test-db', async (_req, res) => {
4252
await dbHealthCheck();
4353
res.json({ message: 'OK ' });
4454
});
45-
export default app;
55+
56+
export default server;
File renamed without changes.

backend/collaboration/src/websocket.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as Y from 'yjs';
2+
import { setPersistence } from '@/y-postgresql-util/utils';
3+
import { IWSSharedDoc } from '@/types/interfaces';
4+
import { PostgresqlPersistence } from 'y-postgresql';
5+
import { dbConfig } from '@/config';
6+
7+
export const setUpPersistence = async () => {
8+
const pgdb = await PostgresqlPersistence.build(dbConfig);
9+
setPersistence({
10+
bindState: async (docName: string, ydoc: IWSSharedDoc) => {
11+
// Get the persisted document from PostgreSQL
12+
const persistedYdoc = await pgdb.getYDoc(docName);
13+
14+
// Apply the current state from the database to the Yjs document
15+
Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc));
16+
17+
// Merge new updates with the persisted state and store
18+
ydoc.on('update', async (update: Uint8Array) => {
19+
const currentUpdates = await pgdb.getYDoc(docName);
20+
const mergedUpdates = Y.mergeUpdates([Y.encodeStateAsUpdate(currentUpdates), update]);
21+
//Remove the previous entry from the database
22+
await pgdb.clearDocument(docName);
23+
// Store the merged updates in the database
24+
await pgdb.storeUpdate(docName, mergedUpdates);
25+
});
26+
},
27+
28+
// This function is called to write the final state (when the document is closed)
29+
writeState: (__docName: string, __ydoc: IWSSharedDoc) => {
30+
return new Promise((resolve) => {
31+
resolve(true);
32+
});
33+
},
34+
});
35+
};

backend/collaboration/src/y-postgresql-util/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
Implementation from yjs-websocket/bin/utils.js
3+
Copied here for safe referencing
4+
*/
15
import * as Y from 'yjs';
26
import * as syncProtocol from 'y-protocols/sync';
37
import * as awarenessProtocol from 'y-protocols/awareness';
@@ -6,7 +10,7 @@ import * as encoding from 'lib0/encoding';
610
import * as decoding from 'lib0/decoding';
711
import * as map from 'lib0/map';
812

9-
import { IPersistence, IWSSharedDoc } from '@/y-postgresql-util/interfaces';
13+
import { IPersistence, IWSSharedDoc } from '@/types/interfaces';
1014

1115
const wsReadyStateConnecting = 0;
1216
const wsReadyStateOpen = 1;

backend/collaboration/src/y-postgresql/interfaces.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)