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
Todd DeRosa 4Todd DeRosa 4 

Problem using CometD in experience cloud

Hi everyone, I am trying to receive a platform event for an aura component to work in both lightning and experience cloud.  I tried the empapi, but that only works in lightning.  I then tried cometd and got it to work in lightning, but when testing in experience cloud am struggling with the following error on the cometd.configure call:  

Access to XMLHttpRequest at 'https://incredible-cloudy-240211-dev-ed.lightning.force.com/cometd/52.0' from origin 'https://incredible-cloudy-240211-dev-ed.live-preview.salesforce-experience.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have added both urls shown above to the CORS policy page and can't find how to add the access-control-allow-origin header to the request. Here is the code where the error is occuring. I am hard coding the url for now until I can get this to work:

       action.setCallback(this, function(response) {
            var sessionId = response.getReturnValue();
            var cometd = new window.org.cometd.CometD();
            cometd.configure({
                
                url:  'https://incredible-cloudy-240211-dev-ed.lightning.force.com' + '/cometd/52.0',
                    requestHeaders: { Authorization: 'OAuth ' + sessionId},
              
                
                appendMessageTypeToURL : false
            });
            cometd.websocketEnabled = false;
MagulanDuraipandianMagulanDuraipandian
I used LWC.
Sample Code: https://www.infallibletechie.com/2023/10/how-to-listen-to-salesforce-platform-events-using-lightning-web-component-in-experience-cloud-site.html