WARNING: this component is partially deprectated.
It is replaced by the official built-in lightning:empApi component in Summer '18.
However, the service is still usefull for specific use cases in which lightning:empApi
is not supported. These include Communities or Visualforce support.
This is a generic and reusable Lightning component that allows to publish and subscribe to platform events. This is a service component so it has no user interface of it's own: it is meant to be called by other components.
Features
The component provides the following features:
- subscribe and publish any platform event
- two deployment modes: as a standalone Lightning component in the App Builder or integrated in markup
Requirements
This component requires that you first install the Server Action Service.
The component is documented using Aura documentation.
You can access it from this URL (replace the domain):
https://<YOUR_DOMAIN>.lightning.force.com/auradocs/reference.app#reference?descriptor=c:PicklistService&defType=component
WARNING: there must always be only one instance of this component on a given Lightning page. If there is more than one, there will be errors and inconsistent behavior due to the behavior. This constraint is imposed by the use of the CometD library.
The component can be used in two ways: in a parent component's markup or a standalone Lightning component in the Lightning App Builder.
The 'markup' approach has the advantage of providing callback functions that allow to capture errors with custom handlers.
The 'App Builder' approach has the advantage of enforcing the use of a single shared service instance per Lightning page.
Start by adding the service in the markup:
<c:PlatformEventService aura:id="peService"/>
Then, use the component's publish
and subscribe
Aura methods to publish and subscribe to platform events:
const pes = component.find('peService');
pes.publish(eventName, eventData, errors => {
if (errors) {
// Optional error handler here (already covered by call to server)
} else {
// Notify success
}
});
Drag and drop the platform event service component in a Lightning page using the Lightning App Builder. Notice that the component is 'invisible' (there is a blank space where the component should be), that is normal.
You then have two complementary ways of interacting with the platform event service:
- Static mode: select the component in the App Builder and enter a list of hard-coded platform event names seperated by commas. These will be susbcribed to when the page loads.
- Dynamic mode: fire or handle the following Application events:
Event name Fired by Handled by Description PlatformEventSubscription You Platform Event Service Event sent to the Platform Event Service in order to subscribe to a platform event IncomingPlatformEvent Platform Event Service You Event sent by the Platform Event Service when receiving a platform event OutgoingPlatformEvent You Platform Event Service Event sent to the Platform Event Service in order to publish a platform event
The default installation installs the component and a sample application available under this URL (replace the domain):
https://<YOUR_DOMAIN>.lightning.force.com/c/SamplePlatformEventApp.app
If you wish to install the project without the sample application, edit sfdx-project.json
and remove the src-sample
path.