A simple Vue 3 composable library to handle centralized Firestore subscriptions — initialized once in your app and accessible anywhere.
🔥 Automatically tracks Firebase Auth state, sets up live listeners, and makes your Firestore data globally available.
- 🔥 One-time setup in
App.vue
- 🔐 Firebase Authentication-aware (
onAuthStateChanged
internally handled) - 📡 Reactive live Firestore subscriptions
- 🧠
userId
placeholder support in query filters - 📦 No need for Vuex/Pinia
- ⚙️ Tiny API:
initFirestoreSubscriber()
anduseFirestoreData()
npm install vue-firestore-subscriber
import { initFirestoreSubscriber } from 'vue-firestore-subscriber';
import { db } from './firebase';
initFirestoreSubscriber(db, {
users: [],
posts: [[['authorId', '==', '{userId}']]],
comments: [[['visible', '==', true]]],
});
import { useFirestoreData } from 'vue-firestore-subscriber';
const { data, loading } = useFirestoreData();
watchEffect(() => {
if (!loading.value) {
console.log(data.value.posts);
}
});
{
collectionName: [
[ [field, operator, value], ... ],
[ [field, operator, value], ... ],
]
}
Use {userId}
as a placeholder to auto-fill with current user UID.
const collections = {
users: [],
posts: [[['authorId', '==', '{userId}']]],
comments: [[['visible', '==', true]]]
};
initFirestoreSubscriber(db, collections);
Initializes the library and sets up subscriptions.
-
db
: Your Firebase Firestore instance -
collections
: Object of collection filters
Returns a global reactive object:
{
data: reactive({ collectionName: [...] }),
loading: ref(true/false)
}
All subscriptions are automatically cleaned up.
Yes! Just call useFirestoreData()
anywhere after initFirestoreSubscriber()
has been called.
MIT