\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{otavio021} \pdfinfo{ /Title (mqtt-lightweight-messaging-protocol.pdf) /Creator (Cheatography) /Author (otavio021) /Subject (MQTT - Lightweight Messaging Protocol Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{190204} \definecolor{LightBackground}{HTML}{F7F7F7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{MQTT - Lightweight Messaging Protocol Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{otavio021} via \textcolor{DarkBackground}{\uline{cheatography.com/35559/cs/11177/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}otavio021 \\ \uline{cheatography.com/otavio021} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 15th March, 2017.\\ Updated 15th March, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{1.59264 cm} x{3.38436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic details and Terminoly}} \tn % Row 0 \SetRowColor{LightBackground} MQTT Control Packet & A packet of information sent across the network \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Application Message & The data carried by the protocol \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Topic Name & A label attached to an Application Message that can be subscribed by clients \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} Topic Filter & An expression used to express interest in one or more topics (can use wildcards) \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} Session & Stateful interaction between a client and a server \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Note}}: TCP ports 1883 (default) and 8883 (TLS) are commonly used for MQTT.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.84609 cm} x{4.13091 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Data Representations}} \tn % Row 0 \SetRowColor{LightBackground} Bits & Bits in a byte, from 7 (MSB) to 0 (LSB). \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{Integers} & Big-endian ordered, 16-bits \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{Strings} & UTF-8 strings, prefixed by its length \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{4.4793 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Quality of Service}} \tn % Row 0 \SetRowColor{LightBackground} At Most Once & 0 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} At Least Once & 1 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Exactly Once & 2 \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Topic Names}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Must be at least 1 character long} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Case sensitive} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Can include spaces} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Name structured divided by slashes} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Example}}: `/news/sports/europe`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.05271 cm} p{0.4577 cm} x{3.06659 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Wildcards}} \tn % Row 0 \SetRowColor{LightBackground} Multi level & \# & Used to match any number of levels within a topic tree, including the parent level itself. \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Single level & + & Used to match a single level within a topic tree. \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} Reserved & \$ & Topics starting with the dollar sign ('\$') are reserved for server purposes and should not be used by clients. \tn % Row Count 11 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\bf{Notes}}: \newline Multi-level wildcard must always be the last symbol on the filter. Either on its own or preceded by the topic level separator. \newline \newline Single-level: can be used in conjunction with the multi-level one. \newline \newline {\bf{Examples}}: \newline `+/sports/\#` - valid \newline `sports+` - not valid} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{MQTT Control Packet Structure}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{The structure is formed by the aggregation of 3 sub-structures: fixed header, variable length header and payload.% Row Count 3 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.59264 cm} x{3.38436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{MQTT Control Packet Structure: Fixed Header}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Field}} & {\bf{Description}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Control Packet Type & 4 bit representation of the packet type. \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Flags & 4 bit flags specific to each packet type. \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} Remaining Length & Number of bytes remaining in the packet. \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Note \#1}}: Control Packet Type and the Flags are stored in a single byte. \newline {\bf{Note \#2}}: The Remaining Length does not include the bytes used to encode itself} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.4931 cm} x{3.4839 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{MQTT Control Packet Structure: Variable Length}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Field}} & {\bf{Description}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Packet Identifier & Used to establish a relationship between different MQTT Control Packets \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} Payload & A payload associated with the MQTT Control Packet \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Note}}: for the PUBLISH control packet, the payload is the application message.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{MQTT Control Packets}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Packet}} & {\bf{Name}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} CONNECT & Client request to connect \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} CONNACK & Connect ACK \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} PUBLISH & Publish message \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} PUBACK & Publish ACK \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} PUBREC & Publish received \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} PUBREL & Publish release \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} PUBCOMP & Publish complete \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} SUBSCRIBE & Client subscribe request \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} SUBACK & Subscribe ACK \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} UNSUBSCRIBE & Unsubscribe request \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} UNSUBACK & Unsubscribe ACK \tn % Row Count 12 (+ 1) % Row 12 \SetRowColor{LightBackground} PINGREQ & Ping request \tn % Row Count 13 (+ 1) % Row 13 \SetRowColor{white} PINGRESP & Ping response \tn % Row Count 14 (+ 1) % Row 14 \SetRowColor{LightBackground} DISCONNECT & Client disconnecting \tn % Row Count 15 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{CONNECT}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Important elements}}: \newline % Row Count 1 (+ 1) - Connect Flags: to specify the behavior of the connection. \newline % Row Count 3 (+ 2) - Keep Alive: maximum time interval, in seconds, that can elapse between client transmission of control packets. \newline % Row Count 6 (+ 3) {\bf{Connect Flags}}: \newline % Row Count 7 (+ 1) - Clean Session: controls the lifetime of the session state (0 to resume state, 1 to discard previous state). \newline % Row Count 10 (+ 3) - Will Flag: indicates that a will message is to be sent upon dirty client disconnection. \newline % Row Count 12 (+ 2) - Will QoS: indicates QoS level for the will message. \newline % Row Count 14 (+ 2) - Will Retain: indicates the retain policy for the will message. \newline % Row Count 16 (+ 2) - Password: indicates whether (1) or not (0) a password must be present in the payload. \newline % Row Count 18 (+ 2) - User Name: indicates whether (1) or not (0) a user name must be present in the payload. \newline % Row Count 20 (+ 2) {\bf{Payload}}: \newline % Row Count 21 (+ 1) Length prefixed fields whose presence is determined according to the value of flags in the variable header. The fields are, in order: client identifier, will topic, will message, user name, password.% Row Count 25 (+ 4) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{CONNACK}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Sent by the server in response to a connection request sent by a client. The important elements on the packet structure are: connect acknowledgement flags and the connect return code.% Row Count 4 (+ 4) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Note}}: if a session is already present and the connection request does not have the clean session, the server must set the session present flag to 1.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{0.84609 cm} x{4.13091 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{CONNACK: Error Codes}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Code}} & {\bf{Description}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} 0x00 & Connection accepted \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} 0x01 & Connection refused (protocol version) \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} 0x02 & Connection refused (identifier rejected) \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} 0x03 & Connection refused (server unavailable) \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} 0x04 & Connection refused (bad user/password) \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} 0x05 & Connection refused (Unauthorized) \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} 6-255 & Reserved \tn % Row Count 13 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{PUBLISH}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{This packet is used to transport application messages for a client to a server or from a server to a client. \newline % Row Count 3 (+ 3) The important elements on the packet structure are: \newline % Row Count 5 (+ 2) - DUP Flag: indicates whether this is the first time the message is being sent (0) or whether it might be a re-delivery attempt (1) of a previous message. \newline % Row Count 9 (+ 4) - QoS Level: quality of service for an application message. \newline % Row Count 11 (+ 2) - Retain Flag: indicates that the application message and its QoS must be stored in the server and delivered to future subscribers of that topic.% Row Count 14 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Note}}: The QoS flag affect how many messages can be stored on the server and sent to the client.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Protocol Exchange: QoS 1}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Client/Server protocol interaction: \newline % Row Count 1 (+ 1) 1. Client \{\{fa-arrow-right\}\} PUBLISH \{\{fa-arrow-right\}\} Server \newline % Row Count 3 (+ 2) 2. Client \{\{fa-arrow-left\}\} PUBACK \{\{fa-arrow-left\}\} Server% Row Count 5 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Protocol Exchange: QoS 2}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Client/Server protocol interaction: \newline % Row Count 1 (+ 1) 1. Client \{\{fa-arrow-right\}\} PUBLISH \{\{fa-arrow-right\}\} Server \newline % Row Count 3 (+ 2) 2. Client \{\{fa-arrow-left\}\} PUBREC \{\{fa-arrow-left\}\} Server \newline % Row Count 5 (+ 2) 3. Client \{\{fa-arrow-right\}\} PUBREL \{\{fa-arrow-right\}\} Server \newline % Row Count 7 (+ 2) 4. Client \{\{fa-arrow-left\}\} PUBCOMP \{\{fa-arrow-left\}\} Server% Row Count 9 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Security}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{TLS is the recommended cryptographic protocol to be used with MQTT. Implementations should use port 8883.% Row Count 3 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}