Uploaded image for project: 'RichFaces'
  1. RichFaces
  2. RF-12072

Push: add onsubscribed event (was: Lost event in push)

    Details

    • Affects:
      Documentation (Ref Guide, User Guide, etc.)
    • Workaround:
      Workaround Exists
    • Workaround Description:
      Hide

      Use a4j:jsFunction and <h:body onload=... />

      <h:body onload="forceReloadChat()">
      	<h:form id="myForm">
              <a4j:push address="chat">
                  <a4j:ajax event="dataavailable" render="count" />
              </a4j:push>
              <a4j:jsFunction name="forceReloadChat" render="count" />
      		<h:outputText value="#{richBean.count}" id="count" />
      	</h:form>
      </h:body>
      
      Show
      Use a4j:jsFunction and <h:body onload=... /> <h:body onload= "forceReloadChat()" > <h:form id= "myForm" > <a4j:push address= "chat" > <a4j:ajax event= "dataavailable" render= "count" /> </a4j:push> <a4j:jsFunction name= "forceReloadChat" render= "count" /> <h:outputText value= "#{richBean.count}" id= "count" /> </h:form> </h:body>

      Description

      Event can be lost if produced while a response is being prepared. I've attached an example where a push event is used to refresh a count whenever an event is fired. It's using Thread.sleep to simulate an event being produced while the response is being prepared. Note, if the sleep just before returning the count is removed it all works fine, and the page is refreshed every 5 seconds, but with the sleep in place the event is lost.

      View:

      <?xml version="1.0" encoding="utf-8"?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB" lang="en-GB"
      	xmlns:f="http://java.sun.com/jsf/core"
      	xmlns:h="http://java.sun.com/jsf/html"
      	xmlns:a4j="http://richfaces.org/a4j"
      	xmlns:rich="http://richfaces.org/rich">
      <h:head>
      	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      </h:head>
      <h:body>
      	<h:form id="myForm">
      		<a4j:push address="chat">
      			<a4j:ajax event="dataavailable" render="count" />
      		</a4j:push>
      		<h:outputText value="#{myBean.count}" id="count" />
      	</h:form>
      </h:body>
      </html>
      

      Bean:

      
      package com.example;
      
      import java.io.Serializable;
      import java.util.concurrent.atomic.AtomicInteger;
      
      import javax.enterprise.context.SessionScoped;
      import javax.inject.Named;
      
      import org.richfaces.application.push.TopicKey;
      import org.richfaces.application.push.TopicsContext;
      
      @Named
      @SessionScoped
      public class MyBean implements Serializable
      {
          private AtomicInteger count = new AtomicInteger();
      
          public int getCount()
          {
              int c = count.get();
      
              new Thread()
              {
                  public void run()
                  {
                      try
                      {
                          Thread.sleep(5000);
      
                          count.incrementAndGet();
      
                          TopicKey topicKey = new TopicKey("chat");
                          TopicsContext topicsContext = TopicsContext.lookup();
                          topicsContext.publish(topicKey, "");
                      }
                      catch (Exception e)
                      {
                          e.printStackTrace();
                      }
      
                  };
              }.start();
              
              try
              {
                  Thread.sleep(10000);
              }
              catch (InterruptedException e)
              {
              }
      
              return c;
          }
      }
      

        Gliffy Diagrams

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  lfryc Lukáš Fryč
                  Reporter:
                  stianst Stian Thorgersen
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  1 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: