JavaZone 2016 : MQTT and CoAP for the Java Developer
-
Upload
mark-west -
Category
Technology
-
view
280 -
download
1
Transcript of JavaZone 2016 : MQTT and CoAP for the Java Developer
MQTTandCoAPfortheJavaDeveloper
MarkWest
50000000000connecteddevices
by2020!!!Source:Cisco
TheIoT Marketwillbeworth
$11000000000000by2025!!!
Source:TheMotleyFool
Internet101:BuildingBlocks
ApplicationLayer(i.e.HTTP,FTP,DNS)
TransportLayer(i.e.TCP,UDP)
NetworkLayer(i.e.IPv4,IPv6,AppleTalk)
DataLinkLayer(i.e.DeviceDrivers,NetworkCard)
DataTransferMedium(i.e.WiFi,Ethernet,BLE)
IoT ProtocolStacks
ApplicationLayer
TransportLayer
NetworkLayer InternetProtocol
TCPTransmissionControlProtocol
UDPUserDatagramProtocol
HTTP MQTT MQTT-SN CoAP
IoT ProtocolStacks
ApplicationLayer
TransportLayer
NetworkLayer InternetProtocol
TCPTransmissionControlProtocol
UDPUserDatagramProtocol
HTTP MQTT MQTT-SN CoAP
WhynotjustuseHTTP?
DrawbackswithHTTP(IoT Specific)
• HighpowerandbandwidthconsumptionduetoverbosityofHTTPandTCP.
• NobuiltinretryabilityorQualityofService.
• Pointtopoint.
• Complexity– multiplemethodsandreturncodes.
ApplicationLayerHTTP
TransportLayerTCP
NetworkLayerIP
HTTP
TCP
IP
HTTPisnotasilverbulletforconnectingdevicestotheIoT
IoT ProtocolStacks:MQTT
ApplicationLayer
TransportLayer
NetworkLayer InternetProtocol
TCPTransmissionControlProtocol
UDPUserDatagramProtocol
HTTP MQTT MQTT-SN CoAP
IntroducingMQTT
• MessageQueueTelemetryTransport.
• ReducesamountofbytesflowingoverthewirecomparedtoHTTP– reducespowerandbandwidthusage.
• Builtinretry/QoSMechanism.
• Pub/Sub architectureandMessageBrokerallowsforflexibilityincommunicationpatterns.
• SimpleAPI,requiresminimal“plumbing”code.
ApplicationLayerMQTT
TransportLayerTCP
NetworkLayerIP
MQTT
TCP
IP
MQTTMessageBroker
MQTTBrokerTopic
“Lightbulbs”
Publish “on”
MQTTQualityofService
• Guaranteesdelivery.
• QoS levelsetforeachmessagebytheClient.
QoS Level0• Atmostonce.• Fireandforget– no deliveryguaranteed.• Fastest.
QoS Level1
• Atleastonce.• Guaranteeddelivery.• Messagerepeatedly sentuntilanacknowledgement
isreceivedfromrecipient.• Duplicatemessages canbereceived.
QoS Level2
• Exactlyonce.• Guaranteeddelivery.• Eachmessagewillbereceivedexactlyonce.• Requiresanextraroundofcommunicationbetween
senderandreceiver.• Verbose solution,onlyuseifneeded.
MQTTSecurity
• ApplicationLayer:• MQTTallowsforaUserIDandPasswordtobetransmittedonconnection.• AuthorizationandAuthenticationcanthenbehandledbytheMQTTBroker.
• TransportLayer:• TransportLayerSecurity(TLS)allowsforencryptionandverificationofidentity.
ApplicationLayerMQTT
TransportLayerTCP
NetworkLayerIP
MQTT
TCP
IP
OtherFeaturesofMQTT
Lastwillandtestament
• AnormalMQTTmessagewithspecifiedtopic,messageandQoS.
• SpecifiedbyClientonconnectiontotheBroker.
• IftheClientabruptlydisconnectstheLWTmessagewillbesenttoalltopicsubscribers.
MQTToverWebSockets
• ModernwebbrowsersarenotbuilttounderstandMQTT.
• MQTToverWebSocketsprovidesamechanismforWebBrowserstodirectlycommunicatewithnMQTTBroker.
• NotallMQTTBrowserssupportWebSocket connections.
RetainedMessages
• AnormalMQTTmessagewithspecifiedtopic,messageandQoS.
• StoredbytheBrokeruntilexplicitlyremoved.
• Senttoallnewtopicsubscribersimmediatelyaftertheysubscribe.
• Usefulfortopicswithlowtraffic.
GettingstartedwithMQTTMQTTClientImplementation MQTTBrokerImplementations
• OpenSourceMQTTClientsforJava,JavaScript,C,C++,Go,Android,C#,Pythonandmore!
• PartoftheEclipseIoT Initiative.
PubliclyavailablebrokersthatsupportWebSockets:• iot.eclipse.org• test.mosquitto.org• broker.mqttdashboard.org
MQTTBrokerstodownloadandinstall:• EclipseMosquitto• ActiveMQ• RabbitMQ
DrawbacksofMQTT
• MQTTisbuiltuponTCP:• Highoverheadsresultinhighpowerandbandwidthrequirements.
• Notsuitableforallconstraineddevices.
• MQTTrequiresaMessageBroker:• Singlepointoffailure.• Additionalcomponenttomaintain.
• Publish/Subscribeisoverkillforsomeusecases.
ApplicationLayerMQTT
TransportLayerTCP
NetworkLayerIP
MQTT
TCP
IP
TCPvs.UDPTransmissionControlProtocol
• Headersizeis20bytes.• Connectionbased,reliable.• Heavyweight- handlesreliability,deliveryorderandcongestioncontrol.• Suitsapplicationsthatrequirehighreliability.• UsedbyHTTP,FTP,etc.
UserDatagramProtocol
• Headersizeis8bytes.• Connectionless,unreliable.• Lightweight– simpletransportlayerontopofIP.• Suitsapplicationsthatrequirespeed.• UsedbyDNS,VOIP,etc.
UDPbasedProtocolsforIoT
ApplicationLayer
TransportLayer
NetworkLayer InternetProtocol
TCPTransmissionControlProtocol
UDPUserDatagramProtocol
HTTP MQTT MQTT-SN CoAP
UDPbasedProtocolsforIoT :MQTT-SN
ApplicationLayer
TransportLayer
NetworkLayer InternetProtocol
TCPTransmissionControlProtocol
UDPUserDatagramProtocol
HTTP MQTT MQTT-SN CoAP
MQTT-SN(SN=SensorNetworks)• Extremelyconstraineddevicescommunicatingoverwirelessnetworks.
• ReducedmessagesizecomparetoMQTT– forexampleusesTopicIDinsteadofTopicName.
• SupportforQoS,retainedmessagesandLWT.
• InadditiontosupportingUDPoverIP,MQTT-SNalsohandlesserialconnections.
• SupportedbyEclipsePAHO(noJavaClientthough).
• RequiresagatewaytobridgebetweenMQTT-SNenableddevicesandTCP/IP(MQTTBroker).
ApplicationLayerHTTP
TransportLayerTCP
NetworkLayerIP
MQTT
UDP
IP
UDPbasedProtocolsforIoT :CoAP
ApplicationLayer
TransportLayer
NetworkLayer InternetProtocol
TCPTransmissionControlProtocol
UDPUserDatagramProtocol
HTTP MQTT MQTT-SN CoAP
IntroducingCoAP
• ConstrainedApplicationProtocol.
• BasedonRESTfularchitecture- withextensions.
• UDP+CoAP minimizesamountofbytesflowingoverthewirecomparedtoHTTP– suitableforextremelyconstrainedenvironments.
• Builtinretrymechanism(QoS).
• Request/Responsemodela-laHTTP(nobroker).
ApplicationLayerMQTT
TransportLayerTCP
NetworkLayerIP
CoAP
UDP
IP
Retry/QoS withCoAP
ConfirmableMessageRequiresAcknowledgement.
Non-confirmableMessage“FireandForget”
• AfundamentaldifferencebetweenofUDPandTCPisthatUDPsacrificesreliabilityforspeed.
• CoAP addressesthatbyimplementingthefollowingtwotypesofmessage.
CoAP Security
• CoAP isunabletotakeadvantageoftheTCPbasedsecuritymechanismssuchasTLS.
• DatagramTransportLayerSecurity(DTLS)totherescue!
• DTLSisbasicallyanimplementationofTLSforUDP,withtheaddedfunctionalityrequiredfortheconnectionlessUDP(forexamplepacketlossandordering).
ObserveaResource
• ActivatedbysendingaGETrequestwithObserveflagswitchedon.
• GoodalternativewhenMQTTandHTTPpollingisimpossible.
• Resultsinstreamingnotificationswhenresourceischanged.
• Canbeterminatedatanypointbybothparties.
ResourceDiscovery
• CoAP supportsresourcediscoverya-laREST.
• Serversprovidealistoftheirresourcesat/.well-known/core.
• Allowclientstodiscoverresources,andfinoutwhichmediatypestheyare.
ContentNegotiation
• SameasstandardHTTP.
• Clientscanexpressapreferredrepresentationofaresource(i.e.XML,JSON,PlainText).
• Serverscantellclientswhattheyaregetting.
OtherFeaturesofCoAP
CoAP Example• LightreadingsLightsensorthroughaGETrequestwith“Observe”flagactivated.
• LightreceivesnotificationofchangesintheLightSensorreadingsandusesthesetoadjustit’sownstatus.
• Peertopeer– nocentralcontroller.
Source:https://www.thoughtworks.com/insights/blog/coap-and-web-things-watching-things
GettingstartedwithCoAP
• OpenSourceJavaClientforCoAP.
• SupportsallCoAP features.
• PartoftheEclipseIoT Initiative.
• Pro-tip:SeealsotheScandium subprojectforaDTLSimplementation.
Summary• NosilverbulletforconnectingThingstotheInternet.
• Manyusecasesrequireacombinationofprotocols.
• Threequestionstoaskwhenchoosing:1. Doyouneedthebenefitsofa
PublishSubscribearchitecture?2. Whattransportprotocolsdo
yourthingssupport?3. Isbatterylifeand/orbandwidth
anissue?
ApplicationLayer
TransportLayer
NetworkLayer InternetProtocol
TCPTransmissionControlProtocol
UDPUserDatagramProtocol
HTTP MQTT MQTT-SN CoAP
WhataboutHTTP2?
• HTTP2offersmanyimprovementsoverHTTP1.1thatcanmakeitattractiveintheIoT space.
ComparedtoMQTT
• HTTP2lacksguaranteeddelivery.
• HTTP2moreverbosethatMQTT.
• HTTP2supportspub/subandrequest/response.
ComparedtoCoAP
• CoAP andHTTP2supportserverpush(i.e.Observe).
• CoAP builtforREALLYconstraineddevicesandnetworks.
• HTTP2builtforTCP,notUDP.
ComparedtoHTTP1.1
• HTTP2introducesheadercompression andisabinaryprotocol– lessbytesoverthewire.
• HTTP2supportspub/subandrequest/response.
Thanksforlistening!
[email protected]@markawest