Skip to content

Socket.io

Eric Stuart edited this page Mar 19, 2022 · 3 revisions

Events

Event Name Description
online Used for retrieving and updating currently online buddies for the client
message Used for sending and receiving messages
read Used for marking sent and received messages as read

Client to server

Event Name Description Request Body
message Send a message event to the server of sender, receiver and the message it self {
senderId: Long,
receiverId: Long,
message: String,
timestamp: DateTime,
}
read Send a read event to the server containing receiver id whose message was read {
receiverId: Long,
timestamp: DateTime,
}

Server to client

Event Name Description Response Body
message Broadcast a message event to the receiver of the message {
senderId: Long,
receiverId: Long,
message: String,
timestamp: DateTime,
}
read Broadcast a read event to the sender of the message {
buddyId: Long,
}
online Broadcast online event, when an update in online buddies occurs, to a client user containing a list of all the online added buddies [{
senderId: Long,
receiverId: Long,
message: String,
timestamp: DateTime,
}]

Frontend (client) usage

The client side socket functionality is contained in /src/api/sockets/Socket.js.

This is a react context that wraps the application and exposes these methods to be used:

Functionality Description
connected Boolean value for whether the client's socket is connected to the server
connect Function for creating a socket and establishing a connection between the client and the server
disconnect Function for destroying the socket and closing the connection between the client and the server
sendMessageEvent Function for sending a message from the current client to a receiving client
sendReadEvent Function for sending a read message event from current client to a receiving client
onMessageEvent Listener that executes a passed in call back whenever a new message event comes in to the current client
onOnlineEvent Listener that executes a passed in call back whenever a new buddy comes online

Backend (server) usage

The server side socket functionality is contained in /src/main/java/socket/ChatEventHandler.java

This class contains the following relevant methods:

Method Description
onMessageReceived Listener that sends message to the receiver when it receives a message event
onReadReceived Listener that sends read notice to the buddy when it receives a read event
onConnected Listener that stores current user connected and reports online users to all users
onDisconnected Listener removes current user from store and reports online users to all users
sendMessage Broadcasts a message event to the receiving client
sendOnline Broadcasts an online event to the current client containing a list of all the online buddies
sendRead Broadcast a read event to the receiving client for a given message

Further information and examples

For more documentation about socket.io please refer to the official socket.io documentation.

For specific Socket.IO server implementation used refer to the netty socket.io implementation on github.