Resources.DocsTutEventInterface History

Hide minor edits - Show changes to markup

April 24, 2013, at 06:49 PM by 213.233.101.41 -
Changed lines 1-227 from:

Resources -> Documentation -> Tutorials -> Event Interface

This page has been visited 447 times. (:toc-float Table of Content:)


This tutorial is for OpenSIPS trunk/devel version

Event Interface

The OpenSIPS Event Interface provides an easy way to notify external applications whenever certain events occur inside OpenSIPS. Basically it provides two interfaces: one for the OpenSIPS internal modules and core, that is used to advertise and raise events, and an interface for the external applications, allowing them to register for specific events, and notifying them when those events are triggered by the modules, core, or by the OpenSIPS script.


Overview

OpenSIPS can notify an external application using different transport protocols:

  • event_datagram - sends Datagrams over UDP or UNIX sockets
  • event_rabbitmq - sends an AMQP message to a RabbitMQ server
  • event_xmlrpc - issues a RPC command on a RPC server
  • event_route - runs an OpenSIPS event_route

In order to receive an event notification, an external application has to subscribe for a specific event to the Event interface. This can be done either using an MI command, or can be done directly from the OpenSIPS script. The subscription command has to specify the event that the external application wants to register to, and a socket where the notification has to be sent. The format of the socket is different, depending on the transport module used (you can find more information about this in the module's documentation page).

When an event is triggered, the Event Interface will send to each application that is subscribed for that event a notification, using the desired transport type. The event details are packed differently for each transport module (consult the transport module's documentation page for more details).


Event Subscription

As mentioned above, an external application can subscribe for an event either using MI, or directly from OpenSIPS script. In the next sections we will describe the use case of each of them.

MI Subscription

An external application should subscribe for an event using MI commands when the subscription nature is very dynamic: it subscribes for a short period, receives notifications, then decides to unsubscribe and so on. The MI function used to subscribe for an event is event_subscribe.

For example, to subscribe for the E_PIKE_BLOCKED event for only 1200 seconds, an external application that listens on localhost, UDP port 8888, has to issue the following MI command:

   $ opensipsctl fifo event_subscribe E_PIKE_BLOCKED udp:127.0.0.1:8888 1200

The first parameter is the name of the event (see here), the second parameter is the listening socket name (the format depends on the transport module used) and the last parameter is the expiration period expressed in seconds. Note that if the last parameter is missing, a default subscription period of 3600s (1h) is assumed.

In order to unsubscribe for an event, the external application has to use the same event and socket used at subscription time, but specify an expiration time of 0. The following command unsubscribes the previously registered application.

   $ opensipsctl fifo event_subscribe E_PIKE_BLOCKED udp:127.0.0.1:8888 0

Script Subscription

The Event Interface allows the script writer to subscribe for an event directly from the OpenSIPS configuration script. This is usually used when a subscription is permanent, that never expires. For example a server that is continuously listening from events from OpenSIPS and process them accordingly. The command used to subscribe for an event from script is subscribe_event.

Usually these kind of subscriptions are permanent, therefore they should be registered at when OpenSIPS starts. Therefore this should be done in startup_route, but this is not a requirement, as the function can be used in any route.

Here is an example of subscription for the E_PIKE_BLOCKED event at OpenSIPS startup, without any expire time, from the OpenSIPS configuration script:

   startup_route() {
      subscribe_event("E_PIKE_BLOCKED", "udp:127.0.0.1:8888");
   }

If the script writer wants the subscription to expire after 1200 second, the third parameter of the subscribe_event function can be specified:

   timer_route[event_subscribe,3600] {
      subscribe_event("E_PIKE_BLOCKED", "udp:127.0.0.1:8888", 1200);
   }

Subscribers

The subscribers_list MI function, exported by the OpenSIPS Core, can be used to list all the external applications subscribed. It also allows you to filter the subscribers based on the event they are registered to. For more information consult the functions documentation page.


Events

There are two types of events: static/predefined events, exported by OpenSIPS code and modules, and documented by each module, and dynamic events that can be raised by the script writer directly from the script. A few examples of predefined events:

  • E_PIKE_BLOCKED - raised when pike module decides an IP should be blocked.
  • E_RTPPROXY_STATUS - raised when a RTPProxy connects or disconnects.
  • E_DISPATCHER_STATUS - raised by the dispatcher module when a destination address becomes active/inactive.
  • E_CORE_THRESHOLD - raised when using debugging bottleneck detection and the limit is exceeded.
  • E_CORE_PKG_THRESHOLD - raised when the private memory usage exceeds a certain limit.
  • E_CORE_SHM_THRESHOLD - raised when the shared memory usage exceeds a certain limit.

Events Name

Although there is not a strict events naming imposed by the Event Interface, it is a good practice for an event's name to follow the format E_MODULE_NAME, where MODULE is the name of the module that exports the event, CORE if the event is exported by OpenSIPS core, and SCRIPT if it is a dynamic event raised from the script, and NAME is the name of the event.

Raising an Event

In order to raise an event from the script, the raise_event function has to be used. The function receives one, two or three parameters, depending on the script writer needs. If the event has no parameters, then only the first parameter is needed - the event's name.

If the script writer wants to attach some extra information to the event, then the second and optionally the third parameter has to be used. The extra information has to be stored in an AVP as values, for example:

    $avp(values) = "value1";
    $avp(values) = "value2";
    ...

If the parameters also have some names, then a new AVP have to store them. Note that the number of values in the first AVP has to be the same as in the one for the attributes:

    $avp(attributes) = "param1";
    $avp(values) = "value1";
    $avp(attributes) = "param2";
    $avp(values) = "value2";
    ...

In order to raise the event from the script, the following code has to be used:

   raise_event("E_SCRIPT_EVENT");     # raises an event without any parameters

   raise_event("E_SCRIPT_EVENT", $avp(values));     # raises an event that has only values parameters

   raise_event("E_SCRIPT_EVENT", $avp(attributes), $avp(values));     # raises an event with multiple attribute-value pairs

Events list

The events_list function can be used to list all the available events.

Examples

A practical use case of dynamic events is raising an event from the script when the TCP load is greater than 80%:

   route[TCP_LOAD_CHECK] {

      if ($stat(tcp-load) > 80) {
         # raise the event, specifying the limit and the real load
         $avp(attrs) = "limit";
         $avp(vals) = 80;
         $avp(attrs) = "load";
         $avp(vals) = $stat(tcp-load);

         raise_event("E_SCRIPT_TCP_LOAD", $avp(attrs), $avp(vals));
      }
   }

Dynamic events can also be used for accounting purposes. For example, you could use three types of dynamic events to notify an external application regarding a call status.

   # this route initializes the accounting variables
   route[ACC_VARS] {
      $avp(attrs) = NULL;
      $avp(vals) = NULL;
      $avp(attrs) = "from";
      $avp(vals) = $fU;
      if (has_totag()) {
         $avp(attrs) = "to";
         $avp(vals) = $rU;
      }
      ...
      $avp(attrs) = "callid";
      $avp(vals) = $ci;
      # populate attrs and vals with whatever extra info you need
   }

   route {
      ...
      if (!has_totag() && is_method("INVITE"))) {
         route(ACC_VARS);
         raise_event("E_SCRIPT_CALL_START", $avp(attrs), $avp(vals));
         t_on_reply("acc");
      } else if is_method("CANCEL|BYE") {
         route(ACC_VARS);
         raise_event("E_SCRIPT_CALL_STOP", $avp(attrs), $avp(vals));
      }
      ...
   }

   onreply_route[acc] {
      ...
      if (t_check_status("200")) {
          route(ACC_VARS);
          raise_event("E_SCRIPT_CALL_ESTABLISHED", $avp(attrs), $avp(vals));
      }
      ...
   }

Transport Modules

There are several transport modules that can be used to notify an external application. At the time this tutorial was written, the following transport methods were supported:

Datagram Messages

External applications can be notified when OpenSIPS internal events are triggered using either UDP or UNIX datagrams. This allows programmers to write a simple UDP server that receives the notification and process it in the preferred programming language.

In order to support this type of communication, the event_datagram module has to be loaded. Consult the module's documentation page for more information about subscription socket and encapsulation format.

RabbitMQ Messages

OpenSIPS can also communicate with a RabbitMQ server using the AMQP (Advanced Message Queuing Protocol) messages. This type of communication leverages all the features the queuing server has.

The event_rabbitmq module provides this functionality. Note that in this case, the "external application" is the RabbitMQ server, so you do not have the possibility to subscribe for events from it. Therefore the programmer should either use a different application to subscribe for the event, or subscribes for it directly from the OpenSIPS script (as discussed here). For more information please visit the module's documentation page.

XMLRPC Commands

OpenSIPS can also execute a remote XMLRPC command when an event is triggered. The module that handles the communication between the Event Interface and the XMLRPC server is event_xmlrpc.

Routes

A special implementation of a transport module is the event_route module, which is used to handle events directly from the OpenSIPS script. This allows the script writer to modify OpenSIPS internal variables when an event is triggered, by writing a special route, called event_route. The name of the route specifies the event that is handled by that route.

In order to retrieve the parameters of the event, the fetch_event_params function has to be used. It receives a list of parameters names, and the pseudo-variables that they have to be assigned to. For more information consult the event_route documentation page.

For example in order to specify a route that handles the E_PIKE_BLOCKED event, and prints the blocked IP, the OpenSIPS script has to contain the following code:

   event_route[E_PIKE_BLOCKED] {
      fetch_event_params("ip=$avp(pike-ip)");
      xlog("IP $avp(pike-ip) has been blocked\n");
   }

Note that specifying the event_route will automatically subscribe the route for the event. Therefore there is no need for explicit subscribing (from either external application or script). The subscription never expires, unless otherwise specified using the subscribe_event MI command.

to:

(:redirect Documentation.Tutorials-EventInterface quiet=1 :)

January 22, 2013, at 01:25 PM by razvancrainea -
Changed lines 7-8 from:

This tutorial is for OpenSIPS 1.9.x version

to:

This tutorial is for OpenSIPS trunk/devel version

Changed lines 129-130 from:

Example

to:

Examples

Added lines 147-189:

Dynamic events can also be used for accounting purposes. For example, you could use three types of dynamic events to notify an external application regarding a call status.

   # this route initializes the accounting variables
   route[ACC_VARS] {
      $avp(attrs) = NULL;
      $avp(vals) = NULL;
      $avp(attrs) = "from";
      $avp(vals) = $fU;
      if (has_totag()) {
         $avp(attrs) = "to";
         $avp(vals) = $rU;
      }
      ...
      $avp(attrs) = "callid";
      $avp(vals) = $ci;
      # populate attrs and vals with whatever extra info you need
   }

   route {
      ...
      if (!has_totag() && is_method("INVITE"))) {
         route(ACC_VARS);
         raise_event("E_SCRIPT_CALL_START", $avp(attrs), $avp(vals));
         t_on_reply("acc");
      } else if is_method("CANCEL|BYE") {
         route(ACC_VARS);
         raise_event("E_SCRIPT_CALL_STOP", $avp(attrs), $avp(vals));
      }
      ...
   }

   onreply_route[acc] {
      ...
      if (t_check_status("200")) {
          route(ACC_VARS);
          raise_event("E_SCRIPT_CALL_ESTABLISHED", $avp(attrs), $avp(vals));
      }
      ...
   }
