Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## 2.0.0
- Added AWS support (thanks @willvincent)
- Started keeping changelog
- Upgraded NPM dependencies to today's versions
94 changes: 3 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,102 +16,14 @@ These are the features that I still want to developing in the near future.
- Check SSL support (currently untested)
- Improve tests and documentation

## Usage
## Installation

```bash
npm i --save adonis-mqtt
adonis install adonis-mqtt
```

To use adonis-mqtt, add the following to your providers:
See [instructions.md](instructions.md) for configuration and usage.

```js

// Add the mqtt provider
const providers = [
// ...
'adonis-mqtt/providers/MqttProvider',
]

// ...
// Add the command provider
const aceProviders = [
// ...
'adonis-mqtt/providers/CommandsProvider',
]
```

Then we'll have to `use` Mqtt somewhere so it gets initialized. I prefer it in the `app.events.js` file, since all event-related stuff is defined there.
This is only needed if you do not use it anywhere else (for example when sending messages).

```js
const Event = use('Event');
const Mqtt = use('Mqtt');

// Listen to some Events of the library
Event.on('MQTT:Connected', 'Message.connected')
Event.on('MQTT:Disconnected', 'Message.disconnected')
```

Lastly we should add some configuration to the `.env` file so MQTT knows where and how to connect

```
MQTT_HOST=yourmqtthost.com
MQTT_PORT=10444
MQTT_USERNAME=username123
MQTT_PASSWORD=password123#
```

Now adonis-mqtt is ready for use.

## Sending messages

Sending messages through your MQTT server is very easy.

```js
const Mqtt = use('Mqtt');

class SomeController {
async index() {
await Mqtt.sendMessage('mytopic', 'My Message', {qos: 1})
}
}

```

## Listeners
You can define multiple MqttListeners, which the provider will automatically pick up on boot.

You can generate a listener by using the adonis cli:

```bash
adonis make:mqtt-listener Test
```

This command will generate a listener class called TestMqttListener.

MqttListeners should are defined in the `app/MqttListeners` folder and should have the following base content:

```js
const MqttListener = use('MqttListener')

class MockListener extends MqttListener {
/*
* Subscription string. Uses the MQTT wildcard format.
*/
get subscription () {
return 'my/+/example/mqtt/string/#'
}

/**
* Message handler is passed the String data of the message and the matched wildcard values
*/
async handleMessage (message, wildcardMatches) {
}
}

module.exports = MockListener

```

## Contributing
You are free to contribute, make pull requests and create issues.
Expand Down
21 changes: 21 additions & 0 deletions instructions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const path = require('path')
const fs = require('fs')

module.exports = async function (cli) {
try {
await cli.makeConfig('mqtt.js', path.join(__dirname, './templates/config.js'))
cli.command.completed('create', 'config/mqtt.js')


let dir = path.join(cli.appDir, './MqttListeners');
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
cli.command.completed('created directory', 'app/MqttListeners')
}
} catch (error) {
console.log(error)
// ignore errors
}
}
91 changes: 91 additions & 0 deletions instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Configuration

## Registering provider
```js

// Add the mqtt provider
const providers = [
// ...
'adonis-mqtt/providers/MqttProvider',
]

// ...
// Add the command provider
const aceProviders = [
// ...
'adonis-mqtt/providers/CommandsProvider',
]
```

Add your configuration to the `.env` file, and/or the config/mqtt.js file so MQTT knows where and how to connect

```
MQTT_HOST=yourmqtthost.com
MQTT_PORT=10444
MQTT_USERNAME=username123
MQTT_PASSWORD=password123#
```

# Usage

We have to `use` Mqtt somewhere so it gets initialized. I prefer it in the `app.events.js` file, since all event-related stuff is defined there.
This is only needed if you do not use it anywhere else (for example when sending messages).

```js
const Event = use('Event');
const Mqtt = use('Mqtt');

// Listen to some Events of the library
Event.on('MQTT:Connected', 'Message.connected')
Event.on('MQTT:Disconnected', 'Message.disconnected')
```

## Sending messages

Sending messages through your MQTT server is very easy.

```js
const Mqtt = use('Mqtt');

class SomeController {
async index() {
await Mqtt.sendMessage('mytopic', 'My Message', {qos: 1})
}
}

```

## Listeners
You can define multiple MqttListeners, which the provider will automatically pick up on boot.

You can generate a listener by using the adonis cli:

```bash
adonis make:mqtt-listener Test
```

This command will generate a listener class called TestMqttListener.

MqttListeners should are defined in the `app/MqttListeners` folder and should have the following base content:

```js
const MqttListener = use('MqttListener')

class MockListener extends MqttListener {
/*
* Subscription string. Uses the MQTT wildcard format.
*/
get subscription () {
return 'my/+/example/mqtt/string/#'
}

/**
* Message handler is passed the String data of the message and the matched wildcard values
*/
async handleMessage (message, wildcardMatches) {
}
}

module.exports = MockListener

```
Loading