lightning:empApi
Let's start off by creating a new Platform Event called "Demo_EmpApi_Event__e"...

...and add a new custom field called "Message__c".
Next, we need to add the reference to Emp API to our lightning component:
<lightning:empapi aura:id="empApi"></lightning:empapi>The bulk of the logic is handled by the init method in the controller class. The first thing that we need to do is to get a reference the Emp API component:
var empApi = cmp.find("empApi");
Once we have a reference to the Emp API component we can subscribe to the event:
var replayId=-1; //use -1 for new events
empApi.subscribe(channel, replayId, callback).then(function(value) { console.log("Subscribed to channel " + channel); sub = value; cmp.set("v.sub", sub); });
When we receive a message we need to handle is somehow. In this case I've created a callback function to popup an alert.
That's pretty much it! Now when we publish an event we get an alert.
var callback = function (message) { alert('Event Received!'); }.bind(this);The final piece of this is to publish the event. In order to do so, I've created an Apex class that instantiates the Demo_EmpApi_Event__e and then uses the EventBus Class to publish it.
Demo_EmpApi_Event__e event = new Demo_EmpApi_Event__e(Message__c=message); Database.SaveResult result = EventBus.publish(event);
I've posted the complete code examples below. Happy coding!
EmpApi_example.cmp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component controller="EmpApi_exampleController" implements="force:appHostable"> | |
<lightning:empApi aura:id="empApi" /> | |
<aura:handler name="init" value="{!this}" action="{!c.doInit}" /> | |
<lightning:card title="Emp API Example"> | |
<lightning:button variant="brand" label="Publish Event" onclick="{!c.doPublishEvent}" /> | |
</lightning:card> | |
</aura:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
doInit : function(cmp, event, helper, isinit) { | |
var empApi = cmp.find("empApi"); | |
// Handle any errors and print them to the console. | |
var errorHandler = function (message) { | |
console.log("Received error ", message); | |
}.bind(this); | |
// Register the error listener | |
empApi.onError(errorHandler); | |
var channel='/event/Demo_EmpApi_Event__e'; | |
var sub; | |
// use -1 for new events | |
var replayId=-1; | |
var callback = function (message) { | |
alert(message); | |
}.bind(this); | |
empApi.subscribe(channel, replayId, callback).then(function(value) { | |
console.log("Subscribed to channel " + channel); | |
sub = value; | |
cmp.set("v.sub", sub); | |
}); | |
}, | |
doPublishEvent : function(cmp, event){ | |
var action = cmp.get("c.publishEvent"); | |
action.setCallback(this, function(response) { | |
var state = response.getState(); | |
if (cmp.isValid() && state === "SUCCESS") { | |
console.log("Event published successfully!"); | |
} | |
else { | |
console.log("Failed with state: " + state); | |
} | |
}); | |
$A.enqueueAction(action); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmpApi_exampleController { | |
@AuraEnabled | |
public static void publishEvent() | |
{ | |
System.debug('Published Event!'); | |
Demo_EmpApi_Event__e event = new Demo_EmpApi_Event__e(Message__c=message); | |
Database.SaveResult result = EventBus.publish(event); | |
if (!result.isSuccess()) { | |
for (Database.Error error : result.getErrors()) { | |
System.debug('Error returned: ' + error.getStatusCode() +' - '+ error.getMessage()); | |
} | |
} | |
} | |
} |
No comments:
Post a Comment