From b26ed97246e7c9739f9b05e9f62391698afca063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Sun, 12 Feb 2017 17:04:37 +0100 Subject: [PATCH] Improve MQTT client example, fixes #1792 --- docs/en/modules/mqtt.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/en/modules/mqtt.md b/docs/en/modules/mqtt.md index 04e6a4bd..6609ac75 100644 --- a/docs/en/modules/mqtt.md +++ b/docs/en/modules/mqtt.md @@ -48,18 +48,22 @@ m:on("message", function(client, topic, data) end) -- for TLS: m:connect("192.168.11.118", secure-port, 1) -m:connect("192.168.11.118", 1883, 0, function(client) print("connected") end, - function(client, reason) print("failed reason: "..reason) end) +m:connect("192.168.11.118", 1883, 0, function(client) + print("connected") + -- Calling subscribe/publish only makes sense once the connection + -- was successfully established. You can do that either here in the + -- 'connect' callback or you need to otherwise make sure the + -- connection was established (e.g. tracking connection status or in + -- m:on("connect", function)). --- Calling subscribe/publish only makes sense once the connection --- was successfully established. In a real-world application you want --- move those into the 'connect' callback or make otherwise sure the --- connection was established. - --- subscribe topic with qos = 0 -m:subscribe("/topic",0, function(client) print("subscribe success") end) --- publish a message with data = hello, QoS = 0, retain = 0 -m:publish("/topic","hello",0,0, function(client) print("sent") end) + -- subscribe topic with qos = 0 + client:subscribe("/topic", 0, function(client) print("subscribe success") end) + -- publish a message with data = hello, QoS = 0, retain = 0 + client:publish("/topic", "hello", 0, 0, function(client) print("sent") end) +end, +function(client, reason) + print("failed reason: " .. reason) +end) m:close(); -- you can call m:connect again