Skip to content

Commit ea987f1

Browse files
committed
Change collaboration socket to use y-websocket default encoder
1 parent cb3300c commit ea987f1

File tree

8 files changed

+174
-216
lines changed

8 files changed

+174
-216
lines changed

backend/collaboration-service/app.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import collaborationRoutes from "./routes/collaborationRoutes";
88
import { errorHandler } from "./middlewares/errorHandler";
99
import { connectToDatabase } from "./utils/database";
1010
import { setUpKafkaSubscribers } from "./utils/kafkaClient";
11-
import { YDocManager } from './utils/yjs';
12-
import * as Y from 'yjs';
11+
const setupWSConnection = require("y-websocket/bin/utils").setupWSConnection;
12+
1313

1414
dotenv.config({ path: path.resolve(__dirname, "./.env") });
1515

@@ -36,40 +36,43 @@ app.use(errorHandler);
3636
const wss = new WebSocket.Server({ server });
3737

3838
wss.on("connection", (ws, req) => {
39-
// Extract sessionId from the URL for connecting to the Yjs document
40-
const sessionId = req.url?.split('/').pop();
39+
// // Extract sessionId from the URL for connecting to the Yjs document
40+
// const sessionId = req.url?.split('/').pop();
41+
42+
// console.log(`A user has connected to session: ${sessionId}!`)
4143

42-
if (sessionId) {
43-
// Initialize or get the Yjs document from YDocManager
44-
const ydoc = YDocManager.getDoc(sessionId) || YDocManager.initializeDoc(sessionId);
44+
// if (sessionId) {
45+
// // Initialize or get the Yjs document from YDocManager
46+
// const ydoc = YDocManager.getDoc(sessionId) || YDocManager.initializeDoc(sessionId);
4547

46-
// Set up the document synchronization logic
47-
const encoder = Y.encodeStateAsUpdate(ydoc);
48-
ws.send(encoder);
48+
// // Set up the document synchronization logic
49+
// const encoder = Y.encodeStateAsUpdate(ydoc);
50+
// ws.send(encoder);
4951

50-
ws.on('message', (message) => {
51-
const update = new Uint8Array(message instanceof ArrayBuffer ? message : (message as Buffer).buffer);
52-
Y.applyUpdate(ydoc, update);
53-
wss.clients.forEach((client) => {
54-
if (client !== ws && client.readyState === WebSocket.OPEN) {
55-
client.send(update);
56-
}
57-
});
58-
});
52+
// ws.on('message', (message) => {
53+
// const update = new Uint8Array(message instanceof ArrayBuffer ? message : (message as Buffer).buffer);
54+
// Y.applyUpdate(ydoc, update);
55+
// wss.clients.forEach((client) => {
56+
// if (client !== ws && client.readyState === WebSocket.OPEN) {
57+
// client.send(update);
58+
// }
59+
// });
60+
// });
5961

60-
ws.on('close', () => {
61-
console.log(`Connection closed for session: ${sessionId}`);
62-
});
63-
64-
} else {
65-
console.error("No session ID found in WebSocket connection URL.");
66-
ws.close();
67-
}
62+
// ws.on('close', () => {
63+
// console.log(`Connection closed for session: ${sessionId}`);
64+
// });
65+
66+
// } else {
67+
// console.error("No session ID found in WebSocket connection URL.");
68+
// ws.close();
69+
// }
70+
setupWSConnection(ws, req);
6871
});
6972

7073
// Start the server
7174
server.listen(port, async () => {
7275
console.log(`Collaboration service running on http://localhost:${port}`);
7376

7477
await setUpKafkaSubscribers();
75-
});
78+
});

backend/collaboration-service/controllers/collaborationController.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { EachMessagePayload } from 'kafkajs';
33
import { COLLAB_TOPIC, producer } from '../utils/kafkaClient';
44
import Session from '../models/Session';
5-
import { YDocManager } from '../utils/yjs';
65

76

87
export const createSession = async ( matchId: string, userIds: string[]) => {
@@ -12,9 +11,6 @@ export const createSession = async ( matchId: string, userIds: string[]) => {
1211
// Create new Session document in mongodb
1312
const newSession = new Session({ sessionId, matchId, userIds, codeContent: '' });
1413
await newSession.save();
15-
16-
// Initialize Yjs document manager for this session
17-
YDocManager.initializeDoc(sessionId);
1814

1915
return sessionId;
2016
};

backend/collaboration-service/package-lock.json

Lines changed: 23 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/collaboration-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@types/express": "^4.17.21",
3333
"@types/jsonwebtoken": "^9.0.7",
3434
"@types/mongoose": "^5.11.97",
35+
"@types/node": "^22.8.6",
3536
"@types/socket.io": "^3.0.1",
3637
"@types/ws": "^8.5.12",
3738
"nodemon": "^3.1.1",

0 commit comments

Comments
 (0)