# system.perspective.sendMessage() function

**URL:** <https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [April 28, 2023, 9:35pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047 "2023-04-28T21:35:56Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Art\_Irvin](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Art\_Irvin](https://forum.inductiveautomation.com/u/Art_Irvin)\
**Post date:** [April 28, 2023, 9:35pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/1 "2023-04-28T21:35:56Z")

</div>

Has anyone been able to use the system.perspective.sendMessage() function when it is triggered by a tag change event?

TIA

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 28, 2023, 9:38pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/2 "2023-04-28T21:38:25Z")

</div>

Without explicitly specifying a session id, what session would you expect it to get sent to in this case?

What are you trying to do?

---

<div class="post-metadata">

**Author:** ![Art\_Irvin](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Art\_Irvin](https://forum.inductiveautomation.com/u/Art_Irvin)\
**Post date:** [April 28, 2023, 9:46pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/3 "2023-04-28T21:46:32Z")

</div>

I am trying to run script on a component by starting it via a message from a tag change event. Yes, I have passed the sessionID and set the scope to session. Even when I pass the sessionID I get an error stating that "No perspective session attached to this thread."

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 28, 2023, 10:00pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/4 "2023-04-28T22:00:27Z")

</div>

Hmm, yeah, not sure if that function works if there's no associated session. Tags are global resources, not part of a project, not associated with a session.

> [@Art\_Irvin](#):
>
> I am trying to run script on a component by starting it via a message from a tag change event.

Change your mindset about these things.

You wrote some logic in a button or component or whatever. Now you want to call that same logic from somewhere else.

Don't think "okay, now I need to click that button somehow!".

Move that code to a function in a script library and just call that function from the tag change script.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 28, 2023, 10:28pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/5 "2023-04-28T22:28:29Z")

</div>

Some further discussions that might help you:

