Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mqtt

Package Overview
Dependencies
Maintainers
7
Versions
203
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mqtt - npm Package Compare versions

Comparing version 5.0.0 to 5.0.1

2

build/package.json
{
"name": "mqtt",
"description": "A library for the MQTT protocol",
"version": "5.0.0",
"version": "5.0.1",
"contributors": [

@@ -6,0 +6,0 @@ "Adam Rudd <adamvrr@gmail.com>",

@@ -6,3 +6,3 @@ /// <reference types="node" />

import { DuplexOptions } from 'readable-stream';
import Store from './store';
import Store, { IStore } from './store';
import { ClientOptions } from 'ws';

@@ -46,4 +46,4 @@ import { ClientRequestArgs } from 'http';

connectTimeout?: number;
incomingStore?: Store;
outgoingStore?: Store;
incomingStore?: IStore;
outgoingStore?: IStore;
queueQoSZero?: boolean;

@@ -132,4 +132,4 @@ log?: (...args: any[]) => void;

reconnecting: boolean;
incomingStore: Store;
outgoingStore: Store;
incomingStore: IStore;
outgoingStore: IStore;
options: IClientOptions;

@@ -136,0 +136,0 @@ queueQoSZero: boolean;

@@ -1052,3 +1052,3 @@ "use strict";

(this.options.clean ||
(this.options.protocolVersion === 5 &&
(this.options.protocolVersion >= 4 &&
!this.connackPacket.sessionPresent)) &&

@@ -1055,0 +1055,0 @@ _resubscribeTopicsKeys.length > 0) {

{
"name": "mqtt",
"description": "A library for the MQTT protocol",
"version": "5.0.0",
"version": "5.0.1",
"contributors": [

@@ -6,0 +6,0 @@ "Adam Rudd <adamvrr@gmail.com>",

@@ -13,7 +13,4 @@ # ![mqtt.js](https://raw.githubusercontent.com/mqttjs/MQTT.js/137ee0e3940c1f01049a30248c70f24dc6e6f829/MQTT.js.png)

> 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
- [**MQTT.js vNext**](#vnext)
- [Upgrade notes](#notes)

@@ -38,8 +35,2 @@ - [Installation](#install)

<a name="vnext"></a>
## Next major version of MQTT.js
There is work being done on the next generation of MQTT.js (vNext). We invite the community to provide their contributions [this repository](https://github.com/mqttjs/mqttjs-v5)
<a name="notes"></a>

@@ -49,3 +40,3 @@

**v5.0.0** (**BETA** 06/2023)
**v5.0.0** (07/2023)

@@ -128,3 +119,3 @@ - Removes support for all end of life node versions (v12 and v14), and now supports node v18 and v20.

```
```sh
Hello mqtt

@@ -142,4 +133,2 @@ ```

to use MQTT.js in the browser see the [browserify](#browserify) section
<a name="import_styles"></a>

@@ -172,8 +161,2 @@

<a name="promises"></a>
## Promise support
If you want to use the new [async-await](https://blog.risingstack.com/async-await-node-js-7-nightly/) functionality in JavaScript, or just prefer using Promises instead of callbacks, [async-mqtt](https://github.com/mqttjs/async-mqtt) is a wrapper over MQTT.js which uses promises instead of callbacks when possible.
<a name="cli"></a>

@@ -193,3 +176,3 @@

```
```sh
mqtt sub -t 'hello' -h 'test.mosquitto.org' -v

@@ -200,3 +183,3 @@ ```

```
```sh
mqtt pub -t 'hello' -h 'test.mosquitto.org' -m 'from MQTT.js'

@@ -216,3 +199,2 @@ ```

$env:DEBUG='mqttjs*'
```

@@ -244,3 +226,3 @@

```
```js
const transformWsUrl = (url, options, client) => {

@@ -288,3 +270,3 @@ client.options.username = `token=${this.get_current_auth_token()}`;

```
```bash
1. PUBLISH topic:'t1', ta:1 (register)

@@ -308,3 +290,3 @@ 2. PUBLISH topic:'t1' -> topic:'', ta:1 (auto use existing map entry)

```
```bash
The broker returns CONNACK (TopicAliasMaximum:3)

@@ -806,2 +788,8 @@ 1. PUBLISH topic:'t1' -> 't1', ta:1 (auto assign t1:1 and register)

MQTT.js is bundled using [browserify](http://browserify.org/). You can find the browser build in the `dist` folder.
```js
import mqtt from 'mqtt/dist/mqtt.min'
```
<a name="cdn"></a>

@@ -815,21 +803,2 @@

<a name="browserify"></a>
### Browserify
In order to use MQTT.js as a browserify module you can either require it in your browserify bundles or build it as a stand alone module. The exported module is AMD/CommonJs compatible and it will add an object in the global space.
```bash
mkdir tmpdir
cd tmpdir
npm install mqtt
npm install browserify
npm install tinyify
cd node_modules/mqtt/
npm install .
npx browserify mqtt.js -s mqtt >browserMqtt.js // use script tag
# show size for compressed browser transfer
gzip <browserMqtt.js | wc -c
```
**Be sure to only use this bundle with `ws` or `wss` URLs in the browser. Others URL types will likey fail**

@@ -841,76 +810,77 @@

Just like browserify, export MQTT.js as library. The exported module would be `const mqtt = xxx` and it will add an object in the global space. You could also export module in other [formats (AMD/CommonJS/others)](http://webpack.github.io/docs/configuration.html#output-librarytarget) by setting **output.libraryTarget** in webpack configuration.
If you are using webpack simply import MQTT.js as you would any other module.
```javascript
npm install -g webpack // install webpack
```js
import mqtt from 'mqtt'
cd node_modules/mqtt
npm install . // install dev dependencies
webpack mqtt.js ./browserMqtt.js --output-library mqtt
const client = mqtt.connect('ws://test.mosquitto.org:8080')
```
you can then use mqtt.js in the browser with the same api than node's one.
If you get errors when building your app with webpack v5, it's because starting from v5, webpack doesn't polyfill Node.js core modules anymore. You can fix this by adding the following to your webpack config:
```html
<html>
<head>
<title>test Ws mqtt.js</title>
</head>
<body>
<script src="./browserMqtt.js"></script>
<script>
const client = mqtt.connect(); // you add a ws:// url here
client.subscribe("mqtt/demo");
```js
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
client.on("message", function (topic, payload) {
alert([topic, payload].join(": "));
client.end();
});
client.publish("mqtt/demo", "hello world!");
</script>
</body>
</html>
module.exports = {
// Other rules...
plugins: [
new NodePolyfillPlugin()
]
}
```
### React
Otherwise just add the missing polyfills manually:
```
npm install -g webpack // Install webpack globally
npm install mqtt // Install MQTT library
cd node_modules/mqtt
npm install . // Install dev deps at current dir
webpack mqtt.js --output-library mqtt // Build
```js
// now you can import the library with ES6 import, commonJS not tested
module.exports = {
// Other rules...
resolve: {
fallback: {
"buffer": require.resolve("buffer/"),
"stream": require.resolve("stream-browserify"),
"process": require.resolve("process/browser"),
"path": require.resolve("path-browserify"),
"fs": false
}
}
}
```
```javascript
import React from 'react';
import mqtt from 'mqtt';
<a name="vite"></a>
export default () => {
const [connectionStatus, setConnectionStatus] = React.useState(false);
const [messages, setMessages] = React.useState([]);
### Vite
useEffect(() => {
const client = mqtt.connect(SOME_URL);
client.on('connect', () => setConnectionStatus(true));
client.on('message', (topic, payload, packet) => {
setMessages(messages.concat(payload.toString()));
});
}, []);
If you are using vite simply import MQTT.js as you would any other module. In order to use MQTT.js with vite, you need to add the following to your vite config:
return (
<>
{messages.map((message) => (
<h2>{message}</h2>
)
</>
)
}
```js
// other imports
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
export default defineConfig({
// Other rules...
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: 'globalThis'
},
// Enable esbuild polyfill plugins
plugins: [
NodeGlobalsPolyfillPlugin({
buffer: true
})
]
}
}
});
```
Your broker should accept websocket connection (see [MQTT over Websockets](https://github.com/moscajs/aedes/blob/master/docs/Examples.md#mqtt-server-over-websocket-using-server-factory) to setup [Aedes](https://github.com/moscajs/aedes)).
This requires the `@esbuild-plugins/node-globals-polyfill` package to be installed:
```bash
npm install --save-dev @esbuild-plugins/node-globals-polyfill
```
<a name="qos"></a>

@@ -932,22 +902,10 @@

This repo bundles TypeScript definition files for use in TypeScript projects and to support tools that can read `.d.ts` files.
Starting from v5 this project is written in TypeScript and the type definitions are included in the package.
### Pre-requisites
Example:
Before you can begin using these TypeScript definitions with your project, you need to make sure your project meets these requirements:
- TypeScript >= 2.1
- Set tsconfig.json: `{"compilerOptions" : {"moduleResolution" : "node"}, ...}`
- Includes the TypeScript definitions for Node and [ws](https://www.npmjs.com/package/ws). These types are used as
parameters to some of the MQTT client's APIs and if you don't install them they get treated as `any`, which means you lose type
safety.
Use npm to install them by typing the following into a terminal window:
`npm install --save-dev @types/node @types/ws`
### TypeScript example
```ts
import { connect } from "mqtt"
const client = connect('mqtt://test.mosquitto.org')
```
import * as mqtt from "mqtt"
let client : mqtt.MqttClient = mqtt.connect('mqtt://test.mosquitto.org')
```

@@ -954,0 +912,0 @@ <a name="weapp-alipay"></a>

@@ -26,3 +26,3 @@ /**

import _debug from 'debug'
import Store from './store'
import Store, { IStore } from './store'
import handlePacket from './handlers'

@@ -175,7 +175,7 @@ import { ClientOptions } from 'ws'

*/
incomingStore?: Store
incomingStore?: IStore
/**
* a Store for the outgoing packets
*/
outgoingStore?: Store
outgoingStore?: IStore

@@ -410,5 +410,5 @@ /** Enable/Disable queue for QoS 0 packets */

public incomingStore: Store
public incomingStore: IStore
public outgoingStore: Store
public outgoingStore: IStore

@@ -2111,4 +2111,7 @@ public options: IClientOptions

!this._firstConnection &&
// Only resubscribe in case of clean connection or if the server does not have a stored session.
// The Session Present flag is available since v3.1.1
// https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349254
(this.options.clean ||
(this.options.protocolVersion === 5 &&
(this.options.protocolVersion >= 4 &&
!this.connackPacket.sessionPresent)) &&

@@ -2115,0 +2118,0 @@ _resubscribeTopicsKeys.length > 0

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc