Categories
Kamailio NATS VoIP

kamailio: nats module

I recently published a new module for kamailio which allows it to connect to NATS and consume messages using queue groups

NATS is a very cool distributed messaging system. One of my favorite things about NATS is, there are no brokers and it’s extremely easy to setup. You can setup a nats-server cluster in about 2 minutes, connect kamailio to it and start receiving messages inside your kamailio routing configuration. Once kamailio connects to the cluster, any of the nats-servers (as long as one nats-server remains up) can be taken down without any impact to the publishers or consumers.

In this post, I will show an example of a simple NATS cluster with two nodes and sample kamailio configuration to connect to the cluster.

NATS Cluster

In this example, node1 will have an IP Address of 10.10.10.11, and node2 will be 10.10.10.12.

After you have downloaded and installed nats-server to your system. Setup node1 with the configuration file (server.conf):

listen: 0.0.0.0:4222
http: 0.0.0.0:8222

cluster {
  listen: 10.10.10.11:4248
  routes = [
    nats-route://10.10.10.12:4248
  ]
}

Setup node2 with the configuration file (server.conf):

listen: 0.0.0.0:4222
http: 0.0.0.0:8222

cluster {
  listen: 10.10.10.12:4248
  routes = [
    nats-route://10.10.10.11:4248
  ]
}

Start nats-server with the following command on each node:
./nats-server -c ./server.conf

Kamailio Setup

You will need to compile the NATS module since nats.c is not shipped with Debian. Once it’s compiled, you can load the module with a configuration like:

loadmodule "nats.so"
modparam("nats", "nats_url", "nats://10.10.10.11:4222")
modparam("nats", "nats_url", "nats://10.10.10.12:4222")
modparam("nats", "subject_queue_group", "voxcom:kamailio")

event_route[nats:connected] {
    xlog("L_INFO", "nats connected!");
}

event_route[nats:disconnected] {
    xlog("L_INFO", "nats disconnected!");
}

event_route[nats:voxcom] {
    xlog("L_INFO", "nats payload received [$natsData]\n");
}

Sample NATS Publisher

Below is a simple go application which will publish a message on the voxcom subject.

package main

import (
	"log"

	"github.com/nats-io/nats.go"
)

func main() {
	nc, err := nats.Connect("10.10.10.12:4222", nats.Name("example"))
	if err != nil {
		log.Fatal(err)
	}
	defer nc.Close()

	if err := nc.Publish("voxcom", []byte("hello kamailio")); err != nil {
		log.Fatal(err)
	}
}

Run the application with go run main.go
You should see the “hello kamailio” text in your kamailio log.

Stop the nats-server on node1

If you are running nats-server in foreground, hit CTRL+C to stop it on node1.

Kamailio should indicate disconnect from node1 but stay connected to node2. You should be able to run the go app and still publish messages. Even if both nodes go down, the nats module will automatically reconnect and begin consuming messages again.

Documentation on the NATS module can be found here: https://www.kamailio.org/docs/modules/devel/modules/nats

3 replies on “kamailio: nats module”

Yes, I would like to add that feature as well. If you would like it expedited, you could offer a bounty.

Leave a Reply

Your email address will not be published. Required fields are marked *