January 22, 2013, at 12:19 PM by razvancrainea -
Changed lines 18-22 from:
  • event_datagram - sends Datagrams over UDP or UNIX sockets
  • event_rabbitmq - sends an AMQP message to a RabbitMQ server
  • event_xmlrpc - issues a RPC command on a RPC server
  • event_route - runs an OpenSIPS event_route
to:
  • event_datagram - sends Datagrams over UDP or UNIX sockets
  • event_rabbitmq - sends an AMQP message to a RabbitMQ server
  • event_xmlrpc - issues a RPC command on a RPC server
  • event_route - runs an OpenSIPS event_route
Changed lines 35-36 from:

An external application should subscribe for an event using MI commands when the subscription nature is very dynamic: it subscribes for a short period, receives notifications, then decides to unsubscribe and so on. The MI function used to subscribe for an event is event_subscribe.

to:

An external application should subscribe for an event using MI commands when the subscription nature is very dynamic: it subscribes for a short period, receives notifications, then decides to unsubscribe and so on. The MI function used to subscribe for an event is event_subscribe.

Changed lines 43-44 from:

The first parameter is the name of the event (see here), the second parameter is the listening socket name (the format depends on the transport module used) and the last parameter is the expiration period expressed in seconds. Note that if the last parameter is missing, a default subscription period of 3600s (1h) is assumed.