[https://forum.inductiveautomation.com/search?q=%40pturmel%20system.perspective.sendMessage%20scope%20session%20order%3Alatest](https://forum.inductiveautomation.com/search?q=%40pturmel%20system.perspective.sendMessage%20scope%20session%20order%3Alatest)

See especially this alternate approach to such problems that better fits a highly multi-user multi-tasking platform like Ignition:

> [@Gateway MessageHandler](https://forum.inductiveautomation.com/t/gateway-messagehandler/69932/2):
>
> Automating in gateway events is a very good idea, as critical operations can be made to run reliably, in one spot, regardless what is happening with client connections. However, including client UIs in the control loop is a very bad idea. Instead, make the event script self-contained, working directly with the tags that control the process, tags and/or database entries that your UI sets to provide guidance, and tags that your script updates to provide UI feedback. The latter would often be da…

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 28, 2023, 11:24pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/6 "2023-04-28T23:24:00Z")

</div>

> [@Art\_Irvin](#):
>
> Has anyone been able to use the system.perspective.sendMessage() function when it is triggered by a tag change event?

There shouldn't be any need to do that.

- A view's component tag binding will refresh any time the tag changes.
- That means you should only need to bind some property of the component to the tag.
- You can then use a transform to do something quite unrelated to the tag value itself.

In the example below I monitor the _memCounter_ tag and when it changes I update the time displayed in the label. The memTag's actual `{value}` isn't used!

 ![MemTag trigger](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/b/3bb8a1650efc9f7ad2082918720ee1f2f8fa6b84.png)

---

<div class="post-metadata">

**Author:** ![Art\_Irvin](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Art\_Irvin](https://forum.inductiveautomation.com/u/Art_Irvin)\
**Post date:** [May 1, 2023, 3:28pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/7 "2023-05-01T15:28:06Z")

</div>

I understand that tags are not specifically associated or part of a session. However, the misleading part of this is that the sendMessage() function accepts a session ID as an input. This to me would mean that it can send messages to one or all sessions. Apparently NOT the case, so this is a misleading parameter for sendMessage()

I did (with help) accomplish my goal by creating session variables that are linked to the actual tags. So when a tag value changes the session variable sees it and changes while initiating the sendMessage() function and running the script on the component.

No doubt I have much to learn in ignition however, sendMessage() apparently does not care nor work across sesssions even when you tell it which session to send the message to.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 1, 2023, 3:31pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/8 "2023-05-01T15:31:30Z")

</div>

Did you include a pageId parameter when attempting to target a specific session?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 1, 2023, 3:48pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/9 "2023-05-01T15:48:46Z")

</div>

> [@Art\_Irvin](#):
>
> misleading part of this is that the sendMessage() function

Which `sendMessage()` are you talking about? `system.util.sendMessage` can be used in gateway scope and can target a specific session, but only delivers to session events. `system.perspective.sendMessage()` only works from within a Perspective event, but can target page and view scopes, too.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 1, 2023, 3:57pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/10 "2023-05-01T15:57:36Z")

</div>

Can you show the script you were using to invoke `sendMessage`, and an example id and source of the ID you were using to target?

It should be a full UUID string, according to the code, so something like `e9262efe-36fe-4228-b8ad-4c3f3023389b`.

---

<div class="post-metadata">

**Author:** ![Art\_Irvin](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Art\_Irvin](https://forum.inductiveautomation.com/u/Art_Irvin)\
**Post date:** [May 1, 2023, 4:03pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/11 "2023-05-01T16:03:44Z")

</div>

It was definately the system.perspective.sendMessage() script.  
**system.perspective.sendMessage(messageType, payload, [scope], [sessionId], [pageId])**  
According to the manual the scope is Gateway, Perspective Session thus my misunderstanding of its allowable use.

---

<div class="post-metadata">

**Author:** ![Art\_Irvin](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Art\_Irvin](https://forum.inductiveautomation.com/u/Art_Irvin)\
**Post date:** [May 1, 2023, 4:06pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/12 "2023-05-01T16:06:18Z")

</div>

Yes, there are two session ID's as I understand, one for the designer and one for a web session. The UUID is (as I understand) for the designer, the other shorter one is for the actual web session. I actually passed both and neither worked.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 1, 2023, 4:10pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/13 "2023-05-01T16:10:49Z")

</div>

I'm going to do some testing. My expectation is the same as yours; anywhere on the gateway you should be able to call `system.perspective.sendMessage`, you'll just have to explicitly pass whatever you're targeting.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 1, 2023, 4:49pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/14 "2023-05-01T16:49:30Z")

</div>

Okay, so this _does_ work the way we both thought, but with a caveat that I suspect is the source of your frustration.

So, what I _think_ you did, because it's a logical thing to do, is that you configured a 'Session Message Handler' that you're trying to reach with `system.perspective.sendMessage`:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/6/a6bee00f977fe8929855205772a810e1cf8f6cf1.png)

I'm guessing this is what you did, because it's what _I_ did at first, before I remembered the distinction. These 'session message handlers' are actually those targeted by [`system.util.sendMessage`](https://docs.inductiveautomation.com/display/DOC81/system.util.sendMessage).

The 'scope' argument of `system.perspective.sendMessage` corresponds to the _listening scope_ of the **component** message handlers on any Perspective component:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/f/cfeca528a3aac2efecec86006eb833a6e6b6f0f0.png)

The granularity of broadcasting/listening scopes is for extra utility, but unfortunately overloads the terminology here. `system.util.sendMessage` long predates Perspective, and has important Vision and EAM functionality, so it wasn't like we could remove or deprecate it, and it made the most sense to extend it for use with Perspective sessions once they were introduced. But then Perspective's own message handling concept happens to overlap, terminology wise, and there's not a ton we can do about it.

I can also confirm that, with the appropriate listening/targeting scopes set up, you can reach either message handler from an isolated gateway script, such as a tag event script:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/2/826f10b00e8b23a04eed01ace085d8bf9f23e764.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/8/f861ab31fb89141af9feded23150036d3ae2e66b.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/a/2a0fbae722cd746744f00c6d2aa93bab8c041901.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 1, 2023, 4:55pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/15 "2023-05-01T16:55:49Z")

</div>

Perhaps it would be best to expand the "Description" box on the component message handlers to clarify that they only listen to messages from `system.perspective.sendMessage()`, and add a "Description" box to the session message handlers indicating that they only listen to `system.util.sendMessage()`.

And add this information to the user manual.

---

<div class="post-metadata">

**Author:** ![Art\_Irvin](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Art\_Irvin](https://forum.inductiveautomation.com/u/Art_Irvin)\
**Post date:** [May 1, 2023, 8:17pm UTC](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047/16 "2023-05-01T20:17:13Z")

</div>

Or perhaps annotate that system.util.sendMessage() must be used when the triggering event is on the gateway or outside of a session (i.e. tag update event etc.)

When the triggering event is in a session system.perspective.sendMessage() may/should be used.

The easy fix for me was to use the custom session properties which I had bound to the tags I was needing to trigger sendMessage() to trigger the script in the component. Seems very simple now.
