| 1 |
NSQ Module |
| 2 |
|
| 3 |
Weave Communications |
| 4 |
|
| 5 |
<[email protected]> |
| 6 |
|
| 7 |
Edited by |
| 8 |
|
| 9 |
Emmanuel Schmidbauer |
| 10 |
|
| 11 |
<[email protected]> |
| 12 |
|
| 13 |
Copyright © 2016 Weave Communications |
| 14 |
__________________________________________________________________ |
| 15 |
|
| 16 |
Table of Contents |
| 17 |
|
| 18 |
1. Admin Guide |
| 19 |
|
| 20 |
1. Overview |
| 21 |
2. How it works |
| 22 |
|
| 23 |
2.1. Acknowledge Messages |
| 24 |
|
| 25 |
3. Dependencies |
| 26 |
|
| 27 |
3.1. Kamailio Modules |
| 28 |
3.2. External Libraries or Applications |
| 29 |
|
| 30 |
4. Parameters |
| 31 |
|
| 32 |
4.1. lookupd_address(str) |
| 33 |
4.2. lookupd_port(int) |
| 34 |
4.3. nsqd_address(str) |
| 35 |
4.4. nsqd_port(int) |
| 36 |
4.5. consumer_use_nsqd(int) |
| 37 |
4.6. consumer_event_key(str) |
| 38 |
4.7. consumer_event_subkey(str) |
| 39 |
4.8. max_in_flight(int) |
| 40 |
4.9. consumer_workers(int) |
| 41 |
4.10. topic_channel(str) |
| 42 |
|
| 43 |
5. Pseudo Variables |
| 44 |
6. Event Routes |
| 45 |
|
| 46 |
List of Examples |
| 47 |
|
| 48 |
1.1. Set lookupd_address parameter |
| 49 |
1.2. Set lookupd_port parameter |
| 50 |
1.3. Set nsqd_address parameter |
| 51 |
1.4. Set nsqd_port parameter |
| 52 |
1.5. Set consumer_use_nsqd parameter |
| 53 |
1.6. Set consumer_event_key parameter |
| 54 |
1.7. Set consumer_event_subkey parameter |
| 55 |
1.8. Set max_in_flight parameter |
| 56 |
1.9. Set consumer_workers parameter |
| 57 |
1.10. Set topic_channel parameter |
| 58 |
1.11. Example usage of $nsqE pseudo variable |
| 59 |
1.12. Define the event routes |
| 60 |
|
| 61 |
Chapter 1. Admin Guide |
| 62 |
|
| 63 |
Table of Contents |
| 64 |
|
| 65 |
1. Overview |
| 66 |
2. How it works |
| 67 |
|
| 68 |
2.1. Acknowledge Messages |
| 69 |
|
| 70 |
3. Dependencies |
| 71 |
|
| 72 |
3.1. Kamailio Modules |
| 73 |
3.2. External Libraries or Applications |
| 74 |
|
| 75 |
4. Parameters |
| 76 |
|
| 77 |
4.1. lookupd_address(str) |
| 78 |
4.2. lookupd_port(int) |
| 79 |
4.3. nsqd_address(str) |
| 80 |
4.4. nsqd_port(int) |
| 81 |
4.5. consumer_use_nsqd(int) |
| 82 |
4.6. consumer_event_key(str) |
| 83 |
4.7. consumer_event_subkey(str) |
| 84 |
4.8. max_in_flight(int) |
| 85 |
4.9. consumer_workers(int) |
| 86 |
4.10. topic_channel(str) |
| 87 |
|
| 88 |
5. Pseudo Variables |
| 89 |
6. Event Routes |
| 90 |
|
| 91 |
1. Overview |
| 92 |
|
| 93 |
The module provides an NSQ consumer for Kamailio configuration file. |
| 94 |
NSQ is a real time distributed messaging platform, more details about |
| 95 |
it can be found at nsq.io. |
| 96 |
|
| 97 |
From a high-level perspective, the module may be used for: |
| 98 |
* Provide a real-time integration with you Kamailio configuration |
| 99 |
file, which can be used as alternative to interact with a database, |
| 100 |
allowing to overlay additional logic in your preferred language |
| 101 |
while utilizing a message bus. |
| 102 |
* Rely on a distributed messaging layer, such that machines |
| 103 |
processing requests/responses/events can go up/down or share the |
| 104 |
workload, without impacting Kamailio's activity. |
| 105 |
|
| 106 |
Supported NSQ operations are: |
| 107 |
* Subscribe to a Topic and Channel |
| 108 |
|
| 109 |
IMPORTANT The `nsq.json` transformation is deprecated in favor of the |
| 110 |
json module's `json.parse` transformation. The `nsq_pua_publish()` |
| 111 |
function is deprecated in favor of pua_json module's |
| 112 |
`pua_json_publish()` function. |
| 113 |
|
| 114 |
2. How it works |
| 115 |
|
| 116 |
2.1. Acknowledge Messages |
| 117 |
|
| 118 |
The module creates an additional NSQ manager process that does the |
| 119 |
communication with NSQ for consuming messages. This one defers the |
| 120 |
message for processing to other NSQ worker process so that it doesn't |
| 121 |
block itself, nor the SIP worker processes. |
| 122 |
|
| 123 |
2.1. Acknowledge Messages |
| 124 |
|
| 125 |
Consumed messages have the option of being acknowledge in two ways: |
| 126 |
* immediately when received |
| 127 |
* after processing by the worker |
| 128 |
|
| 129 |
3. Dependencies |
| 130 |
|
| 131 |
3.1. Kamailio Modules |
| 132 |
3.2. External Libraries or Applications |
| 133 |
|
| 134 |
3.1. Kamailio Modules |
| 135 |
|
| 136 |
The following modules must be loaded before this module: |
| 137 |
* none. |
| 138 |
|
| 139 |
3.2. External Libraries or Applications |
| 140 |
|
| 141 |
The following libraries or applications must be installed |
| 142 |
* libev. |
| 143 |
* libjson. |
| 144 |
* libevbuffsock. |
| 145 |
* libnsq. |
| 146 |
|
| 147 |
4. Parameters |
| 148 |
|
| 149 |
4.1. lookupd_address(str) |
| 150 |
4.2. lookupd_port(int) |
| 151 |
4.3. nsqd_address(str) |
| 152 |
4.4. nsqd_port(int) |
| 153 |
4.5. consumer_use_nsqd(int) |
| 154 |
4.6. consumer_event_key(str) |
| 155 |
4.7. consumer_event_subkey(str) |
| 156 |
4.8. max_in_flight(int) |
| 157 |
4.9. consumer_workers(int) |
| 158 |
4.10. topic_channel(str) |
| 159 |
|
| 160 |
4.1. lookupd_address(str) |
| 161 |
|
| 162 |
The nsqlookupd address. |
| 163 |
|
| 164 |
Usage: nsq related. |
| 165 |
|
| 166 |
Default value is 127.0.0.1 |
| 167 |
|
| 168 |
Example 1.1. Set lookupd_address parameter |
| 169 |
... |
| 170 |
modparam("nsq", "lookupd_address", "nsqlookupd.mydomain.com") |
| 171 |
... |
| 172 |
|
| 173 |
4.2. lookupd_port(int) |
| 174 |
|
| 175 |
The nsqlookupd TCP port. |
| 176 |
|
| 177 |
Usage: nsq related. |
| 178 |
|
| 179 |
Default value is 4161. |
| 180 |
|
| 181 |
Example 1.2. Set lookupd_port parameter |
| 182 |
... |
| 183 |
modparam("nsq", "lookupd_port", 4161) |
| 184 |
... |
| 185 |
|
| 186 |
4.3. nsqd_address(str) |
| 187 |
|
| 188 |
The nsqd address. You can specify connecting directly to nsqd instead |
| 189 |
of using nsqlookupd. |
| 190 |
|
| 191 |
Usage: nsq related. |
| 192 |
|
| 193 |
Default value is 127.0.0.1 |
| 194 |
|
| 195 |
Example 1.3. Set nsqd_address parameter |
| 196 |
... |
| 197 |
modparam("nsq", "nsqd_address", "nsqd.mydomain.com") |
| 198 |
... |
| 199 |
|
| 200 |
4.4. nsqd_port(int) |
| 201 |
|
| 202 |
The nsqd TCP port. |
| 203 |
|
| 204 |
Usage: nsq related. |
| 205 |
|
| 206 |
Default value is 4150. |
| 207 |
|
| 208 |
Example 1.4. Set nsqd_port parameter |
| 209 |
... |
| 210 |
modparam("nsq", "nsqd_port", 4150) |
| 211 |
... |
| 212 |
|
| 213 |
4.5. consumer_use_nsqd(int) |
| 214 |
|
| 215 |
Set to 1 if you'd like to connect to nsqd instead of nsqlookupd. |
| 216 |
|
| 217 |
Usage: nsq related. |
| 218 |
|
| 219 |
Default value is 0. |
| 220 |
|
| 221 |
Example 1.5. Set consumer_use_nsqd parameter |
| 222 |
... |
| 223 |
modparam("nsq", "consumer_use_nsqd", 1) |
| 224 |
... |
| 225 |
|
| 226 |
4.6. consumer_event_key(str) |
| 227 |
|
| 228 |
The default name of the field in json payload to compose the event name |
| 229 |
1st part |
| 230 |
|
| 231 |
Usage: nsq related. |
| 232 |
|
| 233 |
Default value is “Event-Category”. |
| 234 |
|
| 235 |
Example 1.6. Set consumer_event_key parameter |
| 236 |
... |
| 237 |
modparam("nsq", "consumer_event_key", "My-JSON-Field-Name") |
| 238 |
... |
| 239 |
|
| 240 |
4.7. consumer_event_subkey(str) |
| 241 |
|
| 242 |
The default name of the field in json payload to compose the event name |
| 243 |
2nd part |
| 244 |
|
| 245 |
Usage: nsq related. |
| 246 |
|
| 247 |
Default value is “Event-Name”. |
| 248 |
|
| 249 |
Example 1.7. Set consumer_event_subkey parameter |
| 250 |
... |
| 251 |
modparam("nsq", "consumer_event_subkey", "My-JSON-SubField-Name") |
| 252 |
... |
| 253 |
|
| 254 |
4.8. max_in_flight(int) |
| 255 |
|
| 256 |
The number of messages the consumer can receive before nsqd expects a |
| 257 |
response. |
| 258 |
|
| 259 |
Usage: nsq related. |
| 260 |
|
| 261 |
Default value is 1. |
| 262 |
|
| 263 |
Example 1.8. Set max_in_flight parameter |
| 264 |
... |
| 265 |
modparam("nsq", "max_in_flight", 2) |
| 266 |
... |
| 267 |
|
| 268 |
4.9. consumer_workers(int) |
| 269 |
|
| 270 |
Number of consumer connections to NSQ per topic_channel. |
| 271 |
|
| 272 |
Usage: nsq related. |
| 273 |
|
| 274 |
Default value is 4. |
| 275 |
|
| 276 |
Example 1.9. Set consumer_workers parameter |
| 277 |
... |
| 278 |
modparam("nsq", "consumer_workers", 2) |
| 279 |
... |
| 280 |
|
| 281 |
4.10. topic_channel(str) |
| 282 |
|
| 283 |
The NSQ Topic and Channel. Delimiter-separated by “:”. It be set |
| 284 |
multiple times to subscribe to multiple topics and channels. The value |
| 285 |
of consumer_workers is allocated per topic_channel. |
| 286 |
|
| 287 |
Usage: nsq related. |
| 288 |
|
| 289 |
Default value is “Kamailio-Topic:Kamailio-Channel”. |
| 290 |
|
| 291 |
Example 1.10. Set topic_channel parameter |
| 292 |
... |
| 293 |
modparam("nsq", "topic_channel", "My-NSQ-Topic:My-NSQ-Channel") |
| 294 |
modparam("nsq", "topic_channel", "My-NSQ-Topic-2:My-NSQ-Channel-2") |
| 295 |
... |
| 296 |
|
| 297 |
5. Pseudo Variables |
| 298 |
|
| 299 |
Example 1.11. Example usage of $nsqE pseudo variable |
| 300 |
xlog("L_INFO", "received payload $nsqE from NSQ"); |
| 301 |
... |
| 302 |
} |
| 303 |
|
| 304 |
* $nsqE Contains the payload of a consumed message |
| 305 |
|
| 306 |
6. Event Routes |
| 307 |
|
| 308 |
The worker process issues an event-route where we can act on the |
| 309 |
received payload. The name of the event-route is composed by values |
| 310 |
extracted from the payload. |
| 311 |
|
| 312 |
NSQ module will try to execute the event route from most significant to |
| 313 |
less significant. define the event route like |
| 314 |
event_route[nsq:consumer-event[-payload_key_value[-payload_subkey_value |
| 315 |
]]] |
| 316 |
|
| 317 |
We can set the key/subkey pair on a subscription base. check the |
| 318 |
payload on subscribe. |
| 319 |
|
| 320 |
Example 1.12. Define the event routes |
| 321 |
... |
| 322 |
modparam("nsq", "consumer_event_key", "Event-Category") |
| 323 |
modparam("nsq", "consumer_event_subkey", "Event-Name") |
| 324 |
... |
| 325 |
|
| 326 |
event_route[nsq:consumer-event-presence-update] |
| 327 |
{ |
| 328 |
# presence is the value extracted from Event-Category field in json payl |
| 329 |
oad |
| 330 |
# update is the value extracted from Event-Name field in json payload |
| 331 |
if ($(nsqE{json.parse,Event-Package}) == "dialog") { |
| 332 |
xlog("L_INFO", "received $(nsqE{json.parse,Event-Package}) updat |
| 333 |
e for $(nsqE{json.parse,From})"); |
| 334 |
pua_json_publish($nsqE); |
| 335 |
} |
| 336 |
... |
| 337 |
} |
| 338 |
|
| 339 |
event_route[nsq:consumer-event-presence] |
| 340 |
{ |
| 341 |
# presence is the value extracted from Event-Category field in json payl |
| 342 |
oad |
| 343 |
xlog("L_INFO", "received $(nsqE{json.parse,Event-Package}) update for $( |
| 344 |
nsqE{json.parse,From})"); |
| 345 |
... |
| 346 |
} |
| 347 |
|
| 348 |
event_route[nsq:consumer-event-event-category-event-name] |
| 349 |
{ |
| 350 |
# event-category is the name of the consumer_event_key parameter |
| 351 |
# event-name is the name of the consumer_event_subkey parameter |
| 352 |
# this event route is executed if we can't find the previous |
| 353 |
... |
| 354 |
} |
| 355 |
|
| 356 |
event_route[nsq:consumer-event-event-category] |
| 357 |
{ |
| 358 |
# event-category is the name of the consumer_event_key parameter |
| 359 |
# this event route is executed if we can't find the previous |
| 360 |
... |
| 361 |
} |
| 362 |
|
| 363 |
event_route[nsq:consumer-event] |
| 364 |
{ |
| 365 |
# this event route is executed if we can't find the previous |
| 366 |
} |