to:

The first parameter is the name of the event (see here), the second parameter is the listening socket name (the format depends on the transport module used) and the last parameter is the expiration period expressed in seconds. Note that if the last parameter is missing, a default subscription period of 3600s (1h) is assumed.

Changed lines 53-54 from:

The Event Interface allows the script writer to subscribe for an event directly from the OpenSIPS configuration script. This is usually used when a subscription is permanent, that never expires. For example a server that is continuously listening from events from OpenSIPS and process them accordingly. The command used to subscribe for an event from script is subscribe_event.

to:

The Event Interface allows the script writer to subscribe for an event directly from the OpenSIPS configuration script. This is usually used when a subscription is permanent, that never expires. For example a server that is continuously listening from events from OpenSIPS and process them accordingly. The command used to subscribe for an event from script is subscribe_event.

Changed lines 65-66 from:

If the script writer wants the subscription to expire after 1200 second, the third parameter of the subscribe_event function can be specified:

to:

If the script writer wants the subscription to expire after 1200 second, the third parameter of the subscribe_event function can be specified:

Changed lines 75-76 from:

The subscribers_list MI function, exported by the OpenSIPS Core, can be used to list all the external applications subscribed. It also allows you to filter the subscribers based on the event they are registered to. For more information consult the functions documentation page.

to:

The subscribers_list MI function, exported by the OpenSIPS Core, can be used to list all the external applications subscribed. It also allows you to filter the subscribers based on the event they are registered to. For more information consult the functions documentation page.

Changed lines 82-86 from:
  • E_PIKE_BLOCKED - raised when pike module decides an IP should be blocked.
  • E_RTPPROXY_STATUS - raised when a RTPProxy connects or disconnects.
  • E_DISPATCHER_STATUS - raised by the dispatcher module when a destination address becomes active/inactive.
  • E_CORE_THRESHOLD - raised when using debugging bottleneck detection and the limit is exceeded.
to:
  • E_PIKE_BLOCKED - raised when pike module decides an IP should be blocked.
  • E_RTPPROXY_STATUS - raised when a RTPProxy connects or disconnects.
  • E_DISPATCHER_STATUS - raised by the dispatcher module when a destination address becomes active/inactive.
  • E_CORE_THRESHOLD - raised when using debugging bottleneck detection and the limit is exceeded.
  • E_CORE_PKG_THRESHOLD - raised when the private memory usage exceeds a certain limit.
  • E_CORE_SHM_THRESHOLD - raised when the shared memory usage exceeds a certain limit.
Changed lines 95-96 from:

In order to raise an event from the script, the raise_event function has to be used. The function receives one, two or three parameters, depending on the script writer needs. If the event has no parameters, then only the first parameter is needed - the event's name.

to:

In order to raise an event from the script, the raise_event function has to be used. The function receives one, two or three parameters, depending on the script writer needs. If the event has no parameters, then only the first parameter is needed - the event's name.

Changed lines 157-158 from:

In order to support this type of communication, the event_datagram module has to be loaded. Consult the module's documentation page for more information about subscription socket and encapsulation format.

to:

In order to support this type of communication, the event_datagram module has to be loaded. Consult the module's documentation page for more information about subscription socket and encapsulation format.

Changed lines 163-164 from:

The event_rabbitmq module provides this functionality. Note that in this case, the "external application" is the RabbitMQ server, so you do not have the possibility to subscribe for events from it. Therefore the programmer should either use a different application to subscribe for the event, or subscribes for it directly from the OpenSIPS script (as discussed here). For more information please visit the module's documentation page.

to:

The event_rabbitmq module provides this functionality. Note that in this case, the "external application" is the RabbitMQ server, so you do not have the possibility to subscribe for events from it. Therefore the programmer should either use a different application to subscribe for the event, or subscribes for it directly from the OpenSIPS script (as discussed here). For more information please visit the module's documentation page.

