Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MQTT with .NET Core


Published on

MQTT with .NET Core

Published in: Internet
  • Be the first to comment

  • Be the first to like this

MQTT with .NET Core

  1. 1. MQTT with .NET Core Mark Lechtermann 11.04.2018 dSPACE GmbH · Rathenaustr. 26 · 33102 Paderborn · Germany dSPACE-Confidential2
  2. 2. Agenda dSPACE-Confidential3 • Introduction MQTT • Topics • Broker • QoS • Retain Message • Last will and Testament • MQTT with .NET Core • MQTT.fx • MQTTnet • Demo
  3. 3. Who am i?  Mark Lechtermann  dSPACE GmbH  Software Architect  Lead .NET User Group Paderborn  Twitter: @MarkLechtermann  Email: dSPACE-Confidential4
  4. 4. MQTT Message Queuing Telemetry Transport dSPACE-Confidential5
  5. 5. What is MQTT?  A message protocol with „a small code footprint and on-the-wire footprint“  MQTT is a publish-subscribe-based messaging protocol  On top of TCP/IP  websocket support with mosquitto  Requires a broker (e.g. mosquitto, hivemq, Azure IO Hub, …)  ISO standard (ISO/IEC PRF 20922)  A message bus for: unreliable, high latency, low bandwidth  payload with a plain byte array  IANA Port: 1883 and with TLS 8883  … a lightweight message queueing and transport protocol dSPACE-Confidential6 IOT
  6. 6. A Broker - a partial representation of the application state.  Client can publish messages and subscribe to topics  Broker receives subscription requests from clients  Broker receives messages from clients and forward them dSPACE-Confidential7 Broker Client AClient B 2.<<publish to topic home/light/1>>3.<<receive message for home/light/1>>1.<<subscribe to topic home/light/1>>
  7. 7. MQTT Message : 2Byte + payload dSPACE-Confidential8 MESSAGE_TYPE QOS RETAIN Remaining Length 2 Bit4 Bit 8 Bit2 Bit MESSAGE_TYPE: CONNECT; CONNACK; PUBLISH; PUBACK; PUBREC; PUBREL; PUBCOMP; SUBSCRIBE; SUBACK; UNSUBSCRIBE; UNSUBACK; PINGREQ; PINGRESP; DISCONNECT  Maximum payload size 268,435,456 bytes  see:
  8. 8. Topics - Example  Topics allow clients to exchange information with defined semantics  Topic Expamples:  home/livingroom/light/1/status  home/livingroom/temperatur1/celsius  car/frontwheel/left/pressure  Germany/Paderborn/Company/dSPACE/LastTweet  subscription examples:  home/+/light/+/status  car/frontwheel/#  Germany/+/Company/+/LastTweet dSPACE-Confidential9
  9. 9. Payload  MQTT is playload agnostic  … a simple byte array  … a simple string  … or a JSON PUBLISH to home/livingroom/light/1 message dSPACE-Confidential10 {„rgb“: 0xff00ff, „brightness”: 0.5}
  10. 10. Retained Messages  “If the RETAIN flag is set to 1, in a PUBLISH Packet sent by a Client to a Server, the Server MUST store the Application Message and its QoS, so that it can be delivered to future subscribers whose subscriptions match its topic name. When a new subscription is established, the last retained message, if any, on each matching topic name MUST be sent to the subscriber.”   Publish a message on a topic and keep it on the broker  A new subscriber on the same topic receives the message dSPACE-Confidential11
  11. 11. Last will and testament  If the Will Flag is set to 1 this indicates that, if the Connect request is accepted, a Will Message MUST be stored on the Server and associated with the Network Connection. The Will Message MUST be published when the Network Connection is subsequently closed unless the Will Message has been deleted by the Server on receipt of a DISCONNECT Packet   Specifies a message in the CONNECT message with a topic  On unexpected client disconnection, sent this message to all subscibers dSPACE-Confidential12
  12. 12. Quality of Service (QoS)  QoS 0: At most once delivery (fire and forget)  QoS 1: At least once delivery (acknowledged delivery)  QoS 2: Exactly once delivery (assured delivery) dSPACE-Confidential13
  13. 13. Quality of Service (QoS) dSPACE-Confidential14 Publisher SubscriberBroker QoS 0 Publisher SubscriberBroker QoS 1 Publisher SubscriberBroker QoS 2 PUBLISH PUBLISH PUBLISH PUBLISH PUBLISH PUBLISH Delete Delete Delete Delete Delete StoreStore Store PUBACK PUBCOMP PUBREL PUBREC
  14. 14. Security  SSL/TLS support  Username/Password  Encrypt payload (data/payload agnostic)  IOT security should not be underestimated!  SSL/TLS is a must-have dSPACE-Confidential15
  15. 15. Broker and Clients  Mosquitto   HivenMQ   Azure IOT Hub   MQTT.fx   Eclipse Paho   MQTTnet  dSPACE-Confidential16
  16. 16. MQTTnet with .NET Core >> dotnet new >> dotnet add package MQTTnet --version 2.7.0 >> dotnet restore >> code . dSPACE-Confidential17
  17. 17. MQTTNET – Connect var factory = new MqttFactory(); var client = factory.CreateMqttClient(); var options = new MqttClientOptionsBuilder() .WithClientId(Guid.NewGuid().ToString()) .WithTcpServer("localhost") .Build(); client.Connected += async (s, e) => { await client.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build()); }; client.ConnectAsync(options); dSPACE-Confidential18
  18. 18. MQTTNET – Message Received client.ApplicationMessageReceived += (sender, e) => { Console.WriteLine(Encoding.UTF8.GetString(e.ApplicationMessage.Payload)); }; dSPACE-Confidential19
  19. 19. MQTTNET – Publish Message var message = new MqttApplicationMessageBuilder() .WithTopic("test") .WithPayload("my text") .Build(); client.PublishAsync(message); dSPACE-Confidential20
  20. 20. … and what is with REST?  HTTP/REST is useful to handle with documents and resources  MQTT is useful to handle messages  HTTP/REST can be complex and is not always the best solution for simple messages  The MQTT packet size is 2 byte + payload  MQTT supports 1-to-1, 1-to-many and many-to-many messages  request and response vs. publisher and subscriber dSPACE-Confidential21
  21. 21. Thank you for listening! dSPACE-Confidential22
  22. 22. dSPACE-Confidential23 Important Information! © 2018, dSPACE GmbH All rights reserved. Written permission is required for reproduction of all or parts of this publication. The source must be stated in any such reproduction. This publication and the contents hereof are subject to change without notice. Benchmark results are based on a specific application. Results are generally not transferable to other applications. Brand names or product names are trademarks or registered trademarks of their respective companies or organizations.