Changing topic in subscription interceptor is not working
See original GitHub issueHi,
I am having a MQTTnet broker/server which provides MQTT service to multiple teams and each team has its own users, devices, topics, …
First idea is to force the devices to publish/subscribe to a unique topic like: > /team_name/device_label
Another idea came to my mind: instead of forcing devices to set a unique topic in the whole broker to be separated in multiple teams, I prefer the topic to be defined by user freely and unique in its own team, like: /device_label
I tried to change the topic with WithApplicationMessageInterceptor on application message published to set the topic internally in the broker and it is working.
Then I tried to do the same on subscription with WithSubscriptionInterceptor, but it is not connecting to the defined topic.
Here is the simplified code and I am testing with MQTTBox as client:
static async Task Main(string[] args)
        {
            var optionsBuilder = new MqttServerOptionsBuilder()
                                    .WithDefaultEndpoint()
                                    .WithApplicationMessageInterceptor(ctx =>
                                    {
                                        ctx.ApplicationMessage.Topic = "abc";
                                        ctx.AcceptPublish = true;
                                        ctx.CloseConnection = false;
                                    })
                                    .WithSubscriptionInterceptor(ctx =>
                                    {
                                        ctx.TopicFilter.Topic = "abc";
                                        ctx.AcceptSubscription = true;
                                        ctx.CloseConnection = false;
                                    })
                                    .WithDefaultEndpointPort(1883);
            var mqttServer = new MqttFactory().CreateMqttServer();
            await mqttServer.StartAsync(optionsBuilder.Build());
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
            await mqttServer.StopAsync();
        }
As you see, if you publish in ANY topic, it will be published in abc topic. But, the subscribers are not able to subscribe to abc topic.
Am I doing wrong, or my approach is not correct, or there is an issue in broker?
Thanks
Issue Analytics
- State:
 - Created 4 years ago
 - Comments:8 (1 by maintainers)
 

Top Related StackOverflow Question
Hi, this was not intended to work in that way. The only reason why this is possible at all is that the topic property of the topic filter is not immutable so you can set it to a different value.
But I got your point and it is quite easy to support it properly. So I will make this an official feature 😄 I will release this in the next version 3.0.3.
Best regards Christian
Please test with latest version 4.0 and reopen this ticket if the issue still persists. Please also create a proper Unit Tests so that reproducing this issue is easier for all.