Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge with main #14

Merged
merged 4 commits into from
Sep 1, 2023
Merged
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
7 changes: 4 additions & 3 deletions .github/workflows/npm-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
types: [opened, reopened]
branches:
- 'master'
- 'main'
- 'dev'

workflow_dispatch:

Expand All @@ -24,6 +26,5 @@ jobs:
env:
CI: true
- run: npm ci
- run: npm run build --if-present
- run: npm run test --if-present

- run: npm run build
- run: npm run test
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"bracketSpacing": true,
"printWidth": 90,
"singleQuote": true,
"arrowParens": "always",
"semi": true,
"tabWidth": 4,
"trailingComma": "none"
}
68 changes: 42 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# mqtt-router [![npm (scoped)](https://img.shields.io/npm/v/@pera-swarm/mqtt-router.svg)](https://github.com/Pera-Swarm/mqtt-router/) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Pera-Swarm/mqtt-router/%F0%9F%9A%80%20Release)](https://github.com/Pera-Swarm/mqtt-router/releases) [![GitHub issues](https://img.shields.io/github/issues/Pera-Swarm/mqtt-router)](https://github.com/Pera-Swarm/mqtt-router/issues)
[![npm (scoped)](https://img.shields.io/npm/v/@pera-swarm/mqtt-router.svg)](https://github.com/Pera-Swarm/mqtt-router/) [![License: LGPL v3](https://img.shields.io/badge/License-LGPL_v2.1-blue.svg)](https://www.gnu.org/licenses/lgpl-2.1)
# mqtt-router

An easy-to-use and flexible routing library for MQTT.

## Overview
@pera-swarm/mqtt-router is a library for handling MQTT publish/subscribe capabilities with a straight forward routing architecture.

This is a [Node.js](https://nodejs.org/en/) library available on both npm registry and GitHub package registry.

### Usage

#### 1. Installation
Installation done using `npm install` command:
```
$ npm i --save @pera-swarm/mqtt-router

```bash
npm i --save @pera-swarm/mqtt-router
```

Also, you need to install [`mqtt`](https://www.npmjs.com/package/mqtt) library as well.

```bash
npm i --save mqtt
```
$ npm i --save mqtt
```

#### 2. Set up routes
#### 2. Set up Routes

Create routes for subscribe and publish:

##### routes.js
```

```js
// Sample dynamic route list with handler functions
const SAMPLE_ROUTES = [
{
Expand All @@ -40,10 +47,13 @@ const SAMPLE_ROUTES = [
module.exports = SAMPLE_ROUTES;
```

#### 3. Start the router
#### 3. Start the Router

You should configure your own mqttOptions according to your mqtt broker and application settings.

##### index.js
```

```js
// Configure mqttClient
const mqttClient = require('mqtt');
const mqttOptions = {
Expand Down Expand Up @@ -91,8 +101,8 @@ specification and then if the subscriber picked up a message for the associated
You can also wrap the routes using `wrapper` function to include additional higher level attribute to the handler function as well.

##### index.js
```

```js
// Import MQTTRouter and wrapper from mqtt-router
const { MQTTRouter, wrapper } = require('@pera-swarm/mqtt-router');
const routes = require('./routes');
Expand Down Expand Up @@ -130,7 +140,8 @@ router.start();
```

##### routes.js
```

```js
// Sample dynamic route list with handler functions.
// sampleAttrib will be added to the handler function as the second parameter.
const SAMPLE_ROUTES = [
Expand Down Expand Up @@ -161,8 +172,9 @@ module.exports = SAMPLE_ROUTES;
#### 1. Install dependencies

Install project dependencies.
```
$ npm install

```bash
npm install
```

#### 2. Testing
Expand All @@ -171,19 +183,21 @@ $ npm install
and `MQTT_CLIENT`. Please refer `sample.nodemon.json` file for nodemon environment variable configuration.

Manually run the test cases.
```
$ node test/index.js

```bash
node test/index.js
```

or you can use nodemon script once environment variables configured correctly.

```
$ npm run client
```bash
npm run client
```

<hr />
<hr/>

## Documentation

* <a href="#route"><code><b>Route</b></code></a>
* <a href="#mqttrouter"><code><b>MQTTRouter</b></code></a>
* <a href="#mqttrouter-start"><code>mqttRouter.<b>start()</b></code></a>
Expand All @@ -209,39 +223,33 @@ $ npm run client
A route definition for handling route subscription logic. Following are the properties supported on the `Route` definition:

- `topic: string`

The Route topic

- `type: 'String' | 'JSON'`

Payload type (default:String)

- `allowRetained: boolean`

Retain allowance

- `subscribe: boolean`

Subscribe flag

- `publish: boolean`

Publish flag

- `handler: Function`

The default subscribe handler function, called when subscribe:true, packet.retain:true|false and allowRetained:true.
Retained messages and new messages will be handled by default.

- `fallbackRetainHandler?: Function`

Subscribe handler function only for retained messages, but for route specific custom logic. called when subscribe:true,
packet.retain:true and allowRetained:false.
> Note: If specified, `fallbackRetainHandler` function will be called. If not specified, retained messages will be discarded.

<hr />

<a name="mqttrouter"></a>

### MQTTRouter (mqttConnection, routes, options, setup, onError)

The main entrypoint of [mqtt-router](https://github.com/Pera-Swarm/mqtt-router/) that defines the router logic.
Expand All @@ -256,6 +264,7 @@ Parameters supported in the constructor:
<hr />

<a name="mqttrouter-start"></a>

### mqttRouter.start()

The method for starting the mqtt router.
Expand All @@ -264,6 +273,7 @@ The method for starting the mqtt router.
<hr />

<a name="mqttrouter-pushToPublishQueue"></a>

### mqttRouter.pushToPublishQueue(topic, data)

Add a message to the [publish queue](https://github.com/Pera-Swarm/mqtt-router/blob/master/README.md/#queue) that to be
Expand All @@ -284,6 +294,7 @@ Parameter supported:
<hr />

<a name="mqttrouter-removeRoute"></a>

### mqttRouter.removeRoute(topic)

Remove a route from the subscriber routes list.
Expand All @@ -293,6 +304,7 @@ Parameter supported:
<hr />

<a name="wrapper"></a>

### wrapper(routes, property)

Wrap an array of `Route` objects with a higher order property (ex: property can be`this` from the callee class) or
Expand All @@ -304,6 +316,7 @@ Parameters supported:
<hr />

<a name="queue"></a>

### Queue

A Queue implementation for the `mqtt-router` with a scheduler that acts as a "Publish Queue".
Expand All @@ -315,6 +328,7 @@ Parameters supported in the constructor:
<hr />

<a name="queue-begin"></a>

### queue.begin()

Begin the queue processing (scheduler).
Expand Down Expand Up @@ -393,7 +407,9 @@ Parameter supported:
<hr />

### To-Do
- [ ] Fix duplicate topic support for routing.

- Fix duplicate topic support for routing.

### Licence

This project is licensed under [LGPL-2.1 Licence](https://github.com/Pera-Swarm/mqtt-router/blob/main/LICENSE).
Loading
Loading