3

I am working on a project with angular2/firebase and using angularfire2 module. I want some realtime data so I use angularfire2 list and on component side using subscribe over the list. In real-time scenario, if any new item added in firebase database it subscribe but without any query. How I can archive subscribe with query? I am using following code where the query is some selection on which basis 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 creating a query

      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);
      })
4
  • can you provide the Component code which use this getRealtimeData method? and also some logs of the object you receive? Commented Jun 6, 2017 at 13:01
  • Added in description, i want my subscribe get called with query. but it not getting called with query Commented Jun 7, 2017 at 4:57
  • weird, i tried this before, and its worked for me. the only difference is that i made the DB list query inside my component and not in the service. you tried to do that? Commented Jun 7, 2017 at 6:30
  • Yes, but that will not make sense as subscribe always call without query :( Commented Jun 8, 2017 at 6:09

1 Answer 1

1

User ReplaySubject

query$: ReplaySubject<any> = new ReplaySubject(1);

First, create your getRealtimeData function like this 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);
  })

subscribe function will be called when ever you call

this.firebaseService.updateQuery({
            orderByChild: 'is_broadcast',
            equalTo: true // false
        });
// or
this.firebaseService.updateQuery({
            orderByChild: 'timestamp'
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.