# @mangar2/connections

> Manages connections to a MQTT broker

Latest version **1.5.3** (published 2020-12-13) · LGPL-3.0-or-later license · 0 weekly downloads

## Install

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

## 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.5.3 |
| Published | 2020-12-13 |
| First published | 2019-09-22 |
| Weekly downloads | 0 |
| License | LGPL-3.0-or-later |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 69.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Volker Böhm |
| Maintainers | mangar2 |
| Keywords | MQTT, connection, subscribe, connect, disconnect |

## Links

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

## Dependencies (7)

- [@mangar2/message](https://npm.io/package/@mangar2/message.md) latest
- [@mangar2/persist](https://npm.io/package/@mangar2/persist.md) latest
- [@mangar2/errorlog](https://npm.io/package/@mangar2/errorlog.md) latest
- [@mangar2/checkinput](https://npm.io/package/@mangar2/checkinput.md) latest
- [@mangar2/topicmatch](https://npm.io/package/@mangar2/topicmatch.md) latest
- [@mangar2/mqttversion](https://npm.io/package/@mangar2/mqttversion.md) latest
- [@mangar2/configuration](https://npm.io/package/@mangar2/configuration.md) latest

## Recent versions

- 1.5.3 (latest) — 2020-12-13
- 1.5.2 — 2020-12-13
- 1.5.1 — 2020-12-12
- 1.5.0 — 2020-12-10
- 1.4.0 — 2020-01-19
- 1.3.4 — 2020-01-19
- 1.3.3 — 2020-01-17
- 1.3.0 — 2019-10-27
- 1.1.1 — 2019-10-19
- 1.1.0 — 2019-10-16
- 1.0.8 — 2019-10-15
- 1.0.7 — 2019-10-13
- 1.0.6 — 2019-10-13
- 1.0.5 — 2019-10-12
- 1.0.4 — 2019-09-29
- … 4 more at https://npm.io/package/@mangar2/connections/versions

## README

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

# Abstract

Manages connections of the yaha Mqtt broker and provides functionality to receive/send messages from/to the managed connections in three service levels ( qos 0 , 1 , 2 )

## Contents

- [Meta](#Meta)
- [Class Connections](#Class-Connections)
  - [Parameters](#Connections-Parameters)
  - [Methods](#Connections-Methods)
    - [acknowledgeMessage](#acknowledgeMessage)
    - [checkToken](#checkToken)
    - [cleanup](#cleanup)
    - [connect](#connect)
    - [disconnect](#disconnect)
    - [getAllMessagesToSend](#getAllMessagesToSend)
    - [isDuplicateQos2message](#isDuplicateQos2message)
    - [onPubrel](#onPubrel)
    - [persist](#persist)
    - [publishMessage](#publishMessage)
    - [restoreFromFile](#restoreFromFile)
    - [setWill](#setWill)
    - [subscribe](#subscribe)
    - [unsubscribe](#unsubscribe)

## Meta

| | |
| --- | --- |
| **File** | connections.js |
| **Abstract** | Manages connections of the yaha Mqtt broker and provides functionality to receive/send messages from/to the managed connections in three service levels ( qos 0 , 1 , 2 ) |
| **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 Connections

`new Connections(configuration)`

Creates a connection object This class is part of an MQTT style broker . It manages connections to the broker .

### Connections Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `configuration` | `Object` | configuration | |

#### configuration properties

| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| `fileName` | `string` |  |  | filename for persistence | |
| `directory` | `string` |  |  | directory ( file path ) for persistence | |
| `replyTimeoutInMilliseconds` | `nubmber` | optional | 10000 | 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 | 24\*3600\*1000 | timout to wait for a corresponding pubrel message , default one day | |
| `maxRetryCount` | `number` | optional | 10 | amount of resend of messages before forcing a disconnect | |
| `maxQueuSize` | `number` |  |  | maximal amount of entries in a message queue | |

### Connections Methods

#### acknowledgeMessage

`acknowledgeMessage (queueEntry)`

Acknowledges a message ( qos = 1 or 2 only )

##### acknowledgeMessage Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `queueEntry` | `MessageQueueEntry` | entry of message queue acknowledged | |

#### checkToken

`checkToken (token) => {string, undefined}`

Checks , if a token corresponds to a client . Returns the client

##### checkToken Parameters

| Name | Type |
| ---------- | ------------ |
| `token` | `string` | |

##### checkToken throws

| Type | Description |
| ---- | ----------- |
| `string` | error message |

##### checkToken returns

| Type | Description |
| ---- | ----------- |
| `string, undefined` | clientId . |

#### cleanup

`cleanup () => {Object}`

Clean up jobs . Call regularly ( example once a minute ) Automatically disconnects clients not sending data until timeout

##### cleanup returns

| Type | Description |
| ---- | ----------- |
| `Object` | list of clientId beeing automatically disconnected . An object with reason : string and keepAlive : number is included for each disconnected clientId |

#### connect

`connect (clientId, host, port, clean, version, keepAlive) => {Object}`

Connects a client ( Stores a connection to a client )

##### connect Parameters

| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| `clientId` | `any` |  |  | id of the client | |
| `host` | `string` |  |  | host name | |
| `port` | `string` |  |  | port name | |
| `clean` | `bool` |  |  | true , if the connection will be cleaned on disconnect | |
| `version` | `string` | optional | '1 . 0' | client interface version | |
| `keepAlive` | `number` | optional | 3600000 | keep alive time in milliseconds . The broker will disconnect any client not sending messages in the keepAlive interval . Default is one hour | |

##### connect throws

| Type | Description |
| ---- | ----------- |
| `Error` |  |

##### connect returns

| Type | Description |
| ---- | ----------- |
| `Object` | { mqttcode , present } present = = 1 , if an old session is recovered |

#### disconnect

`disconnect (clientId)`

Disconnects a client

##### disconnect Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `clientId` | `string` | id of the client | |

##### disconnect throws

| Type | Description |
| ---- | ----------- |
| `Error` |  |

#### getAllMessagesToSend

`getAllMessagesToSend () => {integer}`

Gets all messages to send for all clients function to be called for each message . The function has the parameter QoS and message

##### getAllMessagesToSend returns

| Type | Description |
| ---- | ----------- |
| `integer` | amount of messages sent |

#### isDuplicateQos2message

`isDuplicateQos2message (clientId, topic, qos, dup, packetid)`

Checks and remembers a qos2 message . Returns true , if the message is a duplicate

##### isDuplicateQos2message Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `clientId` | `string` | id of the client | |
| `topic` | `string` | topic sent | |
| `qos` | `Object` | quality of service received from published headers | |
| `dup` | `boolean` | true , if the message is a duplicate | |
| `packetid` | `string` | id of the sender package | |

#### onPubrel

`onPubrel (token, packetid)`

Handles a pubrel message , deleting the entry from the qos2 queue

##### onPubrel Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `token` | `string` | client send token | |
| `packetid` | `number` | unique identifier of the corresponding publish packet | |

#### persist

`async persist ()`

Persists all connections

#### publishMessage

`publishMessage (payload, qos, retain)`

Adds messages that needs to be published

##### publishMessage Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `payload` | `Message` | message to add to a publish storage | |
| `qos` | `Object` | quality of service received from published headers | |
| `retain` | `boolean` | true , if the message should be retained for future subscribers | |

#### restoreFromFile

`async restoreFromFile ()`

Restores all clients from last valid saved file

#### setWill

`setWill (clientId, will)`

Sets the will message of a client

##### setWill Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `clientId` | `string` | id of the client | |
| `will` | `Object` | will message configuration | |

#### will properties

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `qos` | `number` | will message quality of service | |
| `retain` | `boolean` | true , if the will message is retained | |
| `topic` | `string` | topic of the will message | |
| `value` | `string, number` | value of the will message | |

#### subscribe

`subscribe (clientId, topics) => {Array.<number>}`

Subscribes to topics

##### subscribe Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `clientId` | `string` | id of the client | |
| `topics` | `Object` | { QoS , topics } or { topic1 : QoS1 , topic2 : QoS2 , . . . } | |

##### subscribe returns

| Type | Description |
| ---- | ----------- |
| `Array.<number>` | array of QoS values |

#### unsubscribe

`unsubscribe (clientId, topics)`

Unsubsribes to topics

##### unsubscribe Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `clientId` | `string` | id of the client | |
| `topics` | `Array.<string>` | array of topics strings ( topic with wildchars '+' and '#' ) | |

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