K
K
Kneepy2021-09-13 16:05:38
Node.js
Kneepy, 2021-09-13 16:05:38

How to remove a user from a room if he moved to another room?

There is a comment system. When a user enters a page with a post, he is automatically assigned to a room with a name equal to this very value. And everything would be fine, but if I go to another post, then I will stay in the room of the previous post and see the messages just sent from there. The essence of the question is that it is not clear to me how the user can not be added to this room, but move from the previous one to the new one. Below is the code
of what is currently there.

There is some page that sends the connect-post-room event with data in the form of a post number and on the backend I accept this serial number and create a room with this value:

io.on('connection', (socket) => {
   socket.on('connect-post-room', pid => {
      const postRoom = `post:${pid}`
      socket.join(postRoom)
      socket.on('send-comment', comment => postService.addCommentSocket(comment, io, postRoom))
   })
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2021-09-13
@inoise

We throw out the word "transition" as it means only a process, but has nothing to do with changing the data. First you need to leave the current room (the client sends a request) and only after successfully leaving we join from the new room (if we really need it, we validate the presence of the user in other rooms)
Of course, the user-room mapping must be stored somewhere on the server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question