Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to CloudEvents v1.0 #2092

Open
AlbertoVPersonal opened this issue Oct 21, 2024 · 4 comments
Open

Support to CloudEvents v1.0 #2092

AlbertoVPersonal opened this issue Oct 21, 2024 · 4 comments

Comments

@AlbertoVPersonal
Copy link

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
Copy link
Member

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
Copy link
Author

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

@MD-V
Copy link
Contributor

MD-V commented Oct 22, 2024

@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
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants