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.