holxsam / tldraw-in-obsidian

A plugin to integrate Tldraw into Obsidian.
Apache License 2.0
174 stars 8 forks source link

Support live sync/collaboration #49

Open Dan-Shields opened 1 week ago

Dan-Shields commented 1 week ago

When using obsidian with a sync plugin setup, either official or self-hosted, the underlying markdown file to the tldraw drawing gets synced, but until you close and reopen the selected drawing in the receiving client, the markdown doesn't get re-read.

This means live collaboration doesn't really work using this plugin, as it does on tldraw.com. I'm wondering if there's a way to tap into the collaboration functionality on tldraw.com, or would it require users to create their own sync servers?

Perhaps as a compromise this plugin could listen for changes to the underlying markdown file and tell the tldraw window to re-read it? Would probably require some kind of merge conflict handling.

jon-dez commented 1 week ago

That plugin looks interesting, I don't know much about it since I have not used. Do you know how their underlying sync mechanism works and what kind of metadata it provides, if any? If it is as simple as listening to changes on a markdown file and reloading the current view, then I think I can get it done fairly easily.

On a side note the tldraw.dev website does have a reference implementation of live-sync/collaboration (docs). I haven't dug too deep into it though.

Dan-Shields commented 1 week ago

As far as I'm aware, neither the official Sync plugin, nor the self-hosted livesync plugin offers an API that would help us.

Hooking into the file update event could be enough, but with the 500ms default save delay in this plugin, plus up to 2s delay on the official sync plugin, it still doesn't offer the collaboration features that self-hosted tldraw server has.

I was hoping there was some P2P way of doing this, but looks like a websocket server is required. I think then it'd be nice if this plugin offered a way to configure the client to hook it up to a self-hosted tldraw sync backend.

I'll look into this more next week and see if I can prototype something!

jon-dez commented 1 week ago

I wonder if we can bundle a minimal websocket server alongside this plugin, so that we can achieve a P2P system that way. I don't know if it is outside the scope of what is possible in Obsidian, but that would be cool. It can probably be achieved using web workers: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

Also, I pushed a preview version that allows synchronization between workspace leafs.

https://github.com/jon-dez/tldraw-in-obsidian/releases/tag/1.6.0-preview.1

This isn't the same as listening to changes in the markdown file, but effectively allows multiple tldraw editors open for the same document. Also, if you have an tldraw embed in a markdown file and edit the tldraw document in another workspace leaf, you can "refresh" the diagram by toggling the interactive view within the tldraw embed. It's not the best way to "refresh" the embed, but next thing I was planning to work on is listening to changes on the markdown file to automatically refresh the tldraw document in both the embed and normal view.