Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using @y-sweet/client in nodejs #197

Open
beeing opened this issue Dec 22, 2023 · 3 comments
Open

Using @y-sweet/client in nodejs #197

beeing opened this issue Dec 22, 2023 · 3 comments

Comments

@beeing
Copy link

beeing commented Dec 22, 2023

Hi, just started to explore the library and wondering we can use nodejs as the client?

I'm using ws package as the nodejs WebSocket client but WebSocketPolyfill is not compatible.

Also is it possible to have y-sweet server to easily mutate the document directly without creating a Y Doc instance on client side?

Thanks!

@paulgb
Copy link
Member

paulgb commented Dec 23, 2023

Hi @beeing, we use ws in node for unit tests, but for some reason we have to pass in WebSocketPolyfill: require('ws') instead of importing WebSocket from ws and passing WebSocketPolyfill: WebSocket. I can't remember why that is, but the code is here: https://github.com/drifting-in-space/y-sweet/blob/a519ba5853d2e6cbff8c86577cb117d7de6550c7/tests/src/index.test.ts#L19

I like the idea of supporting server-side mutations without WebSockets. Rather than entirely eliminating the need to create a YDoc instance, you still had to create a YDoc (so that you'd have the same mutator API), but you didn't have to use WebSockets or a provider?

The way it would work is that you'd first send a GET request to the server to get the document, you would do some modifications on the local YDoc, and then you would turn that into an update that you could send as a POST request to the server. Would you use that?

@beeing
Copy link
Author

beeing commented Dec 23, 2023

Hi @paulgb, is it possible for server side mutation to be done without getting the doc and re-sending it back? Otherwise, I think this will be heavy as what you've suggested.

Instead, can the y-sweet server load the doc, do the mutation and saving it back to the file and we just pass in a list of operations like /mutate/docId?actions=["set('key', value)", "set('key2', 'string')"].

The actions are the same / similar to YDoc mutator API and can be in body if using POST. And if there's any conflicts, it can just return RESPONSE as error.

Is this something that is not too complex? I hope so, thanks!

@hobo1618
Copy link

Hello! I have a similar question about server-side mutations in NextJs. Specifically I'd like to clone y-docs on the server, creating a new, disconnected ydoc. I will also likely want to mutate shared types on the server, so GETing the doc first would be fine, though @beeing 's idea would be neat too for simple actions.

Loving y-sweeet btw, exactly what I've been looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants