# Send a message from a gateway script to the perspective sessions

**URL:** <https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [June 24, 2023, 10:16am UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494 "2023-06-24T10:16:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AGOSTINO\_SCLAUZERO](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AGOSTINO\_SCLAUZERO](https://forum.inductiveautomation.com/u/AGOSTINO_SCLAUZERO)\
**Post date:** [June 24, 2023, 10:16am UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/1 "2023-06-24T10:16:48Z")

</div>

I have a machine that gets queried via OPCUA at a fixed, regular rate (say 1 second).  
This is done through a Gateway Event of my project.  
Every now and then (say 10 minutes) the the machine returns new data (it's a 'slow' process).  
When new data data are available, they get saved on the database by the gateway event script.

## The problem

And here is the problem: after saving the data I want that the perspective session (or sessions) of the project get updated to show the new data.

I don't want to query continuously the database. Infact, if I want the refresh to be prompt, the query rate should be hi (say 1 second) but since there are no new events but every 10 minutes, doing so I would put an high pressure on the database for little result, that is inefficient and potentially problematic for the other applications running on the same database.

## Possible solution based on messages

Usually, the solution to this kind of problem is to have some type of message to be sent from the source (Gateway Script) to the application that needs to know that there are updates (perspective sessions).  
So, the first question is: **does Ignition offer specific support for this scenario?** If not, **is there a best practice that states how to cope with this problem?**  
My self-thought solution involves using a memory tag as a signal method, this would be set by the gateway script and queried in someway by perspective sessions; but I would like to hear from you regarding my previous question.

---

<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:** [June 24, 2023, 12:30pm UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/2 "2023-06-24T12:30:29Z")

</div>

A memory tag that you update just after the data is saved in the database is probably the simplest method. (I would use a datetime memory tag and write `system.date.now()` to it.) The consumers in the UI would use a tag binding on a session or view or component custom property, with a change event to perform the necessary UI operations to redisplay content (probably a `.refreshBinding()` method call where the named query resides to make it run again).

The only downside to the memory tag approach is that binding evaluation will likely double-execute the query on view startup.

A more complicated approach that avoids that problem would be to broadcast a message after saving in the database, and having message handlers cause the content redisplay. Take a close look at this topic, and the behavioral differences between `system.util.sendMessage()` and `system.perspective.sendMessage()` in regards to gateway scope broadcasts:

> [@system.perspective.sendMessage() function](https://forum.inductiveautomation.com/t/system-perspective-sendmessage-function/73047):
>
> 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:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 28, 2023, 5:07pm UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/3 "2023-12-28T17:07:06Z")

</div>

I went searching the forums to see if what I just did was okay:

I needed all perspective sessions to update a page (if it's open of course) when an item is added to a database through a vision client.  
So I sent a message to the gateway using `system.util.sendMessage`, caught that in the gateway events, then dispatched a message like this:

```python
for session in system.perspective.getSessionInfo():
    system.perspective.sendMessage('handler', scope='session', id=session.id)

```

Any thoughts on this ?

---

<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:** [December 28, 2023, 5:18pm UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/4 "2023-12-28T17:18:01Z")

</div>

You might want `system.util.sendMessage(...., scope='S')`, but I'm not sure.

I'd use a memory tag bound in the pages that matter--have the gateway message handler set it (latest timestamp, perhaps). Or the vision client can update it. Only pages that care will notice, and Ignition will distribute to anyone who cares. (Other Vision clients, too, if it matters, and viewers connected via remote tag providers.)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 28, 2023, 5:26pm UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/5 "2023-12-28T17:26:33Z")

</div>

I used `system.util.sendMessage(..., scope='G')`, assuming 'sessions' in this case would be vision clients... is there a way to send a message directly to perspective sessions from vision ?

---

<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:** [December 28, 2023, 5:28pm UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/6 "2023-12-28T17:28:29Z")

</div>

> [@pascal.fragnoud](#):
>
> message directly to perspective sessions from vision ?

I don't know. Try `scope='S'` from Vision.

But then, I'm not terribly invested, as I'd use a memory tag as the sentinel. 🤷‍♂️

---

<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:** [December 28, 2023, 6:16pm UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/7 "2023-12-28T18:16:30Z")

</div>

The `system.util.sendMessage` function allows for sending to [only Perspective Sessions](https://docs.inductiveautomation.com/display/DOC81/system.util.sendMessage#:~:text=Limits%20the%20scope%20of%20the%20message%20delivery%20to%20%22C%22%20(clients)%2C%20%22G%22%20(Gateway)%2C%20%22CG%22%20for%20clients%20and%20the%20Gateway%2C%20or%20%22S%22%20Session.%20Any%20combination%20of%20C%2C%20G%2C%20and%20S%20are%20available.) with a `scope` arg value of "S".

That being said, it seems like notifying sessions of some change in data is less efficient than simply updating some tag with the information, and having all sessions bound against the tag.

If you're writing your data to a database table, you could use a Query Tag, but this refreshes/updates at some fixed rate. You could use a DataSet Memory tag, and write to the Tag _after_ you've updated the database table based on a query of the table.

```auto
# event occurs (machine update)
tagPath = "[provider]SomePath"
system.db.runPrepUpdate(...) # update DB
system.tag.writeBlocking([tagPath], [system.db.runPrepQuery(...)]) # write to tag

```

This eliminates the need to to actively notify potential interested parties (sessions) and instead allows them to simply be passive observers of some value.

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [March 26, 2025, 2:32am UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/8 "2025-03-26T02:32:30Z")

</div>

is this still the preferred way over a gateway message script to update perspective pages? Thanks!

---

<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:** [March 31, 2025, 4:44pm UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/9 "2025-03-31T16:44:43Z")

</div>

It depends on what exactly needs to be updated. If your page has data _bound_ to some datapoint, then forcing an update of the page should be unnecessary. If you're updating _state_ and that state is not determined by some sort of binding, then yes - this is still the suggested mechanism.

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [March 31, 2025, 7:18pm UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/10 "2025-03-31T19:18:08Z")

</div>

Thanks, I'm sending messages to run RESTFUL calls from the gateway. After the calls I need to update the page. Initially I was thinking of sending a message back to the page and that was how I found this post.

I'll add a tag and have that refresh the page, thanks.

---

<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:** [March 31, 2025, 8:40pm UTC](https://forum.inductiveautomation.com/t/send-a-message-from-a-gateway-script-to-the-perspective-sessions/75494/11 "2025-03-31T20:40:48Z")

</div>

If the tag represents any sort of state and this tag is updated via the requests, I would bind something on the page against the tag.
