Kinto / elm-kinto

An Elm client for the Kinto API
http://package.elm-lang.org/packages/Kinto/elm-kinto/latest
Other
28 stars 7 forks source link

Offline first capabilities #43

Closed OlegMalkov closed 6 years ago

OlegMalkov commented 6 years ago

Hi! I am new to elm. I want to write an app for managing family budget in elm. I did that already using react native + firebase, and I am sad about where I get there. I need to sync data between devices. This library looks right direction to go. Did any one thought already thought about implementing

I know that there is no solution to talk to storage apis of browser directly from elm as of now, and the recommended approach to use IndexedDB in elm app is ports.

I thought that there may be elm-kinto-offline fork, for now, that require to manually plug in some js to make it work. And may be in future, when elm will provide a way for data persistence ports can be replaced with it and elm-kinto-offline can become elm-kinto

I probably get that questions I am asking here are not directly target to solve my problem (write an app for managing family budget in elm) and solution for that is just use pouchDB / relay / kinto.js via ports. I am doing that because of desire to make contribution for elm ecosystem. But if spending time on topic above is useless, better to take a look for opportunity to contribute some other way.

magopian commented 6 years ago

Hi @OlegMalkov,

I believe you have properly assessed the issue: offline first doesn't exist in elm-kinto, and elm has nothing to talk to IndexedDB or even LocalStorage, without going through ports.

I know of some people who are using service workers to have some equivalent: https://gitlab.com/matheus23/flatawesome-elm/blob/master/src/sw.js This might prove useful for people not wanting to go through ports, and to have a somewhat seamless experience with elm-kinto and offline first.

There is definitely a void in this library regarding offline first and caching, and anyone motivated to have a go at filling this void would do this library a great service! I do believe that this might be a difficult task. I for one have no clue where I would start.

If you want to give it a try, maybe we could start some kind of brainstorming here or on the elm slack or discourse or something?

Another approach would be documenting (blog post, documentation in the library, example code in the repository) how to use ports or service workers for offline first functionality. Maybe that would even be the best first thing to do?

OlegMalkov commented 6 years ago

@magopian I thought to add a topic on discourse with following content

Offline first capabilities for elm-kinto (in browser database with persistence)
We want to use this topic to brainstorm possible solutions. 
Discussion started here https://github.com/Kinto/elm-kinto/issues/43

Offline first solution for kinto database already exist in javascript https://github.com/Kinto/kinto.js/

* Trying to wrap kinto.js is not an option, because that solution will require using ports forever, but kinto.js can be used as an example, as a source of inspiration.
* Elm 0.19, it does not data persistence without ports
* This example looks nice way to communicate with indexedDB https://gitlab.com/TobiDo/elm-indexedDB-example/tree/master

I want to try modify https://gitlab.com/TobiDo/elm-indexedDB-example/tree/master to use elm-kinto

Requirements for ToDo app may be
- first time opening app it should create empty model locally
- when todo item added synchronization with remote server should not be triggered, instead synchronization should happen when user press a sync button
- every todo should have uniq id, all todos should be merged during synchronization
- if same todo item is different on server and client item with latest timestamp should be taken

but not sure that it's right place.

So far I stuck on understanding how elm-kinto can help to synchronize remote database with local one.

I definitely want give it a try. I am exploring how kinto.js doing this synchronisation

P.S. About service workers - definitely good idea, but I think it should be optional for users of elm-kinto, at least when data persistence will be available natively in elm. (And also I have not work with them yet.)

OlegMalkov commented 6 years ago

I am thinking to have a wrapper around IDB that can be accessible either using ports or fetch. In case of fetch service worker will be required. Once that wrapper is there, same thing that is done in kinto.js for offline support can be done in elm. Kinto.js has a dependency on kinto-http, and kinto-http looks similar to kinto-elm (probably kinto-elm support less features than kinto-http?)

https://github.com/jakearchibald/idb Looks like very nice tiny IDB wrapper.

OlegMalkov commented 6 years ago

@magopian sorry, i am stoping progress on this issue as i just found out about https://reasonml.github.io/ And i feel it fits my current needs better than elm. Any way i don't have enough expertise in idb/kinto/elm/serviceWorkers to bring a solid solution for problem of this topic in near future. However, everything changes :)