Javascript Event

From FreeSWITCH Wiki
Jump to: navigation, search

Contents

Invoking Event Object

   // Define callback function
   function on_event(session, type, event_obj, user_data) {
           if (type == "event") {
                   console_log("notice", "Event: " + event_obj.serialize() + "\n"); 
           }
   }
   // Start tone detection and play silent file to wait for event
   session.execute("tone_detect", "voicemail_beep 1400 r");    
   session.streamFile("/usr/local/freeswitch/sounds/silence.wav", on_event);
   session.execute("stop_tone_detect");

Methods


Example of creating Event object and then fire it

    var msg = "Hello, welcome to the FreeSWITCH demo application please enter some text into the chat box";
    e = new Event("custom", "message");
    e.addBody(msg);
    e.fire();

Result

    Content-Length: 608
    Content-Type: text/event-plain
    
    Event-Subclass: message
    Event-Name: CUSTOM
    Core-UUID: 95e16640-7ddf-11dd-adfa-133c220a79a4
    FreeSWITCH-Hostname: johny-ubuntu
    FreeSWITCH-IPv4: 192.168.150.134
    FreeSWITCH-IPv6: 127.0.0.1
    Event-Date-Local: 2008-09-25%2010%3A38%3A42
    Event-Date-GMT: Thu,%2025%20Sep%202008%2014%3A38%3A42%20GMT
    Event-Date-timestamp: 1222353522769992
    Event-Calling-File: mod_spidermonkey.c
    Event-Calling-Function: event_fire
    Event-Calling-Line-Number: 673
    Event-Subclass: message
    Event-Subclass-Owner: src/switch_event.c
    Content-Length: 90
         
    Hello, welcome to the FreeSWITCH demo application please enter some text into the chat box

Subscribing To Custom Events

In JavaScript you need to specify the Event-Subclass when subscribing to list to custom events. To listen for custom events like the above, do this at the event socket:

events plain custom message

The following is WRONG for JavaScript custom events:

events plain custom

In short, be sure to specify the event subclass when listening for custom events created by your JavaScripts.
NOTE: This gives you a bit of flexibility in listening to custom events - you can listen to some custom events but not all custom events.
If you have an event like this:

e = new Event("custom","foo");

Then you can listen for it like this at the event socket:

events plain custom foo


NOTE: It is not required that one create an actual "custom" event. This is perfectly valid:

e = new Event("message");

You could then simply listen for "message" events:

events plain message

With custom events it is important to specify a subclass. Internally FreeSWITCH uses the nomenclature of mod::event_name, so you could do something like this:

e = new Event("custom","my_js::test);

You could then listen specifically for these events:

events plain custom my_js::test

This way you can be very specific with the events you need to see and can program accordingly.