MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

Pass parameter to MessageReceivedEvent

Open Bezebub opened this issue 4 years ago • 5 comments

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!

Bezebub avatar Mar 31 '21 08:03 Bezebub

Did you ever find a solution to this problem?

adam-bentley avatar Oct 04 '21 11:10 adam-bentley

I assume user properties can only be used with MQTT 5 (and might not be fully supported yet).

SeppPenner avatar Oct 04 '21 17:10 SeppPenner

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.

chkr1011 avatar Oct 04 '21 17:10 chkr1011

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.

adam-bentley avatar Oct 05 '21 18:10 adam-bentley

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

mauro-rogledi avatar Nov 26 '21 08:11 mauro-rogledi

Happening also to me @mauro-rogledi Have you been able to fix it or do you have a solution?

alexamz avatar Oct 05 '22 11:10 alexamz

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 avatar Oct 05 '22 14:10 mauro-rogledi

@mauro-rogledi I don't really understand the issue. Please provide a full sample which shows the issue. Which version do you use?

chkr1011 avatar Oct 06 '22 18:10 chkr1011

Closing this due to inactivity. If the issue still exists, please reopen this ticket.

chkr1011 avatar Nov 11 '22 13:11 chkr1011