Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MqttServer] Subscription to similar base and subtopic with wildcards fails #1612

Closed
grobmiller opened this issue Dec 6, 2022 · 5 comments · Fixed by #1622
Closed

[MqttServer] Subscription to similar base and subtopic with wildcards fails #1612

grobmiller opened this issue Dec 6, 2022 · 5 comments · Fixed by #1622
Labels
bug Something isn't working

Comments

@grobmiller
Copy link

We encountered a bug which was not present in earlier major versions of MQTTnet, but we met it since we had a major upgrade to 4.1.2.350. Same applies for the newest version 4.1.2.436

We start an MQTTnet server. Then a client. We add a subscription to these to topics:

house/+/room
house/+/room/+

Then we send messages with these two topics:

house/1/room
house/1/room/bed

We would expect to receive both messages, but only the first one arrives. When testing this with other servers (older MQTTnet server or Moquitto) both messages arrive.

As you can see in the sample code below, we also made some slight variations (# instead of +), but no success. When we only use one subscription (#) everything is fine again. Replacing the mid-level wildcard with a concrete house id (1) also makes it work again.

It is not an ultra-critical bug, since there is a workaround. But we lost data in production since we did not realize this problem immediately. Might be that some others may stumble over this, too.

using System;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Server;

namespace MqttServerTest
{
	class Program
	{
		static void Main(string[] args)
		{
			Task.Run(StartMqttServer).Wait();

			var client = Task.Run(StartClient).Result;

			client.SubscribeAsync("house/+/room");
			client.SubscribeAsync("house/+/room/+");

			//You can try these two subscriptions instead, but this will also fail
			//client.SubscribeAsync("house/+/room");
			//client.SubscribeAsync("house/+/room/#");

			//This works
			//client.SubscribeAsync("house/1/room");
			//client.SubscribeAsync("house/1/room/+");

			//This works, too
			//client.SubscribeAsync("house/1/room/#");

			Publish(client, "house/1/room");
			Publish(client, "house/1/room/bed");

			//Result with first two examples: Only first message will be shown

			Console.ReadLine();
		}

		static async Task<MqttServer> StartMqttServer()
		{
			var mqttFactory = new MqttFactory();
			
			var mqttServerOptions = 
				mqttFactory
					.CreateServerOptionsBuilder()
					.WithDefaultEndpoint()
					.Build();
			var server = mqttFactory.CreateMqttServer(mqttServerOptions);
			await server.StartAsync();
			return server;
		}
		
		static async Task<IMqttClient> StartClient()
		{
			var mqttFactory = new MqttFactory();

			var mqttClientOptions =
				mqttFactory
					.CreateClientOptionsBuilder()
					.WithTcpServer("localhost")
					.WithClientId("test")
					.Build();

			var client = mqttFactory.CreateMqttClient();

			client.ApplicationMessageReceivedAsync += args =>
			{
				Console.WriteLine(args.ApplicationMessage.Topic);
				return Task.CompletedTask;
			};

			await client.ConnectAsync(mqttClientOptions, CancellationToken.None);
			
			return client;
		}
		
		private static Task<MqttClientPublishResult> Publish(IMqttClient client, string topic)
		{
			return client.PublishAsync(new MqttApplicationMessage
			{
				Topic = topic,
				Payload = new byte[] { 1, 2, 3, 4 }
			});
		}
	}
}
@grobmiller grobmiller added the bug Something isn't working label Dec 6, 2022
@logicaloud
Copy link
Contributor

That indeed looks to be a bug. @chkr1011 I think this one is for me; I'll be looking into it if you like.

@chkr1011
Copy link
Collaborator

chkr1011 commented Dec 6, 2022

@logicaloud Yes please have look. I will also have a look tomorrow or so. We can then join with our findings.

@chkr1011
Copy link
Collaborator

chkr1011 commented Dec 6, 2022

@logicaloud Please us the branch created for this ticket.

@logicaloud
Copy link
Contributor

@chkr1011 Will do. I have found the issue and will add a pull request into the branch soon. @grobmiller, thanks for reporting this.

@grobmiller
Copy link
Author

You're welcome, thanx for your great effort and product!

chkr1011 pushed a commit that referenced this issue Dec 7, 2022
* fix wildcard topic subscription

* Remove superfluous ToList calls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants