GitHub: https://github.com/Sokhavuth/chat
Heroku: https://khmerweb-chat.herokuapp.com/
When a user leaves the chat page, related socket client could inform socket server for it to broadcast this information to all connected socket clients. In this case, socket server could delete the user who left the chat page from the users collection object and broadcast this new users object to socket clients.
Top comments (5)
Woah, nice one bro!
Regrads by My Pijari
Should extract all the server side "socket.on('user left'" code (except the socket.disconnect() )
into
or
That way u will also clean things up and emit if the user close the browser/tab
OK, I see in the case the user closes the browser.
Yes...
Scrolled through your code and seems u just generate the userid.. think u need to map the userid to socket.id though, using a hash map for this wld make most sense i guess.
But on other hand, i don't see why use a userid at all. If want cld persist a user name or whatever in local storage and for id just use the socket.id .. this will ofc change every time u connect with a new socket but if this is what u store temporarily on the server and maps the user to then it makes things much easier to handle as wont need any extra mapping between the socket.id and the userid.
Thank you for your advice. I will use socket.id instead of random uuid for socket client.