Changed lines 167-168 from:

OpenSIPS can also execute a remote XMLRPC command when an event is triggered. The module that handles the communication between the Event Interface and the XMLRPC server is event_xmlrpc.

to:

OpenSIPS can also execute a remote XMLRPC command when an event is triggered. The module that handles the communication between the Event Interface and the XMLRPC server is event_xmlrpc.

Changed lines 171-174 from:

A special implementation of a transport module is the event_route module, which is used to handle events directly from the OpenSIPS script. This allows the script writer to modify OpenSIPS internal variables when an event is triggered, by writing a special route, called event_route. The name of the route specifies the event that is handled by that route.

In order to retrieve the parameters of the event, the fetch_event_params function has to be used. It receives a list of parameters names, and the pseudo-variables that they have to be assigned to. For more information consult the event_route documentation page.

to:

A special implementation of a transport module is the event_route module, which is used to handle events directly from the OpenSIPS script. This allows the script writer to modify OpenSIPS internal variables when an event is triggered, by writing a special route, called event_route. The name of the route specifies the event that is handled by that route.

In order to retrieve the parameters of the event, the fetch_event_params function has to be used. It receives a list of parameters names, and the pseudo-variables that they have to be assigned to. For more information consult the event_route documentation page.

Changed line 184 from:

Note that specifying the event_route will automatically subscribe the route for the event. Therefore there is no need for explicit subscribing (from either external application or script). The subscription never expires, unless otherwise specified using the subscribe_event MI command.

to:

Note that specifying the event_route will automatically subscribe the route for the event. Therefore there is no need for explicit subscribing (from either external application or script). The subscription never expires, unless otherwise specified using the subscribe_event MI command.

January 11, 2013, at 01:14 PM by razvancrainea -
Changed lines 171-172 from:

In order to retrieve the parameters of the event, the fetch_even_params function has to be used. It receives a list of parameters names, and the pseudo-variables that they have to be assigned to. For more information consult the event_route documentation page.

to:

In order to retrieve the parameters of the event, the fetch_event_params function has to be used. It receives a list of parameters names, and the pseudo-variables that they have to be assigned to. For more information consult the event_route documentation page.

Changed lines 182-185 from:

Note that specifying the event_route will automatically subscribe the route for the event. Therefore there is no need for explicit subscribing (from either external application or script). The subscription never expires, unless otherwise specified using the subscribe_event MI command.


Development

to:

Note that specifying the event_route will automatically subscribe the route for the event. Therefore there is no need for explicit subscribing (from either external application or script). The subscription never expires, unless otherwise specified using the subscribe_event MI command.

January 11, 2013, at 01:12 PM by razvancrainea -
Changed lines 7-8 from:

This tutorial is for OpenSIPS 1.8.x and development version

to:

This tutorial is for OpenSIPS 1.9.x version

Changed lines 20-22 from:
  • event_xmlrpc - issues a RPC command on a RPC server (available only on trunk)
  • event_route - runs an OpenSIPS event_route (available only on trunk)
to:
  • event_xmlrpc - issues a RPC command on a RPC server
  • event_route - runs an OpenSIPS event_route
Changed lines 71-72 from:

@]

to:

@]

Subscribers

The subscribers_list MI function, exported by the OpenSIPS Core, can be used to list all the external applications subscribed. It also allows you to filter the subscribers based on the event they are registered to. For more information consult the functions documentation page.

Added lines 123-126:

Events list

The events_list function can be used to list all the available events.

Changed lines 147-182 from:

Debugging

to:

Transport Modules

There are several transport modules that can be used to notify an external application. At the time this tutorial was written, the following transport methods were supported:

Datagram Messages

External applications can be notified when OpenSIPS internal events are triggered using either UDP or UNIX datagrams. This allows programmers to write a simple UDP server that receives the notification and process it in the preferred programming language.

In order to support this type of communication, the event_datagram module has to be loaded. Consult the module's documentation page for more information about subscription socket and encapsulation format.

RabbitMQ Messages

OpenSIPS can also communicate with a RabbitMQ server using the AMQP (Advanced Message Queuing Protocol) messages. This type of communication leverages all the features the queuing server has.

