Hons
Hons

Reputation: 4056

Set push transportation programmatically

In Vaadin there is the possibility to specify the transport protocol in the Push annotation at start time.

@Push(transport = Transport.LONG_POLLING)

That works fine, but I'd like to check the URI in the UIInit for instance and set this transport accordingly there. The problem that we face is that not all connections allow us to use websocket so we'd need to switch back to long polling eventually. There should be an automatism in Vaadin that should fallback, but that doesn't seem to work.

Is there a way to set this? It doesn't need to change over time. One UI could stick to one transport, but I cannot hardcode it in an annotation.

Upvotes: 1

Views: 89

Answers (1)

mmlopez
mmlopez

Reputation: 106

Using Vaadin 24, you can use the following workaround. Create a VaadinServiceInitListener and disable push with the @Push annotation, and enable it and set the transport in a UI's BeforeEnterListener:

@Push(value = PushMode.DISABLED, transport = Transport.LONG_POLLING)
public class AppShell implements AppShellConfigurator, VaadinServiceInitListener {
    @Override
    public void serviceInit(ServiceInitEvent event) {
        event.getSource().addUIInitListener(uiEvent -> {
            uiEvent.getUI().addBeforeEnterListener(attachEvent -> {
                UI ui = attachEvent.getUI();
                Transport transport = useWebSocket()?transport = Transport.WEBSOCKET:transport = Transport.LONG_POLLING;
                ui.getPushConfiguration().setTransport(transport); 
                ui.getPushConfiguration().setPushMode(PushMode.AUTOMATIC);
            });
        });
    }
}

Upvotes: 4

Related Questions