1
- .. _net_box-module :
1
+ zc .. _net_box-module:
2
2
3
3
--------------------------------------------------------------------------------
4
4
Module net.box
@@ -125,14 +125,16 @@ Below is a list of all ``net.box`` functions.
125
125
* - :ref: `conn:call() <net_box-call >`
126
126
- Call a stored procedure
127
127
* - :ref: `conn:timeout() <conn-timeout >`
128
- - Set a timeout
128
+ - Set a timeout
129
+ * - :ref: `conn:watch() <conn-watch >`
130
+ - Subscribe to events broadcast by a remote host
129
131
* - :ref: `conn:on_connect() <net_box-on_connect >`
130
132
- Define a connect trigger
131
133
* - :ref: `conn:on_disconnect() <net_box-on_disconnect >`
132
134
- Define a disconnect trigger
133
135
* - :ref: `conn:on_schema_reload() <net_box-on_schema_reload >`
134
136
- Define a trigger when schema is modified
135
- * - :ref: `conn:new_stream() <conn-new_stream >`
137
+ * - :ref: `conn:new_stream() <conn-new_stream >`
136
138
- Create a stream
137
139
* - :ref: `stream:begin() <net_box-stream_begin >`
138
140
- Begin a stream transaction
@@ -216,7 +218,7 @@ Below is a list of all ``net.box`` functions.
216
218
support the specified features, the connection will fail with an error message.
217
219
With ``required_protocol_features = {'transactions'} ``, all connections fail where the
218
220
server has ``transactions: false ``.
219
-
221
+
220
222
.. container :: table
221
223
222
224
.. list-table ::
@@ -240,7 +242,7 @@ Below is a list of all ``net.box`` functions.
240
242
- IPROTO_FEATURE_ERROR_EXTENSION
241
243
- 2 and newer
242
244
* - ``watchers ``
243
- - Requires remote watchers support on the server
245
+ - Requires remote :ref: ` watchers < conn-watch >` support on the server
244
246
- IPROTO_FEATURE_WATCHERS
245
247
- 3 and newer
246
248
@@ -533,7 +535,61 @@ Below is a list of all ``net.box`` functions.
533
535
- B
534
536
...
535
537
538
+ .. _conn-watch :
539
+
540
+ .. method :: watch(key, func)
541
+
542
+ Subscribe to events broadcast by a remote host.
543
+
544
+ :param string key: a key name of an event to subscribe to
545
+ :param function func: a callback to invoke when the key value is updated
546
+ :return: a watcher handle. The handle consists of one method -- ``unregister() ``, which unregisters the watcher.
547
+
548
+ To read more about watchers, see the :ref: `Functions for watchers <box-watchers >` section.
549
+
550
+ The method has the same syntax as the :doc: `box.watch() </reference/reference_lua/box_events/broadcast >`
551
+ function, which is used for subscribing to events locally.
552
+
553
+ Watchers survive reconnection (see the ``reconnect_after `` connection :ref: `option <net_box-new >`).
554
+ All registered watchers are automatically resubscribed when the
555
+ connection is reestablished.
556
+
557
+ If a remote host supports watchers, the ``watchers `` key will be set in the
558
+ connection ``peer_protocol_features ``.
559
+ For details, check the :ref: `net.box features table <net_box-new >`.
560
+
561
+ .. note ::
562
+
563
+ Keep in mind that garbage collection of a watcher handle doesn't lead to the watcher's destruction.
564
+ In this case, the watcher remains registered.
565
+ It is okay to discard the result of ``watch `` function if the watcher will never be unregistered.
566
+
567
+ **Example: **
568
+
569
+ Server:
570
+
571
+ .. code-block :: lua
572
+
573
+ -- Broadcast value 42 for the 'foo' key.
574
+ box.broadcast('foo', 42)
575
+
576
+ Client:
577
+
578
+ .. code-block :: lua
579
+
580
+ conn = net.box.connect(URI)
581
+ local log = require('log')
582
+ -- Subscribe to updates of the 'foo' key.
583
+ w = conn:watch('foo', function(key, value)
584
+ assert(key == 'foo')
585
+ log.info("The box.id value is '%d'", value)
586
+ end)
587
+
588
+ If you don't need the watcher anymore, you can unregister it using the command below:
589
+
590
+ .. code-block :: lua
536
591
592
+ w:unregister()
537
593
538
594
.. _conn-timeout :
539
595
0 commit comments