PouchDB/CouchDB offline first application example


Peter Bradstreet
Hi, I am working on a very simple application to incorporate an offline first pouchdb local db and sync it with an online couchdb db in real time. As this is my first project, I am struggling a bit with getting started on how to approach this. I have sent you an email with a sample file, if you could give me a hand in how to best approach this, I will gladly share the finished example with the community. Cheers, Pete

DecSoft

Hello Peter,

I sent to you a modified version of your sample app, since they contains some syntax errors, Peter, as the debugger already told you. In addition to the refered syntax error, I find that you are trying to mix AB code and Javascript code: there is no problem to do something like this, but, please, take a look at the External Javascript tutorial of the help file, in order to learn how we can deal with app's global variables, for example, from pure Javascript code.

Take a look at the modified app that I sent to you and try to continue with the work, Peter, and, if you find any specific problem, just post here or open a new thread in this support forum, and, we will try to help you.



Peter Bradstreet
Thanks very much David, it still isn't inserting local db records but at least I have the right syntax to work with now so I will keep fighting with it. Cheers, Pete

DecSoft

Hello Peter,

Probably not all the stuff is already working. For example, you define a "remoteCouch" variable (which I convert to a global variable, so we can use outside where we define it), but, I am not sure if you use that variable after all.

According to the documentation, the database (our "window.App.RootScope.DB" global variable) have the right methods to "sync" to and from the server side.

Apparently that's the place to use the referred "remoteCouch" variable. Maybe you only need to use the right URL in the referred methods: in fact depend on the app that we are developing, we can follow one or one other approach, use (to share it) some variables, etc.



Peter Bradstreet
Okay, the remote syncing is working in the html example though. Right now the first step is to getting it to write to the indexexedDB local storage which it isn't doing. I will try removing the reference to the couchdb and see if that is what is stopping it from inserting local data.

DecSoft

Hello Peter,

Ok. Let me to take a look at this... certainly I think that I can prepare a sample for you with not too much time. Let me to try it and I will back here. :-)



Peter Bradstreet
Perfect, thanks!

DecSoft

Hello Peter,

I say it's easy... but it's not so easy... it's always the same history: anything that we can develop tends to be complicated... first of all, I don't know nothing about PouchDB, and, even when their Javascript API appear to be more or less easy, certainly, some errors occurs... and then we need lot of time to deep into the documentation of the project in order to know what happen.

Please, take a look when possible at this sample that I prepare for you:

https://www.decsoftutils.com/temp/PouchDB.zip

As you can see, what I try to do is to allows the user to add books into the database, and, at the same time, shown the existing database books into a Report control. All of these can be done without problems: certainly the API of PouchDB is more or less easy to use.

As you can see, the sample is working as expected, however, when I try to use the "sync" method of PouchDB... some error occur... and I can't know if the error is related with the database, the thirdparty server, etc. So I am stuck at that point, and, have no time to continue right now!

I comment the code that try to use the "sync" method, and, as you can see, the sample works, except that, due to the referred error, apparently the database cannot be synchronized with the server side.

Please, Peter, look at the sample, see how it's working as expected, then comment the "UpdateReport" function (used in both the app's view's Show event and the Add Book button Click event) and uncomment the try of the "sync" method code.

Then you can see the error that appear... and may can investigate about that error in the documentation, the thirdparty server, etc. If you have any specific question after that, post it here or open a new thread in this support forum.



DecSoft

Hello again,...

I want to add something that I think I forget. I mention that the PouchDB appear more or less easy, and, it's truth from some point of view, but, that API is also more or less complex (like any other API, of course), so we can / must deal with callbacks, for example, that, depending on our app, we need to use or not.

For example, and, as you can see in the sample, the "sync" method have various events or callbacks that we can / must use. And the same can be said about other methods, like who insert new registries in the database: this callbacks have their own arguments, etc. We can or must use that callbacks, arguments, etc., in order to properly work with the database.

