Skip to content

Commit

Permalink
made some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MridulDhiman committed Dec 28, 2024
1 parent 5a320fe commit 7e606f6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:

- name: Terraform plan
working_directory: ./terraform
run: terraform plan
run: terraform plan

- name: Terraform apply
working_directory: ./terraform
Expand Down
32 changes: 27 additions & 5 deletions src/services/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ const kvStore = new KVService();
class SocketService {
private _io: Server;
private editorManager: EditorStateManager;
private shardRepo: IShardRepository;
constructor(shardRepo: IShardRepository) {
console.log("Init socket server");
this._io = new Server({
cors: {
origin: process.env.FRONTEND_URL!
}
});

this.shardRepo = shardRepo;
this.editorManager = new EditorStateManager(shardRepo);
}
get io() {
Expand All @@ -39,6 +40,20 @@ class SocketService {
try {

await kvStore.set(socket.id, roomId);
const len = await kvStore.llen(roomId);
if (len == 0) {
// first user joined the room
// get the shard by room id
const room = await this.shardRepo.findById(roomId);
if (room) {
const files = room.files;
// populate all the files to redis
for (let file of files) {
let redisKey = `editor:${roomId}:${file.name}:pending`;
await kvStore.set(redisKey, file.code);
}
}
}
await kvStore.rpush(roomId, socket.id);
} catch (error) {
console.log("Could not join room: ", error)
Expand Down Expand Up @@ -142,10 +157,17 @@ class SocketService {
await kvStore.lrem(roomId, 1, socket.id);
const len = await kvStore.llen(roomId);
if (len == 0) {
const exists = await kvStore.exists(socket.id);
if (exists == 1) {
await kvStore.del(socket.id);
}
// all the users left the room -> depopulate the cache
const room = await this.shardRepo.findById(roomId);
const keys = [socket.id, roomId];
if (room) {
const files = room.files;
for (let file of files) {
const redisKey = `editor:${roomId}:${file.name}:pending`;
keys.push(redisKey);
}
}
await kvStore.del(...keys);
}
console.log("User left the room");
}
Expand Down

0 comments on commit 7e606f6

Please sign in to comment.