# Gateway Scripts Module Always Run on Gateway

**URL:** <https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498>\
**Category:** Module Development\
**Created:** [December 14, 2020, 2:40pm UTC](https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498 "2020-12-14T14:40:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimender2](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@jimender2](https://forum.inductiveautomation.com/u/jimender2)\
**Post date:** [December 14, 2020, 2:40pm UTC](https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498/1 "2020-12-14T14:40:55Z")

</div>

Hello there,

I have written a module that has a few scripts in it. I always want to have these run on the Gateway (since it needs access to the Gateways filesystem) even if I go and run the script from the Script Console. Is this possible?

---

<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:** [December 14, 2020, 2:52pm UTC](https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498/2 "2020-12-14T14:52:01Z")

</div>

> [@jimender2](#):
>
> even if I go and run the script from the Script Console. Is this possible?

You would have to add a message handler and then use system.util.sendMessage to invoke this from the Script Console. You can't just have it run in the gateway scope all the time.

---

<div class="post-metadata">

**Author:** ![jimender2](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@jimender2](https://forum.inductiveautomation.com/u/jimender2)\
**Post date:** [December 14, 2020, 2:57pm UTC](https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498/3 "2020-12-14T14:57:58Z")

</div>

Thats what I was hoping to avoid doing.

What is the proper way to build a message handler in a module? I seem to have skipped it over in the documentation.

---

<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:** [December 14, 2020, 2:58pm UTC](https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498/4 "2020-12-14T14:58:34Z")

</div>

I assume you mean script module, but message handlers belong to a project, not a module. You just write code in the module that you then invoke from the handler.

---

<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 14, 2020, 3:01pm UTC](https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498/5 "2020-12-14T15:01:03Z")

</div>

It helps if you understand that the scripting environments (aka “scope” or “context”) in the gateway, designer, and Vision clients _ **are not connected to each other** _. They run locally wherever they are. To get scripts to run in other contexts, you must send messages.

---

<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 14, 2020, 3:05pm UTC](https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498/6 "2020-12-14T15:05:56Z")

</div>

It also helps if you understand that the gateway has _ **multiple** _ scopes all running at the same time, with some overlap. Each project has a scripting scope, and the top-level names automatically present (like script module names) vary by project scope. Perspective scope adds a layer of additional names and functions on top of the corresponding project scope.

---

<div class="post-metadata">

**Author:** ![jimender2](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@jimender2](https://forum.inductiveautomation.com/u/jimender2)\
**Post date:** [December 14, 2020, 3:07pm UTC](https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498/7 "2020-12-14T15:07:22Z")

</div>

Doing it that way means that it would not be easily reproducible without coping and pasting code between different gateways/projects (which is what I am trying to prevent).

Is there a way to create a handler which can run “globally” that is created from the module itself? I guess I am just trying to make it as easy as possible for others to use this project. I guess if it really came down to it, I could create some sort of jetty handler that should be able to handle this I guess.

---

<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 14, 2020, 3:10pm UTC](https://forum.inductiveautomation.com/t/gateway-scripts-module-always-run-on-gateway/41498/8 "2020-12-14T15:10:04Z")

</div>

You are posting in Module Development. Which means your module has access to all script engines (managers), and can inject any infrastructure you like under the `system.*` namespace. Do this for all script managers and all scopes will have them.

You also have access to custom RPC from Designer and Vision scopes, so functions called in those scopes can seamlessly perform a callout to the gateway to execute.

{ Hmmm. Wrong category? “Module Development” == “Module _ **SDK** _ Development” }
