C# MQTT broker using MQTTnet version 4

Photo by Jan Huber on Unsplash

C# MQTT broker using MQTTnet version 4

A Simple MQTT broker with c#

ยท

3 min read

In previous posts, we worked with MQTTnet version 3, although version 4 of MQTTnet came in Jun 2022. As MQTTnet version 4 has a lot of changes, I thought it would be helpful to write a new article about developing a simple MQTT broker using this version.

What is the MQTT broker?

According to Wikipedia:

The MQTT broker is software running on a computer (running on-premises or in the cloud) and could be self-built or hosted by a third party. It is available in both open source and proprietary implementations.

The broker acts as a post office; MQTT doesn't use the address of the intended recipient but uses the subject line called "Topic," and anyone who wants a copy of that message will subscribe to that topic. Multiple clients can receive the message from a single broker (one to many capabilities). Similarly, multiple publishers can publish topics to a single subscriber (many to one).

Each client can produce and receive data by publishing and subscribing, i.e., the devices can publish sensor data and still receive the configuration information or control commands (MQTT is a bi-directional communication protocol). This helps in both sharing data and managing and controlling devices.

image.png Now we are on the same page, so we can start coding an MQTT broker.

Code a simple MQTT broker

1) Create a project

We need to create a console project. I use dotnet CLI to create a new project.

dotnet new console --name SimpleMQTTBroker4

Now we have a new empty project ๐Ÿ˜.

2) Install dependency

For working in dotnet with MQTT, we need to install the MQTTnet package from NuGet.

dotnet add package MQTTnet --version 4.0.2.221

As I mentioned in the introduction, we are using version 4 of this package.

3) Implementation

Open Program.cs.Add the below code.

using MQTTnet.Server;
using MQTTnet;
using System.Text;
using static System.Console;

// Create the options for MQTT Broker
var options = new MqttServerOptionsBuilder()
    //Set endpoint to localhost
    .WithDefaultEndpoint();
// Create a new mqtt server
var server = new MqttFactory().CreateMqttServer(options.Build());
//Add Interceptor for logging incoming messages
server.InterceptingPublishAsync += Server_InterceptingPublishAsync;
// Start the server
await server.StartAsync();
// Keep application running until user press a key
ReadLine();

Task Server_InterceptingPublishAsync(InterceptingPublishEventArgs arg)
{
    // Convert Payload to string
    var payload = arg.ApplicationMessage?.Payload == null ? null : Encoding.UTF8.GetString(arg.ApplicationMessage?.Payload);


    WriteLine(
        " TimeStamp: {0} -- Message: ClientId = {1}, Topic = {2}, Payload = {3}, QoS = {4}, Retain-Flag = {5}",

        DateTime.Now,
        arg.ClientId,
        arg.ApplicationMessage?.Topic,
        payload,
        arg.ApplicationMessage?.QualityOfServiceLevel,
        arg.ApplicationMessage?.Retain);
    return Task.CompletedTask;
}

By default, the MQTT without encryption uses port 1883. If you want to change it, use the code below

// Create the options for MQTT Broker
var options = new MqttServerOptionsBuilder()
    //Set endpoint to localhost
    .WithDefaultEndpoint()
    // Port going to use 5004
    .WithDefaultEndpointPort(5004);

Now you can test the MQTT broker with any MQTT client you own.

image.png Note In this post, We see how we can develop an MQTT broker. I hope it was helpful. You can see the complete code on Github.

See other posts on the blog about MQTT and IoT.

If you are still unsure, have questions, or got an error, please feel free to use the comment section below, and I will be happy to assist!

ย