MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

[FR] Protobuf

Open MichalNemec opened this issue 1 year ago • 2 comments

Describe the feature request

Im using MQTTnet and we plan to use gRPC with protobuf and our broker (emqx) supports it. Would be nice to have that option with this library.

Which project is your feature request related to?

  • Client
  • ManagedClient
  • Generic

Describe the solution you'd like

Be able to publish and subscribe to topic with specific protobuf, so we dont have to send just strings and have types straight away.

Describe alternatives you've considered

try to deserialize from string to protobuf/any model

Additional context

Flutter mqtt5_client supports protobuf and our dotnet backend uses MQTTnet library, but i dont see any option to publish and receive messages with protobuf

MichalNemec avatar May 06 '24 13:05 MichalNemec

You should be able to do

mqttMessageBuilder
  .WithPayload(protoObject.ToByteArray())
  .WithContentType("application/vnd.google.protobuf")

and put that in an extension method if you prefer.

On the receiving side, just look for the content type and do

ExpectedType.Parser.ParseFrom(msg.Payload)

FWIW I would not expect the string encoding to work as you seem to assume.

simonthum avatar May 10 '24 13:05 simonthum

this sample shows how to use protobuf with MQTTnet

https://github.com/Azure-Samples/MqttApplicationSamples/tree/main/scenarios/command/dotnet

rido-min avatar May 10 '24 14:05 rido-min

You can also take a look at https://github.com/SeppPenner/SparkplugNet which uses ProtoBuf messages over MQTT.

SeppPenner avatar May 13 '24 16:05 SeppPenner

I assume the question is answered. If not, please reopen the ticket.

chkr1011 avatar May 26 '24 08:05 chkr1011