dotnet / MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
MIT License
4.5k stars 1.07k forks source link

Support to CloudEvents v1.0 #2092

Open AlbertoVPersonal opened 1 month ago

AlbertoVPersonal commented 1 month ago

Hi,

Does someone know whether MQTTnet supports the CloudEvents spec?

I'm using MQTTnet in my project. Also I'm using an external runtime that it supports CloudEvents and I would like to analyze a feature that this runtime has.

Regards!

rido-min commented 1 month ago

not sure what you mean by "supports CloudEvent spec". I've been prototyping basic support by adding an extension method to populate the required message properties:

public class CloudEvent(Uri source, string type = "ms.aio.telemetry", string specversion = "1.0")
{
    private string? _id = null!;
    public Uri? Source { get => source; }
    public string SpecVersion { get => specversion;  }
    public string Type { get => type;  }
    public string? Id { get => _id; internal set => _id = value; } // although id is required, we want update it in the same instance from the sender.

    // optional 
    private string? _dataContentType;
    private string? _dataSchema;
    private string? _subject;
    private DateTime? _time;
    public DateTime? Time { get => _time; internal set => _time = value; }
    public string? DataContentType { get => _dataContentType; internal set => _dataContentType = value; }
    public string? Subject { get => _subject; internal set => _subject = value; }
    public string? DataSchema { get => _dataSchema; set => _dataSchema = value; }
}

public static partial class MqttApplicationMessageBuilderExtension
{
    public static MqttApplicationMessageBuilder WithCloudEvents(this MqttApplicationMessageBuilder mb, CloudEvent md)
    {
        mb.WithUserProperty(nameof(md.SpecVersion).ToLowerInvariant(), md.SpecVersion)
          .WithUserProperty(nameof(md.Id).ToLowerInvariant(), md.Id!.ToString())
          .WithUserProperty(nameof(md.Type).ToLowerInvariant(), md.Type)
          .WithUserProperty(nameof(md.Source).ToLowerInvariant(), md.Source!.ToString());

        if (md.Time is not null)
        {
            mb.WithUserProperty(nameof(md.Time).ToLowerInvariant(), md.Time!.Value.ToString("O"));
        }

        if (md.Subject is not null)
        {
            mb.WithUserProperty(nameof(md.Subject).ToLowerInvariant(), md.Subject);
        }

        if (md.DataContentType is not null)
        {
            mb.WithUserProperty(nameof(md.DataContentType).ToLowerInvariant(), md.DataContentType);
        }

        if (md.DataSchema is not null)
        {
            mb.WithUserProperty(nameof(md.DataSchema).ToLowerInvariant(), md.DataSchema);
        }

        return mb;
    }
}
AlbertoVPersonal commented 1 month ago

I mean you are prototyping but embed on the own library as a standard feature.

MD-V commented 1 month ago

@AlbertoVPersonal why should the MQTTnet libary support the CloudEvents spec?
This lib only implements the MQTT protocol and not some specific other specs. You could create a separate lib which supports this functionality.

AlbertoVPersonal commented 1 month ago

@AlbertoVPersonal why should the MQTTnet libary support the CloudEvents spec? This lib only implements the MQTT protocol and not some specific other specs. You could create a separate lib which supports this functionality.

Yes, I agree and I could create the lib because my internal code is ready to do it. Even I could develop a Nuget package but I cannot prioritize that task.

Anyway, I think that it would be a plus because:

  1. Standardization of Event Formats: we could create a standardized way to represent and transmit event data between different systems (internal and external). It reduces the ambiguity and enhances interoperability.
  2. Enhanced Event Metadata: CloudEvents provide a rich set of metadata about events, such as type, source, and time of occurrence. This allows developers to leverage this information for better event handling, routing, and processing.