datastar icon indicating copy to clipboard operation
datastar copied to clipboard

client sync

Open gedw99 opened this issue 1 year ago • 1 comments

Was wondering if we want to have a mechanism to update clients that happen to be looking at the same page URL ?

The idea is that one user is changing some aspect, and when they change it, we want the other users to also see that change.

This is where NATS is pretty cool.

A few examples of where this "pattern" would be useful :

https://data-star.dev/examples/value_select where the models list was changes by 1 user, and other users can then see it change in their drop downs.

https://data-star.dev/examples/infinite_scroll which is read only, but if one of the rows changed ( due to some other user doing a mutation of the underlying data ), all other users would see it.

Why ?

Well one reason is that you get way less Transactions that fail validation rules back in your storage layer.

The other one is for Dashboards where you want real time sync.

gedw99 avatar May 09 '24 05:05 gedw99

I'll be working on a game server that shows this exact behavior between NATS and Datastar

delaneyj avatar May 14 '24 22:05 delaneyj

oh great. I really hope that some of the NATS aspects get shown in the Datastar repo as examples, because I think it will help me and others.

I started using Corrosion as my DB with NATS now. It's a CRDT Sqlite DB in Rust with an elegant HTTP API. SO then its very easy to do a "select * ..." as a subscription and just feed it up to NATS and SSE with Datastar

https://github.com/superfly/corrosion

https://superfly.github.io/corrosion/api/subscriptions.html shows how easy it is.

The DB will scale out master master with nothing needed, thanks to the CRDT. It's working well for me.

I am going to write a golang API to consume easily off it and manage the subscription data to make it more robust.

gedw99 avatar May 20 '24 03:05 gedw99

I'll be working on a game server that shows this exact behavior between NATS and Datastar

Would be good if we had a basic example in this repo. Maybe chat ..

I agree that nats is the way to do it .

gedw99 avatar Jun 04 '24 08:06 gedw99

So I've been trying to think of a way to show this off without a lot of code.. Right now my plan for the front page is to replace the demo with a gameboy emulator that has a shared state of a game like pokemon. Everyone is playing with the same controller. I know twitch did this over 10 years ago but it would be trivial with NATS+Datastar

delaneyj avatar Jun 04 '24 13:06 delaneyj

I don’t know that old demo you are referring too.

I just wanted a simple todo or similar ( chat ) , where all browser tabs are in sync , without hitting refresh.

it’s a very common need.

Very very simple and easy to learn from.

A game is very cool to show off how far it can be taken and is really a good idea too for the front page . But a nice simple todo / chat will help me to see how to do it :)

gedw99 avatar Jun 06 '24 01:06 gedw99

My only concern with chat or a to-do is is very easy to get buried with bad content, then you have to deal with considering it's spam detection type stuff, I guess I can do it and if it becomes a problem we can try something else

delaneyj avatar Jun 06 '24 03:06 delaneyj

Cloudflare turnstile might be a good option to catch bots before they leave spam?

nickchomey avatar Jun 06 '24 03:06 nickchomey

I don't know much about turnstile @nickchomey but I'll take a look!

delaneyj avatar Jun 06 '24 04:06 delaneyj

Its a better, less invasive captcha https://blog.cloudflare.com/turnstile-private-captcha-alternative/

Just need to include a small js script on the page and it'll do the rest

nickchomey avatar Jun 06 '24 04:06 nickchomey

My only concern with chat or a to-do is is very easy to get buried with bad content, then you have to deal with considering it's spam detection type stuff, I guess I can do it and if it becomes a problem we can try something else

Yeah you're right in that bad actors could put up improper content into a chat. If it does become a problem, we can add a 10 minute timer that wipes all the data.

It's just a demo though. I personally don't think anyone will start to abuse it.

I presume you are are thinking of storing the chat state in NATS ? If you want to wipe the data every 10 minutes we can add a timer counter into NATS.

gedw99 avatar Jun 06 '24 04:06 gedw99

This has been covered by #74

delaneyj avatar Jun 17 '24 16:06 delaneyj