# Sending component reference as message payload

**URL:** <https://forum.inductiveautomation.com/t/sending-component-reference-as-message-payload/89636>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [April 29, 2024, 2:42pm UTC](https://forum.inductiveautomation.com/t/sending-component-reference-as-message-payload/89636 "2024-04-29T14:42:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathon\_Platt](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@Jonathon\_Platt](https://forum.inductiveautomation.com/u/Jonathon_Platt)\
**Post date:** [April 29, 2024, 2:42pm UTC](https://forum.inductiveautomation.com/t/sending-component-reference-as-message-payload/89636/1 "2024-04-29T14:42:47Z")

</div>

I recently noticed that a reference to a component (`self`) can be sent as the payload for a perspective message - i.e. in a `ButtonOk` component onActionPerformed:

```python
def runAction(self, event):
	system.perspective.sendMessage(messageType="on_ok", payload=self)

```

And that the component(s) that receives the message have full read/write access to the Buttons props, view params etc:

```python
def onMessageReceived(self, payload):
	payload.props.text = "interesting.."
	system.util.getLogger("ok button params").info(str(payload.view.params))

```

This blew my mind a bit, as this seems to be a way around the limitation of components not being able to traverse or reference other components across views.  
It has also made me re-think some of our current messaging design structure when it comes to flex repeaters / embedded views / data flow.

My question is, is this an intentional / supported feature? Payload is always referred to a dict type in the docs... but I noticed after this it can also be other class types

Are there reasons why passing the component reference like this in the message should be avoided? It seems like a really nice way to make more reusable view components

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [April 29, 2024, 3:12pm UTC](https://forum.inductiveautomation.com/t/sending-component-reference-as-message-payload/89636/2 "2024-04-29T15:12:40Z")

</div>

> [@Jonathon\_Platt](#):
>
> My question is, is this an intentional / supported feature? Payload is always referred to a dict type in the docs... but I noticed after this it can also be other class types

~~The pay load is a dictionary. It just so happens that self is a reference to the JSON for the component, which Ignition automagically converts into a Python Dictionary behind the scenes (yes I know it isn't really a dictionary, but it in python terms it looks and acts like one so it is one).~~ This isn't actually how it works in this case, interestingly.

Doing this between views within the same session is "probably" okay, but I would be very cautious, as you could be passing around references to components which are outside of their intended lifecycle.

I would strongly recommend not doing this even though it works, especially if you're thinking of doing this with a message to a gateway.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [April 29, 2024, 3:22pm UTC](https://forum.inductiveautomation.com/t/sending-component-reference-as-message-payload/89636/3 "2024-04-29T15:22:18Z")

</div>

Agreed. Within the context of a singular confined Perspective Session, this is pretty safe. As soon as you start trying to pass these sorts of references around to other sessions, project libraries, Gateway scripts (and by extension Tag scripts) they lose their utility and "security".

---

<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:** [April 29, 2024, 3:44pm UTC](https://forum.inductiveautomation.com/t/sending-component-reference-as-message-payload/89636/4 "2024-04-29T15:44:44Z")

</div>

> [@lrose](#):
>
> It just so happens that self is a reference to the JSON for the component, which Ignition automagically converts into a Python Dictionary behind the scenes (yes I know it isn't really a dictionary, but it in python terms it looks and acts like one so it is one).

Actually, that's not happening here. I'd argue the documentation is wrong, and doesn't agree with how the code actually works; it's probably copy-pasted from the more general `system.util.sendMessage`.

With `system.perspective.sendMessage`, the payload can be _literally anything_, and it'll be "safe" to send, because no serialization or deserialization is implied - specifically because the target _must_ be another session/page/component running on the same JVM, thus directly accessible.  
It's _more or less_ equivalent to invoking something from the project library, just decoupled, in that you don't have to specify the library name to call (and could potentially 'handle' a message in multiple places.

> [@lrose](#):
>
> you could be passing around references to components which are outside of their intended lifecycle.

This is probably a useful caveat to be aware of, but I wouldn't consider this "unsafe" at all.

Just **don't** try this same pattern with `system.util.sendMessage`.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [April 29, 2024, 4:36pm UTC](https://forum.inductiveautomation.com/t/sending-component-reference-as-message-payload/89636/5 "2024-04-29T16:36:33Z")

</div>

> [@paul-griffith](#):
>
> Actually, that's not happening here. I'd argue the documentation is wrong, and doesn't agree with how the code actually works; it's probably copy-pasted from the more general `system.util.sendMessage`.

Interesting.

---

<div class="post-metadata">

**Author:** ![Jonathon\_Platt](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@Jonathon\_Platt](https://forum.inductiveautomation.com/u/Jonathon_Platt)\
**Post date:** [April 29, 2024, 4:56pm UTC](https://forum.inductiveautomation.com/t/sending-component-reference-as-message-payload/89636/6 "2024-04-29T16:56:01Z")

</div>

Awesome, thanks for the feedback guys! I feel like this is a huge feature of `system.perspective.sendMessage` that makes it even more powerful then advertised in the docs - I will play around with it a bit more to make sure I fully understand any limitations for my intended use cases.
