DEV Community

Cover image for Chat App with WebSocket: Deleting User
Sokhavuth TIN
Sokhavuth TIN

Posted on

Chat App with WebSocket: Deleting User


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)

Collapse
 
pijari profile image
naiteswa

Woah, nice one bro!
Regrads by My Pijari

Collapse
 
zoppatorsk profile image
Zoppatorsk • Edited

Should extract all the server side "socket.on('user left'" code (except the socket.disconnect() )
into

socket.on("disconnect", () => {
Enter fullscreen mode Exit fullscreen mode

or

socket.on('disconnecting', () => {
Enter fullscreen mode Exit fullscreen mode

That way u will also clean things up and emit if the user close the browser/tab

Collapse
 
sokhavuth profile image
Sokhavuth TIN • Edited

OK, I see in the case the user closes the browser.

Collapse
 
zoppatorsk profile image
Zoppatorsk

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.

Thread Thread
 
sokhavuth profile image
Sokhavuth TIN • Edited

Thank you for your advice. I will use socket.id instead of random uuid for socket client.