# @dytesdk/plugin-sdk

> SDK for creating Dyte plugins.

Latest version **2.4.0** (published 2024-04-25) · 0 weekly downloads

## Install

```sh
npm install @dytesdk/plugin-sdk
pnpm add @dytesdk/plugin-sdk
yarn add @dytesdk/plugin-sdk
bun add @dytesdk/plugin-sdk
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.0 |
| Published | 2024-04-25 |
| First published | 2022-04-12 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 164.1 KB |
| Known vulnerabilities | 0 (+24 in 2 direct dependencies) |
| Install scripts | no |
| Maintainers | dyteindia |

## Links

- npm: https://www.npmjs.com/package/@dytesdk/plugin-sdk
- Issues: https://community.dyte.io
- npm.io page: https://npm.io/package/@dytesdk/plugin-sdk

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [axios](https://npm.io/package/axios.md) ^0.25.0
- [events](https://npm.io/package/events.md) ^3.3.0

## Recent versions

- 2.4.0 (latest) — 2024-04-25
- 2.4.2 (next) — 2025-02-21
- 2.3.3 — 2024-04-24
- 2.3.2 — 2024-04-22
- 2.3.1 — 2024-04-22
- 2.2.0 — 2023-07-12
- 2.1.3 — 2023-06-29
- 2.1.2 — 2023-06-27
- 2.1.1 — 2023-06-26
- 2.1.0 — 2023-06-22
- 1.10.6 — 2022-11-30
- 1.10.5 — 2022-11-23
- 1.10.4 — 2022-10-19
- 1.10.3 — 2022-10-18
- 1.10.2 — 2022-09-28
- … 9 more at https://npm.io/package/@dytesdk/plugin-sdk/versions

## README

<!-- PROJECT LOGO -->
<p align="center">
  <a href="https://dyte.io">
    <img src="https://dyte-uploads.s3.ap-south-1.amazonaws.com/dyte-logo-dark.svg" alt="Logo" width="80">
  </a>
  <h2 align="center">Plugin SDK by Dyte</h3>
  <p align="center">
    <br />
    <a href="https://docs.dyte.io"><strong>Explore the docs »</strong></a>
    <br />
    <br />
    <a href="https://github.com/dyte-in/plugin-sdk/issues">Report Bug</a>
    •
    <a href="https://github.com/dyte-in/plugin-sdk/issues">Request Feature</a>
  </p>
</p>

<!-- TABLE OF CONTENTS -->
## Table of Contents
- [About the Project](#about-the-project)
  - [Built With](#built-with)
- [Getting Started](#getting-started)
  - [Prerequisites](#prerequisites)
  - [Installation](#installation)
  <!-- - [Quickstart](#quickstart) -->
- [Usage](#usage)
- [Contributing](#contributing)
- [License](#license)

## About The Project
Plugin SDK let's you build custom plugins that work with all of Dyte's core SDKs.
### Built With
- [Dyte](https://dyte.io/)
- [Typescript](https://typescriptlang.org/)

<!-- GETTING STARTED -->
## Getting Started
### Prerequisites
- npm
### Installation
```sh
npm install @dytesdk/plugin-sdk
```
<!-- ### Quickstart
You can use these sample plugin templates to get started with Plugin SDK.
- [React](https://github.com/dyte-in/dyte-plugin-react-template)
- [Angular]()
- [Vue]()
- [Javascript](https://github.com/dyte-in/dyte-plugin-js-template)

Refer to https://docs.dyte.io/docs/plugins for the complete API reference. -->

<!-- USAGE EXAMPLES -->
## Usage
A plugin object can be created using `DytePlugin.init()` method.
```
const plugin = await DytePlugin.init();
```
The plugin object provides several methods and features to interact with Dyte's core SDKs with ease.

### **Table of Contents**
- [Event Emitter](#event-emitter-on-dyteplugin)
- [Staggered Plugins](#methods-specific-to-staggered-plugins)
- [Data Store](#methods-specific-to-data-store)
- [Other Methods](#other-methods)

<br/>

### Event Emitter on `DytePlugin`
- **Listen for plugin events**
  | Param | Type | Value | Required |
  |---|---|--|--|
  | event | string | `newChatMessage` <br/> `pluginData` <br/> `peerJoined` <br/> `peerLeft` <br/>  custom event | true |


  | Events | Description |
  |---|---|
  | newChatMessage | Emmited when someone sends a new chat message |   
  | pluginData | Emitted when data is sent from meeting to plugin |   
  | peerJoined | Emitted when a new peer joins the meeting | 
  | peerLeft | Emitted when a peer leaves the meeting |

  *You can also listen for custom (user defined) events*
  ```
  plugin.on(event, (data) => {
    console.log('do something')
  })
  ```
  Alias:
  ```
  plugin.addListener(event, (data) => {
    console.log('do something')
  })
  ```
- **Emit custom events to all users**
  ```
  plugin.emit('myAwesomeEvent', data)
  ```

<br/>

### Methods specific to staggered plugins
Staggered plugins are plugins that enabled only for specific users.
By default these are only enabled for the user who initiated the plugin.

- **Enable plugin for a user**

  | Param | Type | Value | Required |
  |--|--|--|--|
  | peerId | string | Id of the peer you want to enable the plugin for. | true |

  ```
  await plugin.enablePluginForUser(peerId);
  ```

- **Disable plugin for a user**

  | Param | Type | Value | Required |
  |--|--|--|--|
  | peerId | string | Id of the peer you want to disable the plugin for. | true
  ```
  await plugin.disablePluginForUser(peerId);
  ```

- **Enable plugin for all users**
  ```
  await plugin.enablePluginForAll();
  ```

<br/>


### Methods specific to data store
Data store is a temporary database provided with Plugin SDK. It has a lot of capabilities and features, like data history and CRDT support to help you manage your data better.

Each entry in the store is a key-pair value.

- **Create a store**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | storeName | string | Store Name | true |
  | local | string | flag to specify weather the store is local or global. <br/> Local stores are not accessible by other users. <br/> false by default. | false |
  ```
  const newStore = await plugin.stores.create(
    storeName,
    local
  );
  ```

- **Get a store**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | storeName | string | Store Name | true |
  ```
  const store = plugin.stores.get(storeName);
  ```

- **Get all stores**

  Returns all stores, local and global.
  ```
  const stores = plugin.stores.all();
  ```

- **Add item to store**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | key | string | Identifier for item in store | true |
  | value | any | data to be stored in the store | true |
  | clearHistory | boolean | boolean flag to clear history on set action. <br/> false by default  | false |
  ```
  const store = plugin.stores.get(storeName);
  store.set(key, value, clearHistory);
  ```

- **Get an item from store**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | key | string | Identifier for item in store | true |
  ```
  const store = plugin.stores.get(storeName);
  store.get(key);
  ```

- **Get all data from store**
  ```
  const store = plugin.stores.get(storeName);
  store.getAll();
  
  ```

- **Update item in store**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | key | string | Identifier for item in store | true |
  | value | any | data to be stored in the store | true |
  | clearHistory | boolean | boolean flag to clear history on update action. <br/> false by default | false |
  ```
  const store = plugin.stores.get(storeName);
  store.update(key, value, clearHistory);
  ```

- **Subscribe to store changes**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | key | string | Identifier for item in store | true |
  NOTE: pass `*` as `key` to all changes on the store.
  ```
  store.subscribe(key, (data) => {
    console.log(data);
  });
  ```

- **Unsubscribe to store changes**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | key | string | Identifier for item in store | true |
  ```
  store.unsubscribe(key);
  ```

<!-- - **Get data history**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | key | string | Identifier for item in store | false |

  NOTE: If no `key` is passed, you get data history for the entire store.
  ```
  const history = store.history(key);
  ```

- **Clear data history**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | key | string | Identifier for item in store | false |

  NOTE: If no `key` is passed, history for the entire store will get cleared.
  ```
  store.clearHistory(key);
  store.clearHistory();
  ``` -->

<br/>

### Other Methods

- **Get room data**
  ```
  const room = await plugin.getRoomState();
  ```

- **Get peer data**
  | Param | Type | Value | Required |
  |--|--|--|--|
  | peerId | string | id of the peer you want to disable the plugin for. (not `userId`) | false

  NOTE: Returns current peer's (self) data if no `peerId` is passed.
  ```
  const peer = await plugin.getPeerInfo(peerId);
  const self = await plugin.getPeerInfo();
  ```

- **Get all peers**
  ```
  const peers = await plugin.getJoinedPeers();
  ```

- **Get room name**
  ```
  const name = plugin.getRoomName(); 
  ```

- **Get plugin initiator's `Peer ID`**

  NOTE: Returns undefined if the initiator has left the meeting.
  ```
  const peerId = await plugin.getPluginInitiatorId();
  ```

- **Get plugin initiator's `User ID`**

  ```
  const userId = await plugin.getPluginInitiatorUserId();
  ```

- **Get meeting title**
  ```
  const title = plugin.getDisplayTitle();
  ```

- **Send a chat message**
  <!-- | Param | Type | Value | Required |
  |--|--|--|--|
  | message | ChatMessage | Message object of type ChatMessage | true
  | message.userId | string | Id of the peer sending the message | true
  | message.read | boolean | Weather or not this message is new | false
  | message.type | string | `text`, `image`, `file` | true
  | message.text | string | body of the text messgae | `type: text`
  | message.link | string | file/image url | `type: image, file`
  | message.name | string | file name | `type: file`
  | message.size | number | file size | `type: file` -->

  | Param | Type | Value | Required |
  |--|--|--|--|
  | message | string | body of the text message | true
  | type | string | `text` | true
  ```
  await plugin.sendChatMessage(message);
  ```

- **Get plugin meta data**

  Returns the plugin meta data along with the entire plugin store.
  ```
  const data = await plugin.getPluginData();
  ```

## Contributing
We really appreciate contributions in the form of bug reports and feature suggestions. Help us make Dyte better with your valuable contributions on our [forum]('https://discord.com/invite/pxRcdNufvk') 🙂.

## License
All rights reserved. © Dyte Inc.

---
_Source: https://npm.io/package/@dytesdk/plugin-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
