MQTT and CoAP: What is it about?

When I was travelling home last weekend, I went into one of the well known newspaper shops available in nearly all german train stations. Usually I sweep trough the shelves and look for some news on the Raspberry Pi. Then I get bored because I’ve already read similar projects on a great blog. This time the german magazin iX came in my view with it’s headlines about IoT and immediately I got interested. It’s normally a good source for everything more enterprise oriented. One of the main articles in the current issue of iX is related to MQTT. I’m a big fan of this so called M2M protocol, it is easy to understand and super easy to implement.

But there is another cool kid called CoAP and from what I’ve seen many leave it on the side of the road because all the buzz is around MQTT. Now I would like to put it out on  the stage and into the spotlight. Just so nobody forgets it.

CoAP is an abbreviation for Constrained Application Protocol, it is pretty similar to a lightweight HTTP. It implements the same basic functions like GET, POST and PUT. In addition, there is an extension of CoAP which implements an observer mode of operation similar to the publish/subscribe paradigm realized in MQTT. CoAP uses unified resource identifiers (URI) and is designed to allow efficient mapping to common HTTP by translation proxies. The following figures showcase the different patterns of MQTT and CoAP:

Left: CoAP, Right: MQTT
Left: CoAP, Right: MQTT

The choice between those two protocols depends heavily on what you plan to implement in your specific use case. I for one would not use CoAP in a scenario where I want to loosely couple my massive sensor network with a centralized server (e.g.a broker). Yet, I would – in most cases – prefer to use CoAP if I need to pull information (e.g. a status) from a device.

Security

Although it is often forgotten: When we as makers want to be serious about the greatness of an internet of things, we have to care about security!

Luckily both MQTT and CoAP support encryption, MQTT through means of TLS and CoAP through DTLS a variant of TLS for UDP protocols.

Usage

After all this talk about how great it is I would like to give a few examples where it is already used.

Lightweight M2M is one of the current attempts to standardize M2M platforms and protocols, it is a relatively lightweight approach and depends heavily on CoAP. There are existing implementations by the Eclipse Foundation e.g. Leshan (LWM2M Server), Wakaama (LWM2M Client) and Californium (CoAP Framework).

And the awesome Particle Photon (ex Spark) also use CoAP for their family of microcontrollers.

I haven’t implemented CoAP in my projects yet though I definitely plan to. If you have experience with CoAP or LightweightM2M I would be pretty interested to hear about it!