알뜰살뜰 정보

통신프로토콜

쉬고싶은 거북이 2020. 3. 25. 10:31

1. MQTT(Message Queue Telemetry Transport)

MQTT는 TCP기반의 경량화된 Publish-Subscribe 메시지 프로토콜입니다. Publish-Subscribe은 SW아키텍처 스타일에서 등장하는 모델과 동일한 내용입니다. 출판/구독 아키턱처에서 특정 이벤트를 발생시키는 '출판자'와 해당 이벤트를 구독하는 '구독자'처럼 MQTT 프로토콜에서는 브로커(Broker)서버가 '토픽'이라고 부르는 이벤트 발생자와 구독자를 다대다(N:M)로 중개하는 메시지 버스 역할을 합니다.


'토픽'은 계층형 자료구조로 마치 디렉토리를 표기하는 것 처럼 /(슬래시)를 기준으로 계층적으로 데이터를 표현합니다. 이 '토픽'은 MQTT Broker를 통해 구독자인 다수의 클라이언트로 전달됩니다.


MQTT의 또 다른 특징은 QoS를 지원한다는 점 입니다. 서비스 종류에 따라 총 3단계의 QoS를 지원하고 있습니다.


2. CoAP (Constrained Application Protocol)

CoAP는 저전력 비동기 통신 프로토콜 입니다. MQTT와는 다르게 UDP를 기반으로 비동기 통신을 사용합니다. 당연히 더 가볍고 부하가 적습니다. 대신 TCP와 다르게 전송 여부 신뢰성을 확인할 수 없으므로 Confirmation/Non-Confirmation message, Reset Message 같은 메시지를 통해 보완을 하고 있습니다. 

프로토콜 경량화를 위해 UDP를 사용하는 것 뿐만 아니라 프로토콜 레이아웃도 단순화 하여 전문의 Header는 Binary 형태로 사이즈를 줄였습니다.

추가적인 특징으로 RESTFul 방식을 지원하며, 암호화를 위해 DTLS(Datagram Transport Layer Security)을 사용합니다.(TCP 기반이 아니므로 SSL/TLS는 사용 불가)


CoAP는 MQTT와 다르게 1:1 프로토콜이며 Pub/Sub 메세지큐 기능을 사용하지 못 합니다.

또한 응용프로그램 수준에서 QoS를 구현해야 하며 요청 혹은 응답 메시지에 confirmable 이나 nonconfirmable을 표기해서 전송하는데 confirmable 표기된 메시지를 받는 경우 Acknowledge 응답을 해야 합니다.


The question setting here is little bit misleading, because actually these protocols cannot at all be compared together. They are like TCP and IP, layers above each other. [1]

Websockets is a low level protocol to provide things that its 'competitor' RESTful http that is on same level does not provide: an always open channel without need for open and close on every request. [2]

MQTT provides a light weight way to publish or subscribe data. The confusion may be that those subscription are some sort of channels, but that is different type of channel. To make a constant open connection in MQTT you need Websockets AND MQTT at same time.

In IoT, as well in any design, you have to select if you need a stream or not (WebSockets vs RESTful) and about MQTT you may have to think whether you want a subscription and publishing mechanism on your app.

On some circumstances you may consider MQTT over WebSockets, if any common thing is around. [3]

Answer to the question:

You say you have a setup of a Rasperry Pi and several sensors around the place. If the sensors are far from Rasperry with their own controllers, you can use MQTT to collect the data. To store data to cloud, send the data in HTTP. In the cloud provide data through rest. [4]

For websockets there is no need, but if you find it useful, do use it.