Categories
JavaScript Answers

How to get connection status on Socket.io client with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *