Skip to main content
added 3 characters in body
Source Link
Mohith Shrivastava
  • 91.9k
  • 19
  • 161
  • 214
import { LightningElement, wire, track } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import cometdlwc from "@salesforce/resourceUrl/cometd";
import getSessionId from '@salesforce/apex/SessionUtil.getSessionId';


export default class Cometdlwc extends LightningElement {
 libInitialized = false;
 @track sessionId;
 @track error;

 @wire(getSessionId)
 wiredSessionId({ error, data }) {
  if (data) {
    console.log(data);
    this.sessionId = data;
    this.error = undefined;
    loadScript(this, cometdlwc)
    .then(() => {
        this.initializecometd()
    });
} else if (error) {
    console.log(error);
    this.error = error;
    this.sessionId = undefined;
  }
}

initializecometd() {

  if (this.libInitialized) {
    return;
  }

 this.libInitialized = true;

 //inintializing cometD object/class
 var cometdlib = new window.org.cometd.CometD();
        
//Calling configure method of cometD class, to setup authentication which will be used in handshaking
  cometdlib.configure({
    url: window.location.protocol + '//' + window.location.hostname + '/cometd/47.0/',
    requestHeaders: { Authorization: 'OAuth ' + this.sessionId},
    appendMessageTypeToURL : false,
    logLevel: 'debug'
});

cometdlib.websocketEnabled = false;

cometdlib.handshake(function(status) {
            
    if (status.successful) {
        // Successfully connected to the server.
        // Now it is possible to subscribe or send messages
        console.log('Successfully connected to server');
        cometdcometdlib.subscribe('/event/PlatformEvent__e', function (message) {
                  console.log('subscribed to message!'+ message);
       });
    } else {
        /// Cannot handshake with the server, alert user.
        console.error('Error in handshaking: ' + JSON.stringify(status));
     }
   });
  }
}
import { LightningElement, wire, track } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import cometdlwc from "@salesforce/resourceUrl/cometd";
import getSessionId from '@salesforce/apex/SessionUtil.getSessionId';


export default class Cometdlwc extends LightningElement {
 libInitialized = false;
 @track sessionId;
 @track error;

 @wire(getSessionId)
 wiredSessionId({ error, data }) {
  if (data) {
    console.log(data);
    this.sessionId = data;
    this.error = undefined;
    loadScript(this, cometdlwc)
    .then(() => {
        this.initializecometd()
    });
} else if (error) {
    console.log(error);
    this.error = error;
    this.sessionId = undefined;
  }
}

initializecometd() {

  if (this.libInitialized) {
    return;
  }

 this.libInitialized = true;

 //inintializing cometD object/class
 var cometdlib = new window.org.cometd.CometD();
        
//Calling configure method of cometD class, to setup authentication which will be used in handshaking
  cometdlib.configure({
    url: window.location.protocol + '//' + window.location.hostname + '/cometd/47.0/',
    requestHeaders: { Authorization: 'OAuth ' + this.sessionId},
    appendMessageTypeToURL : false,
    logLevel: 'debug'
});

cometdlib.websocketEnabled = false;

cometdlib.handshake(function(status) {
            
    if (status.successful) {
        // Successfully connected to the server.
        // Now it is possible to subscribe or send messages
        console.log('Successfully connected to server');
        cometd.subscribe('/event/PlatformEvent__e', function (message) {
                  console.log('subscribed to message!'+ message);
       });
    } else {
        /// Cannot handshake with the server, alert user.
        console.error('Error in handshaking: ' + JSON.stringify(status));
     }
   });
  }
}
import { LightningElement, wire, track } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import cometdlwc from "@salesforce/resourceUrl/cometd";
import getSessionId from '@salesforce/apex/SessionUtil.getSessionId';


export default class Cometdlwc extends LightningElement {
 libInitialized = false;
 @track sessionId;
 @track error;

 @wire(getSessionId)
 wiredSessionId({ error, data }) {
  if (data) {
    console.log(data);
    this.sessionId = data;
    this.error = undefined;
    loadScript(this, cometdlwc)
    .then(() => {
        this.initializecometd()
    });
} else if (error) {
    console.log(error);
    this.error = error;
    this.sessionId = undefined;
  }
}

initializecometd() {

  if (this.libInitialized) {
    return;
  }

 this.libInitialized = true;

 //inintializing cometD object/class
 var cometdlib = new window.org.cometd.CometD();
        
//Calling configure method of cometD class, to setup authentication which will be used in handshaking
  cometdlib.configure({
    url: window.location.protocol + '//' + window.location.hostname + '/cometd/47.0/',
    requestHeaders: { Authorization: 'OAuth ' + this.sessionId},
    appendMessageTypeToURL : false,
    logLevel: 'debug'
});

cometdlib.websocketEnabled = false;

cometdlib.handshake(function(status) {
            
    if (status.successful) {
        // Successfully connected to the server.
        // Now it is possible to subscribe or send messages
        console.log('Successfully connected to server');
        cometdlib.subscribe('/event/PlatformEvent__e', function (message) {
                  console.log('subscribed to message!'+ message);
       });
    } else {
        /// Cannot handshake with the server, alert user.
        console.error('Error in handshaking: ' + JSON.stringify(status));
     }
   });
  }
}
added 165 characters in body
Source Link
Mohith Shrivastava
  • 91.9k
  • 19
  • 161
  • 214
import { LightningElement, wire, track } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import cometdlwc from "@salesforce/resourceUrl/cometd";
import getSessionId from '@salesforce/apex/SessionUtil.getSessionId';


