1
1
.. _box-watchers :
2
2
3
- --------------------------------------------------------------------------------
4
3
Event watchers
5
- --------------------------------------------------------------------------------
4
+ ==============
6
5
7
- The ``box `` module contains some functions related to event subscriptions, also known as watchers.
8
- The subscriptions are used to inform a client about server-side events.
9
- To read more about the built-in events in Tarantool,
10
- check the :doc: `pub/sub </reference/reference_lua/box_events/pub-sub >` section.
6
+ The ``box `` module contains some features related to event subscriptions, also known as :term: `watchers <watcher> `.
7
+ The subscriptions are used to inform a client about server-side :term: `events <event> `.
8
+ Each event subscription is defined by a certain key.
11
9
12
10
.. glossary ::
13
11
14
- Watcher
15
- A watcher is a :doc: ` callback < /book/box/triggers >` that is invoked when a state change occurs.
16
- To register a local watcher, use the `` box.watch() `` function .
17
- To create a remote watcher, user the `` conn:watch() `` function.
18
- Note that it is possible to register more than one watcher for the same key .
12
+ Event
13
+
14
+ An event is a state change or a system update that triggers the action of other systems .
15
+ To read more about the built-in events in Tarantool,
16
+ check the :doc: ` system events < /reference/reference_lua/box_events/system_events >` section .
19
17
20
18
State
21
19
A state is an internally stored key-value pair.
22
20
The key is a string.
23
21
The value is an arbitrary type that can be encoded as MsgPack.
24
22
To update a state, use the ``box.broadcast() `` function.
25
23
26
- First, a watcher callback is invoked after the watcher registration.
24
+ Watcher
25
+ A watcher is a :doc: `callback </book/box/triggers >` that is invoked when a state change occurs.
26
+ To register a local watcher, use the ``box.watch() `` function.
27
+ To create a remote watcher, use the ``watch() `` function from the ``net.box `` module.
28
+ Note that it is possible to register more than one watcher for the same key.
29
+
30
+ How the watcher works
31
+ ---------------------
32
+
33
+ First, you register a watcher.
34
+ After that, the watcher callback is invoked for the first time.
27
35
In this case, the callback is triggered whether or not the key has already been broadcast.
28
- After that, :doc: `box.broadcast() </reference/reference_lua/box_watchers /broadcast >` called on the remote host
29
- triggers all subsequent invocations .
36
+ All subsequent invocations are triggered with the :doc: `box.broadcast() </reference/reference_lua/box_events /broadcast >`
37
+ called on the remote host .
30
38
If a watcher is subscribed for a key that has not been broadcast yet, the callback is triggered only once,
31
39
after the registration of the watcher.
32
40
33
- A watcher callback is passed the name of the key for which it was registered and the current key data.
34
- A watcher callback is always invoked in a new fiber. It means that is is okay to yield in it.
41
+ The watcher callback takes two arguments.
42
+ The first argument is a name of the key for which it was registered.
43
+ The second one contains current key data.
44
+ The callback is always invoked in a new fiber. It means that is is okay to yield in it.
35
45
A watcher callback is never executed in parallel with itself.
36
46
If the key is updated while the watcher callback is running, the callback will be invoked again with the new
37
47
value as soon as it returns.
38
48
39
49
``box.watch `` and ``box.broadcast `` functions can be used before :doc: `box.cfg </reference/reference_lua/box_cfg >`.
40
50
41
- .. note ::
42
-
43
- Keep in mind that garbage collection of a watcher handle doesn't lead to the watcher's destruction.
44
- In this case, the watcher remains registered.
45
- It is okay to discard the result of ``watch `` function if the watcher will never be unregistered.
46
-
47
51
Below is a list of all functions and members related to watchers or events.
48
52
49
53
.. container :: table
@@ -58,21 +62,21 @@ Below is a list of all functions and members related to watchers or events.
58
62
* - Name
59
63
- Use
60
64
61
- * - :doc: `./box_watchers /watch `
65
+ * - :doc: `./box_events /watch `
62
66
- Create a local watcher.
63
67
64
68
* - :ref: `conn:watch() <conn-watch >`
65
69
- Create a watcher for the remote host.
66
70
67
- * - :doc: `./box_watchers /broadcast `
71
+ * - :doc: `./box_events /broadcast `
68
72
- Update a state.
69
73
70
- * - :ref: `˜Built-in events <pubsub >`
71
- - Pub/sub system and predefined events
74
+ * - :ref: `˜Built-in events <system-events >`
75
+ - Predefined events in Tarantool
72
76
73
77
.. toctree ::
74
78
:hidden:
75
79
76
80
box_events/watch
77
81
box_events/broadcast
78
- box_events/pub-sub
82
+ box_events/system_events
0 commit comments