How to dynamically push-update pages in vaadin?

967 views Asked by At

I have a working application with Vaadin and Spring (+Security). I'm now trying to dynamically push updates to the client interfaces using vaadin-push.

I want to create a "view" that is the same for all clients. The view should get a counter incremented dynamically by push. So, while the clients show the page, the counter updates itself (or in my example: appends multiple label components to the page). Every client should see the same value.

But I don't see the changes pushed to the webpage. Why?

//the static controller the dynamically changes content
@Controller
public class ViewPresenter {
    private static int counter = 0;

    @Autowired
    private void StaticView view;

    @Scheduled(fixedRate = 1000)
    public void refresh() {
        //push the content change periodically to frontend
        view.getUI().access(new Runnable() {
            @Override
            public void run() {
                view.addComponent(new Label("refresh: " + counter++);
                Sysout("update executed: " + counter); //OK, is printed
            }
        });
    }
}

//the static test component that gets a new label added on every content change
@VaadinComponent
public class StaticView extends CssLayout {
        //this is the view that every client should see
}

//the secured admin view
@VaadinView(name = "/secured")
@Scope("ui")
@Secured("user")
public class SecuredView extends VerticalLayout implements View {
    @Autowired
    private StaticView staticView;

    @Override
    public void enter(ViewChangeEvent event) {
        addComponent(staticView);
    }
}

//enable push
@Configuration
@Push
public class AppConfig {
}

@VaadinUI
@PreserveOnRefresh
public class ApplicationUI extends UI {

} 

I can see the changes on the client side only if I manually refresh the webpage. But the content itself does not change automatically.

Maybe @Push has to be placed on the UI class? But in this case I'd get the following error:

java.lang.IllegalStateException: Cannot suspend a response longer than the session timeout. Increase the value of session-timeout in web.xml at org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:314) at org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:292) at com.vaadin.server.communication.PushHandler$2.run(PushHandler.java:129) at com.vaadin.server.communication.PushHandler.callWithUi(PushHandler.java:242) at com.vaadin.server.communication.PushHandler.access$200(PushHandler.java:55) at com.vaadin.server.communication.PushHandler$1.onRequest(PushHandler.java:73)

1

There are 1 answers

0
membersound On BEST ANSWER

Solution based on https://github.com/peholmst/vaadin4spring/issues/51#issuecomment-46875255:

server.sessionTimeout=30 with @Push(transport = Transport.LONG_POLLING)