Reputation: 18511
I´m building an MQTT server using Aedes as the sever library with Javascript and mosquitto at the client side, C++.
My server process Aedes events as follows:
//
// Aedes events
//
aedes.on("client", async (client) => {
console.log("On client event for client id " + client.id);
let myClient = await getClient(client.id); // Get client
if (myClient) {
await myClient.connect();
} else {
client.close();
}
console.log("On client event end for client id " + client.id);
});
aedes.on("subscribe", async (subscriptions, client) => {
console.log("On subscribe event for client id " + client.id);
let myClient = await getClient(client.id); // Get client
if (myClient) {
await myClient.subscribe(subscriptions); // process subscriptions
} else {
client.close();
}
console.log("On subscribe event end for client id " + client.id);
});
aedes.on("publish", async (packet, client) => {
// Ignore system messages
if (!client) return;
console.log("On publish event for client id " + client.id);
let myClient = await getClient(client.id); // Get client
if (myClient) {
await myClient.publish(packet.payload); // process subscriptions
} else {
client.close();
}
console.log("On publish event end for client id " + client.id); });
});
What am I facing is that getClient
takes some time to return, so, at the server side, he "client" event is fired, then "subscribe" and then "publish" before the server can even return the response fro the first "client" event.
I´m not sure, but seens that MQTT, receives an ACK for its message (ex: connect, subscribe) directly from Aedes, not waiting for its response.... Is that the correct understanding on how the protocol works?
If so, what would be the correct way "sync" the serve´s response - in other words - the client to send the next message only after server processes the first event?
Upvotes: 1
Views: 16