IonicDB loads inconsistently

Hi there,
I am using http://docs.ionic.io/services/database/ and for some reason the database needs to be “woken up” before it can be used. Whenever I try to load the database from my app, it only works if I’ve recently (in like the last 10 minutes) loaded the app–not necessarily on the same device.
Is there a way to get around this?

This is the console log I get when it does not work:
WebSocket connection to 'wss://api.ionic.io/ionicdb/<appid>' failed: Unexpected response code: 502 polyfills.js:3 Received an error: [object Event] main.js:108846 t main.js:108846 Connection Failed: Unexpected disconnect with error code 1006. main.js:109376 t.call.closeObserver.next main.js:109376 Retrying in 5 seconds. main.js:109378 t.call.closeObserver.next main.js:109378

Thank you!

2 Likes

Hello,

I had the same issue and I sloved it by monitoring db.status( ) in the app.component.ts like it:

this.db.status().subscribe( (status) => {
  if (status.type == 'reconnecting' || status.type == 'error' || status.type == 'disconnected') {
    if(this.auth.isAuthenticated()){
      let loading = this.loadingCtrl.create();
      loading.present();
      setTimeout(()=>{
        this.db.connect();
        loading.dismiss();
      }, 4000);
    }
  }
  if(status.type === 'connected'){
    //Here I load my Services with watch for realtime connections
});

If the app starts and the ionicDb is sleeping you will receive a error/reconnecting status, so, if the user is connected this code try connect to db every 4s, when the db wake up it will connect.

After that, I found other issue, when a user do logout and try login again or with another user, I don’t know why but, is not possible connect to db again, to fix it I restart the app when a user do logout:

        document.location.href = 'index.html';

Sorry for my english… I hope it help you.
Thanks

2 Likes