drachtio-fs-load-balancing-proxy is a nodejs-based SIP load balancer for Freeswitch servers.
It is based on the high-performance drachtio signaling resource framework, which in turn utilizes the sofia SIP stack.
The need to load balance SIP traffic across a horizontal cluster of Freeswitch servers is a common requirement. While other solutions exist (e.g. Kamailio), the solution presented here may be more desirable to nodejs developers who prefer Javascript over proprietary and cryptic configuration-driven logic. Additionally, the underlying framework provides tools to build a wider range of VoIP applications beyond simple SIP proxies.
drachtio is an open-source, nodejs-based ecosystem for creating any kind of VoIP server-based application: registrar, proxy, back-to-back user agent, and many others. Furthermore, when coupled with the drachtio media resource function, rich media-processing applications can be easily built as well. Nodejs developers experienced with the express or connect middleware frameworks will find drachtio very familiar.
Getting Started
Note: API documentation on the key application classes can be found here.
The prerequisites: you will need to install a drachtio SIP server somewhere in your network to handle the SIP message processing.
The basics:$ git clone git@github.com:davehorton/drachtio-fs-load-balancing-proxy.git $ cd fs-load-balancing-proxy $ npm install
Next, copy lib/config.example.js
to config.js
, and edit to specify the coordinates of your freeswitch servers, as well as your drachtio server process.
Then fire it up!
$ node app.js
Hacking the code
Contributors are welcome! Feel free to fork the repo and send me pull requests for any features or bug fixes.
The code is pretty simple, and there isn't that much of it. Here are a few notes to get you started:
- app.js - the main entry point to the application is dead simple. It creates the necessary instances of the drachtio app and srf (signaling resource framework), connects to the drachtio server and the loads in the routes, which are found in
lib/proxy.js
. - lib/proxy.js - this has the invite handler and executes the key line of code that actually makes everything happen:
req.proxy(..)
. It relies onlib/cluster.js
to provide the list of online freeswitch servers to proxy to, so this file is actually pretty simple as well. - lib/cluster.js - this module manages the freeswitch servers; its main responsibility is simply to keep track of which freeswitch servers are online at any point in time. It also watches for changes to the
lib/config.js
file and automatically updates the list of freeswitch servers as any entries are added or removed from the config file -- there is no need to restart anything for changes to take affect. - lib/fsw.js - this modules manages the connection to an individual freeswitch server. It connects to the freeswitch event socket and receives heartbeat messages from the freeswitch server each 20 seconds. Any time the connection is lost it will automatically begin retrying to connect
- lib/config.js - this is the configuration file. You'll need to copy config.example.js to config.js to start with, then edit to your desires. The file itself is documented so it won't be further described here.