# smart-web-messenger

> A library for sending/recieving SMART web messages through HTML5 Web Messaging

Latest version **0.1.5** (published 2020-09-29) · 0 weekly downloads

## Install

```sh
npm install smart-web-messenger
pnpm add smart-web-messenger
yarn add smart-web-messenger
bun add smart-web-messenger
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2020-09-29 |
| First published | 2020-06-16 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 103.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Interopion |
| Maintainers | vasil-interopion |

## Links

- npm: https://www.npmjs.com/package/smart-web-messenger
- npm.io page: https://npm.io/package/smart-web-messenger

## Dependencies (3)

- [rxjs](https://npm.io/package/rxjs.md) ^6.5.5
- [uuid](https://npm.io/package/uuid.md) ^8.1.0
- [domain-matcher](https://npm.io/package/domain-matcher.md) ^1.0.0

## Recent versions

- 0.1.5 (latest) — 2020-09-29
- 0.1.4 — 2020-09-10
- 0.1.3 — 2020-09-10
- 0.1.2 — 2020-08-07
- 0.1.1 — 2020-07-14
- 0.0.5 — 2020-07-06
- 0.0.4 — 2020-06-23
- 0.0.3 — 2020-06-22
- 0.0.2 — 2020-06-17
- 0.0.1 — 2020-06-16

## README

# SMART Web Messenger

## Installation

```
npm install smart-web-messenger --save
```

-----------------------------------------------------------------------------------------

## **Create a listener on a parent frame**

### Create listener

```javascript
import SmartMessenger from 'smart-web-messenger'

const messenger = new SmartMessenger()

const appUrls = ['localhost', '*.example.org', ...] // Domains that are allowed
const {
    observable,
    id
} = messenger.createListener(appURLs)
```

### Subscribe to messages

```javascript
const subscription = observable.subscribe((response) => {
    const [type, action] = response.messageType.split('.')
    const result = {}

    if (type === 'scratchpad') {
        if (action === 'add') {
            result.status = '201 OK'
            result.location = `[ResourceType]/[ResourceId]` 
            result.outcome = [ResourcePayload]

            // Perform any other scratchpad.add tasks
        }

        if (action === 'update') {
            result.status = '200 OK'
            result.location = `[ResourceType]/[ResourceId]` 
            result.outcome = [ResourcePayload]

            // Perform any other scratchpad.update tasks
        }

        if (action === 'delete') {
            result.status = '204 OK'
            result.location = `[ResourceType]/[ResourceId]` 
            result.outcome = [ResourcePayload]

            // Perform any other scratchpad.update tasks
        }
    }

    if (launchedAppData.launchUrl) {
        const iframeEl = document.getElementById(launchedAppData.launchUrl)

        if (iframeEl) {
            messenger.sendSpecificFrame(iframeEl, {
                messageId: messenger.generateId(),
                responseToMessageId: response.messageId,
                payload: result
            })
        }
    }
})
```

------------------------------------------------------------------------------------------

## **Send messages from child frame (iframe)**

```javascript
import SmartMessenger from 'smart-web-messenger'

// SMART Message payload
const payload = {
    resource: {
        resourceType: "[ResourceType]",
        ...
    }
}

const messenger = new SmartMessenger()

// Send SMART message with default timeout before being rejected
messenger.smartSend( `scratchpad.[add|update|delete]}` , payload)
    .then((res) => {
        // res = { status: "201|200|204 OK" }
        // Do something with the response
        ...
    });

// Send SMART message with an explicit timeout before being rejected
messenger.smartSend( `scratchpad.[add|update|delete]}` , payload, 500)
    .then((res) => {
        // res = { status: "201|200|204 OK" }
        // Do something with the response
        ...
    });
```

------------------------------------------------------------------------------------------

## **Setup message default timout**

### Construct messenger with default timeout value (=1000ms)
```javascript
    const messenger = new SmartMessenger();
```

### Construct with custom default timeout

```javascript
    const messenger = new SmartMessenger({ timeout: 2000 })
```

### Change default timeout value 
```javascript
    const messenger = new SmartMessenger()
    messenger.setDefaultTimeout(3000)
```

------------------------------------------------------------------------------------------

## **Building**

### Install dependencies 

```
npm install
```

### Run the build command

```
npm run build
```

### The bundled library will be available in `./dist`

---
_Source: https://npm.io/package/smart-web-messenger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
