onSnapshot appears to stop working after a period of time
Asked Answered
A

2

6

I am using firestore onSnapshot methods in a React Native app on several different sets of data in my app, and for the most part they all appear to work fine. However, after a period of time, perhaps after the app is put into background and re-awakened, the onSnapshot methods stop updating the data.

It is as if they have switched off. Sometimes they seem to stop working just after a period of time, without the app being backgrounded.

If I call the methods again then they start working again, but that seems to defeat the object of using them, I may as well just use normal get() methods and call them when I need.

Surely someone else has experienced this and can help?

Advisement answered 8/4, 2019 at 12:57 Comment(3)
Are you using the react-native-firebase library, or just firebase?Algol
I am using github.com/invertase/react-native-firebase As I say, for the most part it all works as expected, but sometimes it just doesn't, with no explanation. For example, I am outputting a list of objects called "groups" and there is an onSnapshot on that set of data. Sometimes when I add a group, it appears instantly in the app, exactly as it should, and sometimes it does not - until I do a manually pull of the data, when it does appearAdvisement
Observing same issue. Also mentioned in github.com/invertase/react-native-firebase/issues/1992Montez
T
2

I had the same problem using onsnapshot in a script running in node as firebase client. I fixed it by recalling onsnapshot both after some time interval or an error. In my case after 30 minutes.

Like this (firebase 9):

let onSnapRemove;
function onSnap() {
    onSnapRemove = onSnapshot(coll, {
        next: snap => {
            ...
        },
        error: err => {
            console.log(err);
            onSnapRemove();
            onSnap();
        }
    });
}

onSnap();

function repeat() {
    if (typeof onSnapRemove == 'function') onSnapRemove();
    onSnap();
}
setInterval(() => {
    repeat();
}, 1000 * 60 * 30);
Transcontinental answered 28/2, 2023 at 1:10 Comment(1)
I have noticed the same problem in my Node.js application using firebase-admin. It stops working after roughly 24 hours for multiple customers in production. My solution was to add cron_restart to my pm2.json that runs the app, but this solution is better.Crore
A
0

When an error occurs, Firebase will invoke the (optional) second callback that you can pass in to functions like onSnapshot, or a simple get. E.g.

const coleccionRef = database.ref('test');
coleccionRef.on('value', snapshot => {
    snapshot.val();
}, error => {
    console.error(error);
});

You can inspect that error to understand what is going wrong.

Algol answered 8/4, 2019 at 14:49 Comment(2)
I think I've tried this before and there was no error logged, but I will try again and see if that shows any informationAdvisement
Normally a Firebase real time listener (onSnapshot) or even get will either return what you need, or an error if something goes wrong, so I really think this is the right way to go. Let me know if this is not the case with your project.Algol

© 2022 - 2025 — McMap. All rights reserved.