-
-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add support for external eventing (MQTT, webhooks, etc.) #813
Comments
I’m surprised there isn’t a more mature Django package for this. I’m open to this but it will take some research and consideration. |
A potential idea, and shoot it down as you feel: MQTT support? For example, for a simplistic initial use case, define an MQTT broker in a config file. Then if any of the REST endpoints are hit, publish the HTTP verb, endpoint, and its payload to the broker. I have a Mosquitto instance available, and if Baby Buddy was just dumb--in a good way--and republishes the REST requests it gets to the MQTT broker, then another service can subscribe to the broker and take over from there. This way, the solution is loosely coupled and not require Baby Buddy to actually send its own outbound HTTP messages to arbitrary endpoints, and MQTT is by its nature a very light protocol. The MQTT payload could be as simple as a JSON struct like:
Logically, only non-idempotent would need to emit events. I can't think of a scenario where a Of course there could be information exposure, so after a basic implementation, maybe the MQTT relaying could be locked down to specific endpoints, users, etc., but it would be a nice starting point. The de facto standard library for MQTT in Python seems to be https://pypi.org/project/paho-mqtt/ which I've used before and was pretty straightforward, at least as a subscriber |
Webhooks I have at least a passing familiarity with but MQTT is entirely new to me. This looks and sounds interesting but I'd probably have to lean on someone else contributing actual code here. As with webhooks -- I'm open to it, just can't say it's something I'll have the time to jump on. Though MQTT has piqued my curiosity for sure 😄 |
I may mess with it and see what’s possible. Doubtless whatever I contribute would need to be cleaned up by someone, but hey, that’s how this open source thing goes. In the meantime I’m going to donate because this software has been amazing for us and definitely helped improve our sanity taking care of our new baby! |
Appreciate the support! And happy to help review code and move the idea forward. Will be a good way to learn about MQTT myself, hah. |
I made an initial proof-of-concept of this. Keep in mind I'm a hobbyist with Python and this is the first time I've touched Django, so I'm probably violating all kinds of best design principles and this isn't ready for an actual PR. But I welcome your comments. tl;dr: I made Screenshot showing it ending up in MQTT: I'm using Mosquitto as an MQTT broker (server). |
@skjdghsdjgsdj this looks like a pretty interesting and straightforward start! Will you open a PR for the branch? You can set it as draft while we work through it. For testing, it would also be helpful if you can give me some very basic instructions on how to set up the broker for testing. Thanks for working on this! |
I'll figure out the PR bit shortly. In the meantime, the high level approach you need to do is as follows. MQTT is a publish/subscribe mechanism. Clients publish messages to the MQTT "broker" (just a server) with a given topic and payload. Other clients can subscribe to that topic to listen for messages that got published. In this example, Baby Buddy publishes a message to the
My error handling is pretty bad so if it doesn't work, it'll be interesting at least. My development environment involves running Baby Buddy in PyCharm with its built-in server. My MQTT broker is running on a separate Debian Linux server. Its
This adds an optional dependency to For future reference, AWS IoT Core offers an MQTT broker as well for people who don't want to self-host one. |
PR: #894 |
As noted in #926: if this implementation included retained messages for most recent state (feedings, changes, etc.), then clients interested in real-time events via MQTT could receive the retained messages too instead of needing to first contact the REST API to get current state. |
When we use Baby Buddy, we interact with it a couple ways: the web interface and the API through various integrations (Android app, custom devices, etc.). What would be nice is support for webhooks in the API.
Use case: we log a feeding one of three ways:
But, regardless of how we log the feeding, we'd like to get a webhook callback to an arbitrary HTTP(S) URL. That would let us set up a timer (outside of Baby Buddy) that notifies us after a certain time interval like two hours "hey, it's been a while, a feeding time is coming up shortly."
That's a specific use case around feeding, but a generic publish/subscribe mechanism would be nice for things like feedings, changes, etc. Another random example: a webhook around changes could let us separately keep track of likely number of diapers used, and then we get notified "hey, you've logged n changes which probably means n diapers and you might be running low."
I searched the docs and existing issues and didn't see this implemented or requested yet.
The text was updated successfully, but these errors were encountered: