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

Updating Y.Doc server-side #867

Open
mattrossman opened this issue Apr 7, 2024 · 5 comments
Open

Updating Y.Doc server-side #867

mattrossman opened this issue Apr 7, 2024 · 5 comments

Comments

@mattrossman
Copy link

Turning this into an Issue per discussion on Discord.

The y-partykit docs explain how to sync edits to a client-side Y.Doc via the YPartyKitProvider.
https://docs.partykit.io/reference/y-partykit-api/

I am looking for a way to get a handle on this document to make changes on the server. Currently, the server-side interface for the document is through load() and callback.handler() which are invoked for the purpose of syncing the document to external storage, rather than a persistent handle that syncs with clients.

For example, I may want to update a value when an alarm rings and have that propagate to clients.

@threepointone
Copy link
Contributor

can you try this:

import {unstable_getYDoc} from 'y-partykit';

// ...

const doc = await unstable_getYDoc(documentId, options)

Make sure that options is the same that you pass to onConnect

@mattrossman
Copy link
Author

What is documentId expected to be?

If I provide "my-document-name" I get a type error.

Argument of type 'string' is not assignable to parameter of type 'Room'.

The types indicates that it should be of type Party.Room. But when I provide this.room inside of the onAlarm() handler, I get a runtime error:

Uncaught (in alarm) Error: You can not access `Party.id` in the `onAlarm` handler.
This is a known limitation, and may be fixed in a future version of PartyKit.
If you access to the id, you can save it into the Room storage when setting the alarm.

As I'm not accessing Party.id myself here, I'm unsure how to workaround this.

I also tried accessing the document inside onMessage instead, but when I send a custom string message to trigger this it seems to upset the y-partykit messaging system.

✘ [ERROR] Error: Unexpected end of array

@threepointone
Copy link
Contributor

Oh hmm you're right. Thinking about this. Because of how Durable Objects work, the "id" isn't available in alarmsm which you'd need to reference the document. A workaround would be to use room.storage to store the name, but we haven't made that simple so far. Can you tell your usecase?

@threepointone
Copy link
Contributor

Specifically, what is it that you're updating in the document that should happen in an alarm?

@mattrossman
Copy link
Author

An example—I'm using PartyKit to make a realtime bus tracker app. Every few seconds I need to poll a server to fetch the latest tracking info. I do this on the server so there's a central authority for that data, and because sometimes these requests need API tokens or server-specific modules.

Today, I do this without Yjs by sending plain messages to my clients with the latest data. This means I am responsible for keeping state synchronized across server and clients. I would prefer if I could simply write the data to a document on the server, and have clients magically see the latest data when they read the document. This can even be made reactive with a binding like zustand-middleware-yjs.

It's not just for alarms either, sometimes I want a message as the trigger. And example of this is https://hmd.link which uses Firebase as the shared document. Clients submit links to server, server fetches OpenGraph data and writes to the document, client UI magically updates. With PartyKit + Yjs I should be able to achieve that without Firebase.

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

2 participants