Categories
JavaScript Answers

How to set request header when making connection with socket.io client?

Spread the love

Sometimes, we want to set request header when making connection with socket.io client.

In this article, we’ll look at how to set request header when making connection with socket.io client.

How to set request header when making connection with socket.io client?

To set request header when making connection with socket.io client, we can add the extraHeaders option.

For instance, we write

const socket = io("http://localhost", {
  extraHeaders: {
    Authorization: "...",
  },
});

to call io with the socket.io server URL and an object that has the extraHeaders option set to an object with the request headers to send.

We send the Authorization header set to the token as the value.

Conclusion

To set request header when making connection with socket.io client, we can add the extraHeaders option.

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 *