Perspective Message Listener Within a Script

Hi everyone,

First time poster here and quite new to ignition, loving it so far!

I would like to know if there is a function/command that allows me to temporarily listen for a message sent to a handler within a script.

Allow me to elaborate!

  1. I have a button, when someone pushes the button, it runs an onActionPerformed script.
  2. Within this script I open a popup which is a simple confirm/cancel prompt.
  3. When confirm/cancel is pressed, the relevant button sends a message to confirmHandle with 'confirm' or 'cancel', and closes itself.
  4. What I need the original user button to do is after opening the popup, start listening to confirmHandle, and then allow me to do XYZ based on the payload sent to it (In this case, confirm or cancel).

As far as I can see, if I configure an event listener on the button, it will be active all the time. So as this confirm/cancel popup is designed to be generic and reusable, actions are going to be happening everywhere in the SCADA on a button press. What I'm therefore hoping is for a function to just listen during that time. The workaround would be to pass an ID in the params and check if the ID is what we expect, but that's open to accidentally having duplicates.

The other way I think it can be done is with writing to session props and just reading the props at the relevant time, however the docs said about doing these popups returns with messages... so here we are!

Any helps appreciated as I've browsed past questions and am stuck doing circles.

Have a great day!

Don't do this.

Move everything in your script that would be run after the message arrives into the message handler. You are trying to use linear procedural programming techniques in an event-driven environment. Don't do it.

Since you want something generic, add a string parameter to your popup that will hold a function name and another for a unique ID. Look up that function in a project script and pass it the ID along with the payload.


I might not quite understand what you mean. Message handlers respond based on the message name and specified scope.

Your message handler will live on whatever control you are doing something with when the message comes through. You will send the message from your onActionPerformed script.

So you pick "Configure Events" to send a message and "Configure Scripts" to write a message handler for a specific message/scope on a control.

You want to name your messages based on what action is being performed so that only handlers for that action and scope fire.

Perspective Popup Windows by Travis Cox on Exchange answers your problem:

1 Like

Thanks all!

@pturmel, would you be able to elaborate slightly on the 'Look up that function in a project script'. Are you referring to something like the below as a project script?

# --- Individual Functions ---
def __testFunc1(payload):
    # type: (dict) -> None
def __testFunc2(payload):
    # type: (dict) -> None

def __testFunc3(payload):
    # type: (dict) -> None
# --- Overall Handler Function ---
def handler(functionID, payload):
	if functionID == "testFunc1":
	if functionID == "testFunc2":
	if functionID == "testFunc3":

Or would you be using dynamic function calling with namespaces in the 'handler function'? (But with error handling)

# --- Overall Handler Function ---
def handler(functionID, payload):
	funcCall = globals()[functionID]


(P.s. Whats the proper way to post code snippets on here please? :smiling_face:)

Select the text and hit the </> button. You can use the 🖉 edit link to fix your post.


Reporting back - After going away and trying to implement this, I do not understand how you can pass a 'payload' to a view without specifically defining all the dictionary keys, something that I don't want to do here as for it to be generic I'd have to give 'generic names'.
I.e. -> button push opens the confirm popup, but I need to pass a payload to that to then forward onto the project script to do 'stuff'.
Not sure if I'm overcomplicating this or just in the wrong mindset of how it should work!

How about, to make it simple, pressing confirm/cancel sends a confirmMessage with payload {'id': popupid, 'confirmed': True/False}, then you can set up message handlers where the button that opened the popup is, listening for confirmMessage ?
That's a generic popup with non generic handlers, which is not half bad. The id in the payload is used to differentiate between popups if you might have several opened at the same time.
It's actually not less generic than passing a function name to the popup, as the specific function has to be defined anyway, whether it's passed as an argument to the popup or defined/called in a handler.

Python has a built-in function getattr that can look up objects within namespaces. So if you put all of your handler functions in a project script myPopupFunctions, the message handler would do something like this:

def handler(functionID, originID):
    payload = myPopupFunctions.originPayloads.pop(originID)
    getattr(myPopupFunctions, functionID)(**payload)

The originPayloads object would be a dictionary where, before opening the popup, you place the dictionary of your real payload, under the origin ID that gets passed to the popup.

Tweak as needed to pass additional values back from the popup.