Rebooting server with MQTT service - mqtt

Imagine an MQTT broker with remote clients connected, which continuously send QoS 2 data - the standard situation. Clients are configured with "cleansession false" - they have a queue to send messages in case of a connection failure.
On the server, local clients subscribe to topics to receive messages.
Server load:
Launch the MQTT Broker
Running local clients
Connecting remote clients and receiving data from the queue
What if the third point occurs before the second? Are there standard solutions? How not to lose the first messages?

Assuming you are talking about all later reboots of the broker, not the very first time the system is started up then the broker should have stored the persistent subscription state of the clients to disk before it was shutdown and restored this when it restarted. This means that it should queue messages for the local clients.
Also you can always use a firewall to stop the remote client being able to connect until all the local clients have started, this would solve the very first startup issue as well.

Related

MQTT bridge with Sparkplug B -> NDEATH scenarios not working as expected

I have two machines and am testing MQTT bridge connections with Sparkplug B payloads.
I have the bridges working as expected but when I simulate some failure points as annotated in the image below, things are not working as expected. My expectation is an NDEATH will be visible on the broker on Machine B when any of the three points in the image disconnect.
When I kill the publisher or the local MQTT Broker on Machine A, I do indeed see the NDEATH as expected when subscribed to the Machine B MQTT Broker, but when I pull the plug between Machine A & B as noted by #3 in the image, I do not see a NDEATH! I have waited for a long period to make sure the 60 second keep alive has had plenty of time to react which I understand to be 1.5x the keep alive typically.
The publisher and Broker on Machine A continue to operate and when the connection at point #3 is brought back online, all messages are delivered, but I was expecting with the bridge connection down, any nodes that had published a last will & testament (LWT) would see an NDEATH due to the connection loss at any point.
I have tested with mosquitto, vernemq and a little with hive-ce, however hive-ce is severely limited in functionality. Am I missing something with my understanding of MQTT bridging? Shouldn't NDEATH be sent in all three scenarios?
From the sparkplug spec:
A critical aspect for MQTT in a real-time SCADA/IIoT application is making sure that the primary MQTT SCADA/IIoT
Host Node can know the “STATE” of any EoN node in the infrastructure within the MQTT Keep Alive period (refer
to section 3.1.2.10 in the MQTT Specification). To implement the state a known Will Topic and Will Message is
defined and specified. The Will Topic and Will Message registered in the MQTT CONNECT session establishment,
collectively make up what we are calling the Death Certificate. Note that the delivery of the Death Certificate
upon any MQTT client going offline unexpectedly is part of the MQTT protocol specification, not part of this
Sparkplug™ specification (refer to section 3.1 CONNECT in the MQTT Specification for further details on how an
MQTT Session is established and maintained).
So, in MQTT terms, NDEATH is a 'Will' which, as mentioned above, is defined in section 3.1 of the the MQTT spec:
If the Will Flag is set to 1 this indicates that, if the Connect request is accepted, a Will Message MUST be stored on the Server and associated with the Network Connection. The Will Message MUST be published when the Network Connection is subsequently closed unless the Will Message has been deleted by the Server on receipt of a DISCONNECT Packet
In summary NDEATH creates a 'Will' which the MQTT broker publishes if it looses the connection with the publisher (unless a DISCONNECT is received first).
When you establish a bridge this relays messages published on whatever topic(s) the bridge is configured to relay. The bridge only communicates published messages; not information about what clients are connected (or any 'Will' they may have set) so when the bridged connection goes down subscribers will not receive the NDEATH.
Monitoring the connection status of bridges is not something covered by the spec so options vary from broker to broker. For example Mosquitto can (using a 'Will' on the bridge connection) provide a notification when the connection goes down (see notifications in mosquitto.conf). This may provide you with some options to get the information you need.

client <clientname> has exceeded timeout disconnecting

I am using MQTT 3.1.1, I have installed a mosquito as a local server on my computer.
I am sending the Some sensors data from pubsubclient (MQTT client library ) to the mosquito and saving it to the database from the mosquito server
Whenever I start the session upto 5-10 minutes I am getting the messages but after that
MQTT client couldn't send any message and disconnect automatically.
Before disconnecting it prints the following message in command line
client <clientname> has exceeded timeout, disconnecting
Socket error on client <clientname>, disconnecting.
Also I am using the server with default configurations, except the QOS is set to 2
What is causing this error and
What should I do, so that client should not disconnect from my local server ?
The node(s) that is(are) Subscribing (and maybe the Publishing nodes if they take too long to Publish again) need the 'keepalive' field on the Connect call set. Most MQTT Brokers will timeout connections after something like 5 minutes, unless you have modified the timeout value in the settings.
Set the 'keepalive' option to something like 30 or 60 seconds will prevent the MQTT Broker from disconnecting. You Subscribers will start sending PINGREQ packets, and the MQTT Broker will reply with PINGRESP packets.
Read more here: http://www.steves-internet-guide.com/mqtt-keep-alive-by-example/

MQTT - listen to ping, disconnect and connect events

I have "server side" mqtt client which I use to monitor and manage remote mqtt clients. I would like to extend this server module to keep tabs on the connectivity of the remote clients.
During normal operation, the remote clients regularly PING the broker, as per the broker logs:
1532924170: Received PINGREQ from c51
1532924170: Sending PINGRESP to c51
and when a disconnection occurs, the broker logs show this too:
1532924976: Client c51 has exceeded timeout, disconnecting.
1532924976: Socket error on client c51, disconnecting.
as well as the subsequent re-connection:
1532924978: New client connected from X.X.X.X as c51 (c1, k30).
1532924978: Sending CONNACK to c51 (0, 0)
I would like to monitor these 3 events from the mqtt-client held by the server module. Is this possible? If not, what alternative approach to "health" monitoring can you recommend?
No, you can not read these from a connected client.
The only pure MQTT approach is to make use of the Last Will and Testament (LWT) feature. You have the client set up a LWT publish a retained message to a client specific topic that marks it as offline. Then as the client connects it should publish a retained message to show you are online. If you disconnect cleanly (not triggered by keep alive time out you should manually publish the LWT message as the last thing before disconnecting).
It's also worth pointing out that ping messages only get sent if no other messages have been sent between the client and the broker in the keep alive period.

Handling backpressure data tranmission in mqtt client

We have a mosquito broker installed in our cloud server. Our gateway is sending data by using an MQTT client with 2G signal. We are observing data loss in a few scenarios.
When the gateway is disconnected from internet we queue messages in the gateway for few days. When it reaches the internet again then it starts pushing data to broker with two threads: one is for live messages, the other is for queued messages. We get a callback ACK for each and every message but we are loosing some messages on the server side.
How do we make sure that all produced messages will be processed in the broker?
How do we handle the burst of message in the gateway? One option is to delay the sending of each queued message by a few milliseconds (to avoid that they all try to reach the broker at once.
We are using QoS level 1 for our publisher/subscriber and want to continued using QoS level 1.

Losing messages over lost connection xmpp

i went through this question
Lost messages over XMPP on device disconnected
but there is no answer.
When a connection is lost due to some network issue then the server is not able to recognize it and keeps on sending messages to disconnected receiver which are permanently lost.
I have a workaround in which i ping the client from server and when the client gets disconnected server is able to recognize it after 10 sec and save further messages in queue preventing them from being lost.
my question is can 100% fail save message delivery be achieved by using some other way i know psi and many other xmpp client are doing it.
on ios side i am using xmppframework
One way is to employ the Advanced Message Processing (AMP) on your server; another one is to employ the Message Delivery Receipts on your clients.
The former one requires an AMP-enabled server implementation and the initiating client has to be able to tell the server what kind of delivery status reports it wants (it wants an error to be returned if the delivery is not possible). Note that this is not bullet-proof anyway as there is a window between the moment the target client losts its connectivity with the server and the moment the TCP stack on the server's machine detects this and tells the server about it: during this window, everything sent to the client is considered by the server to be sent okay because there's no concept of message boundaries in the TCP layer and hence if the server process managed to stuff a message stanza's XML into the system buffers of its TCP connection, it considers that stanza to be sent—there's no way for it to know which bits of its stream did not get to the receiver once the TCP stack says the connection is lost.
The latter one is bullet-proof as the clients rely on explicit notifications about message reception. This does increase chattiness though. In return, no server support for this feature is required—it's implemented solely in the clients.
go with XEP-0198 and enjoy...
http://xmpp.org/extensions/xep-0198.html
For a XMPP client I'm working on, the following mechanism is used:
Add Reachability to the project, to detect quickly when the phone is having connectivity problems.
Use a modified version of XEP-0198, adding a confirmation sent by the server. So, the client sends a message, the server confirms with a receipt. Later on, the receiving user will also confirm with a receipt. For each message you send, you get two confirmations, one from the server, one from the client. This requires modifications on the server of course.
When the app is not connected to the XMPP server, messages are queued.
When the app is logged in again to the XMPP server, the app takes all messages which were not confirmed by the server and sends them again.
For this to work, you have to locally store the messages in the app with three possible states: "Not sent", "Confirmed by server", "Confirmed by user"

Resources