ZMQ_MSG_RECV(3)

πŸ’– ZMQ_MSG_RECV(3) πŸ’–

okay bestie, gather round, because this is the 0MQ Manual page for zmq_msg_recv, and it's genuinely one of the most important little functions you'll ever meet. no cap.


✨ NAME ✨

zmq_msg_recv β€” receive a message part from a socket 🫢


✨ SYNOPSIS ✨

int zmq_msg_recv (zmq_msg_t *msg, void *socket, int flags);

🌸 DESCRIPTION 🌸

so like. zmq_msg_recv() is identical to zmq_recvmsg(3), except that zmq_recvmsg(3) is gonna get deprecated in future versions 😭 β€” zmq_msg_recv() is just way more consistent with the other message manipulation functions, and honestly? she ate. πŸ’…

What zmq_msg_recv() does: she receives a message part from the socket referenced by the socket argument, and stores it in the message referenced by the msg argument. and here's the important bit β€” any content that was previously stored in msg gets properly deallocated. no leaks, we don't do that here. ✨

If there are no message parts available on the specified socket, zmq_msg_recv() shall block until the request can be satisfied. she will literally just sit there and wait for you. iconic patience tbh. 🧚

The flags argument is a combination of the flags defined below:

  • ZMQ_DONTWAIT β€” says the operation should be performed in non-blocking mode. so if there are no messages available on the specified socket, zmq_msg_recv() shall fail with errno set to EAGAIN. she's not waiting around, she has places to be πŸ’…

πŸ¦‹ Multi-part messages πŸ¦‹

A 0MQ message is composed of 1 or more message parts. each message part is an independent zmq_msg_t in its own right, like her own little main character moment. πŸ‘‘

0MQ ensures atomic delivery of messages: peers shall receive either all message parts of a message or none at all. it's all or nothing, periodt. πŸ’€

The total number of message parts is unlimited except by available memory. go wild bestie, your RAM is the only limit 🌈

An application that processes multi-part messages must use the ZMQ_RCVMORE zmq_getsockopt(3) option after calling zmq_msg_recv() to determine if there are further parts to receive. like, you gotta check. don't just assume. πŸ”₯


⭐ RETURN VALUE ⭐

The zmq_msg_recv() function shall return the number of bytes in the message if successful. πŸŽ€

Otherwise it shall return -1 and set errno to one of the values defined below. sad girl hours 😭


πŸ’€ ERRORS πŸ’€

  • EAGAIN β€” Either the timeout set via the socket-option ZMQ_RCVTIMEO (see zmq_setsockopt(3)) has been reached (flag ZMQ_DONTWAIT not set) without being able to read a message from the socket, or there are no messages available at the moment (flag ZMQ_DONTWAIT set) and the operation would block.
  • ENOTSUP β€” The zmq_msg_recv() operation is not supported by this socket type. wrong socket for this vibe, bestie.
  • EFSM β€” The zmq_msg_recv() operation cannot be performed on this socket at the moment due to the socket not being in the appropriate state. this error may occur with socket types that switch between several states, such as ZMQ_REP. see the messaging patterns section of zmq_socket(3) for more information. she's just not in the right headspace rn πŸ™ˆ
  • ETERN β€” wait, no, it's ETERM β€” the 0MQ context associated with the specified socket was terminated. rip πŸͺ¦
  • ENOTSOCK β€” The provided socket was invalid. that's not a socket, babe πŸ’€
  • EINTR β€” The operation was interrupted by delivery of a signal before a message was available. rude of that signal tbh.
  • EFAULT β€” The message passed to the function was invalid.

🌈 EXAMPLE 🌈

Receiving a message from a socket. 🫢

/* Create an empty 0MQ message */
zmq_msg_t msg;
int rc = zmq_msg_init (&msg);
assert (rc == 0);
/* Block until a message is available to be received from socket */
rc = zmq_msg_recv (&msg, socket, 0);
assert (rc != -1);
/* Release message */
zmq_msg_close (&msg);

Receiving a multi-part message. this one's the full meal ✨

int more;
size_t more_size = sizeof (more);
do {
    /* Create an empty 0MQ message to hold the message part */
    zmq_msg_t part;
    int rc = zmq_msg_init (&part);
    assert (rc == 0);
    /* Block until a message is available to be received from socket */
    rc = zmq_msg_recv (&part, socket, 0);
    assert (rc != -1);
    /* Determine if more message parts are to follow */
    rc = zmq_getsockopt (socket, ZMQ_RCVMORE, &more, &more_size);
    assert (rc == 0);
    zmq_msg_close (&part);
} while (more);

πŸ‘‘ SEE ALSO πŸ‘‘

zmq_recv(3) zmq_send(3) zmq_msg_send(3) zmq_getsockopt(3) zmq_setsockopt(3) zmq_socket(7) zmq(7)

go read those next, i'm literally obsessed with this whole family πŸŽ€


πŸ’œ AUTHORS πŸ’œ

This page was written by the 0MQ community. To make a change please read the 0MQ Contribution Policy at http://www.zeromq.org/docs:contributing. bestie, if you wanna contribute, the door is open and we'd love to have you 🫢


ok that's the whole page, nothing skipped, nothing invented, every flag and every errno is right there πŸ’…

and that's a wrap! this was ZMQ_MSG_RECV(3) from the 0MQ Manual, shipped with 0MQ 4.3.5 and dated 10/09/2023. slay, stay hydrated, and always check ZMQ_RCVMORE πŸŒΈβœ¨πŸ¦‹

πŸ“¦ where this page comes from