2

I need to understand the difference between HTTP and MQTT connection.

I want to send data from a Server to a Client without using port forwarding.

If I understand, using HTTP is not possible; so with HTTP the client will connect to the server and ask( i.e. GET or POST request ) and receive data from it.

But with MQTT is possible the opposite? I mean that the server could send data to the client without having received a request? Simply because the client has subscribed to the broker topic?

1 Answer 1

4

Yes

(I'm not sure what else to say here, what you have described is true).

HTTP is a transactional request/response protocol:

  • A client opens a connection to the server
  • The client sends a request for a specific resource
  • The server replies with that request and closes the connection *

* Yes I know you can set the Keep-Alive header and the connection will be kept open while the server waits for another request, but it's still request driven.

MQTT is a Pub/Sub protocol

  • A client opens a connection to the broker
  • The client sends a subscription request for a topic pattern (this can be an exact topic or a wild card)
  • A second client (already connected) publishes a new messages to the broker with a topic that matches the first client.
  • The broker checks the topic pattern lists for all connected client and delivers the message to all the matching clients.

The connection is persistent for the lifetime of the client. Because the connection is initiated by the client (it opens the outbound connection) this works when the client is behind a NAT gateway. As long as the Broker is publicly available then this all works fine.

In the situation you have described the "Server" would just be another client connected to the Broker that publishes messages that other clients are subscribed to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.