Created
October 11, 2018 20:59
-
-
Save choudharymanish8585/b5345a552e31b98ba4b357ddbc2cda52 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
({ | |
/** | |
* This function calls subscribe method of empApi component | |
* to receive events | |
* @author Manish Choudhari | |
* @version 1.0.0 | |
* */ | |
subscribe: function(component, event, helper) { | |
// Get the empApi component. | |
var empApi = component.find("empApi"); | |
// Get the channel name from attribute | |
var channel = component.get("v.channelName"); | |
//fetch latest events | |
var replayId = -1; | |
// Callback function to be passed in the subscribe call. | |
// After an event is received, this callback fire a custom | |
// event to notify parent component and pass payload object | |
var subscribeCallback = function (message) { | |
//Fire the component event to notify parent component | |
var messageEvent = component.getEvent("onRecordChange"); | |
if(messageEvent!=null) { | |
messageEvent.setParam("recordData", message.data); | |
messageEvent.fire(); | |
} | |
//Display event data in browser console | |
console.log("Received [" + message.channel + | |
" : " + message.data.event.replayId + "] payload=" + | |
JSON.stringify(message.data.payload)); | |
}.bind(this); | |
// Register error listener and pass in the error handler function. | |
empApi.onError(function(error){ | |
console.log("Received error ", error); | |
}.bind(this)); | |
// Subscribe to the channel and save the returned subscription object. | |
empApi.subscribe(channel, replayId, subscribeCallback).then(function(value) { | |
console.log("Subscribed to channel " + channel); | |
component.set("v.subscription", value); | |
}); | |
}, | |
/** | |
* This function will be fired once the component is destroyed | |
* It will call unsubscribe method of empApi component | |
* @author Manish Choudhari | |
* @version 1.0.0 | |
* */ | |
unsubscribe : function (component, event, helper) { | |
try{ | |
// Get the empApi component. | |
var empApi = component.find("empApi"); | |
// Get the channel name from attribute | |
var channel = component.get("v.channelName"); | |
// Callback function to be passed in the unsubscribe call. | |
var unsubscribeCallback = function (message) { | |
console.log("Unsubscribed from channel " + channel); | |
}.bind(this); | |
// Error handler function that prints the error to the console. | |
var errorHandler = function (message) { | |
console.log("Received error ", message); | |
}.bind(this); | |
// Object that contains subscription attributes used to | |
// unsubscribe. | |
var subscription = {"id": component.get("v.subscription")["id"], | |
"channel": component.get("v.subscription")["channel"]}; | |
// Register error listener and pass in the error handler function. | |
empApi.onError(function (error) { | |
console.log("Received error ", error); | |
}.bind(this)); | |
// Unsubscribe from the channel using the sub object. | |
empApi.unsubscribe(subscription, unsubscribeCallback); | |
}catch(e){} | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment