-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[DONT MERGE] Wiki Update: Updated Client.md #1732
base: master
Are you sure you want to change the base?
Conversation
@TCROC please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your original work. Should You wish to Submit materials that are not Your original work, You may Submit them separately 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly license in the Submission to reproduce, prepare derivative works of, publicly display, publicly perform, b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
DO NOT MERGE THIS PR
This is simply an update to the Client.md page in the wiki. Specifically the aws section:
https://github.com/dotnet/MQTTnet/wiki/Client#connecting-with-amazon-aws
GitHub does not currently support PRs for Wikis. StackOverflow recommended this as the next best way to contribute to a wiki. Anyways, here is my contribution as promised. All that is changed is what is shown in the below image. Simply copying and pasting this file into the wiki should update it. Feel free to make any changes to it.
And here is a preview so you can look at it before merging it in:
These samples are only valid for version 3. Please check the Samples directory for samples and documentation for version 4.0+
This video tutorial shows how to setup a MQTTnet client for Windows (UWP and IoT Core): https://www.youtube.com/watch?v=PSerr2fvnyc
Preparation
The following code shows how to create a new MQTT client in the most simple way using the MqttFactory.
Client options
All options for the MQTT client are bundled in one class named MqttClientOptions. It is possible to fill options manually in code via the properties but it is recommended to use the MqttClientOptionsBuilder. This class provides a fluent API and allows setting the options easily by providing several overloads and helper methods. The following code shows how to use the builder with several random options.
Securing passwords
The default implementation uses a
string
to hold the client password. However this is a security vulnerability because the password is stored in the heap as clear text. It is recommended to use aSecureString
for this purpose. But this class is not available for all supported platforms (UWP, netstandard 1.3). This library does not implement it because for other platforms custom implementations like async encryption are required. It is recommended to implement an own IMqttClientCredentials class which returns the decrypted password but does not store it unencrypted.TCP connection
The following code shows how to set the options of the MQTT client to make use of a TCP based connection.
Secure TCP connection
The following code shows how to use a TLS secured TCP connection (properties are only set as reference):
Certificate based authentication
The following code shows how to connect to the server by using certificate based authentication:
Using a Custom CA with TLS
If your server is using a certificate by an unrecognized CA (that is not in the system store), you'll need to add a callback
to verify if the certificate is valid. Note this does not work prior to .NET 5.
TLS using a client certificate
The following code shows how to connect to the server by using a client certificate based authentication:
The CA certificate is in the *.crt format, the client certificate should be in *.pfx and should have the password that was used to export the file from the private key and certificate originally. The *.pfx file can becreated using openssl as below:
Dealing with special certificates
In order to deal with special certificate errors a special validation callback is available (.NET Framework & netstandard). For UWP apps, a property is available.
WebSocket connection
In order to use a WebSocket communication channel the following code is required.
Also secure web socket connections can be used via calling the UseTls() method which will switch the protocol from ws:// to wss://. Usually the sub protocol is required which can be added to the URI directly or to a dedicated property.
Connecting
After setting up the MQTT client options a connection can be established. The following code shows how to connect with a server. The
CancellationToken.None
can be replaced by a valid CancellationToken, of course.Reconnecting
If the connection to the server is lost the Disconnected event is fired. The event is also fired if a call to ConnectAsync has failed because the server is not reachable etc. This allows calling the ConnectAsync method only one time and dealing with retries etc. via consuming the Disconnected event. If the reconnect fails the Disconnected event is fired again. The following code shows how to setup this behavior including a short delay. The
CancellationToken.None
can be replaced by a valid CancellationToken, of course.Consuming messages
The following code shows how to handle incoming messages:
It is also supported to use an async method instead of a synchronized one like in the above example.
Subscribing to a topic
Once a connection with the server is established subscribing to a topic is possible. The following code shows how to subscribe to a topic after the MQTT client has connected.
Publishing messages
Application messages can be created using the properties directly or via using the MqttApplicationMessageBuilder. This class has some useful overloads which allows dealing with different payload formats easily. The API of the builder is a fluent API. The following code shows how to compose an application message and publishing them:
It is not required to fill all properties of an application message. The following code shows how to create a very basic application message:
RPC calls
The extension MQTTnet.Extensions.Rpc (available as nuget) allows sending a request and waiting for the matching reply. This is done via defining a pattern which uses the topic to correlate the request and the response. From client usage it is possible to define a timeout. The following code shows how to send a RPC call.
The device (Arduino, ESP8266 etc.) which responds to the request needs to parse the topic and reply to it. The following code shows how to implement the handler.
Connecting with Amazon AWS
MQTTnet AWS Transport Support
dotnet 7 (specifically tested with 7.0.203):
Example Project: https://github.com/TCROC/aws-iot-custom-auth
Mono (specifically tested with Unity Game Engine 2021.3.24f1)
Example Project: https://github.com/TCROC/mqttnet-unity-alpn.git
^ This project is currently being used to debug TCP as well
Websockets
AWS Sample:
https://github.com/aws-samples/aws-iot-core-dotnet-app-mqtt-over-websockets-sigv4
TCP with certificates
NOTE: The currently tested Unity Game Engine 2021.3.24f1 does not support TCP
.pfx
format:Troubleshooting
Before digging dip on certificates, make sure that
If you're absolutely sure of the above, then start troubleshooting certificates.
Some small snippets
Using the client in ASP.NET Core
When using the client there is no difference between the .Net Framework, .Net Core or ASP.NET Core. The configuration above applies. The client cannot be made available using dependency injection.
A sample example is available here mqtt-client-dotnet-core