JoeDevSharp.MqttNet.ReactiveBinding
1.0.3
dotnet add package JoeDevSharp.MqttNet.ReactiveBinding --version 1.0.3
NuGet\Install-Package JoeDevSharp.MqttNet.ReactiveBinding -Version 1.0.3
<PackageReference Include="JoeDevSharp.MqttNet.ReactiveBinding" Version="1.0.3" />
<PackageVersion Include="JoeDevSharp.MqttNet.ReactiveBinding" Version="1.0.3" />
<PackageReference Include="JoeDevSharp.MqttNet.ReactiveBinding" />
paket add JoeDevSharp.MqttNet.ReactiveBinding --version 1.0.3
#r "nuget: JoeDevSharp.MqttNet.ReactiveBinding, 1.0.3"
#addin nuget:?package=JoeDevSharp.MqttNet.ReactiveBinding&version=1.0.3
#tool nuget:?package=JoeDevSharp.MqttNet.ReactiveBinding&version=1.0.3
π§ Framework β JoeDevSharp.MqttNet.ReactiveBinding
JoeDevSharp.MqttNet.ReactiveBinding is a lightweight Reactive Object Mapper (ROM) for MQTT-based applications in .NET. It abstracts MQTT topics as strongly typed, observable entities, enabling developers to handle real-time data streams using LINQ-style syntax and reactive programming patterns.
Inspired by Entity Frameworkβs DbContext
and DbSet<T>
model, JoeDevSharp.MqttNet.ReactiveBinding
brings structure and clarity to messaging-driven systems by treating MQTT topics as first-class, observable data sources. This makes it easier to reason about, subscribe to, filter, and publish MQTT messages without dealing directly with low-level client code.
π Key Features
- Entity Mapping via Attributes: Define MQTT topics using
[Topic]
attributes on classes, making the topic-to-entity relationship explicit. - Reactive Subscriptions: Use
IObservable<T>
andWhere(...).Subscribe(...)
for reactive, filtered event handling. - Declarative Publishing: Publish entities directly using the
.Publish(entity)
method, hiding low-level MQTT details. - Plug-and-Play Context: Create a custom
MqttContext
to manage all MQTT entities just like a database context. - LINQ-style filtering: Chain
.Where()
and.Select()
on your MQTT streams. - Transparent MQTT integration: Built on top of a pluggable
IMqttBus
, it can be adapted to different MQTT libraries and broker implementations.
π§ Use Case Scenarios
- IoT Gateways: Map sensor data (e.g., temperature, humidity) to typed C# classes and react to threshold violations in real time.
- Industry 4.0 Applications: Monitor and control factory machinery using MQTT messages structured as typed objects.
- Edge Analytics: Apply inline analytics (e.g., filtering, transformation) on the edge without manual MQTT client plumbing.
- Home Automation Systems: React to MQTT events (e.g., motion detection, switch toggles) declaratively in .NET apps.
π§± Architecture Overview
TopicSet<T>
: A typed gateway to subscribe, publish, and filter MQTT messages mapped to the topic defined on typeT
.MqttOrmContext
: The central configuration point that registers all MQTT entities and creates topic sets.IMqttBus
: The abstraction over the MQTT client, which handles publishing, subscribing, and stream conversion.TopicAttribute
: Metadata annotation that binds a C# class to a specific MQTT topic.
π€ Philosophy
JoeDevSharp.MqttNet.ReactiveBinding promotes a clean, reactive, and domain-driven approach to working with MQTT. Instead of treating MQTT as a generic transport layer with string topics and JSON blobs, it treats it as a structured, type-safe message bus that seamlessly integrates with C#'s type system and LINQ capabilities.
The goal is to minimize boilerplate, enforce consistency, and make reactive MQTT applications more expressive and maintainable.
Let me know if you'd like this reformatted as a README.md
, or integrated with badges, install instructions, and GitHub action workflows. I can also generate an architectural diagram or visual overview if needed.
π Developer Documentation β JoeDevSharp.MqttNet.ReactiveBinding
Table of Contents
- β Introduction
- π Installation & Setup
- π§© Defining MQTT Entities
- ποΈ Creating the MQTT Context
- π Subscribing to Messages (Reactive)
- π€ Publishing Messages
- π§ͺ Full Console Example
- π οΈ Best Practices
- βFAQ
β 1. Introduction
JoeDevSharp.MqttNet.ReactiveBinding
is a lightweight framework that simplifies working with MQTT topics as strongly typed reactive entities in .NET applications. Inspired by Entity Framework, it enables a structured, observable, and declarative approach to working with real-time MQTT data.
π 2. Installation & Setup
Install the NuGet package:
dotnet add package JoeDevSharp.MqttNet.ReactiveBinding
Or reference the project directly if youβre working from source.
π§© 3. Defining MQTT Entities
Each entity class must be annotated with the [Topic]
attribute to define the corresponding MQTT topic.
using JoeDevSharp.MqttNet.ReactiveBinding.Attributes;
public class DHT230222_Modules
{
public double Temperature { get; set; }
public double Humidity { get; set; }
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
}
ποΈ 4. Creating the MQTT Context
The context exposes topic sets as typed properties, similar to EFβs DbSet<T>
. Each topics must be annotated with the [Topic]
attribute to define the corresponding MQTT topic.
public class MqttContext : MqttOrmContext
{
// Topic set for the DHT230222 module status updates
[Topic("iot/devices/dht/modules/DHT230222_Modules/status")]
public TopicSet<DHT230222_Modules> DHT230222_Modules { get; }
// Example of another topic set using the '@' wildcard to be replaced by a parameter name
[Topic("iot/devices/dht/sensors/@/status")]
public TopicSet<DHT230222_Modules> EX4008_Sensor { get; }
public MqttContext()
{
DHT230222_Modules = Set<DHT230222_Modules>();
}
}
π 5. Subscribing to Messages
Subscribe to incoming messages using LINQ-like filters:
_context.DHT230222_Modules
.Where(m => m.Temperature > 20)
.Subscribe(m =>
{
Console.WriteLine($"High temperature: {m.Temperature}");
});
Subscriptions are reactive and automatically connected to the MQTT bus.
π€ 6. Publishing Messages
Send messages to the corresponding topic using the Publish
method:
_context.DHT230222_Modules.Publish(new DHT230222_Modules
{
Temperature = 22.5,
Humidity = 50.0
});
Publishing is handled asynchronously behind the scenes, but Publish()
blocks until the operation is completed.
π§ͺ 7. Full Console Example
static void Main(string[] args)
{
var context = new MqttContext();
context.DHT230222_Modules.Subscribe(m =>
{
Console.WriteLine($"Received: Temp = {m.Temperature} Β°C, Humidity = {m.Humidity} %");
});
context.DHT230222_Modules.Publish(new DHT230222_Modules
{
Temperature = 18.0,
Humidity = 45.0
});
Console.ReadLine(); // Keep the application alivez
}
π οΈ 8. Best Practices
- Wait for connection and subscription to complete before publishing.
- Use
.Where()
to filter messages and reduce unnecessary processing. - Avoid long-running operations inside
.Subscribe()
; use async delegates or background workers.
β 9. FAQ
Q: Can I use MQTT wildcards (+, #)?
Yes, set AllowWildcards = true
in the [Topic]
attribute.
Q: What does Set<T>()
do in the context?
It returns a TopicSet<T>
that manages publishing and subscribing to the associated MQTT topic.
Q: Is this compatible with any MQTT broker? Yes. The library works with any broker that supports standard MQTT 3.1.1/5.0 (e.g., Mosquitto, HiveMQ, EMQX).
Product | Versions 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 was computed. 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. |
-
net8.0
- MQTTnet (>= 5.0.1.1416)
- System.Reactive (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.3 | 110 | 6/17/2025 |