| 1 |
NATS Module |
| 2 |
|
| 3 |
Emmanuel Schmidbauer |
| 4 |
|
| 5 |
<[email protected]> |
| 6 |
|
| 7 |
Joe Mordica |
| 8 |
|
| 9 |
<[email protected]> |
| 10 |
|
| 11 |
Edited by |
| 12 |
|
| 13 |
Emmanuel Schmidbauer |
| 14 |
|
| 15 |
<[email protected]> |
| 16 |
|
| 17 |
Copyright © 2021 Voxcom Inc |
| 18 |
|
| 19 |
Copyright © 2021 VOXO |
| 20 |
__________________________________________________________________ |
| 21 |
|
| 22 |
Table of Contents |
| 23 |
|
| 24 |
1. Admin Guide |
| 25 |
|
| 26 |
1. Overview |
| 27 |
2. How it works |
| 28 |
3. Dependencies |
| 29 |
|
| 30 |
3.1. Kamailio Modules |
| 31 |
3.2. External Libraries or Applications |
| 32 |
|
| 33 |
4. Parameters |
| 34 |
|
| 35 |
4.1. nats_url (str) |
| 36 |
4.2. num_publish_workers (int) |
| 37 |
4.3. subject_queue_group (str) |
| 38 |
4.4. event_callback (str) |
| 39 |
|
| 40 |
5. Functions |
| 41 |
|
| 42 |
5.1. nats_publish(subject, payload, reply) |
| 43 |
|
| 44 |
6. Pseudo Variables |
| 45 |
7. Event Routes |
| 46 |
|
| 47 |
List of Examples |
| 48 |
|
| 49 |
1.1. Set nats_url parameter |
| 50 |
1.2. Set num_publish_workers parameter |
| 51 |
1.3. Set subject_queue_group parameter |
| 52 |
1.4. Set event_callback parameter |
| 53 |
1.5. nats_publish usage |
| 54 |
1.6. Example usage of $natsData pseudo variable |
| 55 |
1.7. Define the event routes |
| 56 |
|
| 57 |
Chapter 1. Admin Guide |
| 58 |
|
| 59 |
Table of Contents |
| 60 |
|
| 61 |
1. Overview |
| 62 |
2. How it works |
| 63 |
3. Dependencies |
| 64 |
|
| 65 |
3.1. Kamailio Modules |
| 66 |
3.2. External Libraries or Applications |
| 67 |
|
| 68 |
4. Parameters |
| 69 |
|
| 70 |
4.1. nats_url (str) |
| 71 |
4.2. num_publish_workers (int) |
| 72 |
4.3. subject_queue_group (str) |
| 73 |
4.4. event_callback (str) |
| 74 |
|
| 75 |
5. Functions |
| 76 |
|
| 77 |
5.1. nats_publish(subject, payload, reply) |
| 78 |
|
| 79 |
6. Pseudo Variables |
| 80 |
7. Event Routes |
| 81 |
|
| 82 |
1. Overview |
| 83 |
|
| 84 |
The module provides an NATS consumer for Kamailio. NATS is a real time |
| 85 |
distributed messaging platform, more details about it can be found at |
| 86 |
nats.io . |
| 87 |
|
| 88 |
From a high-level perspective, the module may be used for: |
| 89 |
* Provide a real-time distributed messaging layer in Kamailio |
| 90 |
|
| 91 |
Supported NATS operations are: |
| 92 |
* Subscribe to a Subject and Queue Group |
| 93 |
|
| 94 |
2. How it works |
| 95 |
|
| 96 |
The module creates invokes a consumer process for each defined |
| 97 |
`subject_queue_group`. The messages are visible in event routes |
| 98 |
matching the "subject" name. |
| 99 |
|
| 100 |
3. Dependencies |
| 101 |
|
| 102 |
3.1. Kamailio Modules |
| 103 |
3.2. External Libraries or Applications |
| 104 |
|
| 105 |
3.1. Kamailio Modules |
| 106 |
|
| 107 |
The following modules must be loaded before this module: |
| 108 |
* none . |
| 109 |
|
| 110 |
3.2. External Libraries or Applications |
| 111 |
|
| 112 |
The following libraries or applications must be installed |
| 113 |
* libuv |
| 114 |
* nats.c - https://github.com/nats-io/nats.c/releases |
| 115 |
|
| 116 |
4. Parameters |
| 117 |
|
| 118 |
4.1. nats_url (str) |
| 119 |
4.2. num_publish_workers (int) |
| 120 |
4.3. subject_queue_group (str) |
| 121 |
4.4. event_callback (str) |
| 122 |
|
| 123 |
4.1. nats_url (str) |
| 124 |
|
| 125 |
The nats url. |
| 126 |
|
| 127 |
Usage: nats related. |
| 128 |
|
| 129 |
Default value is nats://127.0.0.1:4222 |
| 130 |
|
| 131 |
Example 1.1. Set nats_url parameter |
| 132 |
... |
| 133 |
modparam("nats", "nats_url", "nats://127.0.0.1:4222") |
| 134 |
modparam("nats", "nats_url", "nats://user1:[email protected]:4222") // with auth |
| 135 |
modparam("nats", "nats_url", "nats://127.1.2.3:4222") |
| 136 |
... |
| 137 |
|
| 138 |
4.2. num_publish_workers (int) |
| 139 |
|
| 140 |
The number of worker threads for publishing messages. |
| 141 |
|
| 142 |
Usage: nats related. |
| 143 |
|
| 144 |
Default value is “2”. |
| 145 |
|
| 146 |
Example 1.2. Set num_publish_workers parameter |
| 147 |
... |
| 148 |
modparam("nats", "num_publish_workers", 4) |
| 149 |
... |
| 150 |
|
| 151 |
4.3. subject_queue_group (str) |
| 152 |
|
| 153 |
The NATS Subject and Queue Group. Separated by ":" |
| 154 |
|
| 155 |
Usage: nats related. |
| 156 |
|
| 157 |
Default value is not set. |
| 158 |
|
| 159 |
Example 1.3. Set subject_queue_group parameter |
| 160 |
... |
| 161 |
modparam("nats", "subject_queue_group", "Kamailio-World:2020") |
| 162 |
modparam("nats", "subject_queue_group", "Kamailio-World:2021") // this will crea |
| 163 |
te two processes for the Kamailio-World subject |
| 164 |
modparam("nats", "subject_queue_group", "MyQueue1:2021") |
| 165 |
modparam("nats", "subject_queue_group", "MyQueue2:2021") |
| 166 |
... |
| 167 |
|
| 168 |
4.4. event_callback (str) |
| 169 |
|
| 170 |
Name of the KEMI function to be executed instead of the event route. |
| 171 |
|
| 172 |
Default value is not set. |
| 173 |
|
| 174 |
Example 1.4. Set event_callback parameter |
| 175 |
... |
| 176 |
modparam("nats", "event_callback", "ksr_nats_event") |
| 177 |
|
| 178 |
-- event callback function implemented in Lua |
| 179 |
function ksr_nats_event(evname) |
| 180 |
KSR.info("===== nats module received event: " .. evname .. |
| 181 |
", data:" .. KSR.pv.gete('$natsData') .. "\n"); |
| 182 |
return 1; |
| 183 |
end |
| 184 |
... |
| 185 |
|
| 186 |
5. Functions |
| 187 |
|
| 188 |
5.1. nats_publish(subject, payload, reply) |
| 189 |
|
| 190 |
5.1. nats_publish(subject, payload, reply) |
| 191 |
|
| 192 |
Publishes the payload to subject, with an optional reply subject. |
| 193 |
|
| 194 |
Example 1.5. nats_publish usage |
| 195 |
... |
| 196 |
$var(my_info) = "$ci=" + $ci + " $fU=" + $fU; |
| 197 |
nats_publish("mysubject", "$var(my_info)"); # publish $var(my_info) to "mysubjec |
| 198 |
t" |
| 199 |
|
| 200 |
# publish $var(my_info) to "mysubject" with a reply.topic topic, so a response |
| 201 |
# can be published back from the other side, like the NATS Request-Reply mode. |
| 202 |
nats_publish("mysubject", "$var(my_info)", "reply.topic"); |
| 203 |
... |
| 204 |
|
| 205 |
6. Pseudo Variables |
| 206 |
|
| 207 |
Example 1.6. Example usage of $natsData pseudo variable |
| 208 |
... |
| 209 |
xlog("L_INFO", "received payload $natsData"); |
| 210 |
} |
| 211 |
|
| 212 |
* $natsData Contains the payload of a consumed message |
| 213 |
|
| 214 |
7. Event Routes |
| 215 |
|
| 216 |
The worker process issues an event-route where we can act on the |
| 217 |
received payload. The name of the event-route name must match the |
| 218 |
subject of the message. |
| 219 |
|
| 220 |
Example 1.7. Define the event routes |
| 221 |
... |
| 222 |
modparam("nats", "subject_queue_group", "Kamailio-World:2021") |
| 223 |
modparam("nats", "subject_queue_group", "MyQueue1:2021") |
| 224 |
... |
| 225 |
|
| 226 |
event_route[nats:Kamailio-World] |
| 227 |
{ |
| 228 |
if ($(natsData{json.parse,Event-Package}) == "dialog") { |
| 229 |
xlog("L_INFO", "received $(natsData{json.parse,Event-Package}) u |
| 230 |
pdate for $(natsData{json.parse,From})"); |
| 231 |
pua_json_publish($natsData); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
event_route[nats:MyQueue1] |
| 236 |
{ |
| 237 |
xlog("L_INFO", "received $(natsData{json.parse,Event-Package}) update fo |
| 238 |
r $(natsData{json.parse,From})"); |
| 239 |
... |
| 240 |
} |