So I’m new to MQTT and I have to make a “messaging” project that loads all the messages (sent to a certain topic) in my index page without a long load time, I’m working with Yii2 Framework Advanced Project and I’ve already done the publish code, in the model I created the AfterSave method and everytime it saves something to the database it publishes one message to my MQTT Broker that is in my Raspberry PI
Example
I own a hotel and everytime I login to my backend I want to have in my index all the reservation requests that were sent to this topic in MQTT mosquitto “request-reservation” (like a notification).
Like I said I’m working with Yii2 Framework Advanced Project and the extension that I’m using for MQTT is BluerhinosphpMQTT
Code that I already have:
Class Model ReservationRequest.php (this is working and sending to the topic because I already tested in the shell)
public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub if ($insert){ $this->publish("request-reservation", "New notification of a reservation request with the ID: '. $this->id . PHP_EOL"); } }
Any advice is welcome, and thanks for the attention
Advertisement
Answer
Sort answer is you don’t.
MQTT is not a message queuing system. It will only queue messages that have not been delivered to a client with a persistent subscription that is is currently that is offline at the time the message is sent.
You probably have 2 options
Publish a single message with the current state of all the rooms, e.g.
{ "room1": "occupied", "room2": "free",...}
and set the retained flag so the latest version of this message will always be delivered to a client when they subscribe to that topic.Make a REST call to the backend that can run a query on the database to retrieve the current state of all the rooms.