/ VAADIN

Server-client push with Vaadin

I’m more and more comitted into Vaadin since I see so many advantages to this solution. This time, I’ve investigated how to push server data to the client.

Vaadin is a general purpose framework that is wise enough not to force you to code in one way or another. Many features are available in the core distribution but many more are available in the add-ons directory. One such add-on let you add push feature to your simple application. This add-on is based on ICEPush technologies and is aptly named ICEPush add-on.

ICEPush is a brand of technology usable with both Java and JavaScript that uses long polling in order to achieve push and as such does not need port opening (save the one the standard HTTP stream uses, of course). The ICEPush Vaadin add-on integrates ICEPush into your Vaadin application in a very simple way, so that only so much steps are needed in order to develop a full-fledged application with push features.

Set-up ICEPush add-on

Once you’ve created your Vaadin project (and application), add the ICEPush libraries to your project. If you use Maven, just add the following snippet to your POM:

<dependency>
   <groupId>org.vaadin.addons</groupId>
   <artifactId>icepush</artifactId>
   <version>0.2.0</version>
</dependency>
<repository>
   <id>vaadin-addons</id>
   <url>http://maven.vaadin.com/vaadin-addons</url>
</repository>

If not, just download the ZIP available in the directory. Beware that all JAR inside should be copied to your WEB-INF/lib. Even though 2 of them look alike, one is the ICEPush JAR, the other the integration layer on Vaadin.

If you use the Vaadin plugin with your IDE, and it asks you to recompile your widgetset, just click yes and be done with it.

Use the right servlet

Replace the default servlet configured in your web.xml (probably com.vaadin.terminal.gwt.server.ApplicationServlet) with org.vaadin.artur.icepush.ICEPushServlet.

Add the ICEPush object to your window

In order to use push technology, you’ll have to add a new ICEPush object to the window in use, like so:

public class PushApplication extends Application {

  private ICEPush push = new ICEPush();

  @Override
  public void init() {
    Window mainWindow = new Window("Push Application Example");
    mainWindow.addComponent(push);
  }
}

Now, each time you nee to update the window, just call push.push() and it’s done!

This method call will update the client’s window with the update you made. Vaadin will do true AJAX in that it won’t reload the page, just update the view with the changes.

As usual, you’ll find the source of this article here, in Eclipse format.

Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.

Read More
Server-client push with Vaadin
Share this