You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EnablingServerPush.asciidoc 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ---
  2. title: Enabling Server Push
  3. order: 10
  4. layout: page
  5. ---
  6. [[enabling-server-push]]
  7. = Enabling server push
  8. The traditional way of communication between client and server in Vaadin
  9. has been through XHR, i.e. AJAX requests. The client does a request to
  10. the server with information about RPC method invocations, which are
  11. executed on the server. As a result the state of one or several
  12. components is updated and the changes are sent back as a response to the
  13. client. Vaadin 7.1 introduces a new way of doing communications using
  14. server push, which enables the server to push state changes to the
  15. client without a request initiated by the client.
  16. To create a push enabled application you start by creating a normal
  17. Vaadin 7 project using Eclipse or Maven. First you need to add the
  18. vaadin-push package to your project, so open pom.xml or ivy.xml and add
  19. a vaadin-push dependency, e.g. in Ivy
  20. [source,xml]
  21. ....
  22. <dependency org="com.vaadin" name="vaadin-push" rev="&vaadin.version;" conf="default->default" />
  23. ....
  24. and in Maven
  25. [source,xml]
  26. ....
  27. <dependency>
  28. <groupId>com.vaadin</groupId>
  29. <artifactId>vaadin-push</artifactId>
  30. <version>${vaadin.version}</version>
  31. </dependency>
  32. ....
  33. Open your UI class and add a @Push annotation to enable push for the ui
  34. [source,java]
  35. ....
  36. @Push
  37. public class PushTestUI extends UI {
  38. ...
  39. }
  40. ....
  41. If you are using a servlet 3.0 compatible server, open *web.xml* and
  42. enable asynchronous processing by adding a
  43. `<async-supported>true</async-supported>` tag.
  44. Your servlet definition should look something like this:
  45. [source,xml]
  46. ....
  47. <servlet>
  48. <servlet-name>MyServlet</servlet-name>
  49. <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
  50. <init-param>
  51. <param-name>ui</param-name>
  52. <param-value>org.vaadin.example.MyUI</param-value>
  53. </init-param>
  54. <async-supported>true</async-supported>
  55. </servlet>
  56. ....
  57. Your application is now setup for push and will automatically push
  58. changes to the browser when needed. You can test it out using e.g. the
  59. following UI which only adds a Label and starts a thread which does the
  60. real initialization:
  61. [source,java]
  62. ....
  63. @Push
  64. public class PushTestUI extends UI {
  65. private VerticalLayout mainLayout;
  66. @Override
  67. protected void init(VaadinRequest request) {
  68. mainLayout = new VerticalLayout();
  69. mainLayout.setSizeFull();
  70. mainLayout.setMargin(true);
  71. setContent(mainLayout);
  72. mainLayout.setDefaultComponentAlignment(Alignment.MIDDLE_CENTER);
  73. Label loadingText = new Label("Loading UI, please wait...");
  74. loadingText.setSizeUndefined();
  75. mainLayout.addComponent(loadingText);
  76. new InitializerThread().start();
  77. }
  78. class InitializerThread extends Thread {
  79. @Override
  80. public void run() {
  81. // Do initialization which takes some time.
  82. // Here represented by a 1s sleep
  83. try {
  84. Thread.sleep(1000);
  85. } catch (InterruptedException e) {
  86. }
  87. // Init done, update the UI after doing locking
  88. access(new Runnable() {
  89. @Override
  90. public void run() {
  91. // Here the UI is locked and can be updated
  92. mainLayout.removeAllComponents();
  93. mainLayout
  94. .addComponent(new TextArea("This is the real UI"));
  95. }
  96. });
  97. }
  98. }
  99. }
  100. ....
  101. `InitializerThread` is a `Thread` which simulates some work using a
  102. sleep and then updates the UI with the real content. The UI update takes
  103. place inside an _access_ call, which ensures the UI (actually the
  104. `VaadinSession`) is locked to prevent synchronizations problems.
  105. Now deploy the application to your server and open the UI in a browser.
  106. You will see the "Loading..." text for roughly a second and when
  107. the initialization is complete, the Label will be replaced by a
  108. `TextArea`.
  109. The changes were automatically pushed to the browser when the
  110. _access_-block was done running. If you want full control on when
  111. changes are pushed to the client, add `@Push(PushMode.MANUAL)` instead and
  112. call `UI.push()` to push the changes (`UI.push()` needs to be called when
  113. the session is locked, so run that inside the _access_ block too).
  114. You can also configure push mode for the whole application using the
  115. `pushmode` servlet init parameter. Possible values are `automatic`, `manual`
  116. and `disabled`.
  117. It is also possible to switch the push mode on the fly using
  118. `UI.getPushConfiguration().setPushMode()`.