Rishabh-malhotraa / caucus

Realtime Collaborate Editor with Embedded Compiler
https://caucus.rishabhmalhotra.in/
MIT License
296 stars 69 forks source link

Code duplication when joining #6

Closed StratusFearMe21 closed 3 years ago

StratusFearMe21 commented 3 years ago

Hello! It's that guy you were talking to from DogeHouse. I didn't have time to tell you this but based on my knowledge of the Web socket protocol. I think the reason that this is happening is because when someone new joins a room, the server has to send the current state of the room to the new user, So it sends the whole thing across the websocket, but instead of sending the code to just that user, it broadcasts the packet to everyone in the room, duplicating the current code to all user in the room. I can't seem to duplicate this though so this might be a little difficult

Rishabh-malhotraa commented 3 years ago

Okay yes, I do broadcast the message to all the users when a new user joins in, that could be the reason for this. but what I don't understand is it works perfectly fine on my localhost which is so confusing

https://user-images.githubusercontent.com/54576074/111921131-deb5bd00-8ab8-11eb-87e9-edfc6f3c3e79.mp4

In the end, the user is redirected to the login page because I have like 4 person limit per room

Rishabh-malhotraa commented 3 years ago

Hello! It's that guy you were talking to from DogeHouse. I didn't have time to tell you this but based on my knowledge of the Web socket protocol. I think the reason that this is happening is because when someone new joins a room, the server has to send the current state of the room to the new user, So it sends the whole thing across the websocket, but instead of sending the code to just that user, it broadcasts the packet to everyone in the room, duplicating the current code to all user in the room. I can't seem to duplicate this though so this might be a little difficult

I get the problem, when a new user joins, I am emitting the event to everyone in the room, I just need to send this to the user who joined

    socket.on("send-message", (body: MessageProps) => {
      const roomID = body.userInfo.roomID;
      body.socketID = socket.id;
      socket.broadcast.to(roomID).emit("message", body);
    });
firasjaber commented 3 years ago

hello, that's how i reproduced the bug caucus_bug_reproduce

Rishabh-malhotraa commented 3 years ago

Thanks @firasjaber , another reason I feel this could be happening is when I checked the network tab in the chrome dev tool all I don't see socket connection but xhr polling for some reason, but that is not the case in localhost, it could be a Heroku specific problem in which sockets connection is not being established, I'll try somethings and try to fix this asap