# Setup

Source: https://docs.interop.io/browser/capabilities/plugins/setup/index.html

## Overview

When designing a multi app project, sometimes you may need to execute initial system logic - initialize data providers (like [search providers](https://docs.interop.io/browser/capabilities/search/search-api/index.md#search_providers)), [register](https://docs.interop.io/browser/capabilities/data-sharing/interop/index.md#method_registration) Interop methods, or even modify the behavior of the [`@interopio/browser-platform`](https://www.npmjs.com/package/@interopio/browser-platform) library (see [Interception](https://docs.interop.io/browser/capabilities/plugins/interception/index.md)). In a desktop app this is usually handled by auto starting hidden apps. In a web project, however, it isn't possible to create a hidden browser window that will execute the required initial logic. The solution is to define a Plugin in the configuration object for the [Main app](https://docs.interop.io/browser/developers/browser-platform/overview/index.md). The Plugin will be initialized on platform startup and will simulate using a hidden service app in your project.

## Usage

Plugins in the context of **io.Connect Browser** are user-defined functions that will be executed upon startup of the [Main app](https://docs.interop.io/browser/developers/browser-platform/overview/index.md). You can also specify whether the initialization of the [`@interopio/browser-platform`](https://www.npmjs.com/package/@interopio/browser-platform) library must wait for the Plugin to be executed. The Plugin receives as a first argument an object containing the fully initialized io.Connect library, which allows you to perform io.Connect operations before the Main app or any of the [Browser Client](https://docs.interop.io/browser/developers/browser-client/overview/index.md) apps has been initialized. As a second argument, the Plugin receives a user-defined configuration object, and as a third - an object that can be used for finer system control (see [Interception](https://docs.interop.io/browser/capabilities/plugins/interception/index.md)).

Use the `plugins` property of the configuration object for initializing the [`@interopio/browser-platform`](https://www.npmjs.com/package/@interopio/browser-platform) library in the [Main app](https://docs.interop.io/browser/developers/browser-platform/overview/index.md) to define Plugins:

```javascript
import IOBrowserPlatform from "@interopio/browser-platform";

const myPlugin = async (io, config) => {
    let meaning;

    if (config.io === 42) {
        meaning = config.io;
    } else {
        meaning = undefined;
    };

    await io.interop.register("MyMethod", () => `Currently, the meaning of life is ${meaning}.`);
};

const config = {
    licenseKey: "my-license-key",
    plugins: {
        // Plugin definitions.
        definitions: [
            {
                name: "my-plugin",
                config: { io: 42 },
                start: myPlugin,
                critical: true
            }
        ]
    }
};

const { io } = await IOBrowserPlatform(config);
```

The Plugin definition object has the following properties:

| Property | Type | Description |
|----------|------|-------------|
| `config` | `object` | Configuration that will be passed to the Plugin. |
| `critical` | `boolean` | If `true`, the [`@interopio/browser-platform`](https://www.npmjs.com/package/@interopio/browser-platform) library will wait for the Plugin to be executed before completing its initialization. |
| `name` | `string` | **Required.** Name for the Plugin. |
| `start` | `function` | **Required.** Function that will receive as arguments a fully initialized `io` object, the `config` object specified in the definition and a `platform` object (see [Interception](https://docs.interop.io/browser/capabilities/plugins/interception/index.md)) that can be used for handling low-level system messages and logging. |
| `version` | `string` | Version of the Plugin. |