We can't expect, for example, that everything work like expected, so we must take care about the available errors callbacks, the available error codes, etc., in order to assert that the app's user is informed if a registry can't be inserted, if we can't sync to the database, etc., etc. It's a question of time of work, to study and finally handle the PouchDB API, and also how to present or require the user's input from AB, etc.

So take it with time, Peter. :-) And, of course, don't hesitate to post in this forum if you have some specific problem.



Peter Bradstreet
Many thanks David, This will certainly send me in the right direction. I will keep you updated with my progress. Pete

DecSoft

No problem! I am sure that finally you can got it. :-)



Paolo Lops
Excuseme, but why you are using the version PouchDB 3.3.0 datated 2015 ? I have see thet online there is this //cdn.jsdelivr.net/npm/pouchdb@7.0.0/dist/pouchdb.min.js pouchdb@7.0.0 do you have try if the sync error are solved with this new one?

DecSoft

Hello to all,

You touch a good point, Paolo. Honestly I don't see that the version that we are trying is not the latest release. Trying with the latest release, certainly I can note a couple of things:

1º Apparently the library don't work in Internet Explorer 11, that is, don't work in the internal AB debugger, since it's based in Internet Explorer 11.

2º However, the stuff works as expected in Firefox, for example, and, trying the "sync" method now, what we get is a different error: apparently the thirdparty server don't set the appropriate CORS permissions.

The error that appear now is different than the first one, but, I am not sure if the thirdparty service can be configured in order to set the CORS permissions than the app needed.

If the above is not possible, probably it's required to prepare a bridge in our own server, which communicate with the thirdparty server: maybe this is not so easy, since I certainly don't know how PouchDB works internally and what is sended to the thirdparty service.

On the other hand, the CORS permissions problem maybe masks another possible errors, that is, once the CORS permissions problem is solved, maybe we get another possible errors, that is, once the connection is allowed. But anyway the CORS permissions problem must be solved firstly.



Paolo Lops
Hi to all if Peter have time he can try to install this in a nodejs personal server and verify that all is vorking as he expect: https://github.com/pouchdb/pouchdb-server

Peter Bradstreet
Hi Paolo, Good spot on the version. I was working from a sample that I downloaded and didn't realize that it was shipped with an older version. I will try the latest version today and see how I make out. I do have a local CouchDB server installed but have been working with IBM Cloudant and it is working on several other downloaded html/js examples that I have downloaded and configured. I will check the CORS permissions but I think that I had them set to be wide open. I am working on the CouchDB syncing today so I will let you know how I make out. Cheers, Pete

DecSoft

Hello to all,

I do have a local CouchDB server installed but have been working with IBM Cloudant and it is working on several other downloaded html/js examples that I have downloaded and configured

Are you talking about the "sync" method? That is, certainly the sample that I prepare yesterday works also as expected using the latest version of PouchDB, however, what do not work is the "sync" method... but your HTML sample do not use the "sync" method, if I am not wrong... for that I ask if you already try the "sync" method in an HTML app... which is that finally AB apps are...



Peter Bradstreet
Yes, I am speaking about the sync method. I realize that the sync function was not in the example that I had sent but I have other examples that do reference it and they do work. The implementation seems like a more simple task than the way that you tried it so I am hoping to port over the other example method into the one that I sent you to get it working today. Pete

Peter Bradstreet
If you are interested in seeing an html/js example that does sync you can download it here: https://mymarina.online/iPort/getting-started-todo-master.zip It needs to be run in a local server to run.

DecSoft

Hello to all,

But we are talking now about a different end point or URL. The sample that you link in your previous post uses this URL:

But the above URL is not the same than the used in the previous sample. And that's the point... maybe when you use the thirdparty to create a database, it's a way to configure the CORS permissions, the authentication, etc?

One thing that I note is that if I use the HTTPS protocol with your yesterday sample URL, the error is not related with CORS, but with the authentication...



