Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
Scenario:
User sets a value that needs to be persisted for the session until the user logs out or sets a new value himself.
What's done so far :
1. Created thingshape > added it to the user subsystem > property in the ThingShape is persistent
2. On the mashup there's a text box to accept value from the user > set button which calls the setter service with following code :
Resources["CurrentSessionInfo"].SetGlobalSessionStringValue({ name: "unitNumber" /* STRING */, value: unitNumber /* STRING */ });
3. There's a label widget on the mashup to show the value set by the user, which calls the Resources["CurrentSessionInfo"].GetGlobalSessionValues which returns a persistent property value unitNumber, set above by the setter service.
4. ServiceInvokeCompleted event from the setter service is bound to the Getter to return this value.
5. This works fine
Problem:
Once the mashup is refreshed or user navigates from current mashup to another and comes back, the session value for unitNumber set by the user disappears.
Not sure if I am missing any further step to persist this. Could someone please help point my mistake here
Thanks!
EDIT:
TWX version : 8.4.2
Solved! Go to Solution.
Ok I think i found the issue, it is about setting textBox widget value directly with the session parameter. This should not be done, (also noted in that article I added in my previous response)
To make it persistent value need to run through the setter service and not via the Session parameter in the mashup.
Hi,
If the getter only executes on service invoke completed of the setter, you will not be able to see the session value on a new page load.
Bind as a trigger to the getter the mashup loaded as well - or bind the session variable directly.
If you're interested, in developer tools you can also use TW.Runtime.Session.GetSessionValue("MyKey") to see what value is stored on the client side - use it only to check, but not in production code, as it's not guaranteed not to change in future.
Thanks! @VladimirRosu
I missed to add this info but i do i have bind on getter when page reloads so basically the getter is also executed them page is refreshed or when user navigates back to this mashup.
This I wasn't aware, but just wondering how does TW.Runtime.Session.GetSessionValue("MyKey") differ from the Resource > GetGlobalSessionValues?
I did test with binding the session variable directly to the label widget.
One is executed server side, the resource approach, and the TW.Runtime is executed locally in the browser, internally, by the Mashup runtime.
I would use the javascript code in the console to check if internally the Mashup Runtime has the value you have set.
Ideally the results of both methods should be in sync.
The Session Variables themselves exist across the browser and the server at the same time.
Can you somehow attach the example mashup here?
Appreciate your quick response on this
Here's the mashup and the service bound to the widget label-49.
I tested with your suggestion executing TW session in dev console, it returns valid value as long as I don't refresh. After refresh value is trashed. Even through the property in the ThingShape is persistent
OK, so in my 8.4.3 ThingWorx version I did the following:
-created a Thing with 2 services: Get -> String, and Set(STRING) -> nothing
-created a TestMashup, which has 2 widgets: Label and Navigation widget. This mashup uses the Get service, and the result is bound to the label. The Navigation points to the SetVarMashup
-created a SetVar mashup, which has 2 widgets: TextBox and Button.This Mashup uses the Set service, and the TextBox Input is bound to the Set service input.
I did not use any direct bindings to session.
I have set the navigation to popup, full screen and replace, and each time I had in the values I set in the TestMashup.
Perhaps unrelated, what I discovered though, is that calling the Set service did not affect the TW.Runtime.Session.GetSessionValue result, and I believe as a consequence, neither the label IF I bounded that to the Session variable in the UI. I think this is a bug, but I need to investigate more
Another discovery. The Server to Client session data synchronization only happens when loading the page. I guessed this was the case, but I'm able to confirm that by looking at the TW.Runtime.SessionStartup function from the mashup-runtime.js?_v=8.4.3 (Developer Tools/Sources, in the Thingworx/Runtime/js folder).
The following behavior is correct:
-if you set a session variable directly with the UI (eg: textbox Text to Session binding), this gets propagated to the server
-if you set a variable on the server (through the Resource approach), it won't get propagated on the client, because of the discovery above. Pressing F5 triggers the reload and you'll get the new session variable.
Good. Let me know what happens ;)
So finally I could get back to this topic and retried with the option that you talked about i.e. :
if you set a session variable directly with the UI (eg: textbox Text to Session binding), this gets propagated to the server
This only works as long as I am not refreshing the mashup or if am not navigating away. Until then I can see the value with the Runtime function that you shared via developer console the moment I refresh value disappears.
BTW, something I noticed there is a function that only shows up when writing JS in the service with CTL + Space called Get & Set SessionValue. So far I was only using SetGlobalSessionStringValue and GetGlobalSessionValues. Do you think GlobalSession services are not the right services to use for setting such session values?
I somehow remember with the older version it was pretty straight forward and all the steps that I am following seems to also match with this article Understanding Session Variables in ThingWorx
Thanks again for your continued help on this!
Ok I think i found the issue, it is about setting textBox widget value directly with the session parameter. This should not be done, (also noted in that article I added in my previous response)
To make it persistent value need to run through the setter service and not via the Session parameter in the mashup.