Emmanuel Katto : How to Push Real-Time Events from Frappe to Android App?

Hi everyone,
I’m Emmanuel Katto, currently working on a chat application using the WhatsApp Business API with Frappe as the backend. All messages are logged in Frappe, and the frontend (Android app) displays these messages. At the moment, I’m using frappe.publish_realtime to broadcast new message events to the Frappe frontend with the help of frappe.realtime.on. However, I now need to push the same events to my Android app.
I’d love some advice on how to send real-time events from Frappe to the Android app efficiently. Should I continue using frappe.publish_realtime, or is there a better method to send the data to my Android app?
Here’s a snippet of my backend code that works for the Frappe frontend:

def after_insert(self):
try:
wa_team = self.whatsapp_team
user = frappe.db.get_value(“WhatsApp Team”, wa_team, “user”)

    message_data = {
        "name": self.name,
        "type": "Incoming",
        "from": getattr(self, "from"),
        "contact": self.contact,
        "message": self.message,
        "creation": self.creation,
    }

    frappe.publish_realtime(
        event="new_whatsapp_message",
        message={"message": message_data, "user": user},
        doctype="WhatsApp Message",
        docname=self.name,
        user=user,
        after_commit=True,
    )
except Exception as e:
    frappe.log_error(frappe.get_traceback(), "Socket Error")

Any suggestions or ideas on how to achieve this would be greatly appreciated!

Regards
Emmanuel Katto

Websocket events will work if the user is active on the app. This is required since you need to update the UI in realtime.

You can also try using Firebase to send push notifications if you want to send events when the user is not active on the app.

Alternatively, you can also use Firebase to send both push notifications and in-app notifications - but websocket events would be better in terms of performance.

1 Like