title: Communication between multiple components with a service by using Observable and Subject in Angular 2
body: <p>I am new to rxjs, so I would like to ask a question regarding <code>Angular 2</code>, <code>Observables</code> and <code>BehaviorSubject/Subject</code>.</p>
  
  <p>So, what I want to achieve is : <code>onclick</code> of a <code>button</code> inside a <code>ComponentA</code> to notify other components for example <code>ComponentB</code>, <code>ComponentC</code>.</p>
  
  <p>What I did so far is to create a <code>service</code>:</p>
  
  <pre><code>private isMenuOpenBS = new BehaviorSubject(false);
  
    toggleMenu(): void {
      this.isMenuOpenBS.next(!this.isMenuOpenBS.getValue());
    }
  
    getMenuState(): Observable&lt;boolean&gt; {
      return this.isMenuOpenBS.asObservable();
    }
  </code></pre>
  
  <p>Then having one component with <code>provide menuService</code> is calling the <code>this.menuService.toggleMenu()</code> which changes the value of the <code>BehaviorSubject</code>. Code :</p>
  
  <pre><code>toggleMenu(): void {
      this.menuService.toggleMenu();
      this.menuService.isMenuOpenBS.subscribe(
        (data) =&gt; {
          console.log(data)
        },
        (e) =&gt; {console.log(e)},
        () =&gt; {console.log('completed')}
      )
    }
  </code></pre>
  
  <p>And another component that <code>OnInit()</code> <code>subscribes</code> to the <code>getMenuState()</code> which is an <code>Observable</code>, but it get's values only on <code>OnInit()</code>. Code:</p>
  
  <pre><code>ngOnInit(): void {
      this.menuService.getMenuState().subscribe(
        (data)=&gt;{
          this.messages = data;
          console.log('nav component');
        },
        (e) =&gt; {console.log(e)},
        () =&gt; {console.log('completed')}
      )
    }
  </code></pre>
  
  <p>The <code>complete</code> function is never called.</p>
  
  <p>Any ideas what am I doing wrong or if i am missing something in the logic ?</p>
  
  <p>--</p>
  
  <p>Edit : So, to explain a bit more the problem was that I could see the first value that the observable had oninit but nothing else. No error no complete after that or no "next" value which was wrong since I was sending new values from the subject. In the end the problem was with the provider list of the components and not a problem of observables or subjects, but before solving the problem it wasn't easy to see that the problem was there.</p>