# SDK transfert data from gateway to perspective

**URL:** <https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349>\
**Category:** Module Development\
**Tags:** ignition80, perspective\
**Created:** [July 3, 2020, 5:27pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349 "2020-07-03T17:27:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 3, 2020, 5:27pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/1 "2020-07-03T17:27:51Z")

</div>

@paul-griffith, @PerryAJ

In a module, I need to transfert data from the gateway scope to a perspective client.

- Is it possible to send a message to a client session/page/view with the sdk ?  
The message will be consume with a script message handler.

- Is it possible to write a session custom prop with the sdk ?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/e/9edc91277ac72e05697ef9a74a5833c6b2f1eb86.png)

---

<div class="post-metadata">

**Author:** ![KGarner](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgarner/32/15999_2.png) [@KGarner](https://forum.inductiveautomation.com/u/KGarner)\
**Post date:** [July 4, 2020, 1:58am UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/2 "2020-07-04T01:58:29Z")

</div>

Not sure if this is what you’re looking for, but there’s an [older post](http://forum.inductiveautomation.com/t/how-to-track-perspective-sessions-from-my-perspective-module/30724/5) that shows how to add listeners for when a session is opened. You might be able to add/modify some properties in that instance.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 4, 2020, 6:49am UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/3 "2020-07-04T06:49:48Z")

</div>

@paul-griffith, @PerryAJ  
in javadoc:  
[http://files.inductiveautomation.com/sdk/javadoc/ignition80/8.0.14/index.html](http://files.inductiveautomation.com/sdk/javadoc/ignition80/8.0.14/index.html)  
there is no package **com.inductiveautomation.perspective**

---

<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:** [July 6, 2020, 3:48pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/4 "2020-07-06T15:48:04Z")

</div>

Perry will know more about the Javadoc situation, but the answer to both of your questions should be yes. As in @KGarner's linked thread, you'll need to have a dependency on the Perspective module in your module's code to be in the right classloader scope, and after that get the PerspectiveGatewayContext.

You'll then have to either know how to get into the session you want (or, and it'll be much easier, just send your message to all sessions). You'll need to 'target' this session by it's ID; see `com.inductiveautomation.perspective.gateway.session.PerspectiveSessionMonitor.findSession()`.  
That gives you an `Optional<InternalSession>`, which extends `com.inductiveautomation.perspective.gateway.api.Session`. `Session` has a `queue` method which returns an `ExecutorService`. **All** operations against Perspective properties must go through this queue - dispatch lambdas using `submit`, `submitOrRun`, etc.

Once you've got a session, you need to decide the scope you're going to send to, and what to actually send. I'd recommend starting at the whole session. Construct a `com.inductiveautomation.perspective.gateway.event.UserScopeMessageEvent` - for the PyObject, just create a `PyDictionary` or `PyStringMap` from a hashmap. Then, get into the queue and dispatch an event to the session:  
`session.queue().submit(() -> session.getEventBus().post(message)));`

Sending to pages or views requires accessing the `pageEventBus` or `viewEventBus`, respectively - you'll need to drill in from the `Session` object. That should be enough to get you started.

> - Is it possible to write a session custom prop with the sdk ?

Yes - similarly, you'll need to have a `Session`, and any operations you do must go through the queue. You'll need to get the `custom` property tree, via `com.inductiveautomation.perspective.gateway.api.PropertyTreeOwner#getPropertyTreeOf` - the `InternalSession` is a `PropertyTreeOwner`, so just `session.getPropertyTreeOf(PropertyType.custom)`. Then there are a few public `write` overloads, such as:

```auto
com.inductiveautomation.perspective.gateway.property.PropertyTree#write(java.lang.String, com.inductiveautomation.ignition.common.gson.JsonElement, com.inductiveautomation.perspective.common.property.Origin, java.lang.Object)

```

The plain Object parameter should just be a reference to wherever you're calling the write from.  
Writing to a session parameter that doesn't exist will implicitly create it.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 6, 2020, 4:10pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/5 "2020-07-06T16:10:05Z")

</div>

Thanks a lot @paul-griffith for all these clarifications 👍  
I will test this as soon as possible !

---

<div class="post-metadata">

**Author:** ![PerryAJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/perryaj/32/3281_2.png) [@PerryAJ](https://forum.inductiveautomation.com/u/PerryAJ)\
**Post date:** [July 6, 2020, 8:28pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/6 "2020-07-06T20:28:19Z")

</div>

I’m looking into why the perspective javadocs aren’t included, will follow up.

---

<div class="post-metadata">

**Author:** ![PerryAJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/perryaj/32/3281_2.png) [@PerryAJ](https://forum.inductiveautomation.com/u/PerryAJ)\
**Post date:** [July 7, 2020, 3:13pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/7 "2020-07-07T15:13:43Z")

</div>

Just to follow up - we are building/uploading new javadocs with Perspective packages included. Should be available within an hour of this post. We will publish them going forward as well.

-Perry

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 8, 2020, 10:22am UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/8 "2020-07-08T10:22:05Z")

</div>

Thanks @PerryAJ  
Packages **com.inductiveautomation.perspective.** \* are now available in javadoc 8.0.14 😀

---

<div class="post-metadata">

**Author:** ![andrea.morando](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andrea.morando/32/8039_2.png) [@andrea.morando](https://forum.inductiveautomation.com/u/andrea.morando)\
**Post date:** [July 29, 2020, 7:03am UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/9 "2020-07-29T07:03:19Z")

</div>

Dears,  
thank you for this post.  
Which is the dependency to add to the module pom, in terms of groupId and artifactId?

Thank you so much.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 29, 2020, 7:10am UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/10 "2020-07-29T07:10:45Z")

</div>

Not tested, but probably something like:

```auto
        <dependency>
            <groupId>com.inductiveautomation.ignitionsdk</groupId>
            <artifactId>perspective-gateway</artifactId>
            <version>${ignition-sdk-version}</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>

```

and perhaps:

```auto
        <dependency>
            <groupId>com.inductiveautomation.ignitionsdk</groupId>
            <artifactId>perspective-common</artifactId>
            <version>${ignition-sdk-version}</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>

```

to confirm by @paul-griffith

---

<div class="post-metadata">

**Author:** ![andrea.morando](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andrea.morando/32/8039_2.png) [@andrea.morando](https://forum.inductiveautomation.com/u/andrea.morando)\
**Post date:** [July 29, 2020, 8:20am UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/11 "2020-07-29T08:20:57Z")

</div>

Thank you @mazeyrat!  
I am able to import the **com.inductiveautomation.perspective.gateway** package.  
I follow the code snippet of @paul-griffith, then when I install the module the following error occurs:

_java.lang.NoClassDefFoundError: com/inductiveautomation/perspective/gateway/api/PerspectiveContext_

It seems the module is not able to interact with the Perspective context in the startup phase.  
Any suggestion will be appreciated.

Thank you.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [July 29, 2020, 8:34am UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/12 "2020-07-29T08:34:40Z")

</div>

have you the dependency with the perspective module in the build.pom

I suppose something like:

```auto
                    <depends>
                        <depend>
                            <scope>G</scope>
                            <moduleId>com.inductiveautomation.perspective</moduleId>
                        </depend>
                        <depend>
                            <scope>D</scope>
                            <moduleId>com.inductiveautomation.perspective</moduleId>
                        </depend>
                    </depends>

```

see gradle example:

```auto
moduleDependencies = [
                [scope: "G", moduleId: "com.inductiveautomation.perspective"],
                [scope: "D", moduleId: "com.inductiveautomation.perspective"]
        ]

```

---

<div class="post-metadata">

**Author:** ![andrea.morando](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andrea.morando/32/8039_2.png) [@andrea.morando](https://forum.inductiveautomation.com/u/andrea.morando)\
**Post date:** [July 30, 2020, 7:42am UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/13 "2020-07-30T07:42:05Z")

</div>

Dear @mazeyrat,  
it works fine! Thank you for your kind support and help.

Best,  
Andrea

---

<div class="post-metadata">

**Author:** ![andrea.morando](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andrea.morando/32/8039_2.png) [@andrea.morando](https://forum.inductiveautomation.com/u/andrea.morando)\
**Post date:** [August 4, 2020, 8:50am UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/14 "2020-08-04T08:50:11Z")

</div>

Must be the message to call a Session Message Handler?  
I run a java method using the snippet by @paul-griffith, the code run with success, but the session message handler is not triggered.

```
    PerspectiveContext perspectiveContext = PerspectiveContext.get(this.context);
    for (UUID sessionId: GatewayHook.perspectiveSessionListener.getSessionIds()
         ) {
        if(perspectiveContext.getSessionMonitor().findSession(sessionId).isPresent()){
            InternalSession session = perspectiveContext.getSessionMonitor().findSession(sessionId).get();
            PyDictionary payloadMap = new PyDictionary();
            payloadMap.put("sessionId", sessionId.toString());
            payloadMap.put("paramUUID", paramUUID);
            UserScopeMessageEvent message = new UserScopeMessageEvent("test", payloadMap);

            session.queue().submit(() -> session.getEventBus().post(message));
        }
    }

```

Thanks a lot.

---

<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:** [August 4, 2020, 4:56pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/15 "2020-08-04T16:56:52Z")

</div>

There’s no logging on the event bus itself, unfortunately. You do have a message handler configured on the target, with the same key defined, right?  
You can clean up the code a little bit and make sure it’s actually calling your message:

```auto
    PerspectiveContext perspectiveContext = PerspectiveContext.get(this.context);
    for (UUID sessionId: GatewayHook.perspectiveSessionListener.getSessionIds()) {
        perspectiveContext.getSessionMonitor().findSession(sessionId).ifPresent(session -> {
            PyDictionary payloadMap = new PyDictionary();
            payloadMap.put("sessionId", sessionId.toString());
            payloadMap.put("paramUUID", paramUUID);
            UserScopeMessageEvent message = new UserScopeMessageEvent("test", payloadMap);
            LoggerEx.newBuilder("messageHandler").build().infof("Calling handler: %s", message)
            session.queue().submit(() -> session.getEventBus().post(message));
        });
    }

```

---

<div class="post-metadata">

**Author:** ![andrea.morando](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andrea.morando/32/8039_2.png) [@andrea.morando](https://forum.inductiveautomation.com/u/andrea.morando)\
**Post date:** [August 6, 2020, 3:23pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/16 "2020-08-06T15:23:09Z")

</div>

Thank you @paul-griffith: it works!

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [August 18, 2020, 12:21pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/17 "2020-08-18T12:21:55Z")

</div>

@paul-griffith  
That’s fine !  
For a script function exposed by a module and called in perspective, can we obtain in the script function the context session Id ? (in order to interact with this session in the script function). I would like to avoid a sessionId parameter…

Seems to be ok with:

```auto
InternalSession.SESSION.get().getSession().getSessionId()

```

Is it the right way to do things ?

---

<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:** [August 18, 2020, 3:12pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/18 "2020-08-18T15:12:09Z")

</div>

If you’re calling the `InternalSession.SESSION` threadlocal directly, then you don’t need to go through the rest of the Perspective context - you can directly call whatever you want to - ie,

```auto
InternalSession.SESSION.get().getEventBus().post();

```

Also, a healthy disclaimer that while it’s _unlikely_ we’ll change things, the InternalSession threadlocal is not considered part of our API surface, so it could break in some future version without notice.

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [August 18, 2020, 3:22pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/19 "2020-08-18T15:22:43Z")

</div>

Is there any other method to retrieve the sessionId ?

---

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [August 21, 2020, 2:59pm UTC](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/20 "2020-08-21T14:59:37Z")

</div>

Can we restrict the **UserScopeMessageEvent** for be send to a page and/or view

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

[Next page](https://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349.md?page=2)
