Push only works after a reload

Hi everyone,

I’m using the @Push annotation in my view to send updates from the server to the client.
I have noticed that on a fresh session (first visit of a client) the client receives a pushConfiguration with pushMode DISABLED. Only after a reload in the browser, the client receives a pushMode AUTOMATIC.

Is this a bug or do I miss something?

I found my mistake :slight_smile:

I was trying to use @Push at my views instead of the layout.

Thanks for sharing the solution!

Philip Behrens:
I found my mistake :slight_smile:

I was trying to use @Push at my views instead of the layout.

Hi Philip, what do you mean by this? I am encountering PUSH to only be working when reloading the page.

In my case the problem occurred when I used @Push in a View Class:

@Route
@Push
public class MainView extends VerticalLayout {
...
}

This doesn’t work properly. I had to reload the page to make it work.

The correct way of using it is to implement a layout class (in my case an empty class) and use it in your views:

@Push
public class MyLayout extends Div implements RouterLayout {
}
@Route(layout = MyLayout.class)
public class MainView extends VerticalLayout {
...
}

I’ve done as you suggested and I still get the same issue, sometimes the Push just doesn’t unless I refresh, even after putting it in the Layout class and assigning the layout class to my view. Any other suggestions would be appreciated.

Stephan Grenier:
I’ve done as you suggested and I still get the same issue, sometimes the Push just doesn’t unless I refresh, even after putting it in the Layout class and assigning the layout class to my view. Any other suggestions would be appreciated.

I think I see something similar. Do you also get a “UI not available” exception from your UI.access?

I have the same problem. Looks like the push does now work reliable. It says sometimes, that push is disabled.

Yes, I am seeing similar behaviors in my 14.1.3 app as well. Does Vaadin have a recommended best practice to enable push globally?

I’m not able to reproduce. Any chances of getting a minimal project to reproduce the problem so I can take a look at it?

Thanks Alejandro for the response.

Here’s my main view, push is not enabled until the user hits login view.

@Route
public class MainView extends Div implements RouterLayout {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private AppAccessControl accessControl;

	public MainView() {
		super();
	}

	@PostConstruct
	private void init() {
		if (accessControl.isUserSignedIn()) {
			UI.getCurrent().getPage().setLocation("home");
		} else {
			UI.getCurrent().getPage().setLocation("login");
		}
	}
}

Login view, push is enabled. However, once I get into the main view layout, pushed is disabled. To enabled push I have to use the below code.

/ Push workaround
		this.getUI().ifPresent(ui -> {
			ui.getPushConfiguration().setPushMode(PushMode.AUTOMATIC);
		});
@Push
@Route("login")
@PageTitle("Login")
@Viewport("width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes")
@PWA(name = "Junologix | Tax Compliance Automation", shortName = "Tax Compliance")
public class LoginView extends FlexLayout {

private void login(LoginForm.LoginEvent event) {
		try {
			if (accessControl.signIn(event.getUsername(), event.getPassword())) {
				// Default view
				UI.getCurrent().navigate(MainView.class);
			} else {
				event.getSource().setError(true);
			}
		} catch (Exception e) {
			event.getSource().setError(true);
		}
	}

}

Hi Alejandro,

I have resolved the issue. Rather than enabling push in the login view (has route annotation), I move the push annotation to the main app layout (no route annotation) and it is working find now.

Realize this is somewhat old, but I’m running into this problem now. Alejandro (or others), seems like people are having various results depending on which class receives the @Push annotation. Can you advise on best practice?