Identifying the origin of ZMQ messages?

If I receive a message via recv () method on a ZeroMQ (0MQ) socket ...

data = s.recv()


... is there a way to get the value getpeername()

for the underlying socket? My goal is to define the source of the message in such a way that it does not rely on the sender to provide accurate information.

I'm using ZMQ (via Python) to collect host metrics, and the sender's address from the receiver's point of view is a useful identifier.

Or is it just a bad idea?


source to share

2 answers

No, you cannot get the sender's address from ZeroMq. You basically have two options; add sender address information to the message itself (not a bad option if you are allowed to modify existing message structures) or add the sender address as part of the message, that is, use ZeroMq multipart messages.

A message with multiple parts will still be delivered as a whole (all or none at all), but you can retrieve the parts individually on the receiving end, this way you can add or add the sender's address to any existing messages without actually touching them (and delivering as address + message as atomic operation).

I'm not sure how this is implemented in the pyzmq binding, but have a look at the socket.pyx source for details (basically, use the SNDMORE flag in the send (..) method).

Also, have a look at the ZeroMq zmq_send () Api docs (3.2.2).

In C ++ it will look something like this:

// Send a multi-part message consisting of sender IP plus another message
zmq_msg_send (&my_ip, my_socket, ZMQ_SNDMORE);
zmq_msg_send (&my_message, my_socket, 0);




It looks like it recently landed on github:



All Articles