From 313171cc8e19aa80158306edf7fae3c514a84a61 Mon Sep 17 00:00:00 2001 From: mjcross Date: Fri, 7 Nov 2025 09:55:23 +0000 Subject: [PATCH 1/2] Add comment to simple-publish.js (binary msg) For a new user it can be confusing how to publish a binary message from the browser. --- examples/client/simple-publish.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/client/simple-publish.js b/examples/client/simple-publish.js index 24d232cb6..05602e6e8 100644 --- a/examples/client/simple-publish.js +++ b/examples/client/simple-publish.js @@ -4,4 +4,6 @@ const mqtt = require('../..') const client = mqtt.connect() client.publish('presence', 'hello!') +// note: for a binary message from the browser you can pass a Uint8Array + client.end() From 91f6e4613fd6131c221cb6dc798b156600368fda Mon Sep 17 00:00:00 2001 From: Martin Crossley Date: Sat, 8 Nov 2025 20:32:20 +0000 Subject: [PATCH 2/2] Revert change to example and propose edit to README.md Illustrates how to publish a binary message (from a browser) --- README.md | 36 +++++++++++++++++++++++++++++-- examples/client/simple-publish.js | 1 - 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d1f4d8027..bb0106449 100644 --- a/README.md +++ b/README.md @@ -119,9 +119,41 @@ output: Hello mqtt ``` +### Binary message +You can publish a binary message by passing a `Buffer` object (or in the browser a `Uint8Array`) to `client.publish()`. + +Here is a **browser** example that uses a secure websocket: + +```js +// browser JS +const mqtt = require("mqtt"); +const client = mqtt.connect("wss://test.mosquitto.org:8081"); + +client.on("connect", () => { + client.subscribe("presence", (err) => { + if (!err) { + client.publish("presence", new Uint8Array([0x01, 0x02, 0x03])); + } + }); +}); + +client.on("message", (topic, message) => { + // message inherits from Uint8Array + console.log(message.toHex()); + client.end(); +}); +``` + +output: + +```sh +010203 +``` + + -### React Native +## React Native MQTT.js can be used in React Native applications. To use it, see the [React Native example](https://github.com/MaximoLiberata/react-native-mqtt.js-example) @@ -614,7 +646,7 @@ By default client connects when constructor is called. To prevent this you can s Publish a message to a topic - `topic` is the topic to publish to, `String` -- `message` is the message to publish, `Buffer` or `String` +- `message` is the message to publish, `Buffer` (in browser `Uint8Array`) or `String` - `options` is the options to publish with, including: - `qos` QoS level, `Number`, default `0` - `retain` retain flag, `Boolean`, default `false` diff --git a/examples/client/simple-publish.js b/examples/client/simple-publish.js index 05602e6e8..ee428bb5f 100644 --- a/examples/client/simple-publish.js +++ b/examples/client/simple-publish.js @@ -4,6 +4,5 @@ const mqtt = require('../..') const client = mqtt.connect() client.publish('presence', 'hello!') -// note: for a binary message from the browser you can pass a Uint8Array client.end()