# Getting events from aurelia-event-aggregator in app.ts from element in node\_modules

**URL:** <https://discourse.aurelia.io/t/getting-events-from-aurelia-event-aggregator-in-app-ts-from-element-in-node-modules/1001>\
**Category:** Help Requests\
**Created:** [March 28, 2018, 2:51pm UTC](https://discourse.aurelia.io/t/getting-events-from-aurelia-event-aggregator-in-app-ts-from-element-in-node-modules/1001 "2018-03-28T14:51:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seiryokusan](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/seiryokusan/32/700_2.png) [@seiryokusan](https://discourse.aurelia.io/u/seiryokusan)\
**Post date:** [March 28, 2018, 2:51pm UTC](https://discourse.aurelia.io/t/getting-events-from-aurelia-event-aggregator-in-app-ts-from-element-in-node-modules/1001/1 "2018-03-28T14:51:44Z")

</div>

Hi,  
I’m trying to use the event aggregator to publish an event when a specific error is received so I only have to registered to this event when in my app to show the same dialog from everywhere.

The event is published from a service created to support our restAPI and is imported in the application to the node\_modules.

This is the code related to my problem:

**app.ts**

```auto
import { EventAggregator } from "aurelia-event-aggregator";
import { autoinject, LogManager } from "aurelia-framework";
import { I18N } from "aurelia-i18n";
import { AuthenticationService } from "genius-services";
import { F7App } from "pages/shared/f7-app";
@autoinject()
export class App extends BasePage {

    constructor(i18n: I18N, f7App: F7App, private authenticationService: AuthenticationService, eventAggregator: EventAggregator) {
        super(i18n, f7App, eventAggregator);
        this.i18n.setLocale(navigator.language)
            .catch(e => { logger.error(`Loading failed with ${e.name}: ${e.message}`); });

        this.eventAggregator.subscribe("genius-services:invalid-session", () => {console.log("event?????????????????????")});
    }
}

```

**rest-service-base.ts**

```auto
import { EventAggregator } from "aurelia-event-aggregator";
import { HttpClient } from "aurelia-fetch-client";
import { autoinject, LogManager } from "aurelia-framework";

@autoinject()
export class RestServiceBase {
    public async setHttpFetchQuery<T>(uri: string, errorMessage: string, httpMethod?: string, body?, useStandardConfig?: boolean) {
        const baseUrl = useStandardConfig ? window.location.origin : this.server;
        const completeUri = `${baseUrl}${uri}`;

        const response = httpMethod !== undefined ? await this.httpClient.fetch(`${completeUri}`, { method: httpMethod, body: JSON.stringify(body) })
            : await this.httpClient.fetch(`${completeUri}`) as any;
        try {
            const jsonResponse = await response.json();
            jsonResponse.Result = this.jsonToCamelOrToPascal(jsonResponse.Result);
            if (jsonResponse.Messages.length > 0 && jsonResponse.Messages[0].Key === "InvalidSession") {
                this.eventAggregator.publish("genius-services:invalid-session", "data");
            }
            return jsonResponse as any as RestApiResult<T>;
        }
        catch (e) {
            logger.error(`${errorMessage}: ${e.message}`);
            return e;
        }
    }
}

```

I basically try to do something similar to the `"i18n:locale:changed"` event in aurelia-i18n. Unfortunately, I can’t get the subscription to catch the event. Both part of the code works if it is called from the same side of the code (all in the service or all in the app), so the event aggregator is definitely working.

Thanks in advance of the help!

---

<div class="post-metadata">

**Author:** ![Alexander-Taran](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alexander-taran/32/2308_2.png) [@Alexander-Taran](https://discourse.aurelia.io/u/Alexander-Taran)\
**Post date:** [March 29, 2018, 4:42pm UTC](https://discourse.aurelia.io/t/getting-events-from-aurelia-event-aggregator-in-app-ts-from-element-in-node-modules/1001/2 "2018-03-29T16:42:11Z")

</div>

I don’t see a constructor in your rest service.  
Where does it get the instance of EventAggregator?

---

<div class="post-metadata">

**Author:** ![seiryokusan](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/seiryokusan/32/700_2.png) [@seiryokusan](https://discourse.aurelia.io/u/seiryokusan)\
**Post date:** [April 2, 2018, 1:48pm UTC](https://discourse.aurelia.io/t/getting-events-from-aurelia-event-aggregator-in-app-ts-from-element-in-node-modules/1001/3 "2018-04-02T13:48:00Z")

</div>

Here is the constructor for rest-service-base:

```auto
    constructor(geniusCookieService: GeniusCookieService, public eventAggregator: EventAggregator) {
        this.httpClient = new HttpClient();
        this.server = geniusCookieService.getCookie().geniusRestApiServer;
        if (this.server.slice(-1) !== "/") {
            this.server += "/";
        }
    }

```

Also, I’m not sure if it was clear, but the two file are not in the same project. I thought about that, and I guess there is a huge chance that they don’t have the same instance of event aggregator because of that. If so, I don’t know how to “sync” them to get the same instance.

Thanks again and sorry for the delay.

---

<div class="post-metadata">

**Author:** ![Alexander-Taran](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alexander-taran/32/2308_2.png) [@Alexander-Taran](https://discourse.aurelia.io/u/Alexander-Taran)\
**Post date:** [April 2, 2018, 5:18pm UTC](https://discourse.aurelia.io/t/getting-events-from-aurelia-event-aggregator-in-app-ts-from-element-in-node-modules/1001/4 "2018-04-02T17:18:51Z")

</div>

so strange…  
can you recreate the relevant part  
by forking this: [https://stackblitz.com/edit/aurelia-base?file=app.js](https://stackblitz.com/edit/aurelia-base?file=app.js)

or this [https://gist.run/?id=8ae06845f2bdf4d533b072c00da82614](https://gist.run/?id=8ae06845f2bdf4d533b072c00da82614)
