NN_POLL(3)

๐Ÿ“ฆ
version 1.2.2 ยท 5 versions
, in fedora 44

๐Ÿ’– NN_POLL(3) ๐Ÿ’–

okay bestie, welcome to nn_poll(3) from the nanomsg fam (manual name NN_POLL, section 3, if you were wondering ๐Ÿซถ). this is the page where we learn how to peek at a whole lil squad of SP sockets and go "hey, can any of y'all talk to me right now?" no cap.


โœจ NAME โœจ

nn_poll โ€” poll a set of SP sockets for readability and/or writability


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <nanomsg/nn.h>

int nn_poll (struct nn_pollfd *fds, int nfds, int timeout);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so basically this function checks a set of SP sockets and reports back whether it's possible to send a message to the socket and/or receive a message from each socket. it's giving "vibe check but for sockets" ๐Ÿ’…

the fds argument is an array of nn_pollfd structures, and the nfds argument specifies the size of that array:

struct nn_pollfd {
    int fd;
    short events;
    short revents;
};

every entry in the array = one SP socket you want checked. the events field says which events you're checking for. its value is a bitwise combination of these icons here:

  • NN_POLLIN โ€” check whether at least one message can be received from the fd socket without blocking. like, is there mail in the mailbox? ๐Ÿ“ฌ
  • NN_POLLOUT โ€” check whether at least one message can be sent to the fd socket without blocking. like, is the mailbox open for business? ๐Ÿ“ฎ

after the function returns, the revents field contains a bitwise combination of NN_POLLIN and NN_POLLOUT depending on whether the socket is readable or writable. so that's where the receipts show up ๐Ÿงพ

the timeout parameter specifies how long โ€” in milliseconds โ€” the function should block if there are no events to report. (yes bestie, milliseconds, don't come at me with seconds ๐Ÿ™ˆ)


๐Ÿ‘‘ RETURN VALUE ๐Ÿ‘‘

upon successful completion, the number of nn_pollfds structures with events signaled is returned. slay. โœจ

if it times out, the return value is 0. ๐Ÿ’€

if there's an error, -1 is returned and errno is set to one of the values below. not the vibe.


๐Ÿ’œ ERRORS ๐Ÿ’œ

  • EBADF โ€” some of the provided sockets are invalid. bestie... those sockets don't exist ๐Ÿ˜ญ
  • EINTR โ€” the operation was interrupted by delivery of a signal before the message was sent. rude of that signal, honestly.
  • ETERM โ€” the library is terminating. it's over, periodt.

๐ŸŽ€ NOTE ๐ŸŽ€

nn_poll is a convenience function. you can achieve the same behaviour by using the NN_RCVFD and NN_SNDFD socket options. however, using the socket options allows for usage that's not possible with nn_poll โ€” such as simultaneous polling for both SP and OS-level sockets, integration of SP sockets with external event loops, etc. so like, nn_poll is the cute little shortcut, but the socket options are the main character energy version ๐Ÿซถ


๐ŸŒˆ EXAMPLE ๐ŸŒˆ

struct nn_pollfd pfd [2];
pfd [0].fd = s1;
pfd [0].events = NN_POLLIN | NN_POLLOUT;
pfd [1].fd = s2;
pfd [1].events = NN_POLLIN;
rc = nn_poll (pfd, 2, 2000);
if (rc == 0) {
    printf ("Timeout!");
    exit (1);
}
if (rc == -1) {
    printf ("Error!");
    exit (1);
}
if (pfd [0].revents & NN_POLLIN) {
    printf ("Message can be received from s1!");
    exit (1);
}

๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ

nn_socket(3) nn_getsockopt(3) nanomsg(7)


๐Ÿ’– AUTHORS ๐Ÿ’–

Martin Sustrik <sustrik@250bpm.com> โ€” the icon who made this, we stan ๐Ÿซก


and that's the whole page bestie!! this was nanomsg version 1.2.2, dated 2026-01-16, and yes it's literally NN_POLL(3) again because we came full circle ๐Ÿฆ‹โœจ go poll something, you got this ๐Ÿ’…

๐Ÿ“ฆ where this page comes from