User specific server push

Hello, I am trying to figure out the best way of using server push to update a ui element for a specific user. This needs to happen from a background thread, I’m assuming.

When a user logs in, a process needs to check the database at an interval, for events specific to the logged in user. When it finds new events for that user, it needs to push an update to a certain ui element.

I have played around with server push and scheduledexecutorservice, however I’m not sure if it’s the best approach.

Does the executorservice only run for the logged in user session? What if the user logs out and the session dies, does the background theads get killed too? I don’t want the background threads to update all users ui.

Ultimately each user logged in, could be thousands at the same time, needs their own UI updated independant of each other, from background processes checking for events in a database. And then all resources a specific user were using, threads etc., needs to be trashed when user logs out.

Any thoughts on the approach for this?

Thanks
Wessel

You can do it this way.

"What if the user logs out and the session dies, does the background theads get killed too? "

No the thread will still run.

You have to stop it when the session/ui is off.

Override the method detach() of UI to do that :

    @Override
    public void detach() {        
        //Add your code to shutdown your threads

        super.detach();        
    }​

There is another way if it’s your vaadin application and not a third app who put theses events in yourdatabase. Because your thread will make some useless requests on the database.

Use a collection of all session/ui (something like the broadcast example https://vaadin.com/docs/-/part/framework/advanced/advanced-push.html)
Modifiy it to store an unique id for each UI to retrieve the specific user you want.

And then call a push method on the specific UI when your events are triggered in your application.

Thanks for the reply. So are the background threads tied to a session/ui though? Meaning, if a user logs in and a new background thread is started for this user to check for events, and the thread executes at a certain interval and I push an update to the UI, will it only update this one specific users UI? Or all UI’s?

Also, how reliable is the detach method of the UI? Will it fire in all circumstances like closing browser window, etc…

Offcourse I can test these things, just want to get some additional insight…

Thanks
Wessel

It will access only on the current UI who creates the Thread. Not all UI.

To access others UI you have to store them in static collection of UI.

“Also, how reliable is the detach method of the UI? Will it fire in all circumstances like closing browser window, etc…”

It will fires when the UI ends.
Not a expert for that. Only informations i got are here :

https://vaadin.com/docs/-/part/framework/application/application-lifecycle.html