{"id":24572797,"url":"https://github.com/rafiulgits/mqtt-client-dotnet-core","last_synced_at":"2025-09-02T19:44:12.622Z","repository":{"id":46593404,"uuid":"255300646","full_name":"rafiulgits/mqtt-client-dotnet-core","owner":"rafiulgits","description":"MQTTNet client implementation on ASP.NET Core. Library https://github.com/dotnet/MQTTnet","archived":false,"fork":false,"pushed_at":"2022-11-15T09:25:09.000Z","size":15,"stargazers_count":71,"open_issues_count":0,"forks_count":23,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-20T22:22:55.640Z","etag":null,"topics":["mqtt","mqtt-client"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rafiulgits.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-13T10:54:03.000Z","updated_at":"2025-06-14T21:53:50.000Z","dependencies_parsed_at":"2023-01-22T03:00:10.566Z","dependency_job_id":null,"html_url":"https://github.com/rafiulgits/mqtt-client-dotnet-core","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rafiulgits/mqtt-client-dotnet-core","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafiulgits%2Fmqtt-client-dotnet-core","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafiulgits%2Fmqtt-client-dotnet-core/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafiulgits%2Fmqtt-client-dotnet-core/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafiulgits%2Fmqtt-client-dotnet-core/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rafiulgits","download_url":"https://codeload.github.com/rafiulgits/mqtt-client-dotnet-core/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafiulgits%2Fmqtt-client-dotnet-core/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273341425,"owners_count":25088346,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-02T02:00:09.530Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mqtt","mqtt-client"],"created_at":"2025-01-23T19:44:40.890Z","updated_at":"2025-09-02T19:44:12.591Z","avatar_url":"https://github.com/rafiulgits.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MQTT Client ASP.NET Core\n\nThis repository is an example of how to use [MQTTnet](https://github.com/chkr1011/MQTTnet) client in ASP.NET core web application with services.\n\n\n\nMQTT Client is running in `MqttClientService` by`IHostedService`, and it is a `singleton` service.\n\nTo access `MqttClientService` from other external services inject `MQTTClientServiceProvider` in service constructor.\n\nHere is an example\n\n```csharp\npublic class ExtarnalService\n{\n    private readonly IMqttClientService mqttClientService;\n    public ExtarnalService(MqttClientServiceProvider provider)\n    {\n        mqttClientService = provider.MqttClientService;\n    }\n}\n```\n\n\n\n**Configuration**\n\nConfigure your MQTT settings in `appSettings.json`\n\n```json\n\"BrokerHostSettings\": {\n    \"Host\": \"localhost\",\n    \"Port\": 1883\n  },\n\n  \"ClientSettings\": {\n    \"Id\": \"5eb020f043ba8930506acbdd\",\n    \"UserName\": \"rafiul\",\n    \"Password\": \"12345678\"\n  },\n```\n\n\n\n**Reconnecting**\n\nReference Code：https://github.com/dotnet/MQTTnet/blob/master/Samples/Client/Client_Connection_Samples.cs\nThe implemented code is in `MqttClientService.cs`\n\n1. Reconnect_Using_Event\n\n   ```c#\n   public async Task HandleDisconnectedAsync(MqttClientDisconnectedEventArgs eventArgs)\n   {\n   \n         if (e.ClientWasConnected)\n         {\n              // Use the current options as the new options.\n              await mqttClient.ConnectAsync(mqttClient.Options);\n         }\n   }\n   \n   ```\n\n   \"Reconnect_Using_Event\" is not recommended, so the code is commented out ,the following code is recommended \"Reconnect_Using_Time\"\n\n2. Reconnect_Using_Timer\n\n   ```c#\n   public async Task StartAsync(CancellationToken cancellationToken)\n   {\n          await mqttClient.ConnectAsync(options);\n          /* \n           * This sample shows how to reconnect when the connection was dropped.\n           * This approach uses a custom Task/Thread which will monitor the connection status.\n           * This is the recommended way but requires more custom code!\n           */\n           _ = Task.Run(\n              async () =\u003e\n              {\n                  // // User proper cancellation and no while(true).\n                  while (true)\n                  {\n                      try\n                      {\n                          // This code will also do the very first connect! So no call to _ConnectAsync_ is required in the first place.\n                          if (!await mqttClient.TryPingAsync())\n                          {\n                              await mqttClient.ConnectAsync(mqttClient.Options, CancellationToken.None);\n   \n                              // Subscribe to topics when session is clean etc.\n                              _logger.LogInformation(\"The MQTT client is connected.\");\n                          }\n                      }\n                      catch (Exception ex)\n                      {\n                          // Handle the exception properly (logging etc.).\n                          _logger.LogError(ex, \"The MQTT client  connection failed\");\n                      }\n                      finally\n                      {\n                          // Check the connection state every 5 seconds and perform a reconnect if required.\n                          await Task.Delay(TimeSpan.FromSeconds(5));\n                      }\n                  }\n              });\n   \n    }\n   ```\n\n   \n\n**Now do whatever you want to do with `MQTTClientService`!**","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafiulgits%2Fmqtt-client-dotnet-core","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frafiulgits%2Fmqtt-client-dotnet-core","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafiulgits%2Fmqtt-client-dotnet-core/lists"}