---
title: "Discussion on \"C# MQTT broker using MQTTnet version 4\" | Hashnode"
description: "Discussion on \"C# MQTT broker using MQTTnet version 4\". A Simple MQTT broker with c#"
url: "https://hashnode.com/posts/c-mqtt-broker-using-mqttnet-version-4/62d2fa7b1f070812ac2ad51d"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [C# MQTT broker using MQTTnet version 4](https://blog.behroozbc.ir/c-mqtt-broker-using-mqttnet-version-4?utm_source=hashnode&utm_medium=feed)

A Simple MQTT broker with c#

Jul 16, 2022 · 3 min read · blog.behroozbc.ir  
Read the article

-   [#iot](https://hashnode.com/tag/iot)
-   [#csharp](https://hashnode.com/tag/csharp)
-   [#net](https://hashnode.com/tag/net)
-   [#internet-of-things](https://hashnode.com/tag/internet-of-things)

## Written by

[behrooz bozorgchamy](https://hashnode.com/@behroozbc)

@behroozbc

  
[View profile](https://hashnode.com/@behroozbc)

Student At FAU

## Responses 1

Markdown supported · ⌘↵ to post

[Bigberm](https://hashnode.com/@bigberm)

[Jul 17, 2022](https://hashnode.com/posts/c-mqtt-broker-using-mqttnet-version-4/62d2fa7b1f070812ac2ad51d/comment/62d3a115e52a5d129a0c2156)

Hi,

I got this error when using await in front of // Start the server await server.StartAsync();

"The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'."

How to fix it?

Thank you,

Bigberm

[behrooz bozorgchamy](https://hashnode.com/@behroozbc)

[Jul 17, 2022](https://hashnode.com/posts/c-mqtt-broker-using-mqttnet-version-4/62d2fa7b1f070812ac2ad51d/comment/62d3a115e52a5d129a0c2156#reply-62d3aab2bc2c7a1dc67267e9)

Hello, First, I need more detail. Can you provide your code?

Second, I provide a [Github repository](https://github.com/behroozbc/SimpleMQTTBroker4) that helps you

[Bigberm](https://hashnode.com/@bigberm)

[Jul 20, 2022](https://hashnode.com/posts/c-mqtt-broker-using-mqttnet-version-4/62d2fa7b1f070812ac2ad51d/comment/62d3a115e52a5d129a0c2156#reply-62d7f0d93df6e54ab8e5b14f)

[behrooz bozorg chami](https://hashnode.com/@behroozbc)

This's my code.

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

namespace MqttBroker { class Program { static void Main(string\[\] args) {

```
        // 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();

        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;
        }
    }
}
```

If I did comment out "//await server.StartAsync();" and used server.StartAsync(); it's ok.

So,let me know solution,and also I am waiting for MQTT broker with username/password authenticated from clients request.
