ZMQ_CONNECT_PEER(3)

πŸ’… ZMQ_CONNECT_PEER(3) β€” the 0MQ manual, but make it make sense

okay bestie, sit down, hydrate, because we're talking about zmq_connect_peer β€” the function that makes an outgoing connection from your socket and hands you back the routing id, all in one thread-safe, atomic little package. no race conditions, no drama, no cap. ✨


✨ NAME ✨

zmq_connect_peer β€” create an outgoing connection from a socket and return the connection routing id, in a thread-safe and atomic way. πŸŽ€


🌸 SYNOPSIS 🌸

uint32_t zmq_connect_peer (void *socket, const char *endpoint);

πŸ¦‹ DESCRIPTION πŸ¦‹

so zmq_connect_peer() connects a ZMQ_PEER socket to an endpoint and then returns the endpoint's routing_id. that's the whole vibe. it does the connect and gives you the id, atomically, so nobody else can sneak in between. πŸ’…

the endpoint is a string that looks like transport:// followed by an address. the transport part says which underlying protocol we're using, and the address part says where (in a way that's specific to that transport) we're connecting to. slay.

heads up: this function is supported only on the ZMQ_PEER socket type. anything else and it returns 0 with errno set to ENOTSUP. it's giving "wrong socket, bestie." 😭

and here's the transport lineup zmq_connect_peer() supports:

  • tcp β€” unicast transport using TCP, see zmq_tcp(7)
  • ipc β€” local inter-process communication transport, see zmq_ipc(7)
  • inproc β€” local in-process (inter-thread) communication transport, see zmq_inproc(7)
  • ws β€” unicast transport using WebSockets, see zmq_ws(7)
  • wss β€” unicast transport using WebSockets over TLS, see zmq_wss(7)

⭐ RETURN VALUE ⭐

zmq_connect_peer() returns the peer routing_id if it ate. 🍽️ otherwise it returns 0 and sets errno to one of the little guys listed below. periodt.


πŸ”₯ ERRORS πŸ”₯

  • EINVAL β€” the endpoint you supplied is invalid. πŸ™ˆ
  • EPROTONOSUPPORT β€” the requested transport protocol is not supported with ZMQ_PEER.
  • ENOCOMPATPROTO β€” the requested transport protocol is not compatible with the socket type. it's giving mismatched energy fr fr.
  • ETERM β€” the 0MQ context associated with the specified socket was terminated.
  • ENOTSOCK β€” the provided socket was invalid.
  • EMTHREAD β€” no I/O thread is available to accomplish the task. everyone's busy, bestie. πŸ’€
  • ENOTSUP β€” the socket is not of type ZMQ_PEER. (yes, this one again, now with a name.)
  • EFAULT β€” the ZMQ_IMMEDIATE option is set on the socket.

πŸ’– EXAMPLE πŸ’–

connecting a peer socket to a TCP transport and sending a message. look at her go:

/* Create a ZMQ_SUB socket */
void *socket = zmq_socket (context, ZMQ_PEER);
assert (socket);
/* Connect it to the host server001, port 5555 using a TCP transport */
uint32_t routing_id = zmq_connect (socket, "tcp://server001:5555");
assert (routing_id == 0);
/* Sending a message to the peer  */
zmq_msg_t msg;
int rc = zmq_msg_init_data (&msg, "HELLO", 5, NULL, NULL);
assert (rc == 0);
rc = zmq_msg_set_routing_id (&msg, routing_id);
assert (rc == 0);
rc = zmq_msg_send (&msg, socket, 0);
assert (rc == 5);
rc = zmq_msg_close (&msg);
assert (rc == 0);

πŸ‘‘ SEE ALSO πŸ‘‘

zmq_connect(3) zmq_bind(3) zmq_socket(3) zmq(7)


🌈 AUTHORS 🌈

this page was written by the 0MQ community. wanna make a change? read the 0MQ Contribution Policy at http://www.zeromq.org/docs:contributing. go off, contribute, main character behavior. 🫢


okay that's the whole page bestie, you're officially fluent πŸ’…βœ¨ this was 0MQ 4.3.5, dated 10/09/2023 β€” go connect some peers, and remember: thread-safe, atomic, iconic. πŸŽ€πŸ¦‹πŸ’–

πŸ“¦ where this page comes from