BiharMQTT 4.1.0

dotnet add package BiharMQTT --version 4.1.0
                    
NuGet\Install-Package BiharMQTT -Version 4.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BiharMQTT" Version="4.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BiharMQTT" Version="4.1.0" />
                    
Directory.Packages.props
<PackageReference Include="BiharMQTT" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BiharMQTT --version 4.1.0
                    
#r "nuget: BiharMQTT, 4.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BiharMQTT@4.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BiharMQTT&version=4.1.0
                    
Install as a Cake Addin
#tool nuget:?package=BiharMQTT&version=4.1.0
                    
Install as a Cake Tool

BiharMQTT

BiharMQTT is a fork of MQTTnet — a high-performance .NET MQTT library — stripped down to a lean TCP + WebSocket broker with a focus on eliminating heap allocations on the publish hot path.

Upstream: dotnet/MQTTnet · License: MIT


What We Changed

The broker has been substantially rewritten. Architecture details are in docs/Server-Architecture.md. Highlights:

Area Change
Receive path SocketAsyncEventArgs + per-channel callback state machine in MqttChannelAdapter. No async/await, no Task allocation, no state-machine boxing per packet. Cached delegate fields avoid closure alloc.
Send path Sync-only MqttChannelAdapter.SendPacket over Socket.Send / SslStream.Write. The async send pipeline was deleted.
Sender concurrency MqttSenderPool — M dedicated background threads (default Environment.ProcessorCount) servicing N connected clients via a ready-queue + semaphore. Replaces one-thread-per-client.
Inline-send fast path Producers bypass the per-session bus entirely when the channel send-lock is uncontended, the bus is empty, and the kernel send buffer accepts immediately. Falls back to the bus on contention or backpressure.
Per-session bus Three priority partitions (Health / Control / Data) backed by PreAllocatedQ<T> over native memory (HugeNativeMemoryPool). No GC interaction on the per-message path.
Publish interceptor unsafe delegate*<in MqttPublishInterceptArgs, PublishInterceptResult> static-only function-pointer hook on MqttServerOptions. Allow / Consume / Reject. The args type is a readonly ref struct — buffers borrowed only for the call.
Server-side publish MqttServer.Publish(ReadOnlySpan<byte> topic, ReadOnlySequence<byte> payload, …) — zero-alloc inject API with the same lifetime contract as the interceptor.
WebSocket transport MQTT over ws:// and wss:// (RFC 6455, subprotocol mqtt) added back without ASP.NET Core / Kestrel: the existing TCP listener performs the HTTP Upgrade handshake in-process and hands a BCL WebSocket to MqttWebSocketChannel, which plugs into the same IMqttChannel / MqttChannelAdapter path as TCP. wss reuses the TLS endpoint's certificate and mTLS settings. Trade-off: WebSocket writes are blocking SendAsync calls and skip the inline-send fast path.
Removed from upstream All ASP.NET Core / Kestrel integration (WebSocket is now served in-process, see above); IMqttServerAdapter / IMqttChannelAdapter interfaces; the entire event/interceptor surface (InterceptingPublishAsync & friends); the shared MessageRingBuffer (replaced by per-session buses).

Allocation Profile

Verified via dotnet-counters against the bombard test (50 publishers × 1000 msg/s × 10 subscribers ≈ 500k deliveries):

  • Total Gen0 + Gen1 + Gen2 collections: 3 over ~60 s wall.
  • Total GC pause: ~12 ms over the run.
  • Total allocated: ~27 MB, ~50 bytes per packet operation.

The receive callback chain, sender pool drain, and inline-send fast path are zero-allocation in steady state.


Getting Started

Samples/Program.cs starts a plain TCP broker on port 1883:

var server = new MqttServerOptionsBuilder()
    .WithDefaultEndpoint()
    .WithDefaultEndpointPort(1883)
    .Build();

unsafe { server.PublishInterceptor = &AllowAllInterceptor; }   // optional

using var mqttServer = new MqttServerFactory().CreateMqttServer(server, logger);
await mqttServer.StartAsync();

static PublishInterceptResult AllowAllInterceptor(in MqttPublishInterceptArgs args)
    => PublishInterceptResult.Allow;

TCP and WebSocket in one server

A single MqttServer can serve every transport at once, each on its own port. All endpoints share the same sessions, subscriptions and retained messages, so a browser on WebSocket and a device on TCP can talk to each other directly:

var options = new MqttServerOptionsBuilder()
    .WithDefaultEndpoint()                  // tcp://   :1883
    .WithEncryptedEndpoint()                // mqtts:// :8883
    .WithEncryptionCertificate(cert)
    .WithWebSocketEndpoint()                // ws://    :8083/mqtt
    .WithEncryptedWebSocketEndpoint()       // wss://   :8084/mqtt (reuses the TLS cert)
    .Build();

WithWebSocketEndpoint(port, path) and WithEncryptedWebSocketEndpoint(port, path) take an optional port and URL path. Clients must offer the mqtt subprotocol (e.g. MQTT.js: mqtt.connect('ws://host:8083/mqtt')).

TestClient/Program.cs is a load generator using the upstream MQTTnet client.

License

MIT, same as the upstream MQTTnet project.

.NET Foundation

The upstream MQTTnet project is supported by the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on BiharMQTT:

Package Downloads
BiharMQTT.Server

This is the server implementation of BiharMQTT.

BiharMQTT.AspNetCore

BiharMQTT is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker) and supports v3.1.0, v3.1.1 and v5.0.0 of the MQTT protocol.

BiharMQTT.Extensions.Rpc

This is an extension library which allows executing synchronous device calls including a response using BiharMQTT.

BiharMQTT.Extensions.TopicTemplate

Provides mqtt topic templating logic to support dispatch, routing and similar functionality based on the well known moustache syntax (AsyncAPI compatible).

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.1.0 103 11/19/2026
3.1.0 300 6/23/2026
3.0.0 172 5/3/2026
2.10.0 161 5/2/2026
2.9.0 166 5/2/2026
2.8.0 147 5/2/2026
2.7.0 159 5/1/2026
2.6.0 166 5/1/2026
2.5.0 179 5/1/2026
2.4.0 155 4/30/2026
2.2.0 168 4/29/2026
2.1.0 155 4/29/2026
2.0.0 168 4/29/2026
1.0.3 235 4/8/2026
1.0.2 199 4/8/2026
1.0.0 180 4/7/2026

* Core: Used new language features across the entire library
* Core: Performance improvements
* Core: Added validations for variable byte integers which do not match the .NET uint perfectly
* Client: Added support for pre-encoded UTF-8 binary buffers for user properties (#2228, thanks to @koepalex)
* Server: Improved performance of retained messages when no event handler is attached (#2093, thanks to @zhaowgit)
* Server: The event `InterceptingClientEnqueue` is now also called for retained messages (BREAKING CHANGE!)
* Server: The local end point is now also exposed in the channel adapter (#2179)
* Marked all projects as AOT compatible
* Restored the strong name of the nugets
* Embedded license file in all nugets (#2197, thanks to @JensNordenbro)
* Added support for dotnet10