Peter Bradstreet
You configure the cors settings at an account level in ibm cloudant and it is set to allow requests for all domains. My first example must have referenced an earlier end point that I was playing with but it would have referenced the same account/cors settings. I will try the new end point in your example to see if I get the same authentication error

Peter Bradstreet

Hi,

I think that I almost have it but am getting an error and I think that it is because I am a total newbie at AB and need to define CouchDB somewhere?

- In the View/Show code I have added the following line below

- I have created a new button called 'sync' for testing and it calls a function called 'remoteSync'

- The remoteSync function code is:

When I press the sync button, I get the following error in the chrome console:

It feels like I am close but need a small hand. I can send you the version if it helps.

Cheers,

Pete

p.s. I tried temporarily swapping the CouchDB url into the PouchDB url and it does read/write to the remote database so I can connect to it so I just need to get the syncing working.


Peter Bradstreet

Sorry, here it is formatted:

Hi,

I think that I almost have it but am getting an error and I think that it is because I am a total newbie at AB and need to define CouchDB somewhere?

- In the View/Show code I have added the following line below 'window.App.RootScope.PouchDB = new PouchDB('books');':

- I have created a new button called 'sync' for testing and it calls a function called 'remoteSync' - The remoteSync function code is:

When I press the sync button, I get the following error in the chrome console:

It feels like I am close but need a small hand. I can send you the version if it helps. Cheers, Pete


Peter Bradstreet
Actually, I figured it out and have it syncing now: Should have been

DecSoft

Hello to all,

I am not sure if you finally got it or not, Peter?



Peter Bradstreet

I have it syncing from the local server to the remote server and back so if I open it up in firefox and chrome, the changes are showing in both browsers. It is still really rough and needs to be updating on db change, deletions etc... but I am on my way. When I have the full crud example working with live time syncing, I will certainly share it. I will let you know if I hit any roadblocks along the way.

My ultimate goal is to develop a mobile app that is offline first and syncs between users that share the same account but for now I am happy with baby steps. Thanks for your help, Pete


DecSoft

Hello Peter,

Well... step by step! On the other hand, I want to ask you a question, because I am curious... apparently we use an URL an no other thing to authenticate in the server side, so, what happen if someone stolen that URL and use it by itself? That is, since we are talking about client apps, anybody can take a look at the source code if they really wanted to do it...

Additionally, I can see that PouchDB can be a good solution in some cases, of course, however, don't forget the "traditional" databases in the server side. Certainly, the only problem here (but not in all the cases, since it's depend on the app that we are developing) is that we need an internet connection to reach the server, but, no more...

I supose you already do it, but, can take a look at the "Database" and the "Login" samples, Peter. Note how the "Login" sample try to be an example of how we can serve private information to an specific user, that is, how we can authenticate an user before send to it any private information.



Peter Bradstreet

Hi, Yes I realize that everything that I am doing now is wide open. I plan on creating a new end point in a couple of days. I will be building in authentication as one of my early steps, There is a CouchDB plugin that will help me with this.

So far it looks like PouchDB is going to be a good solution. I am just using IndexedDB for local storage although SqlLite is possible too. The app that I am starting on needs to work without internet connection and 95% of the functionality will work in that mode. I plan on some of the functionality requiring internet connectivity such as chats between users and list synchronizations between users that share the same account. I plan on detecting whether access to the internet exists and if so, that functionality will be conditionally exposed.


DecSoft

It's ok!Thanks for your reply, Peter. :-)


Todo el mundo puede leer el foro de soporte de DecSoft para aprender del mismo, sin embargo, sólo los clientes de DecSoft pueden abrir nuevos hilos. Compre una o más licencias de productos de DecSoft y obtendrá este y otros beneficios.

Este sitio utiliza "cookies" útiles para almacenar sus preferencias.

Bien. Ocultar esta nota. Obtener más información.