Sending & Receiving Messages¶
Introduction¶
The messenger module offers two ways to send and/or receive messages across the network:
- Messages:
send_message_to()
/wait_for_message_from()
/send_reply_to()
- News:
send_news_to()
/wait_for_news_from()
Functions¶
Sending Messages¶
Messages are exchanged in a strict request / reply sequence. This restricts flexibility but makes the semantics obvious. A message is sent to an address and the process waits until a reply is received. Likewise, a message is awaited from an address and a reply must be sent before another message can be received.
The data sent as part of the message will be serialised as JSON so any object supported by the Python JSON library is a candidate for message payload.
Any number of processes can send a message to one process which is listening. The messages are sent in a strict request-reply sequence so no ambiguity should occur.
-
networkzero.messenger.
send_message_to
(address, message=None, wait_for_reply_s=<Forever>)¶ Send a message and return the reply
Parameters: - address – a nw0 address (eg from nw0.discover)
- message – any simple Python object, including text & tuples
- wait_for_reply_s – how many seconds to wait for a reply [default: forever]
Returns: the reply returned from the address or None if out of time
-
networkzero.messenger.
wait_for_message_from
(address, wait_for_s=<Forever>, autoreply=False)¶ Wait for a message
Parameters: - address – a nw0 address (eg from nw0.advertise)
- wait_for_s – how many seconds to wait for a message before giving up [default: forever]
- autoreply – whether to send an empty reply [default: No]
Returns: the message received from another address or None if out of time
-
networkzero.messenger.
send_reply_to
(address, reply=None)¶ Reply to a message previously received
Parameters: - address – a nw0 address (eg from nw0.advertise)
- reply – any simple Python object, including text & tuples
Sending News¶
News (also known as publications & subscriptions or “pubsub”) allows multiple processes to wait for messages from one (or more) processes. This is not possible in the message-sending functionality above. There is a notion of “topics” which allow a publisher to produce a broader range of news to which a subscriber need only listen for some.
Since a wildcard filter can be used for the topic, the topic used is returned along with the data when news is received.
-
networkzero.messenger.
send_news_to
(address, topic, data=None)¶ Publish news to all subscribers
Parameters: - address – a nw0 address, eg from nw0.advertise
- topic – any text object
- data – any simple Python object including test & tuples [default: empty]
-
networkzero.messenger.
wait_for_news_from
(address, prefix='', wait_for_s=<Forever>)¶ Wait for news whose topic starts with prefix.
Parameters: - address – a nw0 address, eg from nw0.discover
- prefix – any text object [default: all messages]
- wait_for_s – how many seconds to wait before giving up [default: forever]
Returns: a 2-tuple of (topic, data) or (None, None) if out of time