Microservices
MQTT
Use MQTT, the lightweight protocol of devices.
MQTT is a very light publish/subscribe protocol designed for devices (sensors, vehicles, phones) and unreliable or low-bandwidth networks. Brokers include Mosquitto and EMQX.
app.connectMicroservice(MqttOptions("tcp://broker:1883", username = "svc", password = System.getenv("MQTT_PASSWORD")))
import(ClientsModule.register { client("EVENTS", MqttOptions("tcp://broker:1883")) }) // in the caller's module- Requests use a reply topic.
- The connection reconnects automatically and subscriptions are made again on every reconnect (the session is clean, so the broker forgets them).
- Messages are handled at least once at the quality levels used; make handlers idempotent.
- Use authentication (
user,password) and TLS (ssl://) on any broker reachable from outside.
When to choose it
IoT, devices, mobile, links with poor connectivity. For service-to-service traffic inside a data centre, NATS, RabbitMQ or Kafka are usually better.
Your handlers are the same on every transport: see Overview. Compare all of them in Transports compared.