Server Push Configuration
Server push is based on a client-server connection established by the client. The server can then use the connection to send updates to the client. For example, it could send a new chat message to all participants without delay.
The server-client communication uses a WebSocket connection, if supported by the browser and the server. If not, the connection resorts to whatever method is supported by the browser. Vaadin uses the Atmosphere framework, internally.
Enabling Push in Your Application
To enable server push, you’ll need to define the push mode either in the deployment descriptor, or with the @Push
annotation on your application shell class.
The application shell class is a plain Java class implementing the AppShellConfigurator
interface. The class is detected and instantiated by Vaadin during bootstrap. Only one application shell class is allowed. An exception is thrown if more than one class implements AppShellConfigurator
.
For example, you can use the following Application
class — which implements the AppShellConfigurator
interface — to enable server push in your application by adding the @Push
annotation to it.
@Push
public class Application implements AppShellConfigurator {
...
}
Push Modes & Transports
You can use server push in two modes: automatic
and manual
. The automatic mode automatically pushes changes to the browser after access()
finishes. With the manual mode, you can do the push explicitly with push()
, which allows more flexibility.
Server push can use several transports: WebSockets, long polling, or combined WebSockets+XHR. WebSockets+XHR is the default transport.
The @Push Annotation
You can enable server push for the application annotating the application shell class with the @Push
annotation as in the example here. It defaults to automatic mode (i.e., PushMode.AUTOMATIC
).
@Push
public class Application implements AppShellConfigurator {
...
}
To enable manual mode, you need to pass the PushMode.MANUAL
parameter, as follows:
@Push(PushMode.MANUAL)
public class Application implements AppShellConfigurator {
...
}
To use the long polling transport, you’ll need to set the transport parameter to Transport.LONG_POLLING
, as follows:
@Push(transport = Transport.LONG_POLLING)
public class Application implements AppShellConfigurator {
...
}
Servlet Configuration
If you’re manually configuring your servlet, be sure to set the async-supported
parameter.
You can enable server push and define the push mode for an entire application in the servlet configuration with the pushMode
parameter for the servlet in the web.xml
deployment descriptor, or a corresponding @WebServlet
annotation.
On the server side, the push endpoint is mapped to the VAADIN/push
path. This mapping is added either under context root, context path, or the first URL mapping — sorted by natural order and ignoring /VAADIN/*
and /vaadinServlet/*