# IonicDB loads inconsistently

**URL:** <https://forum.ionicframework.com/t/ionicdb-loads-inconsistently/79943>\
**Category:** ionic-v3\
**Created:** [February 16, 2017, 11:18pm UTC](https://forum.ionicframework.com/t/ionicdb-loads-inconsistently/79943 "2017-02-16T23:18:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luke\_rpm](https://avatars.discourse-cdn.com/v4/letter/l/9de053/32.png) [@luke\_rpm](https://forum.ionicframework.com/u/luke_rpm)\
**Post date:** [February 16, 2017, 11:18pm UTC](https://forum.ionicframework.com/t/ionicdb-loads-inconsistently/79943/1 "2017-02-16T23:18:43Z")

</div>

Hi there,  
I am using [http://docs.ionic.io/services/database/](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!

---

<div class="post-metadata">

**Author:** ![brudrigo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/brudrigo/32/106131_2.png) [@brudrigo](https://forum.ionicframework.com/u/brudrigo)\
**Post date:** [February 17, 2017, 12:03pm UTC](https://forum.ionicframework.com/t/ionicdb-loads-inconsistently/79943/2 "2017-02-17T12:03:12Z")

</div>

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
