Skip to content

Commit ca0a5a4

Browse files
committed
feat: storing shapes data in memory to send it to new user joining
1 parent 16b359c commit ca0a5a4

File tree

5 files changed

+201
-78
lines changed

5 files changed

+201
-78
lines changed

apps/collabydraw/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
DATABASE_URL='postgresql://postgres:your_secure_password@localhost:5432/CollabyDraw'
2-
NEXTAUTH_URL="http://localhost:3000"
3-
WS_URL="ws://localhost:8080"
2+
NEXT_PUBLIC_BASE_URL="http://localhost:3000"
3+
NEXT_PUBLIC_WS_URL="ws://localhost:8080"
44
JWT_SECRET=your_secret_here

apps/collabydraw/canvas-engine/CanvasEngine.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ export class CanvasEngine {
150150
}
151151

152152
private connectWebSocket() {
153+
if (
154+
this.socket &&
155+
(this.socket.readyState === WebSocket.CONNECTING ||
156+
this.socket.readyState === WebSocket.OPEN)
157+
) {
158+
console.log("Connection already exists, not creating a new one");
159+
return;
160+
}
161+
162+
console.log(`Connecting to WebSocket with sessionId: ${this.token}`);
163+
153164
const url = `${WS_URL}?token=${encodeURIComponent(this.token!)}`;
154165
this.socket = new WebSocket(url);
155166

@@ -188,6 +199,30 @@ export class CanvasEngine {
188199
}
189200
break;
190201

202+
case WsDataType.EXISTING_SHAPES:
203+
if (Array.isArray(data.message) && data.message.length > 0) {
204+
const decryptedShapes = await Promise.all(
205+
data.message.map(async (shape) => {
206+
if (shape.message) {
207+
const decrypted = await decryptData(
208+
shape.message,
209+
this.encryptionKey!
210+
);
211+
return JSON.parse(decrypted);
212+
}
213+
return null;
214+
})
215+
);
216+
console.log("decryptedShapes = ", decryptedShapes);
217+
218+
const validShapes = decryptedShapes.filter((s) => s !== null);
219+
if (validShapes.length > 0) {
220+
this.updateShapes(validShapes);
221+
this.notifyShapeCountChange();
222+
}
223+
}
224+
break;
225+
191226
case WsDataType.DRAW:
192227
case WsDataType.UPDATE:
193228
if (data.userId !== this.userId && data.message) {
@@ -197,6 +232,7 @@ export class CanvasEngine {
197232
);
198233
const shape = JSON.parse(decrypted);
199234
this.updateShapes([shape]);
235+
this.notifyShapeCountChange();
200236
}
201237
break;
202238

@@ -215,7 +251,7 @@ export class CanvasEngine {
215251
this.isConnected = false;
216252
this.onConnectionChange?.(false);
217253
console.warn("WebSocket closed:", e);
218-
setTimeout(() => this.connectWebSocket(), 1000);
254+
setTimeout(() => this.connectWebSocket(), 2000);
219255
};
220256

221257
this.socket.onerror = (err) => {
@@ -1383,7 +1419,8 @@ export class CanvasEngine {
13831419
);
13841420

13851421
if (rounded === "round") {
1386-
const r = Math.min(normalizedWidth, normalizedHeight) * ROUND_RADIUS_FACTOR;
1422+
const r =
1423+
Math.min(normalizedWidth, normalizedHeight) * ROUND_RADIUS_FACTOR;
13871424

13881425
this.roughCanvas.path(
13891426
`M ${posX + r} ${posY}

apps/collabydraw/utils/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { v4 as uuidv4 } from "uuid";
2+
3+
export const getOrCreateSessionId = (): string => {
4+
const key = "__collab_session_id";
5+
if (typeof window === "undefined") {
6+
return uuidv4();
7+
}
8+
let sessionId = sessionStorage.getItem(key);
9+
if (!sessionId) {
10+
sessionId = uuidv4();
11+
sessionStorage.setItem(key, sessionId);
12+
}
13+
return sessionId;
14+
};

0 commit comments

Comments
 (0)