Fortinet white logo
Fortinet white logo

Angular JS events and the FortiSOAR Web Socket service

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.

  • $emit dispatches an event name upwards through the scope hierarchy and notifies the registered $rootScope.Scope listeners:
    $scope.$emit('eventName', { message: msg });
    For example to trigger the fieldChange event when there is a change in a field use:
    $scope.$emit('fieldChange', $scope.field);
  • $broadcast dispatches an event name downward to all the child scopes in the hierarchy (child scopes and their children) and notifies the registered $rootScope.Scope listeners:
    $scope.$broadcast('eventName', { message: msg });
    For example to broadcast the updateConfigurationFields event when there is an update in a configuration field use:
    $scope.$broadcast('updateConfigurationFields', $scope.field);
  • $on listens to events of a given type. It catches events dispatched by $broadcast and $emit.
    For example to activate a listener for the fieldChange event to listen to an emitted event and perform necessary actions, use:
    scope.$on('fieldChange', function(field) {});
    For example to activate a listener for the updateConfigurationFields event 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);
	}
});

Angular JS events and the FortiSOAR Web Socket service

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.

  • $emit dispatches an event name upwards through the scope hierarchy and notifies the registered $rootScope.Scope listeners:
    $scope.$emit('eventName', { message: msg });
    For example to trigger the fieldChange event when there is a change in a field use:
    $scope.$emit('fieldChange', $scope.field);
  • $broadcast dispatches an event name downward to all the child scopes in the hierarchy (child scopes and their children) and notifies the registered $rootScope.Scope listeners:
    $scope.$broadcast('eventName', { message: msg });
    For example to broadcast the updateConfigurationFields event when there is an update in a configuration field use:
    $scope.$broadcast('updateConfigurationFields', $scope.field);
  • $on listens to events of a given type. It catches events dispatched by $broadcast and $emit.
    For example to activate a listener for the fieldChange event to listen to an emitted event and perform necessary actions, use:
    scope.$on('fieldChange', function(field) {});
    For example to activate a listener for the updateConfigurationFields event 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);
	}
});