Skip to content
Go to file

Latest commit

Problem: If a peer with an unknown uuid connects, and there was known peer with the same endpoint, the previous peer is purged silently.

By removing the peer explicitly, the Pyre user receives an EXIT event for the purged peer and allows proper cleanup.

Git stats


Failed to load latest commit information.


This is a Python port of Zyre 1.0, implementing the same ZRE protocol.

Pyre - an open-source framework for proximity-based peer-to-peer applications


Pyre does local area discovery and clustering. A Pyre node broadcasts UDP beacons, and connects to peers that it finds. This class wraps a Pyre node with a message-based API.

All incoming events are messages delivered via the recv call of a Pyre instance. The first frame defines the type of the message, and following frames provide further values:

ENTER fromnode headers
    a new peer has entered the network
EXIT fromnode
    a peer has left the network
JOIN fromnode groupname
    a peer has joined a specific group
LEAVE fromnode groupname
    a peer has joined a specific group
WHISPER fromnode message
    a peer has sent this node a message
SHOUT fromnode groupname message
    a peer has sent one of our groups a message

In SHOUT and WHISPER the message is a single frame in this version of Pyre. In ENTER, the headers frame contains a packed dictionary, that can be unpacked using json.loads(msg) (see chat client).

To join or leave a group, use the join() and leave() methods. To set a header value, use the set_header() method. To send a message to a single peer, use whisper(). To send a message to a group, use shout().


For now use Pip:

pip install


import pyre
#  Constructor, creates a new Zyre node. Note that until you start the
#  node it is silent and invisible to other nodes on the network.
node = pyre.Pyre()

#  Set node header; these are provided to other nodes during discovery
#  and come in each ENTER message.
node.set_header(name, value)

#  (TODO: Currently a Pyre node starts immediately) Start node, after setting header values. When you start a node it
#  begins discovery and connection.

#  Stop node, this signals to other peers that this node will go away.
#  This is polite; however you can also just destroy the node without
#  stopping it.

#  Join a named group; after joining a group you can send messages to
#  the group and all Zyre nodes in that group will receive them.

#  Leave a group

#  Receive next message from network; the message may be a control
#  message (ENTER, EXIT, JOIN, LEAVE) or data (WHISPER, SHOUT).
#  Returns a list of message frames
msgs = node.recv();

# Send message to single peer, specified as a UUID object (import uuid)
# Destroys message after sending
node.whisper(peer, msg)

# Send message to a named group
# Destroys message after sending
node.shout(group, msg);

#  Send string to single peer specified as a UUID string.
#  String is formatted using printf specifiers.
node.whispers(peer, msg_string)

#  Send message to a named group
#  Destroys message after sending
node.shouts(group, msg_string);
#  Return handle to the Zyre node, for polling
# use node.get_socket().getsockopt(zmq.FD) to acquire 
# the filedescriptor
# Don't use this for getting Pyre events you can use the 
# node.inbox to get those events

Example Chat Client

    from zyre_pyzmq import Zyre as Pyre
except Exception as e:
    print("using Python native module", e)
    from pyre import Pyre 

from pyre import zhelper 
import zmq 
import uuid
import logging
import sys
import json

def chat_task(ctx, pipe):
    n = Pyre("CHAT")
    n.set_header("CHAT_Header1","example header1")
    n.set_header("CHAT_Header2","example header2")

    poller = zmq.Poller()
    poller.register(pipe, zmq.POLLIN)
    poller.register(n.socket(), zmq.POLLIN)
        items = dict(poller.poll())
        print(n.socket(), items)
        if pipe in items and items[pipe] == zmq.POLLIN:
            message = pipe.recv()
            # message to quit
            if message.decode('utf-8') == "$$STOP":
            print("CHAT_TASK: %s" % message)
            n.shouts("CHAT", message.decode('utf-8'))
        #if n.socket() in items and items[n.socket()] == zmq.POLLIN:
            cmds = n.recv()
            msg_type = cmds.pop(0)
            print("NODE_MSG TYPE: %s" % msg_type)
            print("NODE_MSG PEER: %s" % uuid.UUID(bytes=cmds.pop(0)))
            print("NODE_MSG NAME: %s" % cmds.pop(0))
            if msg_type.decode('utf-8') == "SHOUT":
                print("NODE_MSG GROUP: %s" % cmds.pop(0))
            elif msg_type.decode('utf-8') == "ENTER":
                headers = json.loads(cmds.pop(0).decode('utf-8'))
                print("NODE_MSG HEADERS: %s" % headers)
                for key in headers:
                    print("key = {0}, value = {1}".format(key, headers[key]))
            print("NODE_MSG CONT: %s" % cmds)

if __name__ == '__main__':
    # Create a StreamHandler for debugging
    logger = logging.getLogger("pyre")
    logger.propagate = False

    ctx = zmq.Context()
    chat_pipe = zhelper.zthread_fork(ctx, chat_task)
    # input in python 2 is different
    if sys.version_info.major < 3:
        input = raw_input

    while True:
            msg = input()
        except (KeyboardInterrupt, SystemExit):

Look at the ZOCP project for examples of how Pyre can be integrated into different environments and frameworks, i.e.:

Pyre uses the Python Logging module. To change the debug level:

    # Create a StreamHandler for debugging
    logger = logging.getLogger("pyre")
    # i.e. logging.DEBUG, logging.WARNING
    logger.propagate = False


Python only needs PyZMQ. On some older versions of Python it also needs the ipaddress module.

The recommended Python version is 3.3+

Project Organization

Pyre is owned by all its authors and contributors. This is an open source project licensed under the LGPLv3. To contribute to Zyre please read the C4.1 process that we use.

To report an issue, use the PYRE issue tracker at


Python port of Zyre




No releases published


No packages published