The event_rabbitmq module provides this functionality. Note that in this case, the "external application" is the RabbitMQ server, so you do not have the possibility to subscribe for events from it. Therefore the programmer should either use a different application to subscribe for the event, or subscribes for it directly from the OpenSIPS script (as discussed here). For more information please visit the module's documentation page.

XMLRPC Commands

OpenSIPS can also execute a remote XMLRPC command when an event is triggered. The module that handles the communication between the Event Interface and the XMLRPC server is event_xmlrpc.

Routes

A special implementation of a transport module is the event_route module, which is used to handle events directly from the OpenSIPS script. This allows the script writer to modify OpenSIPS internal variables when an event is triggered, by writing a special route, called event_route. The name of the route specifies the event that is handled by that route.

In order to retrieve the parameters of the event, the fetch_even_params function has to be used. It receives a list of parameters names, and the pseudo-variables that they have to be assigned to. For more information consult the event_route documentation page.

For example in order to specify a route that handles the E_PIKE_BLOCKED event, and prints the blocked IP, the OpenSIPS script has to contain the following code:

   event_route[E_PIKE_BLOCKED] {
      fetch_event_params("ip=$avp(pike-ip)");
      xlog("IP $avp(pike-ip) has been blocked\n");
   }

Note that specifying the event_route will automatically subscribe the route for the event. Therefore there is no need for explicit subscribing (from either external application or script). The subscription never expires, unless otherwise specified using the subscribe_event MI command.

January 09, 2013, at 06:46 PM by razvancrainea -
Added lines 120-136:

A practical use case of dynamic events is raising an event from the script when the TCP load is greater than 80%:

   route[TCP_LOAD_CHECK] {

      if ($stat(tcp-load) > 80) {
         # raise the event, specifying the limit and the real load
         $avp(attrs) = "limit";
         $avp(vals) = 80;
         $avp(attrs) = "load";
         $avp(vals) = $stat(tcp-load);

         raise_event("E_SCRIPT_TCP_LOAD", $avp(attrs), $avp(vals));
      }
   }
January 09, 2013, at 06:39 PM by razvancrainea -
Changed lines 76-77 from:

Raising an Event

to:

There are two types of events: static/predefined events, exported by OpenSIPS code and modules, and documented by each module, and dynamic events that can be raised by the script writer directly from the script. A few examples of predefined events:

  • E_PIKE_BLOCKED - raised when pike module decides an IP should be blocked.
  • E_RTPPROXY_STATUS - raised when a RTPProxy connects or disconnects.
  • E_DISPATCHER_STATUS - raised by the dispatcher module when a destination address becomes active/inactive.
  • E_CORE_THRESHOLD - raised when using debugging bottleneck detection and the limit is exceeded.

Events Name

Although there is not a strict events naming imposed by the Event Interface, it is a good practice for an event's name to follow the format E_MODULE_NAME, where MODULE is the name of the module that exports the event, CORE if the event is exported by OpenSIPS core, and SCRIPT if it is a dynamic event raised from the script, and NAME is the name of the event.

Raising an Event

In order to raise an event from the script, the raise_event function has to be used. The function receives one, two or three parameters, depending on the script writer needs. If the event has no parameters, then only the first parameter is needed - the event's name.

If the script writer wants to attach some extra information to the event, then the second and optionally the third parameter has to be used. The extra information has to be stored in an AVP as values, for example:

    $avp(values) = "value1";
    $avp(values) = "value2";
    ...

If the parameters also have some names, then a new AVP have to store them. Note that the number of values in the first AVP has to be the same as in the one for the attributes:

    $avp(attributes) = "param1";
    $avp(values) = "value1";
    $avp(attributes) = "param2";
    $avp(values) = "value2";
    ...

In order to raise the event from the script, the following code has to be used:

   raise_event("E_SCRIPT_EVENT");     # raises an event without any parameters

   raise_event("E_SCRIPT_EVENT", $avp(values));     # raises an event that has only values parameters

   raise_event("E_SCRIPT_EVENT", $avp(attributes), $avp(values));     # raises an event with multiple attribute-value pairs

Example

