# Gateway MessageHandler

**URL:** <https://forum.inductiveautomation.com/t/gateway-messagehandler/69932>\
**Category:** Ignition\
**Tags:** perspective, ignition81, scripting\
**Created:** [February 16, 2023, 12:51pm UTC](https://forum.inductiveautomation.com/t/gateway-messagehandler/69932 "2023-02-16T12:51:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [February 16, 2023, 12:51pm UTC](https://forum.inductiveautomation.com/t/gateway-messagehandler/69932/1 "2023-02-16T12:51:16Z")

</div>

Hello,

I am currently assigned to a project that is supposed to auto refresh some tables, write to tags on refresh and essentially automate our run schedule into the PLC.

I currently am thinking I will have a scheduled gateway event run every 15 minutes. This event will send a message handler, then on each page with the tables I have a checkbox that is listening for that message handler. When it hears it, self.props.selected will become True, and if True it will run my queries to refresh some tables inside of the project and when the table is refreshed, it will have to write the tables values into some tags. I haven't figured out just how to write to tags with table values just yet.

I am struggling with getting the message handler to function properly. I have tried system.perspective.sendMessage() and system.util.sendMessage(); however, I'm not sure if I'm using it improperly or it just cannot be done.

If anyone has any knowledge on this or a better way of accomplishing this task, it'd be much appreciated. I've never used the gateway schedule or a message handler from the gateway level.

Code for my scheduled script:

```auto
project = 'R3'
messageHandler = 'Refresh'
system.util.sendMessage(project, messageHandler)

```

Code for gateway message (if even necessary):

```auto
system.perspective.sendMessage("Refresh")

```

Code for the checkbox script on the page with table:

```auto
self.props.selected = True

```

Code for checkbox self.props.selected change script:

```auto
if self.props.selected == True:
	returnedData = system.db.runNamedQuery("L2 Schedule")
	self.getSibling("Table").props.data = returnedData

```

---

<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:** [February 16, 2023, 1:47pm UTC](https://forum.inductiveautomation.com/t/gateway-messagehandler/69932/2 "2023-02-16T13:47:13Z")

</div>

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 dataset tags or document tags to deliver chunks of fresh data conveniently.

With this arrangement, you need few, if any, message handlers. Your UI would include tag bindings to display the process and feedback tags, and entry fields and/or command scripts that would update the guidance tags and database entries. Treat the gateway event as if it were an extra task running alongside your PLC's realtime tasks. Run it fast enough so that guidance is acted upon with reasonably low latency.

If you still think you need to send messages from gateway scope into your perspective sessions, see this topic for the nuances:

> [@Set/Configure tag binding with script in Perspective](https://forum.inductiveautomation.com/t/set-configure-tag-binding-with-script-in-perspective/68906/13):
>
> So, to get this to work I had to make a handler in the project's "Session Events" to receive from system.util.sendMessage(..., scope='S'). It then just calls system.perspective.sendMessage(..., scope='session') with the same payload. The component can receive this latter broadcast. This is a nuance that should be put in the documentation. (That messages broadcast to Perspective via system.util.sendMessage(..., scope='S') can only be received in "Session Events".)

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [February 16, 2023, 1:59pm UTC](https://forum.inductiveautomation.com/t/gateway-messagehandler/69932/3 "2023-02-16T13:59:38Z")

</div>

I thought to myself "Why not just use a minute tag, then say 'if minute == 0, or 15, or 30, or 45, set checkbox to True?" but even that isn't working for some reason. I am going to try the session event out, thank you for the link.

---

<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:** [February 16, 2023, 2:08pm UTC](https://forum.inductiveautomation.com/t/gateway-messagehandler/69932/4 "2023-02-16T14:08:06Z")

</div>

> [@Robert\_Hrynciw](#):
>
> When it hears it, self.props.selected will become True, and if True it will run my queries to refresh some tables inside of the project and when the table is refreshed, it will have to write the tables values into some tags.

This is a terrible design. Don't do it. Just write to those tags from the gateway event, using the data the gateway event has queried, and that the gateway event will write into a dataset memory tag. The UI table would just **bind** to the dataset tag. No messages needed. No writes from the UI back to PLC control tags in this loop.

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [February 16, 2023, 2:47pm UTC](https://forum.inductiveautomation.com/t/gateway-messagehandler/69932/5 "2023-02-16T14:47:34Z")

</div>

This works great. I am running the query, inserting it into a dataset tag and have the tables linked to that dataset tag. I have a schedule hitting at the hour, 15, 30 and 45. Now I just need to figure out how to loop through the dataset and insert the values into separate tags for each individual value. Thank you for this!
