Angularfire2 / Firebase - request with list of corner lights2 subscribe to runtime, not working
I am working on a project with angular2 / firebase and using angularfire2 module. I want some realtime data, so I am using angularfire2 list and component side using list subscription. In real time, if any new item added to firebase is subscribed, but without any request. How can I archive a request subscription? I am using the following code where request is some selection based on which I want data.
getRealtimeData(query: any){
if(query){
return this.af.database.list(databaseName+"/messages", {
query: {
orderByChild: 'is_broadcast',
equalTo: query.value
}
});
} else {
return this.af.database.list(databaseName+"/messages", {
query: {
orderByChild: 'timestamp'
}
});
}
}
I am calling this in my component by creating a request
var query;
if (this.isBroadcastMessage && this.isGroupChatMessage || !this.isBroadcastMessage && !this.isGroupChatMessage) {
query = null;
} else if (this.isBroadcastMessage && !this.isGroupChatMessage) {
query = {
value: true
};
} else if (!this.isBroadcastMessage && this.isGroupChatMessage) {
query = {
value: false
};
}
this.firebaseService.getRealtimeData(query).subscribe((data: any) => {
console.log("data::", data);
}, (error: any) => {
console.log("error::", error);
})
+3
source to share
1 answer
ReplaySubject user
query$: ReplaySubject<any> = new ReplaySubject(1);
First, create your function getRealtimeData
like this
// you no need to pass any argument here
getRealtimeData():FirebaseListObservable<any>{
return this.af.database.list(databaseName+"/messages", {
query: this.query$
});
then
public updateQuery(query: string): void {
this.query$.next(query);
}
Now
this.firebaseService.getRealtimeData().subscribe((data: any) => {
console.log("data::", data);
}, (error: any) => {
console.log("error::", error);
})
Function subscribe
called when you call
this.firebaseService.updateQuery({
orderByChild: 'is_broadcast',
equalTo: true // false
});
// or
this.firebaseService.updateQuery({
orderByChild: 'timestamp'
});
+1
source to share