MQTTnet
MQTTnet copied to clipboard
Pass parameter to MessageReceivedEvent
Describe your question
Thanks for this great Library! I'ld like to pass an ID to MessageReceived Event. I tried to use the UserProperty with the following code: ... mqClient.UseApplicationMessageReceivedHandler(AddressOf MessageReceived)
Dim Options As New Options.MqttClientOptionsBuilder
Options.WithClientId(Client).WithTcpServer(Server, 1883).WithUserProperty("ConID", connectionID)
Await mqClient.ConnectAsync(Options.Build).ConfigureAwait(False)
...
Private Sub MessageReceived(e As MqttApplicationMessageReceivedEventArgs)
Dim test As String = e.ApplicationMessage.UserProperties(0).Value '<--- Exception UserProperties is nothing
End Sub
How to achieve this goal?
Which project is your question related to?
- Client Thanks in advance!
Did you ever find a solution to this problem?
I assume user properties can only be used with MQTT 5 (and might not be fully supported yet).
You are passing the user properties on connecting. If you want to use them in a message handler you must set them upon publishing instead.
My mistake was that I didn't know I needed to specifically set the use of MQTT version 5, only going by going to 'wiki -> Server and client method documentation -> Client options -> WithProtocolVersion' was this made apparent to me.
Hello, I have the same problem.
I using protocol version V5.0 builder .WithProtocolVersion (MQTTnet.Formatter.MqttProtocolVersion.V500);
And I send a message like this var message = new MqttApplicationMessageBuilder () .WithTopic ("topic") .WithPayload ("payload") .WithUserProperty (IdMessage, id) .To build();
When the message is received on the server or other clients, the user's properties are null.
In the storage message of the client sender I find: { "Id": "0c33f6fc-41ee-4893-8f37-ad32a02e427d", "ApplicationMessage": { "Topic": "aa", "Payload": "ImJiIg==", "QualityOfServiceLevel": 1, "Retain": true, "Dup": false, "UserProperties": [ { "Name": "IdMessage", "Value": "1" } ], "ContentType": "", "ResponseTopic": null, "PayloadFormatIndicator": null, "MessageExpiryInterval": null, "TopicAlias": null, "CorrelationData": null, "SubscriptionIdentifiers": null } }
In server storage message I find: { "Topic": "aa", "Payload": "ImJiIg==", "QualityOfServiceLevel": 0, "Retain": true, "Dup": false, "UserProperties": null, "ContentType": null, "ResponseTopic": null, "PayloadFormatIndicator": null, "MessageExpiryInterval": null, "TopicAlias": null, "CorrelationData": null, "SubscriptionIdentifiers": null },
Thanks
Happening also to me @mauro-rogledi Have you been able to fix it or do you have a solution?
Happening also to me @mauro-rogledi Have you been able to fix it or do you have a solution?
Unfortunately not. I avoided using them
@mauro-rogledi I don't really understand the issue. Please provide a full sample which shows the issue. Which version do you use?
Closing this due to inactivity. If the issue still exists, please reopen this ticket.