forked from Nerixyz/mqtts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
20 lines (17 loc) · 688 Bytes
/
example.ts
File metadata and controls
20 lines (17 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { MqttClient, TcpTransport, MessageWithParams } from 'mqtts';
const client = new MqttClient({
// there are multiple transports for example WebSocket and TLS you may provide your own transport
transport: new TcpTransport({ host: 'broker.hivemq.com', port: 1883 }),
autoReconnect: true,
});
await client.connect();
// subscribe and listen to a topic with params
await client.listenSubscribe(
'/mqtts/test/:param',
async ({ params, payload }: MessageWithParams<{ param: string }>) => {
await client.publish({
topic: '/mqtts/pong',
payload: JSON.stringify({ param: params.param, data: payload.toString() }),
});
},
);