lukeed / sockette

The cutest little WebSocket wrapper! 🧦
MIT License
2.45k stars 81 forks source link

Ability to add custom headers? #28

Closed geekbleek closed 5 years ago

geekbleek commented 6 years ago

I don't see any option to add custom headers to the WS connection. I know this may not be used for browser based implementations of websocket connections, but many server-side client libs need to provide an Authorization header to connect to a websocket. Is this something you could see using on this project? Thanks!

lukeed commented 5 years ago

Hey there,

Unfortunately no, sorry. The browser WebSocket does not allow this behavior so there's no means/purpose in Sockette adding support for it. You can sorta go about this on the browser via protocols, but even so, that's writing into the Sec-WebSocket-Protocol header.

Not certain, but another option is to append a token to the url you're connecting to; ws://localhost:3000?token=abc-123.

This is a nice paper trail: https://github.com/websockets/ws/issues/1333

That said, if you're using Sockette in the server (with a custom WebSocket global), each event handler receives the current ws instance as its event target. Whatever server implementation you are using will be available inside those handlers.

If it were possible, I'd be all for it!

Thanks!