{"id":241,"date":"2021-07-02T09:28:53","date_gmt":"2021-07-02T13:28:53","guid":{"rendered":"https:\/\/blog.voipxswitch.com\/?p=241"},"modified":"2021-07-13T07:30:08","modified_gmt":"2021-07-13T11:30:08","slug":"kamailio-nats-module","status":"publish","type":"post","link":"https:\/\/blog.voipxswitch.com\/?p=241","title":{"rendered":"kamailio: nats module"},"content":{"rendered":"\n<p>I recently published a new module for <code>kamailio<\/code> which allows it to connect to NATS and consume messages using <a href=\"https:\/\/docs.nats.io\/nats-concepts\/queue\">queue groups<\/a><\/p>\n\n\n\n<p>NATS is a very cool distributed messaging system. One of my favorite things about NATS is, there are no brokers and it&#8217;s extremely easy to setup. You can setup a <a href=\"https:\/\/github.com\/nats-io\/nats-server\">nats-server<\/a> cluster in about 2 minutes, connect <code>kamailio<\/code> to it and start receiving messages inside your <code>kamailio<\/code> routing configuration. Once <code>kamailio<\/code> connects to the cluster, any of the <code>nats-servers<\/code> (as long as one <code>nats-server<\/code> remains up) can be taken down without any impact to the publishers or consumers. <\/p>\n\n\n\n<p>In this post, I will show an example of a simple NATS cluster with two nodes and sample <code>kamailio<\/code> configuration to connect to the cluster. <\/p>\n\n\n\n<p class=\"has-larger-font-size\">NATS Cluster<\/p>\n\n\n\n<p>In this example, <code>node1<\/code> will have an IP Address of <code>10.10.10.11<\/code>, and <code>node2<\/code> will be <code>10.10.10.12<\/code>.<\/p>\n\n\n\n<p>After you have downloaded and installed <a href=\"https:\/\/github.com\/nats-io\/nats-server\/releases\/tag\/v2.3.1\">nats-server<\/a> to your system. Setup <code>node1<\/code> with the configuration file (<code>server.conf<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>listen: 0.0.0.0:4222\nhttp: 0.0.0.0:8222\n\ncluster {\n  listen: 10.10.10.11:4248\n  routes = &#91;\n    nats-route:\/\/10.10.10.12:4248\n  ]\n}<\/code><\/pre>\n\n\n\n<p>Setup <code>node2<\/code> with the configuration file (<code>server.conf<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>listen: 0.0.0.0:4222\nhttp: 0.0.0.0:8222\n\ncluster {\n  listen: 10.10.10.12:4248\n  routes = &#91;\n    nats-route:\/\/10.10.10.11:4248\n  ]\n}<\/code><\/pre>\n\n\n\n<p>Start <code>nats-server<\/code> with the following command on each node:<br><code>.\/nats-server -c .\/server.conf<\/code><\/p>\n\n\n\n<p class=\"has-larger-font-size\">Kamailio Setup<\/p>\n\n\n\n<p>You will need to compile the NATS module since <a href=\"https:\/\/github.com\/nats-io\/nats.c\/\">nats.c<\/a> is not shipped with Debian. Once it&#8217;s compiled, you can load the module with a configuration like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>loadmodule \"nats.so\"\nmodparam(\"nats\", \"nats_url\", \"nats:\/\/10.10.10.11:4222\")\nmodparam(\"nats\", \"nats_url\", \"nats:\/\/10.10.10.12:4222\")\nmodparam(\"nats\", \"subject_queue_group\", \"voxcom:kamailio\")\n\nevent_route&#91;nats:connected] {\n    xlog(\"L_INFO\", \"nats connected!\");\n}\n\nevent_route&#91;nats:disconnected] {\n    xlog(\"L_INFO\", \"nats disconnected!\");\n}\n\nevent_route&#91;nats:voxcom] {\n    xlog(\"L_INFO\", \"nats payload received &#91;$natsData]\\n\");\n}<\/code><\/pre>\n\n\n\n<p class=\"has-larger-font-size\">Sample NATS Publisher<\/p>\n\n\n\n<p>Below is a simple go application which will publish a message on the voxcom subject. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"log\"\n\n\t\"github.com\/nats-io\/nats.go\"\n)\n\nfunc main() {\n\tnc, err := nats.Connect(\"10.10.10.12:4222\", nats.Name(\"example\"))\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer nc.Close()\n\n\tif err := nc.Publish(\"voxcom\", &#91;]byte(\"hello kamailio\")); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}<\/code><\/pre>\n\n\n\n<p>Run the application with <code><span class=\"has-inline-color has-primary-color\"><strong>go run main.go<\/strong><\/span><\/code><br>You should see the &#8220;hello kamailio&#8221; text in your kamailio log.<\/p>\n\n\n\n<p class=\"has-larger-font-size\">Stop the <code>nats-server<\/code> on <code>node1<\/code><\/p>\n\n\n\n<p>If you are running <code>nats-server<\/code> in foreground, hit CTRL+C to stop it on <code>node1<\/code>.<\/p>\n\n\n\n<p>Kamailio should indicate disconnect from <code>node1<\/code> but stay connected to <code>node2<\/code>. 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.<\/p>\n\n\n\n<p>Documentation on the NATS module can be found here: <a href=\"https:\/\/www.kamailio.org\/docs\/modules\/devel\/modules\/nats\">https:\/\/www.kamailio.org\/docs\/modules\/devel\/modules\/nats<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s extremely easy to setup. You can setup a nats-server cluster in about [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,10,15],"tags":[],"class_list":["post-241","post","type-post","status-publish","format-standard","hentry","category-kamailio","category-nats","category-voip"],"_links":{"self":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts\/241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=241"}],"version-history":[{"count":3,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts\/241\/revisions"}],"predecessor-version":[{"id":245,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts\/241\/revisions\/245"}],"wp:attachment":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}