1 <?xml version="1.0" encoding='ISO-8859-1'?>
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
3 "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
5 <!-- Include general documentation entities -->
6 <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
10 <!-- Module User's Guide -->
12 <chapter xmlns:xi="http://www.w3.org/2001/XInclude">
13 <title>&adminguide;</title>
17 <title>Overview</title>
18 <para>The module provides an NSQ consumer for &kamailio; configuration file.
19 You can read more about NSQ at <ulink url="http://nsq.io">nsq.io</ulink>.
22 From a high-level perspective, the module may be used for:
26 Provide a real-time integration into your program, instead of your database, so you can overlay additional logic in your preferred language while also utilizing a message bus
31 Utilize messaging to have a distributed messaging layer, such that machines processing requests/responses/events can go up/down or share the workload and your Kamailio node will still be happy
39 supported operations are:
43 subscribe to a Topic and Channel
49 The NSQ module also has support to publish updates to presence module through the nsq_pua_publish function
54 <title>How it works</title>
56 The module works with a main forked process that does the communication with NSQ for consuming messages. When it consumes a message it defers the process to a worker process so that it doesn't block this main process.
59 <title>event routes</title>
61 The worker process issues an event-route where we can act on the received payload. The name of the event-route is composed by values extracted from the payload.
64 NSQ module will try to execute the event route from most significant to less significant.
65 define the event route like event_route[nsq:consumer-event[-payload_key_value[-payload_subkey_value]]]
68 we can set the key/subkey pair on a subscription base. check the payload on subscribe.
71 <title>define the event route</title>
72 <programlisting format="linespecific">
74 modparam("nsq", "consumer_event_key", "Event-Category")
75 modparam("nsq", "consumer_event_sub_key", "Event-Name")
78 event_route[nsq:consumer-event-presence-update]
80 # presence is the value extracted from Event-Category field in json payload
81 # update is the value extracted from Event-Name field in json payload
82 xlog("L_INFO", "received $(nsqE{nsq.json,Event-Package}) update for $(nsqE{kznsqjson,From})");
86 event_route[nsq:consumer-event-presence]
88 # presence is the value extracted from Event-Category field in json payload
89 xlog("L_INFO", "received $(nsqE{nsq.json,Event-Package}) update for $(nsqE{nsq.json,From})");
93 event_route[nsq:consumer-event-event-category-event-name]
95 # event-category is the name of the consumer_event_key parameter
96 # event-name is the name of the consumer_event_sub_key parameter
97 # this event route is executed if we can't find the previous
101 event_route[nsq:consumer-event-event-category]
103 # event-category is the name of the consumer_event_key parameter
104 # this event route is executed if we can't find the previous
108 event_route[nsq:consumer-event]
110 # this event route is executed if we can't find the previous
117 <title>aknowledge messages</title>
119 Consumed messages have the option of being acknowledge in two ways:
123 immediately when received
128 after processing by the worker
139 <title>Dependencies</title>
141 <title>&kamailio; Modules</title>
143 The following modules must be loaded before this module:
147 <emphasis>none</emphasis>.
154 <title>External Libraries or Applications</title>
158 <emphasis>libev</emphasis>.
163 <emphasis>libjson</emphasis>.
168 <emphasis>libevbuffsock</emphasis>.
173 <emphasis>libnsq</emphasis>.
183 <title>Parameters</title>
185 <title>NSQ related</title>
187 <title><varname>lookupd_address</varname>(str)</title>
189 The nsqlookupd address.
192 <emphasis>Default value is 127.0.0.1</emphasis>
195 <title>Set <varname>lookupd_address</varname> parameter</title>
196 <programlisting format="linespecific">
198 modparam("nsq", "lookupd_address", "nsqlookupd.mydomain.com")
204 <title><varname>lookupd_port</varname>(int)</title>
206 The nsqlookupd TCP port.
209 <emphasis>Default value is 4161.</emphasis>
212 <title>Set <varname>lookupd_port</varname> parameter</title>
213 <programlisting format="linespecific">
215 modparam("nsq", "lookupd_port", 4161)
222 <title><varname>nsqd_address</varname>(str)</title>
224 The nsqd address. You can specify connecting directly to nsqd instead of using nsqlookupd.
227 <emphasis>Default value is 127.0.0.1</emphasis>
230 <title>Set <varname>nsqd_address</varname> parameter</title>
231 <programlisting format="linespecific">
233 modparam("nsq", "nsqd_address", "nsqd.mydomain.com")
239 <title><varname>nsqd_port</varname>(int)</title>
244 <emphasis>Default value is 4150.</emphasis>
247 <title>Set <varname>nsqd_port</varname> parameter</title>
248 <programlisting format="linespecific">
250 modparam("nsq", "nsqd_port", 4150)
257 <title><varname>consumer_use_nsqd</varname>(int)</title>
259 Set to 1 if you'd like to connect to nsqd instead of nsqlookupd.
262 <emphasis>Default value is 0.</emphasis>
265 <title>Set <varname>consumer_use_nsqd</varname> parameter</title>
266 <programlisting format="linespecific">
268 modparam("nsq", "consumer_use_nsqd", 1)
275 <title><varname>consumer_event_key</varname>(str)</title>
277 The default name of the field in json payload to compose the event name 1st part
280 <emphasis>Default value is <quote>Event-Category</quote>.</emphasis>
283 <title>Set <varname>consumer_event_key</varname> parameter</title>
284 <programlisting format="linespecific">
286 modparam("nsq", "consumer_event_key", "My-JSON-Field-Name")
293 <title><varname>consumer_event_sub_key</varname>(str)</title>
295 The default name of the field in json payload to compose the event name 2nd part
298 <emphasis>Default value is <quote>Event-Name</quote>.</emphasis>
301 <title>Set <varname>consumer_event_sub_key</varname> parameter</title>
302 <programlisting format="linespecific">
304 modparam("nsq", "consumer_event_sub_key", "My-JSON-SubField-Name")
311 <title><varname>max_in_flight</varname>(int)</title>
313 The number of messages the consumer can receive before nsqd expects a response.
316 <emphasis>Default value is 1.</emphasis>
319 <title>Set <varname>max_in_flight</varname> parameter</title>
320 <programlisting format="linespecific">
322 modparam("nsq", "max_in_flight", 2)
329 <title><varname>consumer_workers</varname>(int)</title>
331 Number of consumer connections to NSQ per topic_channel.
334 <emphasis>Default value is 4.</emphasis>
337 <title>Set <varname>consumer_workers</varname> parameter</title>
338 <programlisting format="linespecific">
340 modparam("nsq", "consumer_workers", 2)
347 <title><varname>topic_channel</varname>(str)</title>
349 The NSQ Topic and Channel. Delimiter-separated by <quote>:</quote>. It be set multiple times to subscribe to multiple topics and channels. The value of consumer_workers is allocated per topic_channel.
352 <emphasis>Default value is <quote>Kamailio-Topic:Kamailio-Channel</quote>.</emphasis>
355 <title>Set <varname>topic_channel</varname> parameter</title>
356 <programlisting format="linespecific">
358 modparam("nsq", "topic_channel", "My-NSQ-Topic:My-NSQ-Channel")
359 modparam("nsq", "topic_channel", "My-NSQ-Topic-2:My-NSQ-Channel-2")
368 <title>Presence Related</title>
370 <title><varname>db_url</varname>(str)</title>
372 The database for the presentity table.
374 <para>If set, the nsq_pua_publish function will update the presentity status in the database.
377 <emphasis>Default value is <quote>NULL</quote>.</emphasis>
380 <title>Set <varname>db_url</varname> parameter</title>
381 <programlisting format="linespecific">
383 modparam("nsq", "db_url", "&defaultdb;")
390 <title><varname>presentity_table</varname>(str)</title>
392 The name of the presentity table in the database.
395 <emphasis>Default value is <quote>presentity</quote>.</emphasis>
398 <title>Set <varname>presentity_table</varname> parameter</title>
399 <programlisting format="linespecific">
401 modparam("nsq", "presentity_table", "my_presentity_table")
416 <title>Functions</title>
419 <title>Presence Pelated</title>
422 <function moreinfo="none">nsq_pua_publish(json_payload)</function>
425 The function build presentity state from json_payload and updates presentity table.
428 This function can be used from ANY ROUTE.
432 <title><function>nsq_pua_publish</function> usage</title>
433 <programlisting format="linespecific">
435 event_route[nsq:consumer-event-presence-update]
437 xlog("L_INFO", "received $(nsqE{nsq.json,Event-Package}) update for $(nsqE{nsq.json,From})");
438 nsq_pua_publish($nsqE);
439 pres_refresh_watchers("$(nsqE{nsq.json,From})", "$(nsqE{nsq.json,Event-Package})", 1);
452 <title>Exported pseudo-variables</title>
456 <emphasis>$nsqE</emphasis>
457 Contains the payload of a consumed message
464 <title>Transformations</title>
465 <para>The prefix for nsq transformations is nsq.</para>
468 <emphasis>json</emphasis>
471 <title><function>nsq.json</function> usage</title>
472 <programlisting format="linespecific">
474 $var(Custom-Data) = $(nsqE{nsq.json,Custom-Data});
475 if($var(Custom-Data) != $null) {
476 xlog("L_INFO", "$ci|log|custom data: $var(Custom-Data)");