January 09, 2013, at 05:17 PM by razvancrainea -
Changed lines 33-35 from:

MI Subscribe

An external application should subscribe for an event using MI commands when the subscription nature is very dynamic: it subscribes for a short period, receives the messages, then unsubscribe and so on.

to:

MI Subscription

An external application should subscribe for an event using MI commands when the subscription nature is very dynamic: it subscribes for a short period, receives notifications, then decides to unsubscribe and so on. The MI function used to subscribe for an event is event_subscribe.

For example, to subscribe for the E_PIKE_BLOCKED event for only 1200 seconds, an external application that listens on localhost, UDP port 8888, has to issue the following MI command:

   $ opensipsctl fifo event_subscribe E_PIKE_BLOCKED udp:127.0.0.1:8888 1200

The first parameter is the name of the event (see here), the second parameter is the listening socket name (the format depends on the transport module used) and the last parameter is the expiration period expressed in seconds. Note that if the last parameter is missing, a default subscription period of 3600s (1h) is assumed.

In order to unsubscribe for an event, the external application has to use the same event and socket used at subscription time, but specify an expiration time of 0. The following command unsubscribes the previously registered application.

   $ opensipsctl fifo event_subscribe E_PIKE_BLOCKED udp:127.0.0.1:8888 0

Script Subscription

The Event Interface allows the script writer to subscribe for an event directly from the OpenSIPS configuration script. This is usually used when a subscription is permanent, that never expires. For example a server that is continuously listening from events from OpenSIPS and process them accordingly. The command used to subscribe for an event from script is subscribe_event.

Usually these kind of subscriptions are permanent, therefore they should be registered at when OpenSIPS starts. Therefore this should be done in startup_route, but this is not a requirement, as the function can be used in any route.

Here is an example of subscription for the E_PIKE_BLOCKED event at OpenSIPS startup, without any expire time, from the OpenSIPS configuration script:

   startup_route() {
      subscribe_event("E_PIKE_BLOCKED", "udp:127.0.0.1:8888");
   }

If the script writer wants the subscription to expire after 1200 second, the third parameter of the subscribe_event function can be specified:

   timer_route[event_subscribe,3600] {
      subscribe_event("E_PIKE_BLOCKED", "udp:127.0.0.1:8888", 1200);
   }

Events


Raising an Event


Debugging


Development

January 09, 2013, at 12:59 PM by razvancrainea -
Added lines 1-35:

Resources -> Documentation -> Tutorials -> Event Interface

This page has been visited 447 times. (:toc-float Table of Content:)


This tutorial is for OpenSIPS 1.8.x and development version

Event Interface

The OpenSIPS Event Interface provides an easy way to notify external applications whenever certain events occur inside OpenSIPS. Basically it provides two interfaces: one for the OpenSIPS internal modules and core, that is used to advertise and raise events, and an interface for the external applications, allowing them to register for specific events, and notifying them when those events are triggered by the modules, core, or by the OpenSIPS script.


Overview

OpenSIPS can notify an external application using different transport protocols:

  • event_datagram - sends Datagrams over UDP or UNIX sockets
  • event_rabbitmq - sends an AMQP message to a RabbitMQ server
  • event_xmlrpc - issues a RPC command on a RPC server (available only on trunk)
  • event_route - runs an OpenSIPS event_route (available only on trunk)

In order to receive an event notification, an external application has to subscribe for a specific event to the Event interface. This can be done either using an MI command, or can be done directly from the OpenSIPS script. The subscription command has to specify the event that the external application wants to register to, and a socket where the notification has to be sent. The format of the socket is different, depending on the transport module used (you can find more information about this in the module's documentation page).

When an event is triggered, the Event Interface will send to each application that is subscribed for that event a notification, using the desired transport type. The event details are packed differently for each transport module (consult the transport module's documentation page for more details).


Event Subscription

As mentioned above, an external application can subscribe for an event either using MI, or directly from OpenSIPS script. In the next sections we will describe the use case of each of them.

MI Subscribe

An external application should subscribe for an event using MI commands when the subscription nature is very dynamic: it subscribes for a short period, receives the messages, then unsubscribe and so on.


Page last modified on April 24, 2013, at 06:49 PM