Closed tobimori closed 3 months ago
Hi, @tobimori.
It is true that y-durableobjects
cannot be obtained via jsprc because the YDoc on the server side is set to private.
From your usage example, it seems that you want to obtain/update YDoc via http request, but this library updates YDoc in yjs via websocket.
Can you tell me more about the use case? I will consider how to respond.
Thanks for your answer, for my case, you can imagine my usecase like a todo app using y.js:
When using the normal app, syncing between clients should happen using websockets. But I also want to provide a REST API to customers, such as for adding to-dos. When adding a to-do, changes should be sent to all clients instantly via the websocket.
Okay, I'll prepare a dedicated method using DurableObjects' js rpc. How about something that can be used in this format?
I think an API that exchanges data using Uint8Array
would be simple.
app.get('/state/:id', async (c) => {
const id = c.env.Y_SYNC.idFromName(c.req.param('id'));
const obj = c.env.Y_SYNC.get(id);
const state: Uint8Array = await obj.getYDoc();
return c.json({
// ...state from doc
})
})
app.get('/update/:id', async (c) => {
const id = c.env.Y_SYNC.idFromName(c.req.param('id'));
const obj = c.env.Y_SYNC.get(id);
await obj.updateYDoc(/** update message Uint8Array */);
return c.json({
// new state from doc
})
})
looks good!
@tobimori released! https://github.com/napolab/y-durableobjects/releases/tag/v1.0.0
The package works great so far, thank you for that.
I'd like to make my Cloudflare Worker interact with the Durable Objects on a normal API request. How would I do that? I don't have much knowledge of DO yet, and the DO class seems to have all properties set to private.
Basically like this: