How to stop Cordova App to reconnect in background?


#1

I’ve created a publication subscribed by all logged in users to get user data

function({}) {
                if (this.userId) {
                    console.log('user online', this.connection.id);

                    // if subscription stops
                    this.onStop(() => {
                        console.log('user not online', this.connection.id);
                    });

                    // return user data
                    const result = Meteor.users.find(
                        { _id: this.userId },
                        {
                            fields: userFields
                        }
                    );

                    return result;
                } else {
                    this.ready();
                }
            }

I’ve added console logging above to know if the client is connected to the server or not. Everything is working fine when the user logs out or the user explicitly closed the cordova app.

What I observed is that if I put the app in the background, after sometime, it would automatically disconnects which was expected. The issue is that it would again automatically reconnects.

Here is a sample console log:

I20180511-03:32:01.900(8)? user online KCSk7uopzAAkdEtoQ
I20180511-03:33:01.839(8)? user not online KCSk7uopzAAkdEtoQ
I20180511-03:33:04.088(8)? user online eJpkHLMXrRzp5CoFm

Now, I’m planning to use Meteor.disconnect() whenever the app moves into the background, and then Meteor.reconnect() when the app gains focus. The same thing happened. When the app moved into the background, the app would disconnect, but it would automatically reconnect again.

Here is the code:

// app in background
        document.addEventListener(
            'pause',
            () => {
                console.log('app pauses');
                Meteor.disconnect();
            },
            false
        );

        // app resumes
        document.addEventListener(
            'resume',
            () => {
                console.log('app resumes');
            },
            false
        );

Is this the expected behavior?

How can I disconnect the app so that it will minimize connection to the server when the app is in the background?

(by the way, I’m running Meteor 1.7-rc.6 if that matters)