Skip to content

Commit

Permalink
chore: add example for ZMQ (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Jan 19, 2025
1 parent 70b2ee2 commit ff94499
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/example_zmq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys
import zmq


def main() -> None:
if len(sys.argv) < 2:
print("Usage: python3 ./example_zmq.py <connect_to> [topic topic ...]")
sys.exit(1)

connect_to = sys.argv[1]
topics = sys.argv[2:]
ctx = zmq.Context()
s = ctx.socket(zmq.SUB)
s.connect(connect_to)

# manage subscriptions
if not topics:
print("Receiving messages on ALL topics...")
s.setsockopt(zmq.SUBSCRIBE, b"")
else:
print(f"Receiving messages on topics: {topics} ...")
for t in topics:
s.setsockopt(zmq.SUBSCRIBE, bytes.fromhex(t))
print
try:
while True:
msg = s.recv_multipart()
hex_string = [b.hex() for b in msg]
print("msg: {}".format(hex_string))

except KeyboardInterrupt:
pass
print("Done.")


if __name__ == "__main__":
main()

0 comments on commit ff94499

Please sign in to comment.