shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
548 stars 176 forks source link

Filter user's own messages #200

Closed ldemyanenko closed 3 years ago

ldemyanenko commented 4 years ago

Is it possible to somehow filter out messages sent by the user? Avoiding them in subscription or somehow filter them when got the message.

shamblett commented 4 years ago

There's the MqttClientTopicFilter class maybe, from its docs -

// This class allows specific topics to be listened for. It essentially
/// acts as a bandpass filter for the topics you are interested in if
/// you subscribe to more than one topic or use wildcard topics.
/// Simply construct it, and listen to its message stream rather than
/// that of the client. Note this class will only filter valid receive topics
/// so if you filter on wildcard topics for instance, which you should only
/// subscribe to,  it  will always generate a no match.

Its the only topic filtering the client currently provides.

ldemyanenko commented 4 years ago

Maybe somehow by messageId or by clientId?

I've tried with messageId but looks like I'm sending and getting different values In my case, I'm creating several MQTT clients(every time when a user opens specific screen) in the app. Looks like MessageIdentifierDispenser is a singleton and it increments values on messageId through all lifecycle of app, but when I'm creating new MQTT client and connect it, somehow message-ids I'm getting from my own received messages are different from created ids(after creating of new client each new message-id will start from 0)

Like that.

Sending flutter (19489): messageId 7 And recieving my own message I/flutter (19489): Publish Variable Header: TopicName={projects/6b609d5b-ffe8-4c0e-b7a0-b2f669ac103c/messages}, MessageIdentifier={3}, VH Length={58}

next

I/flutter (19489): messageId 8 I/flutter (19489): Publish Variable Header: TopicName={projects/6b609d5b-ffe8-4c0e-b7a0-b2f669ac103c/messages}, MessageIdentifier={4}, VH Length={58}

My idea was to store all messageId that was sent and filter out all incoming messages with ids from that list. But somehow ids aren't consistent.

shamblett commented 4 years ago

Message identifiers are used only in support of the MQTT protocol as per the spec. They are used only to co-ordinate message transfer between the client and the broker for subscribing and publishing at the different qos levels.

Basically every new message from the client has a new identifier, the broker sends this back in message exchange sequences. As you say the message identifiers are allocated from 0 onwards, per client, so you can't assume anything about the identifiers across different clients. Also you can't assume that a broker will send you any specific identifier, these are purely under the control of the broker, not the client.

Why do you think you can use message identifiers in this way? I don't think there's anything in the spec that says this.

ldemyanenko commented 4 years ago

It was my gues. If I'm able to get a message ID before sending it, I expected the same message when receiving it. Looks like avoiding local messages is available in MQTT v5.0 https://stackoverflow.com/a/61896310/7755629

shamblett commented 4 years ago

Yep, take a look at the mqtt5_client , you can set this flag as a subscription option when you subscribe to a topic.