title: Server Push JSF page to client
body: <p>For my Java EE 6 (Richfaces 4.1) based application I am trying to achieve this: the user opens a browser window which makes a long poll until a given REST service is invoked (the rest service will receive a facelets name together with a list of parameters). As a result of the Webservice call, the JSF page is rendered in the browser with the parameters specified in the REST call. </p>
  
  <p>As a proof of concern, I've first tried with AsyncContext and CDI Events and I can print the REST parameters in the browser:</p>
  
  <pre><code>@WebServlet(name = "Notifier", urlPatterns = {"/Notifier"},asyncSupported = true)
  public class Notifier extends HttpServlet {
  
      @Inject
      Event&lt;NotificationReq&gt; events;
  
      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
              AsyncContext startAsync = request.startAsync();
              startAsync.setTimeout(0);
              events.fire(new NotificationReq(startAsync));
      }
  }
  -------------------------
      @ConcurrencyManagement(ConcurrencyManagementType.BEAN)
      @Singleton
      @Path("notify")
      public class RESTNotifier {
  
          @PostConstruct
          public void onInit() {
              this.browsers = new CopyOnWriteArrayList&lt;NotificationReq&gt;();
          }
          private CopyOnWriteArrayList&lt;NotificationReq&gt; browsers;
  
          public void onNewNotificationRequest(@Observes NotificationReq nr) {
              this.browsers.add(nr);
          }
  
          @GET
          @Path("{message}")
          public void specific(@PathParam("message") String message) {
              for (NotificationReq notificationRequest : browsers) {
                  notificationRequest.sendMessage(message);
                  this.browsers.remove(notificationRequest);
              }
          }
      }
      -----------------------------------------------------
          public class NotificationReq {
  
              private AsyncContext asyncContext;
  
              public NotificationReq(AsyncContext asyncContext) {
                  this.asyncContext = asyncContext;
              }
  
              public void sendMessage(String message){
                  try {
                      PrintWriter out = this.asyncContext.getResponse().getWriter();
                      out.println(message);//TODO: Invoke and Render JSF instead of printing message!
                      this.asyncContext.complete();
                  } catch (IOException ex) {
                      Logger.getLogger(NotificationReq.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
  </code></pre>
  
  <p>As I use richfaces, I then thought I could better do this with a4j:push: <a href="http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=push&amp;sample=pushCdi" rel="nofollow">http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=push&amp;sample=pushCdi</a></p>
  
  <p>My questions are:</p>
  
  <p>1) What you think is the best approach (I would say Richfaces, as the AsyncContext terminates as soon as the service is invoked, and I would like to have a sort of "infinite" long polling)?</p>
  
  <p>2) Can you please point me to any example showing how to invoke a JSF page programmatically (this is the TODO you can see in my code)</p>
  
  <p>Thanks a lot!</p>