Directory

← Back

ServerPush

ServerPush for Vaadin

Author

Contributors

Rating

A component that adds TRUE push support with a variety of push methods to Vaadin!

There are two methods for using this add-on:

  1. Use regular Vaadin servlet and manually add ServerPush widget to application as well as manually call push() when you want to push to client(s)
  2. Use ServerPushApplicationServlet instead of the default Vaadin servlet (ServerPush widget is automatically added at the bottom of main window upon new Application creation)

The difference between the two methods is that the former requires explicit calling of push() by the developer, which allows the developer control over when updates are pushed, whereas the latter method provides a seamless push on every repaint request with no explicit calls to push(). The latter method is also much more aggressive in pushing updates to the client(s) since it is invoked on every repaint request.

This component is based on the Atmosphere Framework 0.7.2, which can be configured to use one of four push methods:

  1. Blocking I/O (also acts as a fallback for the next three)
  2. Native Comet support in app server
  3. Servlet 3.0 Async
  4. WebSockets

The best available method will be chosen unless explicitly told which method to use in the Atmosphere servlet configuration.

Make sure that you add the Atmosphere and SLF4J JARs to your WEB-INF/lib. Note that Atmosphere uses SLF4J and this add-on only includes the API JAR; therefore, users of this add-on will need to add a concrete implementation of this API to the app (log4j, java.util, simple, etc.).

NOTE: All samples are in GitHub; they are not included in the download.


NOTE: This add-on is no longer actively developed. I have moved my efforts to DontPush OzoneLayer in conjunction with Matti Tahvonen which is a much more elegant solution that does not require a Component to be added to a Window. New project at: https://vaadin.com/directory#addon/dontpush-ozonelayer


Sample code

package org.vaadin.addons.serverpush.example;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

import org.vaadin.addons.serverpush.ServerPush;

/**
 * Demo class for server push widget
 */
public class ServerPushDemo extends Application {

    private ServerPush push = new ServerPush();

    @Override
    public void init() {
        Window mainWindow = new Window("ServerPush Application");
        setMainWindow(mainWindow);

        // Add the push component
        mainWindow.addComponent(this.push);

        // Add a button for starting background work
        getMainWindow().addComponent(
            new Button("Start background thread", new ClickListener() {
                public void buttonClick(ClickEvent event) {
                    getMainWindow().addComponent(new Label("Waiting for background thread to complete..."));
                    new BackgroundThread().start();
                }
            }));
    }

    public class BackgroundThread extends Thread {

        @Override
        public void run() {
            // Simulate background work
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }

            // Update UI
            synchronized (ServerPushDemo.this) {
                getMainWindow().addComponent(new Label("This label was pushed to client"));
            }

            // Push the changes
            ServerPushDemo.this.push.push();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<atmosphere-handlers>
    <atmosphere-handler context-root="/server-push" class-name="org.vaadin.addons.serverpush.VaadinServerPushHandler">
         <property name="heartbeat" value="5000"/>
     </atmosphere-handler>
</atmosphere-handlers>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_3.0.xsd"
   version="3.0">

    <servlet>
        <description>AtmosphereServlet</description>
        <servlet-name>AtmosphereServlet</servlet-name>
        <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
        <init-param>
            <!-- prevent deadlocks -->
            <param-name>org.atmosphere.disableOnStateEvent</param-name>
            <param-value>true</param-value>
        </init-param>
<!--        <init-param>
            <param-name>org.atmosphere.useWebSocket</param-name>
            <param-value>true</param-value>
        </init-param>-->
        <!--<init-param>
            <param-name>org.atmosphere.useNative</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>org.atmosphere.useBlocking</param-name>
            <param-value>true</param-value>
        </init-param>-->
        <load-on-startup>1</load-on-startup>
        <!--Uncomment if you want to use Servlet 3.0 Async Support-->
        <async-supported>true</async-supported>
    </servlet>

    <servlet>
        <servlet-name>gui</servlet-name>
        <servlet-class>org.vaadin.addons.serverpush.ServerPushApplicationServlet</servlet-class>
        <init-param>
            <param-name>application</param-name>
            <param-value>MyApplication</param-value>
        </init-param>
        <init-param>
            <param-name>productionMode</param-name>
            <param-value>false</param-value>
        </init-param>
        <!--<init-param>
            <param-name>codeBasePath</param-name>
            <param-value>/gui</param-value>
        </init-param>-->
        <init-param>
            <description>Application widgetset</description>
            <param-name>widgetset</param-name>
            <param-value>mywidgetset</param-value>
        </init-param>
        <async-supported>true</async-supported>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>AtmosphereServlet</servlet-name>
        <url-pattern>/server-push</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>gui</servlet-name>
        <url-pattern>/gui/*</url-pattern>
    </servlet-mapping>

</web-app>

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Fix potential red wheel on application close. Patch by Fabian Lange.

Released
2012-07-27
Maturity
STABLE
License
Apache License 2.0

Compatibility

Framework
Vaadin 6.0+
Vaadin 6.6+ in 1.0.4
Browser
Browser Independent
Online