简体   繁体   English

使用AngularJS和Symfony 2进行开/关线数据同步的最佳实践

[英]Best practice for on/off line data synchronization using AngularJS and Symfony 2

I'm building a relatively complex and data heavy web application in AngularJS. 我正在AngularJS中构建一个相对复杂且数据量很大的Web应用程序。 I'm planning to use php as a RESTful backend (with symfony2 and FOSRESTbundle). 我打算使用php作为RESTful后端(使用symfony2和FOSRESTbundle)。 I have spent weeks looking around for different solutions to on/off line synchronization solutions and there seem to be many half solutions (see list below for some examples). 我花了几周时间寻找不同的线路/线路同步解决方案解决方案,似乎有很多半解决方案(参见下面的列表中的一些示例)。 But non of them seem to fit my situation perfectly. 但他们中的任何一个似乎都完全符合我的情况。 How do I go about deciding which strategy will suite me? 我如何决定采用哪种策略?

What issues that might determine “best practices” for building an on/off line synchronization system in AngularJS and symfony 2 needs some research, but on the top of my head I want to consider things like speed, ease of implementation, future proof (lasting solution), extensibility, resource usage/requirements on the client side, having multiple offline users editing the same data, how much and what type of data to store. 哪些可能决定在AngularJS和symfony 2中构建开/关线同步系统的“最佳实践”的问题需要一些研究,但在我的头脑中,我想考虑诸如速度,易于实现,未来证明(持久性)等问题解决方案),可扩展性,客户端的资源使用/需求,让多个离线用户编辑相同的数据,存储多少和什么类型的数据。

Some of my requirements that I'm presently aware of are: 我目前了解的一些要求是:

  • The users will be offline often and then needs to synchronize (locally created) data with the database 用户经常离线,然后需要与数据库同步(本地创建)数据
  • Multiple users share some of the editable data (potential merging issues needs to be considered). 多个用户共享一些可编辑数据(需要考虑潜在的合并问题)。
  • User's might be logged in from multiple devices at the same time. 用户可能同时从多个设备登录。
  • Allowing large amount of data to be stored offline(up to a gigabyte) 允许大量数据离线存储(最高可达千兆字节)
  • I probably want the user to be able to decide what he wants to store locally. 我可能希望用户能够决定他想在本地存储什么。
  • Even if the user is online I probably want the user to be able to choose whether he uses all (backend) data or only what's available locally. 即使用户在线,我也可能希望用户能够选择是使用所有(后端)数据还是仅使用本地可用的数据。

Some potential example solutions 一些潜在的示例解决方

  • PouchDB - Interesting strategies for synchronizing changes from multiple sources PouchDB - 用于同步来自多个源的更改的有趣策略
  • Racer - Node lib for realtime sync, build on ShareJS Racer - 用于实时同步的节点库,基于ShareJS构建
  • Meteor - DDP and strategies for sync Meteor - DDP和同步策略
  • ShareJS - Node.js operational transformation, inspired by Google Wave ShareJS - Node.js运营转型,受Google Wave的启发
  • Restangular - Alternative to $resource Restangular - 替代$ resource
  • EmberData - EmberJS's ORM-like data persistence library EmberData - EmberJS的类似ORM的数据持久性库
  • ServiceWorker ServiceWorker
  • IndexedDB Polyfill - Polyfill IndexedDB with browsers that support WebSQL (Safari) IndexedDB Polyfill - Polyfill IndexedDB,带有支持WebSQL的浏览器(Safari)
  • BreezeJS BreezeJS
  • JayData JayData
  • Loopback's ORM 环回的ORM
  • ActiveRecord ActiveRecord的
  • BackBone Models BackBone模型
  • lawnchair - Lightweight client-side DB lib from Brian Leroux lawnchair - Brian Leroux的轻量级客户端数据库库
  • TogetherJS - Mozilla Labs' multi-client state sync/collaboration lib. TogetherJS - Mozilla Labs的多客户端状态同步/协作库。
  • localForage - Mozilla's DOMStorage improvement library. localForage - Mozilla的DOMStorage改进库。
  • Orbit.js - Content synchronization library Orbit.js - 内容同步库

( https://docs.google.com/document/d/1DMacL7iwjSMPP0ytZfugpU4v0PWUK0BT6lhyaVEmlBQ/edit#heading=h.864mpiz510wz ) https://docs.google.com/document/d/1DMacL7iwjSMPP0ytZfugpU4v0PWUK0BT6lhyaVEmlBQ/edit#heading=h.864mpiz510wz

Any help would be much appreciated :) 任何帮助将非常感激 :)

You seem to want a lot of stuff, the sync stuff is hard... I have a solution to some of this stuff in an OSS library I am developing. 你似乎想要很多东西,同步的东西很难......我正在开发一个OSS库中的一些东西的解决方案。 The idea is that it does versioning of local data, so you can figure out what has changed and therefore do meaningful sync, which also includes conflict resolution etc. This is sort-of the offline meteor as it is really tuned to offline use (for the London Underground where we have no mobile data signals). 我们的想法是它对本地数据进行版本控制,因此您可以找出已更改的内容,从而进行有意义的同步,其中还包括冲突解决等。这类似于脱机流星,因为它实际上已调整为脱机使用(对于我们没有移动数据信号的伦敦地铁)。

