To get connection status on Socket.io client with JavaScript, we use the socket.connected
property.
For instance, we write
const socket = io.connect();
console.log("check 1", socket.connected);
socket.on("connect", () => {
console.log("check 2", socket.connected);
});
to get the connection status from the socket.connected
boolean variable.
If it’s true
, then the client is connected.