2

I am new to Vaadin and just implement a Prototype for testing the functionality. Currently I play around with Vaadin Server-Push feature. I like to do a simple counter which can be increased by Button. After that I like to open this side from different Browser and when I count up I like all Browser to be updated.

For now I've got that the current Browser is updated by a separated Thread.

public class PushTestView extends VerticalLayout implements View {

   private static final long serialVersionUID = -8056849522760207972L;
   private Label counter;

   public void enter(ViewChangeEvent event) {
      removeAllComponents();
      PushTestController controller = PushTestController.getInstance();
      counter = new Label("" + controller.getCounter());
      Button button = new Button("Count up");

      button.addClickListener(new ClickListener() {
         private static final long serialVersionUID = -1437136914245802675L;

         @Override
         public void buttonClick(ClickEvent event) {
            InitializerThread thread = new InitializerThread();
            thread.run();
         }
      });

      addComponent(counter);
      addComponent(button);

   }

   class InitializerThread extends Thread {
      @Override
      public void run() {
         // Init done, update the UI after doing locking
         UI.getCurrent().access(new Runnable() {
            @Override
            public void run() {
               // Here the UI is locked and can be updated
               PushTestController.getInstance().countUp();
               counter.setValue("" + PushTestController.getInstance().getCounter());
               UI.getCurrent().notifyAll();
            }
         });
      }
   }
}

This view I embedded into my UI which is annotated by @Push. And here is my controller:

public class PushTestController {

   private static PushTestController instance = new PushTestController();
   private int counter = 0;

   private PushTestController() {
      //Private Constructor for Singleton 
   }

   public static PushTestController getInstance() {
      return instance;
   }

   public int getCounter() {
      return counter;
   }

   public String getCounterString() {
      return "" + counter;
   }

   public void countUp() {
      counter++;
   }
}

How do I have to extend this example that it works to update the UI in different browsers?

Dominic Weiser
  • 1,446
  • 1
  • 20
  • 32
  • 1
    FYI, I posted a minimal example of using Push in Vaadin 7.3.7 in [my Answer](http://stackoverflow.com/a/27808461/642706) to another Question. – Basil Bourque Jan 06 '15 at 23:05
  • FYI… I posted a [complete working example app using Push](https://stackoverflow.com/a/50885540/642706) to a similar Question, [*Vaadin: Update UI after data returned*](https://stackoverflow.com/q/50880506/642706) – Basil Bourque Jun 18 '18 at 19:49

2 Answers2

3

Take a look at this one, section 11.16.3: https://vaadin.com/book/vaadin7/-/page/advanced.push.html

Gabor
  • 61
  • 5
2

You have to use servlet 3.0 on your project and edit your ivy.xml(or if you use Maven then pom.xml). After that you have to add the @Push annotation to enable push for the ui. Something like this:

<dependency org="com.vaadin" name="vaadin-push" rev="&vaadin.version;" conf="default->default" />

in Maven:

<dependency>
  <groupId>com.vaadin</groupId>
  <artifactId>vaadin-push</artifactId>
  <version>${vaadin.version}</version>
</dependency>

The servlet configuration:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
    <init-param>
        <param-name>ui</param-name>
        <param-value>org.vaadin.example.MyUI</param-value>
    </init-param>
    <async-supported>true</async-supported>
</servlet>

And at last the @Push

@Push 
public class PushTestUI extends UI 

Reference here

Slenkra
  • 820
  • 1
  • 8
  • 14
  • I have still done this Test. And so far it works. But now I like to update a UI from one Browser to another. – Dominic Weiser Jul 16 '13 at 14:26
  • 1
    @TooR push itself only allows for direct communication between the server and each UI instance (each Browser so to say). If you want to notify other UIs/sessions you have to do this yourself, e.g. by triggering a background operation that updates the other UIs two. – Johannes Wachter Aug 22 '13 at 10:37
  • 1
    @TooR , you need to use a bit the Atmosphere framework infrastructure, by implementing the BroadcasterListener interface on the UI:https://vaadin.com/wiki/-/wiki/Main/Broadcasting+messages+to+other+users – frandevel Oct 02 '14 at 07:20
  • 1
    @TooR The Book Of Vaadin now includes [an example](https://vaadin.com/book/-/page/advanced.push.html#advanced.push.pusharound) of exactly what you want, broadcasting from one user to other users. See Chapter 11.16, Server Push. – Basil Bourque Jan 06 '15 at 23:09