I have also developed an eco system around it which includes a connection manager and server. 我还围绕它开发了一个生态系统,包括连接管理器和服务器。 The main project is at https://github.com/forbesmyester/SyncIt and is very well documented and tested. 主要项目位于https://github.com/forbesmyester/SyncIt,并且有很好的文档和测试。 The test app for the ecosystem will be at https://github.com/forbesmyester/SyncItTodoMvc but I have yet to write virtually any docs for it. 该生态系统的测试应用程序将位于https://github.com/forbesmyester/SyncItTodoMvc,但我还没有为它编写任何文档。

It is currently using LocalStorage but will be easy to move to localForage as it actually is using a wrapper around localStorage to make it an async API... Another one for the list maybe? 它目前正在使用LocalStorage,但很容易移动到localForage,因为它实际上是使用localStorage的包装器使其成为异步API ...列表中的另一个可能?

To work offline with your requeriments I suggest to divide problem into two scenarios: content (html, js, css) and data (API REST). 为了与您的requeriments脱机工作,我建议将问题分为两个场景:内容(html,js,css)和数据(API REST)。

The content 内容

Will be stored offline by appcache for small apps or for advanced cases with the awesome serviceworkers . 将通过appcache离线存储为小型应用程序或高级服务 工作者的高级案例。 Chrome 40+. Chrome 40+。

The data 数据

Require solve the storage and synchronization and It becames a more difficult problem. 需要解决存储和同步问题,这就成了一个更难的问题。 I suggest a deep reading of the Differential Synchronization algorimth, and take next tips in consideration: 我建议深入阅读差异同步算法,并考虑下一个提示:

Frontend 前端

Store the resource and shadow (using for example url as key) into the localstorage for small apps or into more advanced alternatives (pouchdb,indexdb,...). 将资源和阴影(使用例如url作为键)存储到localstorage中以用于小型应用程序或更高级的替代项(pouchdb,indexdb,...)。 With the resource you could work offline and when needs synchronize with the server use jsonpath to get diffs between the resource-shadow and to send it to server the PATCH request. 使用资源,您可以脱机工作,当需要与服务器同步时,使用jsonpath获取资源阴影之间的差异,并将其发送到服务器PATCH请求。

Backend 后端

At backend take in consideration storage the shadow copies into redis. 在后端考虑将阴影副本存储到redis中。

The two sides (Frontend/Backend) needs to identify the client node, to do so you could use x- syn-token at HTTP header (send it in all request of the client with angular interceptors). 双方(前端/后端)需要识别客户端节点,为此您可以在HTTP标头处使用x- syn-token (在具有角度拦截器的客户端的所有请求中发送它)。

https://www.firebase.com/ it's reliable and proven, and can be used as a backend and sync library for what you're after. https://www.firebase.com/它可靠且经过验证,可以用作后端和同步库,以满足您的需求。 but, it costs, and requires some integration coding. 但是,它的成本,并需要一些集成编码。
https://goinstant.com/ is also a good hosted option. https://goinstant.com/也是一个很好的托管选项。

In some of my apps, I prefer to have both: syncing db source AND another main database. 在我的一些应用程序中,我更喜欢同时使用:同步数据库源和另一个主数据库。 ( mogno/express, php/mysql, etc.. ). mogno / express,php / mysql等... )。 then each db handles what's its best with, and it's features (real-time vs. security, etc...). 然后每个数据库处理最好的功能,它的功能(实时与安全等)。 This is true regardless to sync-db provider ( be it Racer or Firebase or GoInstant ... ) 无论是sync-db提供者( 无论是Racer还是Firebase或GoInstant ...... )都是如此

The app I am developing has many of the same requirements and is being built in AngularJS. 我正在开发的应用程序具有许多相同的要求,并且正在AngularJS中构建。 In terms of future proofing, there are two main concerns that I have found, one is hacking attempts requiring encryption and possible use of one time keys and an backend key manager and the other is support for WebSQL being dropped by the standards consortium in preference to indesedDB. 在未来验证方面,我发现有两个主要问题,一个是需要加密的黑客攻击和可能使用一个时间密钥和一个后端密钥管理器,另一个是支持WebSQL被标准联盟放弃而不是indesedDB。 So finding an abstraction layer that can support both is important. 因此,找到一个可以支持两者的抽象层非常重要。 The solution set I have come up with is fairly straight forward. 我提出的解决方案集非常简单。 Where offline data is is loaded first into the UI and a request goes out to the REST Server if in an online state. 其中脱机数据首先加载到UI中,如果处于联机状态,请求将发送到REST服务器。 As for resolving data conflicts in a multi user environment, that becomes a business rule decision. 至于解决多用户环境中的数据冲突,这成为业务规则决策。 My decision was to simplify the matter and not delve into data mergers but to use a microtime stamp comparison to determine which version should be kept and pushed out to clients. 我的决定是简化问题而不是深入研究数据合并,而是使用微量时间戳比较来确定应该保留哪个版本并推送给客户。 When in offline mode, store data as a dirty write and the push to server when returning to an online state. 处于脱机模式时,将数据存储为脏写,并在返回到联机状态时将其存储到服务器。

Or use ydn-db, which I am evaluating now as it has built in support for AWS and Google cloud storage built in. 或者使用ydn-db,我现在正在评估它,因为它内置了对AWS和Google云存储的内置支持。

另一个建议是: Yjs利用类似OT的算法来共享各种支持的数据类型,您可以选择将共享数据存储在IndexedDB中(因此可以进行离线编辑)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM