<commit_before># bus

**bus** provides a high-level way to work with MQTT. Under the hood it uses
the excellent [MQTT.js](https://github.com/mqttjs/MQTT.js) module.

This module is written using TypeScript. It is currently a work-in-progress and
as such is not published on npm (yet). This documentation will be updated
accordingly.

## Quick Example

> These examples assume you have downloaded the files directly or cloned this repo

### Instantiating, connecting and subscribing

```ts
import Bus from './bus'

// First argument is an (optional) context to bind `onLabel` and `onceLabel` to
const Bus: Bus = Bus.create(null, 'my-client-id', 'mqtt://localhost')

bus.connect().then(() => {
  // This function will be called each time `statusUpdate` is received
  // By not using an arrow function `bus` can bind it to the context given in
  // `Bus.create`
  this.onLabel('statusUpdate', function (packet) {
    console.log(`Device ${packet.params.deviceId} is now ${packet.payload}`)
  })

  // Create the label `statusUpdate` for the given topic
  return bus.subscribe('statusUpdate', 'devices/+deviceId/status')
})
```

### Publishing

```ts
// Add a topic pattern with the given label
// #keys at the end is a parameteraized wildcard
bus.addPattern('updateConfig', 'devices/my-device/config/#keys')

// This will send the word 'online' to devices/my-device/config/status
bus.publish('updateConfig', { keys: [ 'status' ] }, 'online')
```

## To Do

This list is not exhaustive and my change at any moment.

- Catch possible thrown error by `params` in `bus.publish()`
- Find a good name for `bus.onLabel` and `bus.onceLabel` and `bus.addPattern`
- Improve `bus.isAvailable()` so the correct statuses are taken into account
- Better handling of the mqtt.js `error` event in `bus._addEventListeners()`

## Done

- De-promisify `bus.addPattern()`

## Inspired by

- [MQTT.js](https://github.com/mqttjs/MQTT.js), which this module uses under the hood
- [mqtt-regex](https://github.com/RangerMauve/mqtt-regex), whose support for parameterized
topics is integrated

## License

Copyright 2017 [<NAME>](http://www.michielvdvelde.nl).

This software is licensed under the [MIT License](LICENSE).
<commit_msg>Fix example<commit_after># bus

**bus** provides a high-level way to work with MQTT. Under the hood it uses
the excellent [MQTT.js](https://github.com/mqttjs/MQTT.js) module.

This module is written using TypeScript. It is currently a work-in-progress and
as such is not published on npm (yet). This documentation will be updated
accordingly.

## Quick Example

> These examples assume you have downloaded the files directly or cloned this repo

### Instantiating, connecting and subscribing

```ts
import Bus from './bus'

// First argument is an (optional) context to bind `onLabel` and `onceLabel` to
const Bus: Bus = Bus.create(null, 'my-client-id', 'mqtt://localhost')

bus.connect().then(() => {
  // This function will be called each time `statusUpdate` is received
  // By not using an arrow function `bus` can bind it to the context given in
  // `Bus.create`
  bus.onLabel('statusUpdate', function (packet) {
    console.log(`Device ${packet.params.deviceId} is now ${packet.payload}`)
  })

  // Create the label `statusUpdate` for the given topic
  return bus.subscribe('statusUpdate', 'devices/+deviceId/status')
})
```

### Publishing

```ts
// Add a topic pattern with the given label
// #keys at the end is a parameteraized wildcard
bus.addPattern('updateConfig', 'devices/my-device/config/#keys')

// This will send the word 'online' to devices/my-device/config/status
bus.publish('updateConfig', { keys: [ 'status' ] }, 'online')
```

## To Do

This list is not exhaustive and my change at any moment.

- Catch possible thrown error by `params` in `bus.publish()`
- Find a good name for `bus.onLabel` and `bus.onceLabel` and `bus.addPattern`
- Improve `bus.isAvailable()` so the correct statuses are taken into account
- Better handling of the mqtt.js `error` event in `bus._addEventListeners()`

## Done

- De-promisify `bus.addPattern()`

## Inspired by

- [MQTT.js](https://github.com/mqttjs/MQTT.js), which this module uses under the hood
- [mqtt-regex](https://github.com/RangerMauve/mqtt-regex), whose support for parameterized
topics is integrated

## License

Copyright 2017 [<NAME>](http://www.michielvdvelde.nl).

This software is licensed under the [MIT License](LICENSE).

{'fraction_non_alphanumeric': 0.12707056954844564, 'fraction_numerical': 0.0018152938506920807, 'mean_word_length': 4.235154394299287, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '158678', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1303, 'n_words': 499}