Basic Y.js Websocket

Hi. I’m having trouble implementing Y-websocket with React. I’m running a y-websocket-server then using the code below to connect.

 const wsProvider = new WebsocketProvider(
        'ws://localhost:1234', 'room',
        doc,
        { WebSocketPolyfill: WebSocket }
      )

When I run it locally it works, but I want other devices in the same network to access the websocket. When I use my IP address instead of localhost, it doesn’t connect. So, it’s currently only working on the device running the websocket server, but not any other local devices.

Am I approaching this wrong? How would I fix this?

You probably just have to set HOST=0.0.0.0 y-websocket-server instead of localhost.

Your OS might also prevent outside communication. Check your firewall settings and search for similar issues for your operating system.

1 Like

Hi ! I am still having trouble implementing y-websocket with JS.

const provider = new WebsocketProvider('ws://192.168.xxx.xxx:1234', 'rectangle-demo', ydoc);

I tried changing “ws://localhost:1234” to “ws://192.168.xxx.xxx:1234” and it still doesn’t work.

I tried setting the HOST=0.0.0.0 as you recommend using $env:HOST=0.0.0.0

And I am running the websocket using the command: npx y-websocket-server --host 0.0.0.0 --port 1234
gives the result running at 'localhost' on port 1234
Shouldn’t the “localhost” change to the ip address?

In the console this pops : WebSocket connection to 'ws://192.168.xxx.xxx:1234/rectangle-demo' failed:

It just works on the localhost :man_shrugging:

netstat -an | findstr 1234
  TCP    [::1]:1234             [::]:0                 LISTENING
  TCP    [::1]:1234             [::1]:56984            ESTABLISHED
  TCP    [::1]:56984            [::1]:1234             ESTABLISHED

And the port is just listening to ipv6 but not on ipv4. So I guess --host is not helping here.
What mistake am I making? And how to fix this?
@dmonad @canadaduane

As I said, check your firewall and maybe try out if other applications work. I’m not a network expert and I don’t understand your setup (my router is also preventing direct communication between devices).

y-websocket simply uses the http module of nodejs. If you can get to work, you can get y-websocket to work.