MQTT Service
# help
r
I wanted to use the Datacake package as an example for writing a general MQTT service. It turns out the Datacake code is much more complicated than I expected, especially the Networkmodule part. Could someone (@kasperl ?) explain to me the rationale for this. Second question: how would you implement subscribing to topics and receiving answers?
k
Yeah, the complexity in the implementation is a valid point. I wanted to build a service that would connect on demand and the NetworkModule handles that well. We use it for net.open/close.
So if two separate clients start at the same time, the share one connection to Datacake.
Once the last one goes away, we close down the connection.
There are multiple options for subscribing to topics and receiving answers. One is a polling approach and one is a pushing approach. It is probably nice to subscribe on the client and then maintain a simple map in the service (topic -> resource) and use the notification mechanism to push data to the clients that want the data.
So https://github.com/kasperl/toit-datacake/blob/main/src/service.toit#L164 is the resource you can call
notify_
on, possibly through a new public method on that class.
r
Thanks for the info. The resource/notify is what I also came up with. For the complexity I will have to decide whether to try to understand it, or think of something simpler.
f
The Datacake package is a bit more sophisticated (as Kasper explained for several reasons). If you just want to use MQTT, or have a simple example for using a different broker, then have a look at the Adafruit example: https://github.com/toitware/mqtt/blob/main/examples/adafruit.toit
r
That stage I already passed; see: https://github.com/robvanlopik/toit-pharo
f
I see. Nice.
r
Now I specifically want to have a base container to do the MQTT stuff and load different containers with services that talk to my Pharo systems through MQTT. In toit-pharo I expose for example I2C to the Pharo side, where I have code to run different I2C devices, but it makes more sense to move that over to the ESP32 side.
f
Sounds good.