December 2017
Intermediate to advanced
260 pages
7h 34m
English
The Socket.IO library works on a simple principle—emit and listen. Clients emit the messages and a listener listens to those messages and performs an action associated with them.
So now, whenever a user successfully logs in, we will emit an event named add_user and the server will add it to an online user's list.
The following code line emits the message:
socket.emit("add_user", it)
The following code snippet listens to the message and adds a user to the list:
socket.on("add_user", { nickName -> socket.nickname= nickName numOfUsers = numOfUsers.inc() usersList.add(nickName as String) })
The socket.on function will listen to the add_user event and store the nickname in the socket.
Read now
Unlock full access