export default class Cometdlwc extends LightningElement {
 libInitialized = false;
 @track sessionId;
 @track error;

 @wire(getSessionId)
 wiredSessionId({ error, data }) {
  if (data) {
    console.log(data);
    this.sessionId = data;
    this.error = undefined;
    loadScript(this, cometdlwc)
    .then(() => {
        this.initializecometd()
    });
} else if (error) {
    console.log(error);
    this.error = error;
    this.sessionId = undefined;
  }
}

initializecometd() {

  if (this.libInitialized) {
    return;
  }

 this.libInitialized = true;

 //inintializing cometD object/class
 var cometdlib = new window.org.cometd.CometD();
        
//Calling configure method of cometD class, to setup authentication which will be used in handshaking
  cometdlib.configure({
    url: window.location.protocol + '//' + window.location.hostname + '/cometd/47.0/',
    requestHeaders: { Authorization: 'OAuth ' + this.sessionId},
    appendMessageTypeToURL : false,
    logLevel: 'debug'
});

cometdlib.websocketEnabled = false;

cometdlib.handshake(function(status) {
            
    if (status.successful) {
        // Successfully connected to the server.
        // Now it is possible to subscribe or send messages
        console.log('Successfully connected to server');
        cometd.subscribe('/event/PlatformEvent__e', function (message) {
                  console.log('subscribed to message!'+ message);
       });
    } else {
        /// Cannot handshake with the server, alert user.
        console.error('Error in handshaking: ' + JSON.stringify(status));
     }
   });
  }
}
import { LightningElement, wire, track } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import cometdlwc from "@salesforce/resourceUrl/cometd";
import getSessionId from '@salesforce/apex/SessionUtil.getSessionId';


export default class Cometdlwc extends LightningElement {
 libInitialized = false;
 @track sessionId;
 @track error;

 @wire(getSessionId)
 wiredSessionId({ error, data }) {
  if (data) {
    console.log(data);
    this.sessionId = data;
    this.error = undefined;
    loadScript(this, cometdlwc)
    .then(() => {
        this.initializecometd()
    });
} else if (error) {
    console.log(error);
    this.error = error;
    this.sessionId = undefined;
  }
}

initializecometd() {

  if (this.libInitialized) {
    return;
  }

 this.libInitialized = true;

 //inintializing cometD object/class
 var cometdlib = new window.org.cometd.CometD();
        
//Calling configure method of cometD class, to setup authentication which will be used in handshaking
  cometdlib.configure({
    url: window.location.protocol + '//' + window.location.hostname + '/cometd/47.0/',
    requestHeaders: { Authorization: 'OAuth ' + this.sessionId},
    appendMessageTypeToURL : false,
    logLevel: 'debug'
});

cometdlib.websocketEnabled = false;

cometdlib.handshake(function(status) {
            
    if (status.successful) {
        // Successfully connected to the server.
        // Now it is possible to subscribe or send messages
        console.log('Successfully connected to server');
    } else {
        /// Cannot handshake with the server, alert user.
        console.error('Error in handshaking: ' + JSON.stringify(status));
     }
   });
  }
}
import { LightningElement, wire, track } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import cometdlwc from "@salesforce/resourceUrl/cometd";
import getSessionId from '@salesforce/apex/SessionUtil.getSessionId';


export default class Cometdlwc extends LightningElement {
 libInitialized = false;
 @track sessionId;
 @track error;

 @wire(getSessionId)
 wiredSessionId({ error, data }) {
  if (data) {
    console.log(data);
    this.sessionId = data;
    this.error = undefined;
    loadScript(this, cometdlwc)
    .then(() => {
        this.initializecometd()
    });
} else if (error) {
    console.log(error);
    this.error = error;
    this.sessionId = undefined;
  }
}

initializecometd() {

  if (this.libInitialized) {
    return;
  }

 this.libInitialized = true;

 //inintializing cometD object/class
 var cometdlib = new window.org.cometd.CometD();
        
//Calling configure method of cometD class, to setup authentication which will be used in handshaking
  cometdlib.configure({
    url: window.location.protocol + '//' + window.location.hostname + '/cometd/47.0/',
    requestHeaders: { Authorization: 'OAuth ' + this.sessionId},
    appendMessageTypeToURL : false,
    logLevel: 'debug'
});

cometdlib.websocketEnabled = false;

cometdlib.handshake(function(status) {
            
    if (status.successful) {
        // Successfully connected to the server.
        // Now it is possible to subscribe or send messages
        console.log('Successfully connected to server');
        cometd.subscribe('/event/PlatformEvent__e', function (message) {
                  console.log('subscribed to message!'+ message);
       });
    } else {
        /// Cannot handshake with the server, alert user.
        console.error('Error in handshaking: ' + JSON.stringify(status));
     }
   });
  }
}
added 31 characters in body
Source Link
Mohith Shrivastava
  • 91.9k
  • 19
  • 161
  • 214

I also uploaded the cometd.js file that i have used for testing. You can get it here

Here is the sample code to get started

Here is the sample code to get started

I also uploaded the cometd.js file that i have used for testing. You can get it here

Here is the sample code to get started

added 31 characters in body
Source Link
Mohith Shrivastava
  • 91.9k
  • 19
  • 161
  • 214
Loading
added an update with sample code to address comments
Source Link
Mohith Shrivastava
  • 91.9k
  • 19
  • 161
  • 214
Loading
Source Link
Mohith Shrivastava
  • 91.9k
  • 19
  • 161
  • 214
Loading