Skip to content

Commit 9d3a717

Browse files
committed
docs(binding-mqtt): add selfHost documentation for MQTT broker
Add comprehensive documentation for the selfHost feature that allows hosting an embedded MQTT broker within node-wot applications. Includes examples for basic setup, authentication, and TLS configuration. Closes #876 Signed-off-by: jona42-ui <jonathanthembo123@gmail.com>
1 parent fcaa652 commit 9d3a717

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

packages/binding-mqtt/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,65 @@ try {
176176
}
177177
```
178178

179+
### Hosting your own MQTT Broker
180+
181+
Instead of connecting to an external MQTT broker, you can host your own MQTT broker within your node-wot application by setting the `selfHost` option to `true`. This is useful for testing, development, or when you want to run a self-contained application.
182+
183+
```js
184+
const { Servient } = require("@node-wot/core");
185+
const { MqttBrokerServer } = require("@node-wot/binding-mqtt");
186+
187+
// create Servient with self-hosted MQTT broker
188+
const servient = new Servient();
189+
servient.addServer(new MqttBrokerServer({ uri: "mqtt://localhost:1883", selfHost: true }));
190+
191+
servient.start().then((WoT) => {
192+
// Your Things will now use the self-hosted broker
193+
WoT.produce({
194+
title: "My Thing",
195+
// ... rest of Thing Description
196+
}).then((thing) => {
197+
thing.expose();
198+
});
199+
});
200+
```
201+
202+
#### Self-Hosted Broker with Authentication
203+
204+
You can also add authentication to your self-hosted broker by providing credentials:
205+
206+
```js
207+
const servient = new Servient();
208+
servient.addServer(
209+
new MqttBrokerServer({
210+
uri: "mqtt://localhost:1883",
211+
selfHost: true,
212+
selfHostAuthentication: [
213+
{ username: "user1", password: "password1" },
214+
{ username: "user2", password: "password2" },
215+
],
216+
})
217+
);
218+
```
219+
220+
#### Self-Hosted Broker with TLS
221+
222+
For secure communication, you can enable TLS on your self-hosted broker:
223+
224+
```js
225+
const fs = require("fs");
226+
227+
const servient = new Servient();
228+
servient.addServer(
229+
new MqttBrokerServer({
230+
uri: "mqtts://localhost:8883",
231+
selfHost: true,
232+
key: fs.readFileSync("path/to/private-key.pem"),
233+
cert: fs.readFileSync("path/to/certificate.pem"),
234+
})
235+
);
236+
```
237+
179238
### More Examples
180239

181240
There are example implementations provided in the [example/scripting folder](https://github.com/eclipse-thingweb/node-wot/tree/master/examples/scripts).

0 commit comments

Comments
 (0)