# API

**Kind**: interface | **Module**: [Channels](https://docs.interop.io/desktop/reference/javascript/channels/index.md) | **Access**: `io.channels`

**Source**: https://docs.interop.io/desktop/reference/javascript/channels/api/index.html

Channels API.

## Properties

- **`mode`** (`OperationMode`, required) default: `"single"` *(since io.Connect Desktop 9.7, @interopio/desktop 6.10.0)*
  Retrieves the current Channel mode (single or multi Channel). Set by the io.Connect framework based on configuration.

## Methods

### add

Adds a new Channel.

```ts
(info: ChannelContext) => Promise<ChannelContext>
```

**Parameters**

- **`info`** (`ChannelContext`, required)
  Initial Channel context.

**Returns**: `Promise<ChannelContext>`

### all

Retrieves a list of all Channel names.

```ts
() => Promise<string[]>
```

**Returns**: `Promise<string[]>`

### clearChannelData

Clears the context data of the current Channel. The `data` property of the Channel context is set to an empty object. Pass a Channel name as an argument to clear the data of a specific Channel.

*Since: @interopio/desktop 6.12.0*

```ts
(channel?: string) => Promise<void>
```

**Parameters**

- **`channel`** (`string`, optional)
  Name of the Channel whose context data to clear.

**Returns**: `Promise<void>`

### get

Retrieves the context of a specified Channel.

```ts
(name: string, options?: FDC3Options) => Promise<ChannelContext>
```

**Parameters**

- **`name`** (`string`, required)
  The name of the Channel whose context to retrieve.
- **`options`** (`FDC3Options`, optional)
  Settings for retrieving an FDC3 context published in the io.Connect Channel. *Available since@interopio/desktop 6.9.0.*

**Returns**: `Promise<ChannelContext>`

### getMy

Retrieves the context of the current Channel.
It's recommended to use the `getMyChannels()` method instead, which can retrieve the contexts of the currently joined Channels both in single and in multi Channel mode.

```ts
(options?: FDC3Options) => Promise<ChannelContext>
```

**Parameters**

- **`options`** (`FDC3Options`, optional)
  Settings for retrieving an FDC3 context published in the io.Connect Channel. *Available since@interopio/desktop 6.9.0.*

**Returns**: `Promise<ChannelContext>`

### getMyChannels

Retrieves a list with the contexts of the current Channels. Can be used both in single and in multi Channel mode.
If used in single Channel mode, the returned array will always contain a single member if the window is joined to a Channel, or will be empty otherwise.

*Since: io.Connect Desktop 9.7, @interopio/desktop 6.10.0*

```ts
(options?: FDC3Options) => Promise<ChannelContext[]>
```

**Parameters**

- **`options`** (`FDC3Options`, optional)

**Returns**: `Promise<ChannelContext[]>`

### getRestrictions

Retrieves the restrictions applied to the current window for publishing or subscribing to Channels. Pass a window ID to retrieve the restrictions for the specified window.

*Since: io.Connect Desktop 9.3, @interopio/desktop 6.3.1*

```ts
(windowId?: string) => Promise<Restrictions>
```

**Parameters**

- **`windowId`** (`string`, optional)
  ID of the window for which to retrieve the applied restrictions for publishing or subscribing to Channels.

**Returns**: `Promise<Restrictions>`

### getWindowsOnChannel

Retrieves all windows on a specified Channel.

```ts
(channel: string) => Promise<IOConnectDesktop.Windows.IOConnectWindow[]>
```

**Parameters**

- **`channel`** (`string`, required)

**Returns**: `Promise<IOConnectDesktop.Windows.IOConnectWindow[]>`

### getWindowsWithChannels

Retrieves all windows that can use Channels together with their current Channel.

```ts
(filter?: WindowWithChannelFilter) => Promise<WindowOnChannelInfo[]>
```

**Parameters**

- **`filter`** (`WindowWithChannelFilter`, optional)
  Filter describing which windows to retrieve. If no filter is supplied, will return all windows that can use Channels.

**Returns**: `Promise<WindowOnChannelInfo[]>`

### join

Joins a new Channel by name. Leaves the current Channel.

```ts
(name: string, windowId?: string) => Promise<void>
```

**Parameters**

- **`name`** (`string`, required)
  The name of the Channel to join.
- **`windowId`** (`string`, optional)
  ID of a window which to join to a Channel. If not provided, will join the current window to the specified Channel.

**Returns**: `Promise<void>`

### leave

Removes the current or a specified window from the current or a specified Channel.

```ts
(options?: string | { windowId?: string, channel?: string }) => Promise<void>
```

**Parameters**

- **`options`** (`string | { windowId?: string, channel?: string }`, optional)
  Options for leaving Channels. You can provide only the window ID, or an object holding the window ID and the name of the Channel to leave.
  In either case, if only a window ID is provided, the specified window will be removed from the current Channel if in single Channel mode,
  or from all currently joined Channels if in multi Channel mode.
  If you provide only the name of the Channel to leave, the current window will be removed from the specified Channel.
  If you don't provide an argument, the current window will be removed from the current Channel if in single Channel mode,
  or from all currently joined Channels if in multi Channel mode.

**Returns**: `Promise<void>`

### list

Retrieves a list of all Channel contexts.

```ts
() => Promise<ChannelContext[]>
```

**Returns**: `Promise<ChannelContext[]>`

### my

Retrieves the name of the current Channel.
It's recommended to use the `myChannels()` method instead, which can retrieve the names of the currently joined Channels both in single and in multi Channel mode.

```ts
() => string
```

**Returns**: `string`

### myChannels

Retrieves a list with the names of the current Channels. Can be used both in single and in multi Channel mode.
If used in single Channel mode, the returned array will always contain a single member if the window is joined to a Channel, or will be empty otherwise.

*Since: io.Connect Desktop 9.7, @interopio/desktop 6.10.0*

```ts
() => string[]
```

**Returns**: `string[]`

### onChanged

Notifies when the current window joins or leaves a Channel. Returns an unsubscribe function.
It's recommended to use the `onChannelsChanged()` method instead, which can handle Channel changes both in single and in multi Channel mode.

```ts
(callback: (channel: string) => void) => () => void
```

**Parameters**

- **`callback`** (`(channel: string) => void`, required)
  Callback function for handling the event. Receives as an argument the name of the newly joined Channel. If the window leaves a Channel without joining a new one, the argument will be `undefined`.

**Returns**: `() => void`

### onChannelsChanged

Notifies when the current window joins or leaves a Channel. Can be used both in single and in multi Channel mode.
If used in single Channel mode, when the window joins a Channel, the array argument passed to the callback for handling the event
will always contain a single member; when the window leaves the current Channel, the array will be empty. Returns an unsubscribe function.

*Since: io.Connect Desktop 9.7, @interopio/desktop 6.10.0*

```ts
(callback: (channels: string[]) => void) => () => void
```

**Parameters**

- **`callback`** (`(channels: string[]) => void`, required)
  Callback function for handling the event. Receives a list with the names of the currently joined Channels as an argument.

**Returns**: `() => void`

### publish

Updates the context of the current or a specified Channel.

```ts
(data: any, options?: string | PublishOptions) => Promise<void>
```

**Parameters**

- **`data`** (`any`, required)
  Data object with which to update the Channel context.
- **`options`** (`string | PublishOptions`, optional)
  The name of the Channel to update, or an object containing the name of the Channel to update and a flag indicating whether the published data is an FDC3 context. If no options are provided, the current Channel will be updated. *Passing a `PublishOptions` object is available since@interopio/desktop 6.7.0.*

**Returns**: `Promise<void>`

### remove

Removes a Channel.

```ts
(channel: string) => Promise<void>
```

**Parameters**

- **`channel`** (`string`, required)
  The name of the Channel to remove.

**Returns**: `Promise<void>`

### restrict

Prevents or allows the current or another window to publish or subscribe to a specific Channel.

*Since: io.Connect Desktop 9.3, @interopio/desktop 6.3.1*

```ts
(restrictions: ChannelRestrictions) => Promise<void>
```

**Parameters**

- **`restrictions`** (`ChannelRestrictions`, required)
  Restrictions for publishing or subscribing to a specific Channel.

**Returns**: `Promise<void>`

### restrictAll

Prevents or allows the current or another window to publish or subscribe to all Channels.

*Since: io.Connect Desktop 9.3, @interopio/desktop 6.3.1*

```ts
(restrictions: RestrictionsConfig) => Promise<void>
```

**Parameters**

- **`restrictions`** (`RestrictionsConfig`, required)
  Restrictions for publishing or subscribing to all Channels.

**Returns**: `Promise<void>`

### setPath

Sets a specified path within the Channel context to the provided value. If the path doesn't exist, it will be created.

*Since: @interopio/desktop 6.5.0*

```ts
(path: IOConnectDesktop.Contexts.PathValue, name?: string) => Promise<void>
```

**Parameters**

- **`path`** (`IOConnectDesktop.Contexts.PathValue`, required)
  Object containing the path to update and the value with which to update it. The path must be specified as a dot-separated string (e.g., `"prop1.prop2"`).
- **`name`** (`string`, optional)
  Name of the Channel to update. If not specified, will update the current Channel.

**Returns**: `Promise<void>`

### setPaths

Sets a list of specified paths within the Channel context to the provided values. If a path doesn't exist, it will be created.

*Since: @interopio/desktop 6.5.0*

```ts
(paths: IOConnectDesktop.Contexts.PathValue[], name?: string) => Promise<void>
```

**Parameters**

- **`paths`** (`IOConnectDesktop.Contexts.PathValue[]`, required)
  List of objects each containing a path to update and the value with which to update it. The path must be specified as a dot-separated string (e.g., `"prop1.prop2"`).
- **`name`** (`string`, optional)
  Name of the Channel to update. If not specified, will update the current Channel.

**Returns**: `Promise<void>`

### subscribe

Subscribes for data updates of the current Channel. Persisted after a Channel change. Returns an unsubscribe function.

```ts
(callback: (data: any, context: ChannelContext, updaterId: string) => void, options?: FDC3Options) => () => void
```

**Parameters**

- **`callback`** (`(data: any, context: ChannelContext, updaterId: string) => void`, required)
  Callback function for handling the received data. The callback receives as arguments the updated Channel data, the current Channel context and the ID of the updating app instance.
- **`options`** (`FDC3Options`, optional)
  Settings for subscribing to an FDC3 context published in the io.Connect Channel. *Available since@interopio/desktop 6.9.0.*

**Returns**: `() => void`

### subscribeFor

Subscribes for data updates of a specified Channel. Resolves with an unsubscribe function.

```ts
(name: string, callback: (data: any, context: ChannelContext, updaterId: string) => void, options?: FDC3Options) => Promise<() => void>
```

**Parameters**

- **`name`** (`string`, required)
  The Channel to which to subscribe.
- **`callback`** (`(data: any, context: ChannelContext, updaterId: string) => void`, required)
  Callback function for handling the received data. The callback receives as arguments the updated Channel data, the current Channel context and the ID of the updating app instance.
- **`options`** (`FDC3Options`, optional)
  Settings for subscribing to an FDC3 context published in the io.Connect Channel. *Available since@interopio/desktop 6.9.0.*

**Returns**: `Promise<() => void>`

## Related types

- [ChannelContext](https://docs.interop.io/desktop/reference/javascript/channels/channelcontext/index.md)
- [ChannelRestrictions](https://docs.interop.io/desktop/reference/javascript/channels/channelrestrictions/index.md)
- [FDC3Options](https://docs.interop.io/desktop/reference/javascript/channels/fdc3options/index.md)
- [IOConnectWindow](https://docs.interop.io/desktop/reference/javascript/windows/ioconnectwindow/index.md)
- [PathValue](https://docs.interop.io/desktop/reference/javascript/shared%20contexts/pathvalue/index.md)
- [PublishOptions](https://docs.interop.io/desktop/reference/javascript/channels/publishoptions/index.md)
- [Restrictions](https://docs.interop.io/desktop/reference/javascript/channels/restrictions/index.md)
- [RestrictionsConfig](https://docs.interop.io/desktop/reference/javascript/channels/restrictionsconfig/index.md)
- [WindowOnChannelInfo](https://docs.interop.io/desktop/reference/javascript/channels/windowonchannelinfo/index.md)
- [WindowWithChannelFilter](https://docs.interop.io/desktop/reference/javascript/channels/windowwithchannelfilter/index.md)
