yjs / y-websocket

Websocket Connector for Yjs
https://docs.yjs.dev/ecosystem/connection-provider/y-websocket
MIT License
492 stars 255 forks source link

cannot establish WebSocket connection from browser #136

Closed rozek closed 1 year ago

rozek commented 1 year ago

Hello,

while it is possible to establish a connection to a running y-websocket server from a different machine using Node.js, trying to do the same from within a browser (on the machine where Node.js succeeds) always fails.

Unfortunately, the browser console is not of much help, it just states: "Websocket connection to 'wss://...' failed:" and points to line 131 of y-websocket.js: const websocket = new provider._WS(provider.url). And the network tab is also useless as it is the connection, which cannot be established.

I tried several Mac's from macOS High Sierra to macOS Ventura, with Intel and M1 processors.

Is there any "trick" I could try to get it working?

rozek commented 1 year ago

I meanwhile also tested one of the isomorphic WebSocket wrappers (namely "isomorphic-ws") - but that fails as well.

For that reason, I'm expecting a problem related to the browser and its security mechanisms (I'm using the latest Chrome and Brave browsers)

Just to clarify: "Content Security Policies" don't seem to be the cause - the browser does not complain about any mismatches

dmonad commented 1 year ago

wss only succeeds if you are running on localhost (depending on the browser) or if you run on https://. Try ws://.

In any case, this doesn't seem to be an issue with y-websocket. So I'll close this ticket. If you need more help, please provide specific steps to reproduce the issue (i.e. how are you running the server, what is the URL you are pointing to?).

rozek commented 1 year ago

Thanks for the quick response.

Actually, I AM using ws:// - it is the browser which changes this internally to wss://. [Edit] even from a page that was loaded from "localhost" (and, thus, using HTTP)

I just also tested a Chrome extension (the "WebSocket test Client"): here, the socket can be opened.

Thus, it seems as I will need a method to prevent Chrome/Brave/... from rewriting ws to wss...

rozek commented 1 year ago

FYI: I solved the problem by rewriting the y-websocket server to use https instead of http using a certificate loaded from file system - and a certificate generated and signed by my local CA which my PCs were configured to trust...changes are in my local fork of your repo. If you like, I may start a pull request