Angular JS events and the FortiSOAR Web Socket service
Angular JS Events
AngularJS provides the $on, $emit, and $broadcast services for event-based communication between controllers.
$emitdispatches an event name upwards through the scope hierarchy and notifies the registered$rootScope.Scopelisteners:$scope.$emit('eventName', { message: msg });
For example to trigger thefieldChangeevent when there is a change in a field use:$scope.$emit('fieldChange', $scope.field);$broadcastdispatches an event name downward to all the child scopes in the hierarchy (child scopes and their children) and notifies the registered$rootScope.Scopelisteners:$scope.$broadcast('eventName', { message: msg });
For example to broadcast theupdateConfigurationFieldsevent when there is an update in a configuration field use:$scope.$broadcast('updateConfigurationFields', $scope.field);$onlistens to events of a given type. It catches events dispatched by$broadcastand$emit.
For example to activate a listener for thefieldChangeevent to listen to an emitted event and perform necessary actions, use:scope.$on('fieldChange', function(field) {});
For example to activate a listener for theupdateConfigurationFieldsevent to listen to a broadcasted event and perform necessary actions, use:self.$scope.$on('updateConfigurationFields', self.saveFields);
FortiSOAR Web Socket Service
The FortiSOAR Web Socket Service allows users to create a channel between the UI and the database to fetch and update real-time data.
The following function is commonly used to listen to the websocket:reconnect event that is broadcasted when a socket is open for connection:
scope.$on('websocket:reconnect', function () {
initWebsocket();
}
);
Once the socket is open, users can subscribe to the socket in the widget using the websocketService.subscribe() function.
The following sample initWebsocket() method gets called when a widget is initialized and to subscribe to the websocket:reconnect:
var subscription;
function initWebsocket(){
websocketService.subscribe('runningworkflow',function(data){
}).then(function(data){
subscription = data;
});
}
Once the widget lifecycle is closed, users must unsubscribe from the web socket using the websocketService.unsubscribe() function:
$scope.$on('$destroy', function() {
if(subscription){
// Unsubscribe
websocketService.unsubscribe(subscription);
}
});