1

I'm trying to update a datatable after a row is added using primepush. Meaning, I want user1 to update the datatable and user2 should see the added row without refreshing the page.

My Primefaces version is 5.2 and Atmosphere runtime version is 2.3.3

Here's my relevant code which I tried to implement based on Primefaces Showcase and the User Guide:

web.xml:

<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">

    <display-name>PrimeFaces Web Application</display-name>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>        

    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>AccessFilter</filter-name>
        <filter-class>view.AccessFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>AccessFilter</filter-name>
        <url-pattern>/secured/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Push Servlet</servlet-name>
        <servlet-class>org.primefaces.push.PushServlet</servlet-class>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Push Servlet</servlet-name>
        <url-pattern>/primepush/*</url-pattern>
    </servlet-mapping>
</web-app>

Backing Bean:

@ManagedBean
@ViewScoped
@PushEndpoint("/main")
public class MainBean implements Serializable{

...

    public void addContact(ActionEvent event){

        ... // database operations

        Contact con = new Contact(); 

        contacts.add(con); // this is an ArrayList

        EventBus eventBus = EventBusFactory.getDefault().eventBus();
        eventBus.publish("/main", contacts);
    }

    @OnMessage(encoders = {JSONEncoder.class})
    public ArrayList<Contact> getContacts(){
        return contacts;
    }
}    

main.xhtml:

...

<p:commandButton value="Add Contact" update=":form:datatable" 
    actionListener="#{mainBean.addContact}" ajax="true"/>

<h:form id="form">
    <p:dataTable id="datatable" var="contact" value="#{mainBean.contacts}" 
     selectionMode="single" rowKey="#{contact.firstName}" 
     selection="#{mainBean.selectedContact}">

        <p:column headerText="First Name">
            <h:outputText value="#{contact.firstName}"/>
        </p:column>
        <p:column headerText="Last Name">
            <h:outputText value="#{contact.lastName}"/>
        </p:column>
    </p:dataTable>
</h:form>
<p:socket channel="/main">
    <p:ajax event="message" update=":form:datatable"/>
</p:socket>

When I try to add a row to the table, no errors occur but some rows just get lost. When I refresh, the table reloads as the way it should. The added row is also not passed between different users. User2 still needs to refresh to see the changes after user1 adds a value to the table.

I've also tried the suggestions in this link with no success. I'm pretty new to primepush and the examples I found online (other than the User Guide) are for really old versions of Primefaces and push feature seems to changed a lot after Primefaces 5. Any help is appreciated.

Edit: I've also tried these links: link, link, link.

Edit 2: I've logged if the push is working and it seems that it is indeed. When I navigate to the page which has push enabled, I can observe on the console that it gets activated. However, when I try to update the table, the get method of the list which is filling the table is called three times consecutively after pushing...

Community
  • 1
  • 1
tankucukoglu
  • 455
  • 6
  • 20
  • Can you try adding an actionListener in the ajax tag that calls some dummy method on the server? Afaik there is no round-trip when there is nothing to call. – Kukeltje Jul 09 '15 at 15:26
  • @Kukeltje `` doesn't have actionListener attribute? – tankucukoglu Jul 09 '15 at 15:31
  • Sorry, listener... It's the idea that counts. Reason is the same – Kukeltje Jul 09 '15 at 15:32
  • @Kukeltje Tried it, no chance. But it seems no rows were lost during add that time. Other user still needs to refresh to see changes though. – tankucukoglu Jul 09 '15 at 15:42
  • No chance should most likely read no change? And what do you mean by _"But it seems no rows were lost during add that time."_? – Kukeltje Jul 09 '15 at 15:43
  • And the second answer in http://stackoverflow.com/questions/19159598/update-primeface-datatable-from-backing-bean (the one about remote command) should certainly work!!! I use that in two places (only remembered when I saw it in that post) – Kukeltje Jul 09 '15 at 15:47
  • @Kukeltje I said that in my question when I add rows with push enabled some of the already existing rows were disappearing but with the last thing we tried seems to have fixed that but pushing still doesn't work. I also tried that remote command solution but it didn't work. Maybe it will work with the dummy method you suggested. Let me try it and get back to you. – tankucukoglu Jul 09 '15 at 15:58
  • @tk12 does the contact arraylist have a setter method in the managed bean? if no, could the try creating a setter for same and test if push is working? – mehere Jul 09 '15 at 16:04
  • @Kukeltje I used `` after the datatable tag and used ` ` as the socket but it didn't work. – tankucukoglu Jul 09 '15 at 16:17
  • in the remote command, your 'update' is wrong, should be ':form:datatable'. If you use the remote command, the ajax tag is not needed. – Kukeltje Jul 09 '15 at 16:19
  • @EvaMariam tried with a set method as you suggested. Push does not seem to work. The error is possibly in my xhtml. – tankucukoglu Jul 09 '15 at 16:22
  • @Kukeltje `` and `` didn't work as well. Maybe I configured something incorrectly after all? – tankucukoglu Jul 09 '15 at 16:27
  • Is push not even working? Did I read that correctly? Then do more debugging in the developer console. See what does go on – Kukeltje Jul 09 '15 at 17:00
  • @Kukeltje I did everything textbook and my logs doesn't show anything out of ordinary. That's why I asked here. – tankucukoglu Jul 09 '15 at 17:05
  • I'm not talking about errors, debugging is more.Does your developer tool network tab show the socket works? Is an ajax call fired? Is the remote command fired? What are the responses etc – Kukeltje Jul 09 '15 at 17:07
  • @Kukeltje I've edited the post to provide additional information. – tankucukoglu Jul 10 '15 at 08:26
  • so it does work now but a certain getter is called 3 times? – Kukeltje Jul 10 '15 at 10:14
  • @Kukeltje I can push messages between users but datatable still isn't getting pushed. Apparently, my datatable update logic is flawed. – tankucukoglu Jul 10 '15 at 11:16
  • 1
    The datatable is not ever 'pushed'... it is updated in an ajax response, either via the message event or the one of the remoteCommand. That is why I asked to do debugging of the responses. If the datatable is in there, the technical update works. If your model is stale, you would not see any differences. That is why you should always, always, always, look at things from a developer perspective, not an end user perspective... – Kukeltje Jul 10 '15 at 11:25
  • @Kukeltje thank you for your extended attention. Seems I have to dig deeper to find the issue. I'll get back with a more concrete improvement, hopefully the solution. – tankucukoglu Jul 10 '15 at 11:59

0 Answers0