如果是脱机优先应用程序,如何与远程数据库同步数据? [英] How do I sync data with remote database in case of offline-first applications?

查看:132
本文介绍了如果是脱机优先应用程序,如何与远程数据库同步数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我正在构建一个TODO应用程序,它使用Service Workers来缓存请求的响应,如果用户处于脱机状态,则会向用户显示缓存的数据。

  • 服务器公开一个REST-ful端点,该端点具有为资源公开的POST,PUT,DELETE和GET端点。

  • 考虑到当用户离线并提交TODO项目时,我将其保存到本地IndexedDB,但由于没有网络连接,我无法发送此POST请求。对于用户更新或删除现有TODO项目的PUT,DELETE请求也是如此

问题


  • 当连接重新联机时,正在使用什么模式将待处理请求与REST-ful服务器同步?

推荐答案


正在使用哪些模式来同步待处理的请求连接重新联机时使用REST-ful服务器?

What patterns are in use to sync the pending requests with the REST-ful Server when the connection is back online?

后台同步API 适用于此场景。它使Web应用程序能够在后台同步数据。通过这种方式,它可以推迟操作直到用户具有可靠的连接,从而确保实际发送用户想要发送的任何内容。即使用户导航或关闭浏览器,也会执行操作,如果需要,您可以通知用户。

Background Sync API will be suitable for this scenario. It enables web applications to synchronize data in the background. With this, it can defer actions until the user has a reliable connection, ensuring that whatever the user wants to send is actually sent. Even if the user navigates away or closes the browser, the action is performed and you could notify the user if desired.

由于您要保存到IndexDB,您可以在用户添加,删除或更新TODO项目时注册同步事件

Since you're saving to IndexDB, you could register for a sync event when the user add, delete or update a TODO item

function addTodo(todo) {
  return addToIndeDB(todo).then(() => {
    // Wait for the scoped service worker registration to get a
    // service worker with an active state
    return navigator.serviceWorker.ready;
  }).then(reg => {
    return reg.sync.register('add-todo');
  }).then(() => {
    console.log('Sync registered!');
  }).catch(() => {
    console.log('Sync registration failed :(');
  });
}

您已经注册了一个类型为 add-todo 的同步事件,您将在服务工作者和然后,当您收到此事件时,您将从IndexDB检索数据并对Restful API执行POST。

You've registered a sync event of type add-todo which you'll listen for in the service-worker and then when you get this event, you retrieve the data from the IndexDB and do a POST to your Restful API.

self.addEventListener('sync', event => {
if (event.tag == 'add-todo') {
      event.waitUntil(
      getTodo().then(todos => {
        // Post the messages to the server
        return fetch('/add', {
          method: 'POST',
          body: JSON.stringify(todos),
          headers: { 'Content-Type': 'application/json' }
        }).then(() => {
          // Success!
        });
      })
      })
    );
   }
});

这只是一个如何使用后台同步实现它的示例。请注意,您必须在服务器上处理冲突解决方案。

This is just an example of how you could achieve it using Background Sync. Note that you'll have to handle conflict resolution on the server.

您可以在客户端使用PouchDB,在服务器上使用Couchbase或CouchDB。使用客户端上的PouchDB,您可以将数据保存在客户端上,并将其设置为在用户联机时自动同步/复制数据。当数据库同步并且存在冲突的更改时,CouchDB将检测到此情况,并使用特殊属性_ conflicts标记受影响的文档:true 。它确定将哪一个用作最新版本,并将其他版本保存为该记录的先前版本。它不会尝试合并冲突的修订版。由您决定如何在您的应用程序中完成合并。它与Couchbase没有什么不同。有关冲突解决的更多信息,请参阅以下链接。

You could use PouchDB on the client and Couchbase or CouchDB on the server. With PouchDB on the client, you can save data on the client and set it to automatically sync/replicate the data whenever the user is online. When the database synchronizes and there are conflicting changes, CouchDB will detect this and will flag the affected document with the special attribute "_conflicts":true. It determines which one it'll use as the latest revision, and save the others as the previous revision of that record. It does not attempt to merge the conflicting revision. It is up to you to dictate how the merging should be done in your application. It's not so different from Couchbase too. See the links below for more on Conflict Resolution.

  • Conflict Management with CouchDB
  • Understanding CouchDB Conflict
  • Resolving Couchbase Conflict
  • Demystifying Conflict Resolution in Couchbase Mobile

我已经使用了pouchDB和couchbase / couchdb / IBM cloudant,但我已经通过连帽衫完成了它有用户开箱即用的身份验证,处理冲突管理等等。把它想象成你的后端。在你的TODO应用程序中,连帽衫将非常适合。我已经写了一些关于如何使用连帽衫的内容,请参阅下面的链接:

I've used pouchDB and couchbase/couchdb/IBM cloudant but I've done that through Hoodie It has user authentication out-of-the box, handles conflict management, and a few more. Think of it like your backend. In your TODO application, Hoodie will be a great fit. I've written something on how to use Hoodie, see links Below:

  • How to build offline-smart application with Hoodie
  • Introduction to offline data storage and sync with PouchBD and Couchbase

这篇关于如果是脱机优先应用程序,如何与远程数据库同步数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