cd education/nng
python -m venv .env
Windows:
.env\Scripts\activate
Mac/Linux:
source .env/bin/activate
pip install pynng
Mac
brew install nng
Linux
sudo apt-get install nng
To navigate into a specific communication protocol folder, use one of the following commands:
cd pubsub
cd pushpull
cd reqrep
Open two terminal windows and run the appropriate file in each one:
- Pub/Sub: Run
publisher.py
first, thensubscriber.py
. - Req/Rep: Run
reply.py
first, thenrequest.py
. - Push/Pull: Run
push.py
first, thenpull.py
.
Pub/Sub (Publish/Subscribe) is a messaging pattern where a publisher sends messages to specific topics, and subscribers who have subscribed to those topics receive the messages.
# pubsub/publisher.py
import pynng
import time
with pynng.Pub0() as pub:
pub.listen("tcp://127.0.0.1:5001")
while True:
pub.send(b"Hello, from Sidepit!")
time.sleep(1)
# pubsub/subscriber.py
import pynng
with pynng.Sub0() as sub:
sub.dial("tcp://127.0.0.1:5001")
sub.subscribe(b"")
while True:
message = sub.recv()
print(f"Received: {message.decode()}")
# Open the first terminal and run this command FIRST:
nngcat --pub --listen tcp://127.0.0.1:5001 --data "cuckoo" --interval 1
# Open the second terminal and run this command SECOND:
nngcat --sub --dial tcp://127.0.0.1:5001 --quoted
Req/Rep (Request/Reply) is a messaging pattern where a client sends a request to a server, and the server processes the request and sends back a reply.
# reqrep/request.py
import pynng
import time
with pynng.Req0() as req:
req.dial("tcp://127.0.0.1:5001")
while True:
req.send(b"Send me a message...")
reply = req.recv()
print(reply.decode())
time.sleep(1)
# reqrep/reply.py
import pynng
with pynng.Rep0() as rep:
rep.listen("tcp://127.0.0.1:5001")
while True:
received_message = rep.recv()
print(received_message.decode())
rep.send(b"Hello, from Sidepit!")
# Open the first terminal and run this command FIRST:
nngcat --rep --listen tcp://127.0.0.1:5001 --data "42" --quoted
# Open the second terminal and run this command SECOND:
nngcat --req --dial tcp://127.0.0.1:5001 --data "what is the answer?" --quoted
Push/Pull is a messaging pattern often used for task distribution where a producer (or pusher) sends messages (tasks or jobs) to workers (or pullers). The workers pull these messages and process them.
# pushpull/push.py
import pynng
with pynng.Push0() as push:
push.listen("tcp://127.0.0.1:5001")
while True:
push.send(b"Hello from push")
# pushpull/pull.py
import pynng
import time
with pynng.Pull0() as pull:
pull.dial("tcp://127.0.0.1:5001")
while True:
received_message = pull.recv()
print(received_message.decode())
time.sleep(1)
# Open the first terminal and run this command FIRST:
nngcat --push --listen tcp://127.0.0.1:5001 --data "Here is a job for you..."
# Open the second terminal and run this command SECOND:
nngcat --pull --dial tcp://127.0.0.1:5001 --quoted
- listen(): Opens a socket to listen for incoming connections or messages.
- dial(): Initiates a connection to a remote socket by dialing to it.
- subscribe(): Subscribes to messages from a specific set of topics. Used by subscribers in a Pub/Sub pattern only.
- send(): Sends a message through the socket to the connected peer.
- recv(): Receives a message from the connected peer.
- decode(): Converts the received message from a binary format into a string or other usable format.
For a complete list of methods, please visit the pynng documentation and the nngcat command line methods.