Container Application Development with Go

MQTT Sample Application

This demo shows you how to set up a MQTT broker and a
MQTT client application written in Go, running in a container.

As a MQTT broker you can choose between a container or a Ubuntu linux server running the Mosquitto MQTT broker service.

For the MQTT connection the application uses the Paho Go Client.

Use these MQTT settings for broker and publisher:


Table of contents

1a   Set up a container with the MQTT broker service

1b   Set up the MQTT broker service on an Ubuntu server

2     Create the MQTT publisher for your container


1a) Set up a container with the MQTT broker service

1. Install the SDK

Follow the Install guide to set up the SDK on your development machine.

2. Create a container with the MQTT broker service

To create a container with the Mosquitto MQTT broker service please follow these steps:

     2.1 Start VirtualBox with your SDK
     2.2 Change to the root directory of the git repository (most likely ~/M3_Container)
     $ cd ~/M3_Container
     2.3 Use this build script to cross compile all necessary software and create an upload package with the final container
     $ ./scripts/create_container_mosquitto_mqtt_broker.sh

3. Import the container

Now start up your MRX device and log in to the web interface.

Go to /Administration/Container, click the choose file button and select the created container (see ~/M3_Container/images/ in your git repository).

After that, click OK to start the upload. Then configure Bridge to Net and IP address of the container.

4. Configure the MQTT Broker service

Open a new tab and type in the IP address of the MQTT broker container to see the web interface.
The overview page tells you more details and shows you how to configure the MQTT broker.

1b) Set up the MQTT broker service on an Ubuntu server

Start an Ubuntu 14.04 instance and set up the Mosquitto MQTT service as described below. (This demonstration uses a Digital Ocean droplet)

1. Create user mosquitto

The Mosquitto process should run as a normal user (not root).


$ adduser mosquitto

2. Install Mosquitto

Update the Ubuntu repositories and install the Mosquitto dependencies.


$ apt-get update
$ apt-get install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc

Download the latest Mosquitto sources, compile and install them.


$ sudo - mosquitto
$ wget http://mosquitto.org/files/source/mosquitto-1.4.10.tar.gz
$ tar xvzf mosquitto-1.4.10.tar.gz
$ cd mosquitto-1.4.10
$ make
$ exit
$ cd /home/mosquitto/mosquitto-1.4.10
$ make install

The binaries are now available under /usr/local

3. Set up Mosquitto

Create the Mosquitto user joe for the demo application.


$ mosquitto_passwd -c /etc/mosquitto/pwfile joe

Enter the password as described at the beginning of this tutorial.

Create the Mosquitto database directory.


$ mkdir /var/lib/mosquitto/
$ chown mosquitto:mosquitto /var/lib/mosquitto/ -R

Create your Mosquitto configuration file


$ cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
$ vi /etc/mosquitto/mosquitto.conf

and add following configuration at the end of the configuration file:

listener 8883 <your-server-ip-address>
persistence true
persistence_location /var/lib/mosquitto/
persistence_file mosquitto.db
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous false
password_file /etc/mosquitto/pwfile

Run once:


$ /sbin/ldconfig

4. Configure and start Mosquitto service

Test your Mosquitto binaries.


$ mosquitto -c /etc/mosquitto/mosquitto.conf

This command should run without errors. Open another window, start a new SSH
session to your Ubuntu server and try to connect to the Mosquitto service.

$ mosquitto_sub -h <your-server-ip> -p 8883 -v -t 'first/demo' -u joe -P secret

If you are connected successfully, no errors appear and the Mosquitto service
logs a successful connection.

Stop both processes and create an upstart service.


$ vi /etc/init/mosquitto.conf

Paste following content into the file:

description "Mosquitto MQTT broker"
start on net-device-up
respawn
exec /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Start the service with:

$ service mosquitto start

You're running a MQTT broker, now!

2 Create the MQTT publisher for your container

On your Go development machine install the Paho Go Client.


$ go get github.com/eclipse/paho.mqtt.golang

Create a new Go source file mqttpub.go and paste following source code.
Replace the IP address with your Ubuntu server address.

package main

import (
    "fmt"
    "time"
    //import the Paho Go MQTT library
    MQTT "github.com/eclipse/paho.mqtt.golang"
)

func main() {
    opts := MQTT.NewClientOptions().AddBroker("tcp://<your-server-ip-address>:8883")
    opts.SetClientID("insys")
    opts.SetUsername("joe")
    opts.SetPassword("secret")

    c := MQTT.NewClient(opts)
    if token := c.Connect(); token.Wait() && token.Error() != nil {
        panic(token.Error())
    }

    for i := 0; i < 5; i++ {
        text := fmt.Sprintf("this is msg #%d!", i)
        token := c.Publish("first/demo", 0, false, text)
        token.Wait()
    }

    time.Sleep(3 * time.Second)

    c.Disconnect(250)
}

Open a SSH session on your Ubuntu based MQTT server and start a test
subscriber process.


$ mosquitto_sub -h <your-server-ip-address> -p 8883 -v -t 'first/demo' -u joe -P secret

If your development machine has a full connection to the Ubuntu server, run the
application locally first.


$ go run mqttpub.go

On the Ubuntu server session you should see the following output:

first/demo this is msg #0!
first/demo this is msg #1!
first/demo this is msg #2!
first/demo this is msg #3!
first/demo this is msg #4!

Build on Mac OS X:


$ cd /Users/maxmuster/go/src/github.com/maxmuster.de/mqtt/mqttpub.go
$ env GOOS=linux GOARCH=arm GOARM=7 go build -v /Users/maxmuster/go/src/github.com/maxmuster.de/mqtt/mqttpub.go

Build on Linux:


$ cd ~/mqtt
$ GOPATH=$(pwd -P) go get github.com/eclipse/paho.mqtt.golang
$ GOPATH=$(pwd -P) GOOS=linux GOARCH=arm GOARM=7 go build -v mqttpub.go

Copy the binary and execute the application on the container.

$ scp mqttpub root@<your-container-ip-address>:~/
$ ssh root@<your-container-ip-address>
password: *****
root@container-1234 ~  $ ./mqttpub

Your subscriber should show the same output as the previous run on your
local machine. You're running a MQTT client, written with Go,
inside your container successfully. Done!