+ All Categories
Home > Technology > JavaZone 2016 : MQTT and CoAP for the Java Developer

JavaZone 2016 : MQTT and CoAP for the Java Developer

Date post: 16-Apr-2017
Category:
Upload: mark-west
View: 280 times
Download: 1 times
Share this document with a friend
31
MQTT and CoAP for the Java Developer Mark West
Transcript
Page 1: JavaZone 2016 : MQTT and CoAP for the Java Developer

MQTTandCoAPfortheJavaDeveloper

MarkWest

Page 2: JavaZone 2016 : MQTT and CoAP for the Java Developer

50000000000connecteddevices

by2020!!!Source:Cisco

Page 3: JavaZone 2016 : MQTT and CoAP for the Java Developer

TheIoT Marketwillbeworth

$11000000000000by2025!!!

Source:TheMotleyFool

Page 4: JavaZone 2016 : MQTT and CoAP for the Java Developer

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)

Page 5: JavaZone 2016 : MQTT and CoAP for the Java Developer

IoT ProtocolStacks

ApplicationLayer

TransportLayer

NetworkLayer InternetProtocol

TCPTransmissionControlProtocol

UDPUserDatagramProtocol

HTTP MQTT MQTT-SN CoAP

Page 6: JavaZone 2016 : MQTT and CoAP for the Java Developer

IoT ProtocolStacks

ApplicationLayer

TransportLayer

NetworkLayer InternetProtocol

TCPTransmissionControlProtocol

UDPUserDatagramProtocol

HTTP MQTT MQTT-SN CoAP

Page 7: JavaZone 2016 : MQTT and CoAP for the Java Developer

WhynotjustuseHTTP?

Page 8: JavaZone 2016 : MQTT and CoAP for the Java Developer

DrawbackswithHTTP(IoT Specific)

• HighpowerandbandwidthconsumptionduetoverbosityofHTTPandTCP.

• NobuiltinretryabilityorQualityofService.

• Pointtopoint.

• Complexity– multiplemethodsandreturncodes.

ApplicationLayerHTTP

TransportLayerTCP

NetworkLayerIP

HTTP

TCP

IP

Page 9: JavaZone 2016 : MQTT and CoAP for the Java Developer

HTTPisnotasilverbulletforconnectingdevicestotheIoT

Page 10: JavaZone 2016 : MQTT and CoAP for the Java Developer

IoT ProtocolStacks:MQTT

ApplicationLayer

TransportLayer

NetworkLayer InternetProtocol

TCPTransmissionControlProtocol

UDPUserDatagramProtocol

HTTP MQTT MQTT-SN CoAP

Page 11: JavaZone 2016 : MQTT and CoAP for the Java Developer

IntroducingMQTT

• MessageQueueTelemetryTransport.

• ReducesamountofbytesflowingoverthewirecomparedtoHTTP– reducespowerandbandwidthusage.

• Builtinretry/QoSMechanism.

• Pub/Sub architectureandMessageBrokerallowsforflexibilityincommunicationpatterns.

• SimpleAPI,requiresminimal“plumbing”code.

ApplicationLayerMQTT

TransportLayerTCP

NetworkLayerIP

MQTT

TCP

IP

Page 12: JavaZone 2016 : MQTT and CoAP for the Java Developer

MQTTMessageBroker

MQTTBrokerTopic

“Lightbulbs”

Publish “on”

Page 13: JavaZone 2016 : MQTT and CoAP for the Java Developer

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.

Page 14: JavaZone 2016 : MQTT and CoAP for the Java Developer

MQTTSecurity

• ApplicationLayer:• MQTTallowsforaUserIDandPasswordtobetransmittedonconnection.• AuthorizationandAuthenticationcanthenbehandledbytheMQTTBroker.

• TransportLayer:• TransportLayerSecurity(TLS)allowsforencryptionandverificationofidentity.

ApplicationLayerMQTT

TransportLayerTCP

NetworkLayerIP

MQTT

TCP

IP

Page 15: JavaZone 2016 : MQTT and CoAP for the Java Developer

OtherFeaturesofMQTT

Lastwillandtestament

• AnormalMQTTmessagewithspecifiedtopic,messageandQoS.

• SpecifiedbyClientonconnectiontotheBroker.

• IftheClientabruptlydisconnectstheLWTmessagewillbesenttoalltopicsubscribers.

MQTToverWebSockets

• ModernwebbrowsersarenotbuilttounderstandMQTT.

• MQTToverWebSocketsprovidesamechanismforWebBrowserstodirectlycommunicatewithnMQTTBroker.

• NotallMQTTBrowserssupportWebSocket connections.

RetainedMessages

• AnormalMQTTmessagewithspecifiedtopic,messageandQoS.

• StoredbytheBrokeruntilexplicitlyremoved.

• Senttoallnewtopicsubscribersimmediatelyaftertheysubscribe.

• Usefulfortopicswithlowtraffic.

Page 16: JavaZone 2016 : MQTT and CoAP for the Java Developer

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

Page 17: JavaZone 2016 : MQTT and CoAP for the Java Developer

DrawbacksofMQTT

• MQTTisbuiltuponTCP:• Highoverheadsresultinhighpowerandbandwidthrequirements.

