Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a websocket client in a browser send headers?

There is a debate raging in the office about a browsers ability to send handshake headers are part of establishing a websocket connection.

The websocket spec makes it clear that headers are possible. And, infact, in my non-browser client code I do set handshake headers and they are received without and issue.

The open debate is: Can a web browser using javascript insert headers are part of the handshake? The spec seems to indicate there is nothing against it but no one can find client side documentation that says how.

There is a lot of conflicting information. It's obviously possible because I am able to do it in Java without a problem. But for Javascript no one is seeing how. Is it a browser restriction?

To clarify how my question is different than others: Is the inability to do this a limitation of the default javascript libs? The web browser or the spec? It looks like it's a library limitation only

like image 703
Christian Bongiorno Avatar asked Oct 27 '25 04:10

Christian Bongiorno


1 Answers

The issue is that the standard API for creating a webSocket connection in a browser does not provide any mechanism for setting custom headers. So, it is a limitation of the webSocket browser API.

You can control the URL you send including query parameters and cookies are sent with the request so you have the ability to use the path, query parameters or cookies to send additional information with the connection request.

WebSocket WebSocket(
  in DOMString url,
  in optional DOMString protocols
);
like image 160
jfriend00 Avatar answered Oct 29 '25 17:10

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!