Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
VaadinSession, getUIs, and UI.getCurrent
If I open my app in Chrome and I open it in Safari I would expect that, that would be two different VaadinSessions with one UI object each. However, when I access the UI's using:
I am finding that the two different browsers are using the same VaadinSession and that VaadinSession has two UI objects in it.
I have multiple users hitting the app and I am storing some user specific info in my UI class. I was hoping to be able to grab a UI from the current VaadinSession and access that user info from outside of the UI Thread (My project is in Scala and I'm trying to access the UI in the package object). Also, any attempts to use UI.getCurrent() result in a null. I've tried calling it from a variety of spots in a variety of different situations and each time it is null. Any advice would be very helpful.
Under the hood UI.getCurrent() use thread local.
That's why it return null if you call it outside of your UI thread.
There is another way to find out UI it is thread independent.
You can ask from component who(UI) owns it. componet.getUI() https://vaadin.com/api/com/vaadin/ui/Component.html#getUI--
That method is not listed in the doc as being Static which implies to me that I would need to have a component object. There doesn't appear to be a getCurrent for Component. How would I get the component object? The method that I need the current UI in is not necessarily being called from a vaadin component. It might be from another helper function or another part of the app which is why I am trying to grab the UI object globally through either the static getCurrent method on UI or the current VaadinSession.
The real root of the issue is the fact that VaadinSession isn't behaving how the documentation leads me to believe. Shouldn't two seperate browsers (chrome and safari) be two seperate VaadinSessions??
The VaadinSession may have more UI instances for the same browser (e.g. you refresh the page or open the same app on another tab) but It should never share UIs from different browser; VaadinSession is stored in HttpSession so different browser should create different instances.
Are you using some custom VaadinSession extension?
I'm not using any VaadinSession extensions. Is it possible to gain access to the main thread so that UI.getCurrent returns a non null value? That would work too. The only example I can find is locking a ui object that you already have, which I don't have in this case. I would hate to have to find the hundreds of places that are calling my helper function and make them pass in the ui object. I also am unable to do that in all cases as some of the places I would need to do that are class extensions which don't take any parameters by design.
UPDATE: It looks like auth0's login as user functionality was causing some confusion with VaadinService. Signing in organically doesn't seem to cause that issue any more. Now when I open up a tab in safari and sign in as X then open chrome and sign in as Y, I don't see two UI's. However, now what I'm experincing is that both instances VaadinService.getCurrent.getSession.getId is the exact same thing and the UI that is returned is from the first logged in user.
UPDATE: I'm still seeing this behavior. I'm printing off the browser that the vaadin session belives created it and I'm seeing whatever browser opened first for both browsers (if I open it in safari first I see safari even when I'm doing things in chrome). Any ideas? Is there some context in which VaadinSession is stored off and getCurrent isn't the current?