FreeSWITCH ESL

Connect via TCP to FreeSWITCH's Event Socket Library to control and monitor a running FreeSWITCH instance.

Edit the FreeSWITCH tcp in node to connect to the correct host at port 8021 (Make sure if Node RED is not running on the same server to listen on the given IP address and change the password) in event_socket.conf.xml

Update the password accordingly in the Set authentication node (change ClueCon (default) into your password)

Example command payloads are:

  • api show registrations as json
  • event json all
  • noevents

This flow will try and create a payload for every complete response with msg.headers: a key-value pair of the received headers

msg.payload: a javascript object (where applicable)

[{"id":"159e17b2.fe5b28","type":"subflow","name":"FreeSWITCH ESL","info":"Connect via TCP to FreeSWITCH's\nEvent Socket Library to control and monitor\na running FreeSWITCH instance.\n\nExample command payloads are:\n`\"api show registrations as json\"`\n`\"event json all\"`\n`\"noevents\"`\n\nThis flow will try and create a payload\nfor every complete response with\nmsg.headers: a key-value pair\nof the received headers\n\nmsg.payload: a javascript object\n(where applicable)\n","category":"","in":[{"x":40,"y":40,"wires":[{"id":"a2fbecaf.81b5f"}]}],"out":[{"x":1080,"y":200,"wires":[{"id":"c5264aea.cdeba8","port":0}]}],"inputLabels":["Command"],"outputLabels":["Event"]},{"id":"ded4d708.0e2458","type":"tcp out","z":"159e17b2.fe5b28","host":"","port":"","beserver":"reply","base64":false,"end":false,"name":"FreeSWITCH","x":1020,"y":40,"wires":[]},{"id":"13e3825f.19f7ce","type":"tcp in","z":"159e17b2.fe5b28","name":"FreeSWITCH","server":"client","host":"localhost","port":"8021","datamode":"stream","datatype":"utf8","newline":"","topic":"","base64":false,"x":90,"y":200,"wires":[["c5264aea.cdeba8"]]},{"id":"c5264aea.cdeba8","type":"function","z":"159e17b2.fe5b28","name":"TCP Stream to messages","func":"// Make incoming packets a stream\nvar stream = (flow.get( \"stream\" )|| \"\" ) + msg.payload;\n\n// Read existing headers\nvar headers = flow.get( \"headers\" ) || new Map();\n\nvar loop;\ndo\n{\n    // No existing headers?\n    if ( !headers.size )\n    {\n        // Extract headers from stream, update stream\n        var sep = stream.indexOf( \"\\n\\n\" );\n        if ( sep !== -1 )\n        {\n            headers = extractHeaders( stream.substr( 0, sep ) );\n            stream = stream.substr( sep + 2 );\n        }\n    }\n\n    var len = headers.get( \"Content-Length\" ) | 0;\n    \n    // Do we have headers and have a stream that fits within the content length?\n    if ( headers.size && stream.length >= len )\n    {\n        var body = stream.substr( 0, len );\n        try\n        {\n            body = JSON.parse( body );\n        }\n        catch( _e )\n        {\n            ;\n        }\n        \n        var headerobj = {}\n        headers.forEach( (v,k)=>headerobj[k]=v );\n        node.send( { headers: headerobj, payload: body } );\n        headers.clear();\n        stream = stream.substr( len );\n        //  loop if \n        loop = true;\n    }\n    else\n    {\n        loop = false;\n    }\n} while ( loop )\n\n// Save headers and remaining stream\nflow.set( \"headers\", headers );\nflow.set( \"stream\", stream );\n\nfunction extractHeaders( _headerstring )\n{\n    return new Map( _headerstring.split( \"\\n\" ).map( function( _val )\n    {\n        var sep = _val.indexOf( \": \" );\n        if ( sep === -1 )\n            return null;\n    \n        return [ _val.substr( 0, sep ), _val.substr( sep + 2 ) ];\n    } ) );\n}","outputs":1,"noerr":0,"x":310,"y":200,"wires":[["4b7afb9.70a1304"]]},{"id":"4b7afb9.70a1304","type":"switch","z":"159e17b2.fe5b28","name":"Needs authentication?","property":"headers.Content-Type","propertyType":"msg","rules":[{"t":"eq","v":"auth/request","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":460,"y":100,"wires":[["18c9c60.13c573a"]]},{"id":"18c9c60.13c573a","type":"change","z":"159e17b2.fe5b28","name":"Set authentication","rules":[{"t":"set","p":"payload","pt":"msg","to":"auth ClueCon","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":100,"wires":[["a2fbecaf.81b5f"]]},{"id":"a2fbecaf.81b5f","type":"function","z":"159e17b2.fe5b28","name":"add newlines","func":"msg.payload += \"\\n\\n\";\nreturn msg;","outputs":1,"noerr":0,"x":830,"y":40,"wires":[["ded4d708.0e2458"]]}]

Flow Info

Created 6 years, 10 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • change (x1)
  • function (x2)
  • switch (x1)
  • tcp in (x1)
  • tcp out (x1)
Other
  • subflow (x1)

Tags

  • FreeSWITCH
  • VoIP
  • Event_Socket_Library
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option