2

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

Community
  • 1
  • 1
Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76
  • It can't be a browser restriction, can it? Everyone can write their own browser if they so wish. – RaminS Oct 04 '16 at 17:42
  • Possible duplicate of [HTTP headers in Websockets client API](http://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api) – vtortola Oct 04 '16 at 19:29

2 Answers2

1

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
);
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

I am afraid that the WebSocket API in web browsers does not allow to set custom headers. Still, the browser sets cookies, if any.

vtortola
  • 34,709
  • 29
  • 161
  • 263