websockets icon indicating copy to clipboard operation
websockets copied to clipboard

Allow dictionary as second param in WebSocket constructor

Open KhafraDev opened this issue 2 years ago • 4 comments

This would allow server environments (specifically talking about nodejs/undici) that implement WebSocket some flexibility, for example, to add custom headers, etc. While we could change it on our side, it would be incompatible with other implementations. Similarly, it'll cause type issues with people who use typescript in cross-env packages (ie. https://github.com/nodejs/undici/issues/1943).

Proposed change:

dictionary WebSocketInit {
  protocols: DOMString or sequence<DOMString>
}

interface WebSocket : EventTarget {
  constructor(USVString url, optional (DOMString or sequence<DOMString> or WebSocketInit) options = []);

KhafraDev avatar Feb 24 '23 23:02 KhafraDev

It's a sensible extension point and as far as I know compatible with existing practice. But it's hard to motivate it from a browser implementation point of view.

In other words, I'm not opposed, but I don't know how I'd justify the work.

ricea avatar Feb 27 '23 11:02 ricea

understandable, I was hoping it would be minor enough for browser implementers to not mind lol

KhafraDev avatar Feb 28 '23 01:02 KhafraDev

Bun also allows a dictionary as second parameter for setting custom headers, example here.

KhafraDev avatar Apr 23 '23 05:04 KhafraDev

it was also suggested in https://github.com/whatwg/websockets/issues/16#issuecomment-881547333 which I didn't know about

KhafraDev avatar Apr 23 '23 21:04 KhafraDev