title: Aurelia Unsubscribe Event Aggregator
body: <p>I am using <code>Aurelia Framework</code> with <code>Typescript</code> and in the <code>event aggregator</code> I am able to publish and subscribe to channels.</p>
  
  <p>The problem is that I am unable to unsubscribe from a channel.</p>
  
  <blockquote>
    <p>Note: All forms of the subscribe method return a dispose function. You can call this function to dispose of the subscription and discontinue receiving messages. A good place to dispose is either in a view-model's deactivate callback if it is managed by a router, or in its detached callback if it is any other view-model.</p>
  </blockquote>
  
  <p>This is taken from the <a href="http://aurelia.io/docs.html#the-event-aggregator" rel="noreferrer">aurelia official documentation website</a> and I don't really seem to understand how to implement this.</p>
  
  <p>I went on the <a href="https://gitter.im/Aurelia/Discuss" rel="noreferrer">aurelia gitter channel</a> and I found 3 discussions about this, where one of them gave the following example for unsubscribe:</p>
  
  <pre><code>sub = ea.subscribe();
  
  //unsubscribe the event
  sub();
  </code></pre>
  
  <p>The problem is that this code doesn't work in TypeScript.</p>
  
  <p>How do I unsubscribe from an <code>event aggregator</code> in Typescript?</p>
  
  <p>Now, using this code</p>
  
  <pre><code>    @inject(Publisher, Subscriber)
  export class Home {
      publisher: Publisher;
      subscriber: Subscriber;
      channelName = "testChannel";
  
      constructor(pub: Publisher, sub: Subscriber) {
          this.publisher = pub;
          this.subscriber = sub;
  
          this.subscriber.subscribe(this.channelName);
  
          this.publisher.publish(this.channelName, "Ana are mere");
      }
  }
  
  
  @inject(EventAggregator)
  export class Publisher {
      eventAggregator: EventAggregator = null;
  
      constructor(agg: EventAggregator) {
          this.eventAggregator = agg;
      }
  
      publish(channelName: string, object: Object) {
          this.eventAggregator.publish(channelName, object);
      }
  }
  
  
  @inject(EventAggregator)
  export class Subscriber {
      eventAggregator: EventAggregator = null;
  
      constructor(agg: EventAggregator) {
          this.eventAggregator = agg;
      }
  
      subscribe(channelName: string) {
          this.eventAggregator.subscribe(channelName, object =&gt; {
              //TODO do something with received object
              alert(object);
          });
      }
      unsubscribe(channelName: string) {
          debugger;
      }
  }
  </code></pre>
  
  <p>when executing the <code>Home</code> component, the method for subscribe isn't executed just once, but as many times as the cosntructor has been called. So, if I have been on the home page 3 times, it will get executed 3 times.</p>
  
  <p>So: 
  Why is my subscriber method fired multiple times ?
  How do I unsubscribe from <code>event-aggregatoor</code> in TypeScript?</p>
  
  <p>Thanks!</p>