-
Notifications
You must be signed in to change notification settings - Fork 3
/
MosquittoNotes.txt
51 lines (38 loc) · 1.3 KB
/
MosquittoNotes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Mosquitto Notes
Overview
- "Mosquitto" is a name for the MQTT protocol.
- It supports publish and subscribe operations.
To install
- brew install mosquitto
To have the MQTT server automatically start on reboots
- ln -sfv /usr/local/opt/mosquitto/*.plist ~/Library/LaunchAgents
To initially start the MQTT server
- launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mosquitto.plist
- this starts a process using /usr/local/opt/mosquitto/sbin/mosquitto
To stop the server
- launchctl stop homebrew.mxcl.mosquitto
To restart the server
- launchctl start homebrew.mxcl.mosquitto
To subscribe to topic
- mosquitto_sub -t topic/state
- no quotes are needed around the -t value
To publish a message on a topic
- open a new terminal
- mosquitto_pub -t topic/state -m "Hello World"
- no quotes are needed around the -t value
To use from Node.js or browser JavaScript
- npm install mqtt
- example code:
const mqtt = require('mqtt');
const host = 'localhost';
const port = 1883;
const client = mqtt.connect('mqtt://' + host + ':' + port);
const topic = 'foo/bar';
client.on('connect', () => {
client.subscribe(topic);
client.publish(topic, 'Hello from MQTT!');
});
client.on('message', (topic, message) => {
console.log(message.toString()); // message is a Buffer
client.end(); // don't call to continue listening
});