Disconnecting a socket

Whenever any user leaves the chat room, we will show other online users a message saying x user left. Socket.IO will send a notification to the server when any client disconnects. Upon receiving the disconnect, the event server will remove the user from the list, decrement the count of online users, and broadcast the event to all clients. The code can look something like this:

    socket.on("disconnect", {      usersList.remove(socket.nicknameas String)      numOfUsers = numOfUsers.dec()      val userJoinedData = json(Pair("numOfUsers", numOfUsers),          Pair("nickName", socket.nickname))      socket.broadcast.emit("user_left", userJoinedData)    })

Now, it's the client's responsibility to show the message for that event on the UI. The client will ...

Get Kotlin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.