pocketbase / dart-sdk

PocketBase Dart SDK
https://pub.dev/packages/pocketbase
MIT License
503 stars 50 forks source link

hi, will it support local database synchronization as well? #6

Closed lucasjinreal closed 2 years ago

lucasjinreal commented 2 years ago

let's users can view local data even with not celluar connection.

ganigeorgiev commented 2 years ago

For now there are no plans to implement offline data storage in the SDK and this is left to the developers to handle on their own.

Custom record IDs are supported (15 chars string) and you can create and store your records locally on the device without communing with the web APIs. When cellular connection is available - push/sync with the remote.

lucasjinreal commented 2 years ago

@ganigeorgiev thanks. may i ask does realtime data using websocket? Is that possible using packetbase implement a very simple chat app?

ganigeorgiev commented 2 years ago

Server-sent events (SSE) are used for the realtime subscriptions.

Yes, you can build a simple chat apps with PocketBase, the only difference from using websockets is that the push/submit action is through regular HTTP POST requests (aka. client.records.create(...)).

lucasjinreal commented 2 years ago

@ganigeorgiev may i ask further how does it implemented? is that keeping a heart beat on both server and client? if so, will the heart beat cost too much battery life?

ganigeorgiev commented 2 years ago

When you call for example:

client.realtime.subscribe("example", (e) {
  print(e.action); // create, update, delete
  print(e.record); // the changed record
});

It will start a persistent/long-lived HTTP connection with the server. There is no "connection upgrade" like in WebSocket.

If the client doesn't receive any messages for more than 5 minutes, the server will drop the connection, and the dart client will try to reconnect automatically. The Dart SSE client code coud be found in https://github.com/pocketbase/dart-sdk/blob/master/lib/src/sse/sse_client.dart.


if so, will the heart beat cost too much battery life?

I haven't done any benchmarks for the battery life usage, but it shouldn't be too much different than a WebSocket implementation.

lucasjinreal commented 2 years ago

@ganigeorgiev then what is the advantages for using http as long live connection? does there any further concern for this design pattern?

ganigeorgiev commented 2 years ago

The advantages over websockets is that it is a regular HTTP connection, there is no need of the hacky "http->websocket" protocol upgrade mechanism, no special SSL handling or firewall rules to manage, etc.

One "drawback" of SSE comprared to WebSockets is that with SSE you have only unidirectional connection, aka. server->client, thats why I mentioned that if you want to push/submit data back to the server (eg. as in sending a chat message), you have to send another regular POST request (client.records.create(...)), while if you used WebSockets, you'll probably submit the chat message using the already opened connection because WebSockets are bidirectional.

gedw99 commented 3 months ago

let's users can view local data even with not celluar connection.

Marmot works with pocketbsse to do database synchronisation on the server.

so if one server goes down everything g keeps working .

https://github.com/maxpert/marmot-pocketbase-flyio Works “ out of the box “

The pocketbase change events happen on the way through the middle tier. So they will continue to work I think but @ganigeorgiev would be able to answer that. I mean this: https://pocketbase.io/docs/go-event-hooks

I am using this setup with golang and htmx and it’s working well across 5 data centers.

The only thing you can’t do is a db schema update in real time - . Marmot does not support it yet. So you have to stop the server and restart it, and the normal pocket base migration will kick in.

I am planning to expose the marmot system inside pocketbsse gui so that all admins and devs can fully understand the replication going on. It will just show all servers and their sync state.

Marmot can also do a snapshot and then you can add another server and it will pull the snapshot and then catchup .