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
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


<a name="example-react-native"></a>

### 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)

Expand Down Expand Up @@ -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`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is enough, no need to also add an example

Copy link
Author

@mjcross mjcross Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - although as a user, I can say examples are incredibly valuable (and it also illustrates how to make a WSS connection, which gives a 'self contained' example for how to connect from a browser)

- `options` is the options to publish with, including:
- `qos` QoS level, `Number`, default `0`
- `retain` retain flag, `Boolean`, default `false`
Expand Down
1 change: 1 addition & 0 deletions examples/client/simple-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ const mqtt = require('../..')
const client = mqtt.connect()

client.publish('presence', 'hello!')

client.end()