Categories
JavaScript Answers

How to close client connection with Node.js socket.io and JavaScript?

Spread the love

Sometimes, we want to close client connection with Node.js socket.io and JavaScript.

In this article, we’ll look at how to close client connection with Node.js socket.io and JavaScript.

How to close client connection with Node.js socket.io and JavaScript?

To close client connection with Node.js socket.io and JavaScript, we call the on method.

For instance, we write

socket.on("end", () => {
  socket.disconnect(0);
});

to call on to listen to the 'end' event on the server.

In the event handler, we call disconnect to close the client connection.

Then on client side, we write

const socket = io();
socket.emit("end");

to call socket.emit with 'end' to emit the end event to the server.

Conclusion

To close client connection with Node.js socket.io and JavaScript, we call the on method.

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 *