doc/book/en/devrepo/repo/hooks.rst
changeset 8211 543e1579ba0d
parent 8190 2a3c1b787688
child 8483 4ba11607d84a
equal deleted inserted replaced
8210:1d1cfc97f6b9 8211:543e1579ba0d
   160 
   160 
   161 A more realistic example can be found in the advanced tutorial chapter
   161 A more realistic example can be found in the advanced tutorial chapter
   162 :ref:`adv_tuto_security_propagation`.
   162 :ref:`adv_tuto_security_propagation`.
   163 
   163 
   164 
   164 
       
   165 Inter-instance communication
       
   166 ----------------------------
       
   167 
       
   168 If your application consists of several instances, you may need some means to
       
   169 communicate between them.  Cubicweb provides a publish/subscribe mechanism
       
   170 using ØMQ_.  In order to use it, use
       
   171 :meth:`~cubicweb.server.cwzmq.ZMQComm.add_subscription` on the
       
   172 `repo.app_instances_bus` object.  The `callback` will get the message (as a
       
   173 list).  A message can be sent by calling
       
   174 :meth:`~cubicweb.server.cwzmq.ZMQComm.publish` on `repo.app_instances_bus`.
       
   175 The first element of the message is the topic which is used for filtering and
       
   176 dispatching messages.
       
   177 
       
   178 .. _ØMQ: http://www.zeromq.org/
       
   179 
       
   180 .. sourcecode:: python
       
   181 
       
   182   class FooHook(hook.Hook):
       
   183       events = ('server_startup',)
       
   184       __regid__ = 'foo_startup'
       
   185 
       
   186       def __call__(self):
       
   187           def callback(msg):
       
   188               self.info('received message: %s', ' '.join(msg))
       
   189           self.repo.app_instances_bus.subscribe('hello', callback)
       
   190 
       
   191 .. sourcecode:: python
       
   192 
       
   193   def do_foo(self):
       
   194       actually_do_foo()
       
   195       self._cw.repo.app_instances_bus.publish(['hello', 'world'])
       
   196 
       
   197 The `zmq-address-pub` configuration variable contains the address used
       
   198 by the instance for sending messages, e.g. `tcp://*:1234`.  The
       
   199 `zmq-address-sub` variable contains a comma-separated list of addresses
       
   200 to listen on, e.g. `tcp://localhost:1234, tcp://192.168.1.1:2345`.
       
   201 
       
   202 
   165 Hooks writing tips
   203 Hooks writing tips
   166 ------------------
   204 ------------------
   167 
   205 
   168 Reminder
   206 Reminder
   169 ~~~~~~~~
   207 ~~~~~~~~