0

So I am currently exploring a few efficient ways to transfer data over MQTT. JSON is just too large for me. So I can across protobuf and this seems to fit the use-case.

But the issue I am having is that MQTT doesn't have a way to tell me where the message come from. So for instance, if I get a message I have no way to tell if it came from for source A or source B in some cases this isn't a problem but in my case, these have different data so I cannot know what model I have to use to deserialize.

I am using the C# implementation of protobuf. Is there some way to maybe partially deserialize a message if I enforce them to have a common field? (messageType field). And then being able to correctly deserialize the entire message.

Any help is appreciated.

Steven Ackley
  • 593
  • 7
  • 31

2 Answers2

1

MQTT doesn't have a way to tell me where the message come from

Of course it does. This is the purpose of message topic. You will be publishing topics like sourceA/messageTypeX or sourceB/messageTypeY.

Partial deserialization would imply some kind of inheritance (all your message types implement a common field), which is not how protobuf is designed.

Don't go looking for facilities similar to class inheritance, though – protocol buffers don't do that.

https://developers.google.com/protocol-buffers/docs/csharptutorial

Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
0

For those who come in later: Your first path should be a way to include the source and message type in the topic. Just as @Zdenek says above. However, in the case that you need to do some kind of partial deserialization (especially with proto 3), you could do that by using a message struct that just has the fields you want to use, with the same exact numeric identifiers. See Protobuf lazy decoding of sub message

kalyanswaroop
  • 403
  • 4
  • 5