Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-debian-9
- http://www.steves-internet-guide.com/topic-restriction-mosquitto-configuration/
- =========
- MOSQUITTO
- =========
- Install
- -------
- $ sudo apt update
- $ sudo apt install mosquitto mosquitto-clients
- --------------------------
- Configure password and SSL
- --------------------------
- $ sudo mosquitto_passwd -c /etc/mosquitto/passwd sammy
- $ sudo nano /etc/mosquitto/conf.d/default.conf
- allow_anonymous false
- password_file /etc/mosquitto/passwd
- acl_file /etc/mosquitto/aclfile
- listener 1883 localhost
- listener 8883
- certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
- cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
- keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
- $ sudo nano /etc/mosquitto/aclfile
- #anonymous user
- topic read $SYS/#
- #user sammy (#=any topic)
- user sammy
- topic readwrite #
- #all client (%c = client id, %u = user)
- pattern readwrite %u/#
- $ sudo ufw allow 8883
- $ sudo systemctl restart mosquitto
- $ mosquitto_sub -h mqtt.example.com -p 8883 --capath /etc/ssl/certs/ -t "test/topic" -u "sammy" -P "password"
- $ mosquitto_pub -h mqtt.example.com -p 8883 --capath /etc/ssl/certs/ -t "test/topic" -m "hello world" -u "sammy" -P "password"
- https://stackoverflow.com/questions/62299097/nodejs-connect-to-mqtt-server-with-credentials-and-ssl
- ------
- NodeJS
- ------
- $ npm install mqtt --save
- const mqtt = require( 'mqtt' )
- const fs = require( 'fs' )
- const client = new mqtt.connect( 'mqtts://' + SERVERNAME, {
- rejectUnauthorized: false,
- username: SERVERUSERNAME,
- password: SERVERPASSWORD,
- // port: 8883
- connectTimeout: 5000,
- ca: [ fs.readFileSync( MYCAFILENAME ) ],
- cert: fs.readFileSync( MYCERTFILENAME ),
- key: fs.readFileSync( MYKEYFILENAME )
- } )
- client.on( 'connect', () => {
- console.log( 'connected' )
- client.subscribe( TOPIC, ( err ) => {
- console.log( 'subscribed' )
- if ( ! err ) {
- client.publish( TOPIC, "hello world" )
- }
- } )
- } )
- client.on( 'message', function( topic, msg ){
- console.log( msg.toString() )
- client.end()
- } )
Add Comment
Please, Sign In to add comment