Categories
JavaScript Answers

How to get session id of socket.io client in Client with JavaScript?

Spread the love

Sometimes, we want to get session id of socket.io client in Client with JavaScript.

In this article, we’ll look at how to get session id of socket.io client in Client with JavaScript.

How to get session id of socket.io client in Client with JavaScript?

To get session id of socket.io client in Client with JavaScript, we use the id property.

For instance, we write

const sio = require("socket.io");
const app = require("express").createServer();

app.listen(8080);
const io = sio.listen(app);

io.on("connection", (client) => {
  console.log("client connected");

  client.send(client.id);

  client.on("disconnect", () => {
    console.log("client disconnected");
  });
});

to call io.on to listen for the connection event.

In the event listener, we get the client’s ID with client.id.

Conclusion

To get session id of socket.io client in Client with JavaScript, we use the id property.

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 *