Install/Mosquitto Mqtt Broker: Difference between revisions

From Playful Exploration
Jump to navigation Jump to search
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 14: Line 14:


== Enable User Authentication ==
== Enable User Authentication ==
Without this anyone can connect without authentication.
  nano /etc/mosquitto/conf.d/auth.conf
  nano /etc/mosquitto/conf.d/auth.conf


Add:
Add (leave acl_file out to allow all users access to all mqtt topics):
  allow_anonymous false
  allow_anonymous false
  password_file /etc/mosquitto/passwd
  password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl
Add a user and password or change the existing password:
mosquitto_passwd /etc/mosquitto/passwd fred && service mosquitto restart
Or delete a user (fred):
mosquitto_passwd -D /etc/mosquitto/passwd fred && service mosquitto restart


Create an encrypted password (see different ways [http://unix.stackexchange.com/questions/52108/how-to-create-sha512-password-hashes-on-command-line here])
doveadm pw -s SHA512-CRYPT
{SHA512-CRYPT}$6$4ETbaj7n.....
Copy everything after the {SHA512-CRYPT} part.


Create the password file:
Create/edit the acl file:
  nano /etc/mosquitto/passwd
  nano /etc/mosquitto/acl


Add your new user (fred), one user per line:
Add access rights for the new user (see [http://mm011106.github.io/reference/mosquitto_conf.html acl_file option]):
  fred:$6$4ETbaj7n.....
  user fred
topic read #
###################################
pattern /usr/%u/#
pattern /usr/%u/
pattern read /pub/#
pattern read /pub/
pattern /pub/%u/#
pattern /pub/%u/


Restart the broker service:
Restart the broker service:
  service mosquitto stop
  service mosquitto restart
service mosquitto start
  tail /var/log/mosquitto/mosquitto.log # ensure no errors
  tail /var/log/mosquitto/mosquitto.log # ensure no errors
Try it out.  Terminal A listens to all messages:
mosquitto_sub -u fred -P password -v -t "#"
Terminal B send a message:
mosquitto_pub -u fred -P password -t "/usr/fred/a/b/c/d" -m "test message"
mosquitto_pub -u fred -P password -t "/usr/nope/a/b/c/d" -m "this message never arrives"


== Enable Websocket ==
== Enable Websocket ==
Line 41: Line 60:
  nano /etc/mosquitto/conf.d/ws.conf
  nano /etc/mosquitto/conf.d/ws.conf


For secure websocket (wss) add this:
For secure websocket (wss) add this (I wasn't successful having nginx securing the ws connection, so this here worked):
  listener 9001
  listener 9001
  protocol websockets
  protocol websockets

Latest revision as of 04:38, 25 May 2016

See http://mosquitto.org

Enable Mqtt

nano /etc/mosquitto/conf.d/mqtt.conf

Add:

listener 1883
protocol mqtt

Restart the broker service:

service mosquitto stop
service mosquitto start
tail /var/log/mosquitto/mosquitto.log # ensure no errors

Enable User Authentication

Without this anyone can connect without authentication.

nano /etc/mosquitto/conf.d/auth.conf

Add (leave acl_file out to allow all users access to all mqtt topics):

allow_anonymous false
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl

Add a user and password or change the existing password:

mosquitto_passwd /etc/mosquitto/passwd fred && service mosquitto restart

Or delete a user (fred):

mosquitto_passwd -D /etc/mosquitto/passwd fred && service mosquitto restart


Create/edit the acl file:

nano /etc/mosquitto/acl

Add access rights for the new user (see acl_file option):

user fred
topic read #

###################################
pattern /usr/%u/#
pattern /usr/%u/
pattern read /pub/#
pattern read /pub/
pattern /pub/%u/#
pattern /pub/%u/

Restart the broker service:

service mosquitto restart
tail /var/log/mosquitto/mosquitto.log # ensure no errors

Try it out. Terminal A listens to all messages:

mosquitto_sub -u fred -P password -v -t "#"

Terminal B send a message:

mosquitto_pub -u fred -P password -t "/usr/fred/a/b/c/d" -m "test message"
mosquitto_pub -u fred -P password -t "/usr/nope/a/b/c/d" -m "this message never arrives"

Enable Websocket

See secure websocket

nano /etc/mosquitto/conf.d/ws.conf

For secure websocket (wss) add this (I wasn't successful having nginx securing the ws connection, so this here worked):

listener 9001
protocol websockets
certfile /etc/letsencrypt/live/playfulexploration.com/cert.pem
cafile /etc/letsencrypt/live/playfulexploration.com/chain.pem
keyfile /etc/letsencrypt/live/playfulexploration.com/privkey.pem

Alternatively for unsecure websocket (ws) omit the cert- and keyfiles:

listener 9001
protocol websockets

Restart the broker service:

service mosquitto stop
service mosquitto start
tail /var/log/mosquitto/mosquitto.log # ensure no errors

On the wappage's javascript use this to connect:

wss://playfulexploration.com:9001

or

ws://playfulexploration.com:9001