Tutorial: ExternalApplication.EventEmitter

ExternalApplication.EventEmitter

When an instance of fin.ExternalApplication is created, it inherits an EventEmitter with the below methods so that it is possible to listen to OpenFin events. The below methods are asynchronous as they must cross process boundaries and setup the listener in the browser process. When the EventEmitter receives an event from the browser process and emits on the renderer, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and will be discarded. If the execution context of the window is destroyed by page navigation or reload, any events that have been setup in that context will be destroyed. It is important to keep in mind that when an ordinary listener function is called, the standard this keyword is intentionally set to reference the EventEmitter instance to which the listener is attached. It is possible to use ES6 Arrow Functions as listeners, however, when doing so, the this keyword will no longer reference the EventEmitter instance.

addListener(event, listener)

Adds a listener to the end of the listeners array for the specified event.

const externalApp = await fin.ExternalApplication.wrap('my-uuid');

externalApp.addListener("connected", function(event) {
    console.log("The external app connected.");
});

on(event, listener)

Adds a listener to the end of the listeners array for the specified event.

const externalApp = await fin.ExternalApplication.wrap('my-uuid');

externalApp.on("connected", function(event) {
    console.log("The external app connected.");
});

once(event, listener)

Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed.

const externalApp = await fin.ExternalApplication.wrap('my-uuid');

externalApp.once("connected", function(event) {
    console.log("The external app connected.");
});

prependListener(event, listener)

Adds a listener to the beginning of the listeners array for the specified event.

const externalApp = await fin.ExternalApplication.wrap('my-uuid');

externalApp.prependListener("connected", function(event) {
    console.log("The external app connected.");
});

prependOnceListener(event, listener)

Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed. The listener is added to the beginning of the listeners array.

const externalApp = await fin.ExternalApplication.wrap('my-uuid');

externalApp.prependOnceListener("connected", function(event) {
    console.log("The external app connected.");
});

removeListener(event, listener)

Remove a listener from the listener array for the specified event. Caution: Calling this method changes the array indices in the listener array behind the listener.

const externalApp = await fin.ExternalApplication.wrap('my-uuid');
const callback = function(event) {
  console.log('The external application connected');
};

externalApp.on('connected', callback);
externalApp.removeListener("connected", callback);

removeAllListeners([event])

Removes all listeners, or those of the specified event.

const externalApp = await fin.ExternalApplication.wrap({ uuid: 'my-uuid'});
externalApp.removeAllListeners("connected");

Supported external application event types

  • connected
  • disconnected

External Application Events

connected

Generated when an external application has authenticated and is connected.

//This response has the following shape:
{
    topic: "externalapplication",
    type: "connected",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // the UUID of the external application
}

disconnected

Generated when an external application has disconnected

//This response has the following shape:
 {
    topic: "externalapplication",
    type: "disconnected",
    uuid: "454C7F31-A915-4EA2-83F2-CFA655453C52" // the UUID of the external application
}