NodeJS support for y-websocket?

I’m curious what it would take to make y-websocket support the nodejs runtime environment.

Currently, when I try to create a new WebsocketProvider in node, it fails with “ReferenceError: window is not defined”, and indeed, it looks like there is a reference to window which makes it browser-only at the moment.

There appears to be an old version of y-websockets-client that supported node.

How difficult would it be to make y-websocket support it as well?

The reference to window can easily be fixed. The only problem is that NodeJs doesn’t support Websocket by default. There is a Websocket polyfill for nodejs that you could use https://github.com/websockets/ws#sending-and-receiving-text-data

I would imagine that the WebsocketProvider receives an Websocket constructor as a argument.

Another approach would be to set Websocket as a global variable in Nodejs.

1 Like

Perhaps it could default to the browser-based Websocket, but allow it to be overridden if passed in.

Exactly. I’m going to republish most of the y-* package. So I’ll get that in by tomorrow.

1 Like