Author: jokke009

Setup MQTT in Angular 2 project.

Today we’re going to be using MQTT in our angular 2 program. Gathering on;y bits and pieces after hours of browsing the internet, I’ve decided to combine everything in this I hope easier to follow guide to get you going.

The solution I’ve found (may not be the most up to date nor correct), did prove to run the code I need for my application.

In this guide, I’m going to assume you’ve got your project folder setup, any you’re ready to start adding mqtt services to it.

For reference, Usually I like to boiler-plate off the angular 2 Tour Of Heroes demo, and using the Visual Code as IDE, but you could easily add this to any existing project.

Installing Mqtt

Command prompt to your project directory and issue the following commands

npm install mqtt --save

As it’s just javascript, and we want to make it typescript’able; we’re gonna need its typings:

npm install --save-dev @types/mqtt

Now  we can use mqtt in our program:

import 'mqtt';
import { Client, Packet, connect } from 'mqtt';
import { ClientOptions } from 'mqtt';

I’ve created a seperate service that’ll take care of the mqtt bits.


Now for the actual implementation of the service I’m going to be referring to this Chinese Site. I did find the host parameter missing in their config.json file. Couldn’t comment on their site so I’ll correct it here then: Could comment using the wechat app.

 "host": "",
 "port": 9001,
 "ssl": false,

"user": "john",
 "pass": "smith",

