Message Handler not receiving message

Hi all!

Maybe a silly issue, but I'm trying to get a message handler on a component on any session of the 'message' project to execute and I'm not sure what I'm doing wrong.

I specifically want to trigger the message handler on a component in the session from a tag change script in my gateway, shown below.

system.util.sendMessage('message','test')

However I cannot get the component's message handler to execute. I also get no error logs in my gateway. I know my message handler is named 'test' and is listening for all scopes. I can get an identically named script in the session events to hear the message, but then I can't modify a component on the page using the payload (to my knowledge) since I'm at the session layer.

Does anyone see what I'm doing wrong or is this an impossible task and I'm not aware of it?

Thank you!

Pretty sure the payload must be a dict not just a str

3 Likes

Are you confusing the function of system.util.sendMessage and system.perspective.sendMessage?

system.util.sendMessage sends a message to a gateway message handler, not a component message handler.

system.perspective.sendMessage sends messages to perspective component message handlers within their scope.

3 Likes

it definitely does, in this case though 'test' should be the messageType. trying to trim out variability, I'm just trying to see if I can get my message to the destination. Payloads will be next though

As @zacht mentioned, system.util.sendMessage will send a message from the gateway that, when expected to be received by a Perspective Component handler, requires an intermediate Session Message Handler under the projects Session Events.

From there, you will need to re-send a message using system.perspective.sendMessage, which will be broadcast from the Session to the components handler.

You're using system.util.sendMessage() so the first parameter is the "Project" name. Is your project actually named "message"?

To send messages in perspective, generally you would use the system.perspective.sendMessage() this would make the first parameter the message type, and the second parameter the payload, which would need to be a dictionary for it to work.

To send a message across sessions you would need something like this:

system.perspective.sendMessage('test',{},scope = "session")

So I had been using system.util.sendMessage because I didn't think I would be able to use system.perspective.sendMessage from a tag change event script for some reason.

My goal in this is to be able to change a component property (on all active sessions of a given project) via a tag change event script that's built into a UDT. No idea if it's practical, but I thought it should be possible so I wanted to try it.

yeah, I just called it 'message' since it's kind of an experiment for me right now. So I can successfully message from a button using system.Util.sendMessage but only to the session events message handler. I will give just the system.perspective.sendMessage a try

You can not. Tags exist outside of the perspective scope.

You would need to use system.util.sendMessage()

system.util.sendMessage('project name','messageHandler',scope = "S")

The message handler listening for this message would need to be in the session events. Then from there you can use system.perspective.sendMessage() to target a specific componet,view,etc...

1 Like

So here is my handler, on the root container of the view that contains the button I'm trying to test with:

def onMessageReceived(self, payload): system.tag.writeBlocking('[default]NewTag', 'roooooooot')

and here is the script that's sending the message:

def runAction(self, event):
	system.perspective.sendMessage('test', payload={'Message':'hi'}, scope='session')

The tag write doesn't execute but I get no errors, be it in a popup, the gateway log, or anywhere else. Not sure what's going on

handler is set to listen to all scopes, is named 'test'

Sweet, so that makes me feel a lot better, as I had gotten to the point of messaging the session events successfully. It's just the handler there was still failing to execute the system.perspective.sendMessage portion of it.

Or rather, the handler I'm trying to target didn't receive the message, would be the more correct way to explain it.

I feel silly asking, but is my system.perspective.sendMessage just broken?

I doubt it. Need to see how you have everything set up to be sure.

Make sure that the message type you use in the script matches exactly what you typed into the message handler (case sensitive).

Also, how are you verifying that the message was received or not? Could be an issue with the tag write, or otherwise.

Try this in the message handler?

def onMessageReceived(self, payload):
    system.perspective.print('Message Received')
    tagWrite = system.tag.writeBlocking(['[default]NewTag'],['rooooooooot'])
    system.perspective.print(tagWrite)

You should see something print out in the output console if the message is being received.

1 Like

It doesn't scale.

You should be including bindings to tags of interest in your session custom properties, and, if necessary, run a Perspective change event there.

Whenever you find yourself trying to "push" data to your user interface instead of letting your UI "subscribe" to the data the UI needs, step back and rethink what you are doing.

3 Likes