Nicolas Frankel is an IT consultant with 10 years experience in Java / JEE environments. He likes his job so much he writes technical articles on his blog and reviews technical books in his spare time. He also tries to find other geeks like him in universities, as a part-time lecturer. Nicolas is a DZone MVB and is not an employee of DZone and has posted 232 posts at DZone. You can read more from them at their website. View Full User Profile

Server-client Push with Vaadin

10.26.2010
| 11189 views |
  • submit to reddit

I’m more and more committed to 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.

Note: 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.

To go further:

From http://blog.frankel.ch/server-client-push-with-vaadin

Published at DZone with permission of Nicolas Frankel, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Jamie Craane replied on Tue, 2010/10/26 - 2:26am

Nice article!

I am also investigating this solution for my Vaadin application. What can you say about the scalability of IcePUSH in terms of maximum concurrent users? PS. I am deploying my application on a JBoss 4.2 application server.

Joonas Lehtinen replied on Wed, 2010/11/10 - 11:06am

Did you notice that there is another (prototype level) push solution available for Vaadin? "DontPush" add-on is technically much more sophisticated that the ICEPush. It uses HTML5 WebSockets and integrates deeply in Vaadin: no explicit push() calls are needed any more...

Lautaro Brasseur replied on Tue, 2010/12/07 - 3:26pm

Excellent article.

I'm trying to make it work, but my application uses a custom servlet, so I can't use org.vaadin.artur.icepush.ICEPushServlet, which extends com.vaadin.terminal.gwt.server.ApplicationServlet. I'm trying to modify it:

https://github.com/R2R/ICEPush-for-Vaadin/blob/master/src/org/vaadin/artur/icepush/ICEPushServlet.java

in order to convert it to a filter. It requires changing the the class in order to implement Filter instead of inherinting from such servlet and changing the following line:

 

super.service(request, response);


by

 

filterChain.doFilter(servletRequest, servletResponse);

 

The class looks like this one:

 

public class ICEPushFilter implements Filter {

/**
* The ICEPush Servlet.
*/
private MainServlet ICEPushServlet;
/**
* The Javascript provider.
*/
private JavascriptProvider javascriptProvider;

@Override
public void init(FilterConfig filterConfig) throws ServletException {

ICEPushServlet = new MainServlet(filterConfig.getServletContext());
try {
javascriptProvider = new JavascriptProvider(filterConfig.getServletContext().getContextPath());
ICEPush.setCodeJavascriptLocation(javascriptProvider.getCodeLocation());
}
catch (IOException e) {
throw new ServletException("Error initializing JavascriptProvider", e);
}
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String pathInfo = request.getPathInfo();
if (pathInfo != null && pathInfo.equals("/" + javascriptProvider.getCodeName())) {
// Serve icepush.js
serveIcePushCode(request, response);
return;
}
if (request.getRequestURI().endsWith(".icepush")) {
// Push request
try {
ICEPushServlet.service(request, response);
}
catch (ServletException e) {
throw e;
}
catch (IOException e) {
throw e;
}
catch (Exception e) {
throw new RuntimeException(e);
}
} else {
// Vaadin request
filterChain.doFilter(servletRequest, servletResponse);
}
}

/**
* Serves IcePushCode.
*
* @param request The HTTP servlet request
* @param response The HTTP servlet response
* @throws IOException At any IO error
*/
private void serveIcePushCode(HttpServletRequest request, HttpServletResponse response) throws IOException {

String icepushJavscript = javascriptProvider.getJavaScript();
response.setHeader("Content-Type", "text/javascript");
response.getOutputStream().write(icepushJavscript.getBytes());
}

@Override
public void destroy() {

ICEPushServlet.shutdown();
}
}

 

 I'm still trying to make it work (I have some problems with OSGi).

 

Nicolas Frankel replied on Wed, 2010/12/08 - 4:53am in response to: Lautaro Brasseur

Thanks.

I did not look in detail what the ICEPushServlet did. Yet, that you instantiate a new servlet in your filter seems very fishy to me. Just my 2 cents...

Lautaro Brasseur replied on Wed, 2010/12/08 - 11:23am

Thanks for the suggestion. I didn't checked the original code in detail, just copied ICEPushServlet class, which already created the ICEPush servlet. Maybe it can be avoided by making an internal forward (and adding the ICEPush servlet to web.xml).

Liezel Jane Jandayan replied on Thu, 2011/08/25 - 7:01am

In contrast to Javascript libraries and browser-plugin based solutions it features a server-side architecture, which means that the majority of the logic runs on the servers. Ajax technology is used at the browser-side to ensure a rich and interactive user experience. On client-side Vaadin is built on top of and can be extended with Google Web Toolkit.-Jonathan Berkowitz

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.