"subscribe": [
 "publish": [

"keepalive": 10

In my config, you’ll see I’m using port 9001, a websocket port I discussed in my other blog.


My Debian Server favourites


Whilst you may be running debian as an HTPC (Home Theater PC) connected to your TV running Kodi , the machine can be servered-up do additional tasks.

For instance I would also like it to host my angular app controlling my MQTT devices, or make files accessible though the internet using ssh/ftp/nfs ..

For that reason this blog is a list of addtional services I’ve installed on that machine. No need to waste the resources of buying an extra machine for the server or home automation.



Allows the server to share a folder using the smb:// protocol. This makes it’s shares visible to the windows workgroup network. Easy for accessing files between windows-linux from explorer-thunar (in debian XFCE )

The easiest in use and configuration by far is from the gadmin package:

sudo apt-get install gadmin-samba


Now you may be wondering, why a VNC client when we’ve got teamviewer? Well while Teamviewer is easy to install and use, there are some drawbacks:

  • they’ve been hacked not long ago exposing accounts and passwords
  • it’s commercial soft; the annoying nag screen that comes on the remote computer after disconnecting.. messing up your movie/game experience
  • it prevents some games going fullscreen correctly

Vnc is light and easy to install using the following steps (for debiaan 8 xfce )

apt-get install xfce4 xfce4-goodies gnome-icon-theme tightvncserver

Additional tasks

External Access: anywhere through the internet to your server

For most people, the server is on a NAT behind the ISP’s router. In my case, the ISP gives you one dynamic external IP address… Easier would be to link it to a dynamic dns service. Create a free account on , just gotta log in every 30days to keep it alive. The client software can also be found on their site.

tar zxvf noip-duc-linux.tar.gz
cd noip-2.1.9-1
sudo make
sudo make install

The make install asks for your credentials you credited in the noip account earlier. Don’t forget to setup your account and one hostname first! ( I recommend following the new device configurator). It’s ver straight forward for the average user.


Once you’ve got your connection tested from the noip configuration wizard, you only need to forward your ports on your router that your apps use on the remote server.

HTPC (entertainment + server in one ! )

In my case my debian server will not be head-less. Since the server is sitting near the TV we might as well give it some added functionality like KODI and some simple games

Debian Software Common Packages

This adds the add-apt commands for example making it easier to add repositories:

sudo apt-get install software-properties-common


It’s always fun having the old-school emulators to play games from N64, Sega .. Let’s get our emulators installed first. So let’s get retroarch installed: dependencies for debian 8.7

apt-get -y install build-essential libxkbcommon-dev zlib1g-dev libfreetype6-dev libegl1-mesa-dev libgles2-mesa-dev libgbm-dev nvidia-cg-toolkit nvidia-cg-dev libavcodec-dev libsdl2-dev libsdl-image1.2-dev libxml2-dev

Then clone the repo so we can make a fresh build.

git clone git://

cd libretro-super




Wait a long time for the compilation of all the cores..

mkdir -p ~/ra/cores
cd retroarch
make DESTDIR=~/ra install
cd .. #to libretro-super directory
./ ~/ra/cores



Setting up an MQTT server on Debian Stretch

Setting up an MQTT server on Debian Stretch


Today we`re going to be setting up our own home automation server in a dedicated linux server. It’s gonna host our platform using the the Node.Js environment. The different devices around the house are going to be speaking MQTT to this server. I’ve chosen MQTT because of it’s robustness and lightweightness allowing for easy deployment on my favourite controllers: ESP8266. The esp-01, still the cheapest to this date,only sports 4mb of flash, has no trouble at all controlling a couple of relay’s for now. In the future the newer/bigger boards can be used to get more IO and function, but for now I’m going to be using the esp-01’s extensively around the house flexibly (it’s wireless…).


The idea is to have the server online 24/7, so consider a computer thats not to power-hungry; like a Raspberry Pi or a small embedded box computer. Alternatively you can boot up a guest in virtual box like I’m doing for developing purposes. I’m still a fan of debian, which can be run on outdated hardware; it`s memory footprint is small, and fast to boot up.


After installing the base system, (I’ve used the netboot distro , and selected the XFCE environment) we can install the required packages.


The setup guide can be found here: nodejs setup

At the time of writing, my machine is running Jessie Debian 8.7. I also wanted to go with the latest nodejs v7 so the commands become:

sudo apt-get install curl
curl -sL | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

Check if everything is fine and installed:

node -v
npm -v

Update the npm to the latest version:

sudo npm install npm -g

Mosquitto + Websockets

Is what will be using as an MQTT broker the esp’s can publish an subscribe to. As we’ll be using the websockets for our frontend app later on, we’ll have to build mosquitto with libwebsockets.

sudo apt-get install build-essential python quilt devscripts python-setuptools python3 libssl-dev cmake libc-ares-dev uuid-dev daemon

Next will be downloading and building libwebsockets: (link is updated)

Also as of stretch, zlib needs to be installed prior to install libwebsockets (in bold)

tar zxvf libwebsockets-1.4-chrome43-firefox-36.tar.gz
cd libwebsockets-1.4-chrome43-firefox-36
mkdir build
cd build
<strong>sudo apt-get install zlibc zlib1g zlib1g-dev</strong>
cmake ..
sudo make install
sudo ldconfig

Download, build and install mosquitto (the broker server):

tar zxvf mosquitto-1.4.2.tar.gz
cd mosquitto-1.4.2

Change the file with your favorite editor (mousepad in xfce debian) to enable the build with websockets:

sudo mousepad

Adapt the following code:


Then build:

sudo make install
sudo cp mosquitto.conf /etc/mosquitto
sudo adduser mosquitto

Edit the mosquitto.conf file we just copied to enable the webscoket on port 9001.

sudo mousepad /etc/mosquitto/mosquitto.conf

Add this to the config file:

port 1883
listener 9001
protocol websockets
pid_file /var/run/
Optional: I'm going to add security by forcing credentials
You can do so by creating as password file:
mosquitto_passwd -c /etc/mosquitto/passwd yourloginname

Also, back in


set the

allow_anonymous false

Save and Reboot.
After logging launch mosquitto with the config file just modified:

mosquitto -c /etc/mosquitto/mosquitto.conf

This gave a command not found error. The executable was located in the /usr/local/sbin folder.

To solve that were gonna make a link like so:

sudo ln -s /usr/local/sbin/mosquitto /bin/mosquitto

and then:

mosquitto -c /etc/mosquitto/mosquitto.conf

The broker should be listening for websockets on port 9001. The MQTT port remains the default of 1883.

After testing that, we’re gonna want to start it automatically. Using the systemd (Jessie) we can accomplish that easily:

sudo nano /etc/systemd/system/mosquitto.service

Paste in the following:

Description=Mosquitto MQTT Broker daemon

ExecStartPre=/usr/bin/rm -f /var/run/
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d
ExecReload=/bin/kill -HUP $MAINPID


You can auto start the service by:

sudo systemctl enable mosquitto
sudo systemctl start mosquitto

That’s it! Done.