Popup message using frappe.publish_realtime

Hello;

I succeeded to play popup message to be an pop notification using hooks (using scheduler event), but the problem that this message is disappearing once the user is clicking on any place at the screen and that will let the message to disappear. I used:

frappe.publish_realtime(event=‘msgprint’, message=‘Popup Msg As Test’, user=frappe.session.user,doctype=‘Trip Order’)

I am sure there should be a way to display the message in dialogue box and stay on the screen until user close it or bless on any button related to this message.

The problem that all the dialogues that mentioned in this link: Dialogs Types are required to be written in .js and because I need to execute the script from hooks, so I need it to be written by python

I need the help please for this issue.

Regards
Bilal

3 Likes

The event here doesn’t correspond to the method you want to call from the server/client side, but rather the event that you’ll be passing from the server to the client.

As an example, see the event setup_task in the setup wizard used to execute .js code from .py:

The primary purpose of frappe.publish_realtime() is to call client-side scripts via the server side. So, I think even if you call your .py function via hooks, you can use this functionality to render any .js dialog you want. You can also pass all relevant parameters you may need from .py in the second argument of publish_realtime, like the data passed in the above example.

2 Likes

Wonderful and I am fully thanks for you.

The main problem I am facing now that if I displayed a message using msgprint or using frappe.confirm or using show_alert, when the user click on any place at the screen, the message will disappear.

How can I disallow the message from being disappear and making the button selection to be obligatory to disappear? I need to confirm that the user read the message.

Regards
Bilal

@bghayad Try this :

frappe.publish_realtime(event=‘eval_js’, message=‘var e;e=1==confirm(“Press a button!”)?“You pressed OK!”:“You pressed Cancel!”’, user=frappe.session.user)

Thank you Mohammed.

Are you sure from the syntax?

Regards
Bilal

Hello @Mohammed_Redha

I succeeded to play alert from python script using frappe.publish_realtime using the below code:

frappe.publish_realtime(event='eval_js', message='alert("This is alert Message")', user=frappe.session.user)

But the problem now, if I need to display variable values in the message, and these variables are existed in the python code, how I can pass it?

I tried the below code but did not work:

frappe.publish_realtime(event='eval_js', message='alert("{0}", msg_var)', user=frappe.session.user)

What is the solution please?
Regards
Bilal

@bghayad This will work for you:

frappe.publish_realtime(event='eval_js', message='alert("{0}")'.format(msg_var), user=frappe.session.user)

3 Likes

Thank you @Mohammed_Redha
It worked successfully.

From the other side, I was trying to write a method in .js and calling it from .py using publish_realtime, and it worked but it was required that the document to be opened. So for example, if I need to display the alert using this method in javascript, I have to be at the document it self. I placed this method under onload and I placed it under setup and in all the cases, I have to be in the document it self.

As I am going to run this method using hooks and schedule, so how I can place this javascript method in a place that will be executed even I am at the desktop of the erpnext and not at the document it self?

Regards
Bilal

@bghayad Could you tell me about your use case in more details?

OK, I am trying the below code:

at trip_order.py:

       return frappe.publish_realtime('display_notification', msg_var, user=frappe.session.user)

at trip_order.js:

    setup: function(frm) {
            frappe.realtime.on("display_notification", function(data) {
                   alert("Test Message" + data);
            })
     }

It is working only if I am at Trip Order document.
But I need it to work even if I am at the desktop or any other module in the ERPNext.

I tried to place frappe.realtime.on("display_notification", function(data) under onload:function(frm) and the same thing, I have to be at the Trip Order document to get the message.

I need the display_notification to be executed from all modules or if I am at the desktop screen of the ERPNext and not to be a condition that the Trip Order document to be opened. How?

Regards
Bilal

I need the display_notification to be executed from all modules

What is the trigger action to show display_notification?

Hello @Mohammed_Redha

From hooks.py:

scheduler_events = {
“cron”: {
“*/3 * * * *”: [
“taxi.taxi.doctype.trip_order.trip_order.popup_notification”
]
}
}

From trip_order.py:

   return frappe.publish_realtime('display_notification', msg_var, user=frappe.session.user)

At trip_order.js:

setup: function(frm) {
        frappe.realtime.on("display_notification", function(data) {
               alert("Test Message" + data);
        })
 }

It is working but I have to be at Trip Order document.
I need it to work even if I am not at the module. How?

Regards
Bilal

Hello @Mohammed_Redha @pratu16x7

Thank you for helping.

I need to use cur_frm.set_value("last_note_time", cur_time) from frappe.publish_realtime using eval_js as it is assumed to run it as javascript, I tried the below in python but it did not work:

frappe.publish_realtime(‘eval_js’, message=‘cur_frm.set_value(“last_note_time”, {0})’.format(cur_time))

cur_time is variable in python, so I can pass it for cur_frm.set_value using publish_realtime?

Regards
Bilal

@bghayad yes, you can
and don’t forget user=frappe.session.user

Thank you @Mohammed_Redha.
My problem with frappe.session.user that is always return Administrator even the logged in user is other than Administrator. Why? What it should normally return?
Also, if I need to notify all the logged in users, what should I pass for user argument in publish_real?

Regards
Bilal

1 Like

Hi

@Mohammed_Redha does frappe.publish_realtime always needs user parameter?

@bghayad Have you get the asnwer?

Thanks

@rahy No, you can read the documentation of that function :

"""Publish real-time updates

:param event: Event name, like `task_progress` etc.
:param message: JSON message object. For async must contain `task_id`
:param room: Room in which to publish update (default entire site)
:param user: Transmit to user
:param doctype: Transmit to doctype, docname
:param docname: Transmit to doctype, docname
:param after_commit: (default False) will emit after current transaction is committed
"""
1 Like

Where can I read the complete docs?

2 Likes

Thanks @Mohammed_Redha