• Notsuitableforallconstraineddevices.

• MQTTrequiresaMessageBroker:• Singlepointoffailure.• Additionalcomponenttomaintain.

• Publish/Subscribeisoverkillforsomeusecases.

ApplicationLayerMQTT

TransportLayerTCP

NetworkLayerIP

MQTT

TCP

IP

Page 18: JavaZone 2016 : MQTT and CoAP for the Java Developer

TCPvs.UDPTransmissionControlProtocol

• Headersizeis20bytes.• Connectionbased,reliable.• Heavyweight- handlesreliability,deliveryorderandcongestioncontrol.• Suitsapplicationsthatrequirehighreliability.• UsedbyHTTP,FTP,etc.

UserDatagramProtocol

• Headersizeis8bytes.• Connectionless,unreliable.• Lightweight– simpletransportlayerontopofIP.• Suitsapplicationsthatrequirespeed.• UsedbyDNS,VOIP,etc.

Page 19: JavaZone 2016 : MQTT and CoAP for the Java Developer

UDPbasedProtocolsforIoT

ApplicationLayer

TransportLayer

NetworkLayer InternetProtocol

TCPTransmissionControlProtocol

UDPUserDatagramProtocol

HTTP MQTT MQTT-SN CoAP

Page 20: JavaZone 2016 : MQTT and CoAP for the Java Developer

UDPbasedProtocolsforIoT :MQTT-SN

ApplicationLayer

TransportLayer

NetworkLayer InternetProtocol

TCPTransmissionControlProtocol

UDPUserDatagramProtocol

HTTP MQTT MQTT-SN CoAP

Page 21: JavaZone 2016 : MQTT and CoAP for the Java Developer

MQTT-SN(SN=SensorNetworks)• Extremelyconstraineddevicescommunicatingoverwirelessnetworks.

• ReducedmessagesizecomparetoMQTT– forexampleusesTopicIDinsteadofTopicName.

• SupportforQoS,retainedmessagesandLWT.

• InadditiontosupportingUDPoverIP,MQTT-SNalsohandlesserialconnections.

• SupportedbyEclipsePAHO(noJavaClientthough).

• RequiresagatewaytobridgebetweenMQTT-SNenableddevicesandTCP/IP(MQTTBroker).

ApplicationLayerHTTP

TransportLayerTCP

NetworkLayerIP

MQTT

UDP

IP

Page 22: JavaZone 2016 : MQTT and CoAP for the Java Developer

UDPbasedProtocolsforIoT :CoAP

ApplicationLayer

TransportLayer

NetworkLayer InternetProtocol

TCPTransmissionControlProtocol

UDPUserDatagramProtocol

HTTP MQTT MQTT-SN CoAP

Page 23: JavaZone 2016 : MQTT and CoAP for the Java Developer

IntroducingCoAP

• ConstrainedApplicationProtocol.

• BasedonRESTfularchitecture- withextensions.

• UDP+CoAP minimizesamountofbytesflowingoverthewirecomparedtoHTTP– suitableforextremelyconstrainedenvironments.

• Builtinretrymechanism(QoS).

• Request/Responsemodela-laHTTP(nobroker).

ApplicationLayerMQTT

TransportLayerTCP

NetworkLayerIP

CoAP

UDP

IP

Page 24: JavaZone 2016 : MQTT and CoAP for the Java Developer

Retry/QoS withCoAP

ConfirmableMessageRequiresAcknowledgement.

Non-confirmableMessage“FireandForget”

• AfundamentaldifferencebetweenofUDPandTCPisthatUDPsacrificesreliabilityforspeed.

• CoAP addressesthatbyimplementingthefollowingtwotypesofmessage.

Page 25: JavaZone 2016 : MQTT and CoAP for the Java Developer

CoAP Security

• CoAP isunabletotakeadvantageoftheTCPbasedsecuritymechanismssuchasTLS.

• DatagramTransportLayerSecurity(DTLS)totherescue!

• DTLSisbasicallyanimplementationofTLSforUDP,withtheaddedfunctionalityrequiredfortheconnectionlessUDP(forexamplepacketlossandordering).

Page 26: JavaZone 2016 : MQTT and CoAP for the Java Developer

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

Page 27: JavaZone 2016 : MQTT and CoAP for the Java Developer

CoAP Example• LightreadingsLightsensorthroughaGETrequestwith“Observe”flagactivated.

• LightreceivesnotificationofchangesintheLightSensorreadingsandusesthesetoadjustit’sownstatus.

• Peertopeer– nocentralcontroller.

Source:https://www.thoughtworks.com/insights/blog/coap-and-web-things-watching-things

Page 28: JavaZone 2016 : MQTT and CoAP for the Java Developer

GettingstartedwithCoAP

• OpenSourceJavaClientforCoAP.

• SupportsallCoAP features.

• PartoftheEclipseIoT Initiative.

• Pro-tip:SeealsotheScandium subprojectforaDTLSimplementation.

Page 29: JavaZone 2016 : MQTT and CoAP for the Java Developer

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

Page 30: JavaZone 2016 : MQTT and CoAP for the Java Developer

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.

Page 31: JavaZone 2016 : MQTT and CoAP for the Java Developer

Thanksforlistening!

[email protected]@markawest


Recommended