function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
asish1989asish1989 

Pushtopic is not working, the new value is not refreshing

Hi All, 
My requirement is show a popup message on the opportunity page(Standard page is overidden with custom vf page using apex detail) when opportunity is closed. Say two persone is wokring on same opportunity then one person closd that opportunity from his side, so other person should see the message that opportunity is being closed without having to refrsh the page.

For this requirement i thought of implmenting Striming API, so I have craeted a pushtopic and craeted to dummy vf page just to check whether the stage chnage is reflected or not, unfortunatley it is not refreshing. Could you please help me to solve my issue.

Here is pushtopic i created.
PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'InvoiceStatementUpdates';
pushTopic.Query = 'SELECT Id,Name,StageName FROM Opportunity';
pushTopic.ApiVersion = 39.0;
pushTopic.NotifyForOperationUpdate = true;
pushTopic.NotifyForOperationDelete = true;
pushTopic.NotifyForFields = 'Referenced';
pushTopic.NotifyForOperations = 'Update';
insert pushTopic;

Here is vf page
<apex:page id="PG" controller="StreamingAPIController">
<apex:form id="FRM">

    <apex:includeScript value="{!$Resource.cometd}"/>
    <apex:includeScript value="{!$Resource.jquery_StrimingApi}"/>
    <apex:includeScript value="{!$Resource.json2}"/>
    <apex:includeScript value="{!$Resource.jquery_cometd}"/>

    <apex:actionFunction name="GetRefreshedOpportunities" reRender="PB,PBT"/>

    <script type="text/javascript">
        (function($)
        {
            $(document).ready(function() {
                
                // Connect to the CometD endpoint
                $.cometd.init({
                    url: window.location.protocol+'//'+window.location.hostname+'/cometd/36.0/',
                    requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
                });
                
                // Subscribe to a topic. JSON-encoded update will be returned in the callback
                // In this example we are using this only to track the generated event
                $.cometd.subscribe('/topic/InvoiceStatementUpdates', function(message)
                {
                    //You can use message as it will return you many attributes
                    //I am just using to track that event is generated
                    GetRefreshedOpportunities();
                });

            });
        })(jQuery)
    </script>

    <apex:pageBlock id="PB">
        <apex:variable var="count" value="{!0}" />
        <apex:pageBlockTable id="PBT" value="{!getRefreshedOpportunity}" var="opp">
            
            <apex:column headerValue="S.No.">
                <apex:variable var="count" value="{!count+1}" />
                {!count}
            </apex:column>
            <apex:column value="{!opp.id}" headerValue="id"/>
            <apex:column value="{!opp.Name}" headerValue="Name"/>
            <apex:column value="{!opp.StageName }" headerValue="StageName "/>
            
        </apex:pageBlockTable>
    </apex:pageBlock>

</apex:form>
</apex:page>

here is controller 
public class StreamingAPIController
{
    //Everytime page is reRendered it will get refreshed values of Opportunity 
    public List<Opportunity> getRefreshedOpportunity
    {
        get
        {
            return [select Id, Name,StageName from Opportunity  LIMIT 10 ] ;
        }
        set;
    }
    
    public StreamingAPIController()
    {
    }
}
But in workbench i can see the notification and stage name, I am confused why it is not reflectying in Vf pages. 
Message received from: /topic/InvoiceStatementUpdates
{
  "channel": "/topic/InvoiceStatementUpdates", 
  "clientId": "1ix3i3kz92l8b8z13m10pafuf5tz", 
  "data": {
    "event": {
      "type": "updated", 
      "createdDate": "2017-01-26T04:28:36.000+0000"
    }, 
    "sobject": {
      "StageName": "Closed Lost", 
      "Id": "006G000000P3j6CIAR", 
      "Name": "MVC Componentes Plasticos Ltda.-LTD&CFR Domestic-Sep-15-13"
    }
  }
}

Please help me 
Javier CGJavier CG
I have the same problem.
I have created a pushtopic and have checked that it works fine as for in workbench I receive the notifications when a record/field affected by the query on the pushtopic is changed.
But my VF page doesn´t update when a change has been made. If I refresh the page manually, changes are shown, but it is not automatically refreshed when a change is made.

PushTopic:
PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'PushTopic_Opportunity';
pushTopic.Query = 'SELECT Id,Name, LastModifiedDate, LastModifiedById FROM Opportunity';
pushTopic.ApiVersion = 47.0;
pushTopic.NotifyForFields ='All';
pushTopic.NotifyForOperationCreate = true;
pushTopic.NotifyForOperationUpdate = true;
pushTopic.NotifyForOperationUndelete = true;
pushTopic.NotifyForOperationDelete = true;
insert pushTopic;

Controller:
public class StreamingAPIController
{        
    public StreamingAPIController()
    {
    }
    
    public List<Opportunity> getlistOpp()
    {
        return [SELECT Id,Name, LastModifiedDate, LastModifiedById FROM Opportunity];
    }

    public PageReference refresh()
    {
        getlistOpp();
        return null;
    }
}

VF Page:
<apex:page controller="StreamingAPIController">
    <apex:includeScript value="{!$Resource.cometd}"/>
    <apex:includeScript value="{!$Resource.jquery}"/>
    <apex:includeScript value="{!$Resource.json2}"/>
    <apex:includeScript value="{!$Resource.jquery_cometd}"/>
    <script type="text/javascript">
    (function($){
        $(document).ready(function() {
            $.cometd.init({
               url: window.location.protocol+'//'+window.location.hostname+'/cometd/24.0/',
               requestHeaders: { Authorization: 'OAuth {!$Api.Session_ID}'}
           });

           $.cometd.subscribe('/topic/PushTopic_Opportunity', function(message) {
               getlistOpp();
            });
        });
   })(jQuery)
   function disconnect() {
       $.cometd.disconnect();
   }
   window.onbeforeunload = disconnect;
   </script>
   <apex:form >
   <apex:actionFunction name="getlistOpp" reRender="PB,PBT"/>
   
<apex:pageBlock id="PB">
   
    <apex:pageBlockTable id="PBT" value="{!listVhOP}" var="Opp">
   
        <apex:column value="{!Opp.Name}"/>
   
        <apex:column value="{!Opp.LastModifiedDate}"/>
   
        <apex:column value="{!Opp.LastModifiedById}"/>
   
    </apex:pageBlockTable>
   
</apex:pageBlock>
   
</apex:form>
</apex:page>

Someone can help me?