Skip to content

Commit

Permalink
Merge 5844d96 into 8382a32
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored Jul 19, 2023
2 parents 8382a32 + 5844d96 commit 1bdf0cc
Show file tree
Hide file tree
Showing 60 changed files with 10,540 additions and 10,712 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/types/
/examples/
/doc/
/dist/
/test/typescript/
33 changes: 33 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true,
node: true,
mocha: true,
},
extends: ['airbnb-base', 'plugin:prettier/recommended'],
parserOptions: {
ecmaVersion: 'latest',
},
rules: {
'global-require': 'off',
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-unused-vars': 'off',
'no-underscore-dangle': 'off',
'no-param-reassign': 'off',
'no-restricted-syntax': 'off',
camelcase: 'off',
'default-case': 'off',
'consistent-return': 'off',
'import/order': 'off',
'max-classes-per-file': 'off',
'no-plusplus': 'off',
'guard-for-in': 'off',
'no-bitwise': 'off',
'class-methods-use-this': 'off',
'no-continue': 'off',
'prefer-destructuring': 'off',
'no-use-before-define': 'off',
},
}
16 changes: 11 additions & 5 deletions .github/workflows/mqttjs-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,33 @@ jobs:
fail-fast: false

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- name: Install Dependencies
run: npm ci

- name: Lint
if: matrix.node-version == '20.x'
# only run on latest node version, no reason to run on all
run: |
npm run lint
- name: Test NodeJS
run: npm run test:node
run: npm run test:node # npx mocha test/unique_message_id_provider_client.js --exit
env:
CI: true
DEBUG: "mqttjs"
DEBUG: "${{ runner.debug == '1' && 'mqttjs:*' || '' }}"

- name: Test Typescript
run: npm run test:typescript
env:
CI: true
DEBUG: "mqttjs"
DEBUG: "${{ runner.debug == '1' && 'mqttjs:*' || '' }}"

- name: Test Browser
if: matrix.node-version == '20.x'
Expand Down
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.md
README.md
/types/
/examples/
/doc/
/dist/
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
semi: false,
singleQuote: true,
useTabs: true,
tabWidth: 4,
endOfLine: "lf",
};
10 changes: 7 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"standard.enable": true,
"standard.autoFixOnSave": true,
"editor.defaultFormatter": "standard.vscode-standard"
"eslint.format.enable": true,
"eslint.lintTask.enable": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.fixAll.markdownlint": true
},
}
62 changes: 36 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
MQTT.js is a client library for the [MQTT](http://mqtt.org/) protocol, written
in JavaScript for node.js and the browser.

> MQTT [5.0.0-beta.0](https://github.com/mqttjs/MQTT.js/releases/tag/v5.0.0-beta.0) is now available! Try it out and give us feedback! `npm i mqtt@5.0.0-beta.0`
> MQTT [5.0.0 BETA](https://www.npmjs.com/package/mqtt/v/beta) is now available! Try it out and give us [feedback](https://github.com/mqttjs/MQTT.js/issues/1639): `npm i mqtt@beta`
## Table of Contents

Expand Down Expand Up @@ -101,15 +101,15 @@ For the sake of simplicity, let's put the subscriber and the publisher in the sa
const mqtt = require("mqtt");
const client = mqtt.connect("mqtt://test.mosquitto.org");

client.on("connect", function () {
client.subscribe("presence", function (err) {
client.on("connect", () => {
client.subscribe("presence", (err) => {
if (!err) {
client.publish("presence", "Hello mqtt");
}
});
});

client.on("message", function (topic, message) {
client.on("message", (topic, message) => {
// message is Buffer
console.log(message.toString());
client.end();
Expand Down Expand Up @@ -305,23 +305,24 @@ Also user can manually register topic-alias pair using PUBLISH topic:'some', ta:

## API

- <a href="#connect"><code>mqtt.<b>connect()</b></code></a>
- <a href="#client"><code>mqtt.<b>Client()</b></code></a>
- <a href="#publish"><code>mqtt.Client#<b>publish()</b></code></a>
- <a href="#subscribe"><code>mqtt.Client#<b>subscribe()</b></code></a>
- <a href="#unsubscribe"><code>mqtt.Client#<b>unsubscribe()</b></code></a>
- <a href="#end"><code>mqtt.Client#<b>end()</b></code></a>
- <a href="#removeOutgoingMessage"><code>mqtt.Client#<b>removeOutgoingMessage()</b></code></a>
- <a href="#reconnect"><code>mqtt.Client#<b>reconnect()</b></code></a>
- <a href="#handleMessage"><code>mqtt.Client#<b>handleMessage()</b></code></a>
- <a href="#connected"><code>mqtt.Client#<b>connected</b></code></a>
- <a href="#reconnecting"><code>mqtt.Client#<b>reconnecting</b></code></a>
- <a href="#getLastMessageId"><code>mqtt.Client#<b>getLastMessageId()</b></code></a>
- <a href="#store"><code>mqtt.<b>Store()</b></code></a>
- <a href="#put"><code>mqtt.Store#<b>put()</b></code></a>
- <a href="#del"><code>mqtt.Store#<b>del()</b></code></a>
- <a href="#createStream"><code>mqtt.Store#<b>createStream()</b></code></a>
- <a href="#close"><code>mqtt.Store#<b>close()</b></code></a>
- [`mqtt.connect()`](#connect)
- [`mqtt.Client()`](#client)
- [`mqtt.Client#connect()`](#client-connect)
- [`mqtt.Client#publish()`](#publish)
- [`mqtt.Client#subscribe()`](#subscribe)
- [`mqtt.Client#unsubscribe()`](#unsubscribe)
- [`mqtt.Client#end()`](#end)
- [`mqtt.Client#removeOutgoingMessage()`](#removeOutgoingMessage)
- [`mqtt.Client#reconnect()`](#reconnect)
- [`mqtt.Client#handleMessage()`](#handleMessage)
- [`mqtt.Client#connected`](#connected)
- [`mqtt.Client#reconnecting`](#reconnecting)
- [`mqtt.Client#getLastMessageId()`](#getLastMessageId)
- [`mqtt.Store()`](#store)
- [`mqtt.Store#put()`](#put)
- [`mqtt.Store#del()`](#del)
- [`mqtt.Store#createStream()`](#createStream)
- [`mqtt.Store#close()`](#close)

---

Expand Down Expand Up @@ -368,7 +369,7 @@ The arguments are:
the `connect` event. Typically a `net.Socket`.
- `options` is the client connection options (see: the [connect packet](https://github.com/mcollina/mqtt-packet#connect)). Defaults:
- `wsOptions`: is the WebSocket connection options. Default is `{}`.
It's specific for WebSockets. For possible options have a look at: https://github.com/websockets/ws/blob/master/doc/ws.md.
It's specific for WebSockets. For possible options have a look at: <https://github.com/websockets/ws/blob/master/doc/ws.md>.
- `keepalive`: `60` seconds, set to `0` to disable
- `reschedulePings`: reschedule ping messages after sending packets (default `true`)
- `clientId`: `'mqttjs_' + Math.random().toString(16).substr(2, 8)`
Expand All @@ -386,9 +387,11 @@ The arguments are:
- `outgoingStore`: a [Store](#store) for the outgoing packets
- `queueQoSZero`: if connection is broken, queue outgoing QoS zero messages (default `true`)
- `customHandleAcks`: MQTT 5 feature of custom handling puback and pubrec packets. Its callback:

```js
customHandleAcks: function(topic, message, packet, done) {/*some logic wit colling done(error, reasonCode)*/}
```

- `autoUseTopicAlias`: enabling automatic Topic Alias using functionality
- `autoAssignTopicAlias`: enabling automatic Topic Alias assign functionality
- `properties`: properties MQTT 5.0.
Expand Down Expand Up @@ -424,6 +427,7 @@ The arguments are:
subscribed topics are automatically subscribed again (default `true`)
- `messageIdProvider`: custom messageId provider. when `new UniqueMessageIdProvider()` is set, then non conflict messageId is provided.
- `log`: custom log function. Default uses [debug](https://www.npmjs.com/package/debug) package.
- `manualConnect`: prevents the constructor to call `connect`. In this case after the `mqtt.connect` is called you should call `client.connect` manually.

In case mqtts (mqtt over tls) is required, the `options` object is
passed through to
Expand Down Expand Up @@ -497,7 +501,7 @@ The following TLS errors will be emitted as an `error` event:

`function () {}`

Emitted when <a href="#end"><code>mqtt.Client#<b>end()</b></code></a> is called.
Emitted when [`mqtt.Client#end()`](#end) is called.
If a callback was passed to `mqtt.Client#end()`, this event is emitted once the
callback returns.

Expand Down Expand Up @@ -535,6 +539,12 @@ and connections

---

<a name="client-connect"></a>

### mqtt.Client#connect()

By default client connects when constructor is called. To prevent this you can set `manualConnect` option to `true` and call `client.connect()` manually.

<a name="publish"></a>

### mqtt.Client#publish(topic, message, [options], [callback])
Expand Down Expand Up @@ -746,9 +756,9 @@ Closes the Store.

### Via CDN

The MQTT.js bundle is available through http://unpkg.com, specifically
at https://unpkg.com/mqtt/dist/mqtt.min.js.
See http://unpkg.com for the full documentation on version ranges.
The MQTT.js bundle is available through <http://unpkg.com>, specifically
at <https://unpkg.com/mqtt/dist/mqtt.min.js>.
See <http://unpkg.com> for the full documentation on version ranges.

<a name="browserify"></a>

Expand Down
28 changes: 17 additions & 11 deletions benchmarks/bombing.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
#! /usr/bin/env node

const mqtt = require('../')
const client = mqtt.connect({ port: 1883, host: 'localhost', clean: true, keepalive: 0 })
const mqtt = require('..')

const client = mqtt.connect({
port: 1883,
host: 'localhost',
clean: true,
keepalive: 0,
})

let sent = 0
const interval = 5000

function count () {
console.log('sent/s', sent / interval * 1000)
sent = 0
function count() {
console.log('sent/s', (sent / interval) * 1000)
sent = 0
}

setInterval(count, interval)

function publish () {
sent++
client.publish('test', 'payload', publish)
function publish() {
sent++
client.publish('test', 'payload', publish)
}

client.on('connect', publish)

client.on('error', function () {
console.log('reconnect!')
client.stream.end()
client.on('error', () => {
console.log('reconnect!')
client.stream.end()
})
28 changes: 17 additions & 11 deletions benchmarks/throughputCounter.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#! /usr/bin/env node

const mqtt = require('../')
const mqtt = require('..')

const client = mqtt.connect({ port: 1883, host: 'localhost', clean: true, encoding: 'binary', keepalive: 0 })
const client = mqtt.connect({
port: 1883,
host: 'localhost',
clean: true,
encoding: 'binary',
keepalive: 0,
})
let counter = 0
const interval = 5000

function count () {
console.log('received/s', counter / interval * 1000)
counter = 0
function count() {
console.log('received/s', (counter / interval) * 1000)
counter = 0
}

setInterval(count, interval)

client.on('connect', function () {
count()
this.subscribe('test')
this.on('message', function () {
counter++
})
client.on('connect', () => {
count()
this.subscribe('test')
this.on('message', () => {
counter++
})
})
14 changes: 7 additions & 7 deletions bin/mqtt.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
'use strict'

/*
* Copyright (c) 2015-2015 MQTT.js contributors.
Expand All @@ -10,18 +9,19 @@
const path = require('path')
const commist = require('commist')()
const helpMe = require('help-me')({
dir: path.join(path.dirname(require.main.filename), '/../doc'),
ext: '.txt'
dir: path.join(path.dirname(require.main.filename), '/../doc'),
ext: '.txt',
})

commist.register('publish', require('./pub'))
commist.register('subscribe', require('./sub'))
commist.register('version', function () {
console.log('MQTT.js version:', require('./../package.json').version)

commist.register('version', () => {
console.log('MQTT.js version:', require('../package.json').version)
})
commist.register('help', helpMe.toStdout)

if (commist.parse(process.argv.slice(2)) !== null) {
console.log('No such command:', process.argv[2], '\n')
helpMe.toStdout()
console.log('No such command:', process.argv[2], '\n')
helpMe.toStdout()
}
Loading

0 comments on commit 1bdf0cc

Please sign in to comment.