# @mangar2/broker

> implements a 'mqtt type' http server

Latest version **1.4.0** (published 2021-12-08) · LGPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install @mangar2/broker
pnpm add @mangar2/broker
yarn add @mangar2/broker
bun add @mangar2/broker
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2021-12-08 |
| First published | 2019-10-27 |
| Weekly downloads | 0 |
| License | LGPL-3.0-or-later |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 25 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Volker Böhm |
| Maintainers | mangar2 |
| Keywords | http, https, server, mqtt |

## Links

- npm: https://www.npmjs.com/package/@mangar2/broker
- Repository: https://github.com/Mangar2/yaha/blob/master/node_modules/@mangar2/broker
- Homepage: https://github.com/Mangar2/yaha.git
- npm.io page: https://npm.io/package/@mangar2/broker

## Dependencies (8)

- [@mangar2/mqtt](https://npm.io/package/@mangar2/mqtt.md) latest
- [@mangar2/config](https://npm.io/package/@mangar2/config.md) latest
- [@mangar2/connect](https://npm.io/package/@mangar2/connect.md) latest
- [@mangar2/shutdown](https://npm.io/package/@mangar2/shutdown.md) latest
- [@mangar2/callbacks](https://npm.io/package/@mangar2/callbacks.md) latest
- [@mangar2/checkinput](https://npm.io/package/@mangar2/checkinput.md) latest
- [@mangar2/httpservice](https://npm.io/package/@mangar2/httpservice.md) latest
- [@mangar2/configuration](https://npm.io/package/@mangar2/configuration.md) latest

## Recent versions

- 1.4.0 (latest) — 2021-12-08
- 1.3.0 — 2020-12-09
- 1.2.7 — 2020-01-19
- 1.2.6 — 2020-01-19
- 1.2.5 — 2020-01-19
- 1.2.4 — 2020-01-19
- 1.2.3 — 2020-01-18
- 1.2.2 — 2020-01-17
- 1.2.1 — 2020-01-17
- 1.2.0 — 2020-01-17
- 1.1.0 — 2019-10-27

## README

<!-- This file is generated by jsmddoc version 0.1 -->

# Abstract

The ( mqtt ) broker is a central service receiving and distributing messages to all subscribed receipiens . Use the "brokerservice" module to start the broker .

## Contents

- [Meta](#Meta)
- [Class Broker](#Class-Broker)
  - [Parameters](#Broker-Parameters)
  - [Methods](#Broker-Methods)
    - [close](#close)
    - [connect](#connect)
    - [on](#on)
    - [run](#run)

## Meta

| | |
| --- | --- |
| **File** | broker.js |
| **Abstract** | The ( mqtt ) broker is a central service receiving and distributing messages to all subscribed receipiens . Use the "brokerservice" module to start the broker . |
| **Author** | Volker Böhm |
| **Copyright** | Copyright ( c ) 2020 Volker Böhm |
| **License** | This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose . |

## Class Broker

`new Broker(configuration)`

Creates a new broker

### Example

```javascript
const configuration = {
     port: 10000,
     persistInterval: 0,
     connections: {
          directory: '.',
          fileName: 'broker',
          log: [
              {
                  module: 'received',
                  topic: '#'
              }
          ]
      }
  }
const broker = new Broker(configuration)
broker.run()
```

### Broker Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `configuration` | `Object` | configuration for the broker | |

#### configuration properties

| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| `port` | `number, string` |  |  | port number the broker will listen on | |
| `persistInterval` | `number` | optional | 60 \* 1000 | Intervall between persisting internal state to file in milliseconds . Persistance is not called if this value is 0 | |
| `connections` | `Object` |  |  | connection settings for the broker | |

#### connections properties

| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| `fileName` | `string` |  |  | filename for persistence | |
| `directory` | `string` |  |  | directory ( file path ) for persistence | |
| `replyTimeoutInMilliseconds` | `number` |  |  | timeout to wait for an answer of a http call | |
| `inFlightWindow` | `number` | optional | 1 | amount of qos 1 and qos 2 messages send for the same topic at the same time | |
| `pubrelTimeoutInMilliseconds` | `number` | optional | one day | temeout to wait for a corresponding pubrel message | |
| `maxRetryCount` | `number` |  |  | amount of resend of messages before forcing a disconnect | |
| `maxQueuSize` | `number` |  |  | maximal amount of entries in a message queue | |
| `log` | `Object` |  |  | log settings | |

### Broker Methods

#### close

`async close ()`

Closes the broker , stops listening

#### connect

`connect (host, port)`

Connects to a friendly broker and subscribs for all messages

##### connect Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `host` | `string` | name of the host ( or his ip ) | |
| `port` | `number` | port number to connect to | |

#### on

`on (event, callback)`

Sets a callback .

##### on Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `event` | `string` | callback name ( supports 'send' and 'publish' ) 'send' is called to send data . This function is provided by the broker but could be overwritten 'publish' is called on receiving data . This function if provided by the broker but could be overwritten | |
| `callback` | `function` |  | |

##### on throws

| Type | Description |
| ---- | ----------- |
| `Error` | if the event is not supported |
| `Error` | if the callback is not 'function' |

#### run

`run ()`

Starts the broker . It will listen to message , send messages and periodically store its status

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