zmq_socket.txt 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. zmq_socket(3)
  2. =============
  3. NAME
  4. ----
  5. zmq_socket - create 0MQ socket
  6. SYNOPSIS
  7. --------
  8. *void *zmq_socket (void '*context', int 'type');*
  9. DESCRIPTION
  10. -----------
  11. The 'zmq_socket()' function shall create a 0MQ socket within the specified
  12. 'context' and return an opaque handle to the newly created socket. The 'type'
  13. argument specifies the socket type, which determines the semantics of
  14. communication over the socket.
  15. The newly created socket is initially unbound, and not associated with any
  16. endpoints. In order to establish a message flow a socket must first be
  17. connected to at least one endpoint with linkzmq:zmq_connect[3], or at least one
  18. endpoint must be created for accepting incoming connections with
  19. linkzmq:zmq_bind[3].
  20. .Key differences to conventional sockets
  21. Generally speaking, conventional sockets present a _synchronous_ interface to
  22. either connection-oriented reliable byte streams (SOCK_STREAM), or
  23. connection-less unreliable datagrams (SOCK_DGRAM). In comparison, 0MQ sockets
  24. present an abstraction of an asynchronous _message queue_, with the exact
  25. queueing semantics depending on the socket type in use. Where conventional
  26. sockets transfer streams of bytes or discrete datagrams, 0MQ sockets transfer
  27. discrete _messages_.
  28. 0MQ sockets being _asynchronous_ means that the timings of the physical
  29. connection setup and tear down, reconnect and effective delivery are transparent
  30. to the user and organized by 0MQ itself. Further, messages may be _queued_ in
  31. the event that a peer is unavailable to receive them.
  32. Conventional sockets allow only strict one-to-one (two peers), many-to-one
  33. (many clients, one server), or in some cases one-to-many (multicast)
  34. relationships. With the exception of 'ZMQ_PAIR' and 'ZMQ_CHANNEL', 0MQ sockets may be connected
  35. *to multiple endpoints* using _zmq_connect()_, while simultaneously accepting
  36. incoming connections *from multiple endpoints* bound to the socket using
  37. _zmq_bind()_, thus allowing many-to-many relationships.
  38. .Thread safety
  39. 0MQ has both thread safe socket type and _not_ thread safe socket types.
  40. Applications MUST NOT use a _not_ thread safe socket
  41. from multiple threads under any circumstances. Doing so results in undefined
  42. behaviour.
  43. Following are the thread safe sockets:
  44. * ZMQ_CLIENT
  45. * ZMQ_SERVER
  46. * ZMQ_DISH
  47. * ZMQ_RADIO
  48. * ZMQ_SCATTER
  49. * ZMQ_GATHER
  50. * ZMQ_PEER
  51. * ZMQ_CHANNEL
  52. .Socket types
  53. The following sections present the socket types defined by 0MQ, grouped by the
  54. general _messaging pattern_ which is built from related socket types.
  55. Client-server pattern
  56. ~~~~~~~~~~~~~~~~~~~~~
  57. The client-server pattern is used to allow a single 'ZMQ_SERVER' _server_ talk
  58. to one or more 'ZMQ_CLIENT' _clients_. The client always starts the conversation,
  59. after which either peer can send messages asynchronously, to the other.
  60. The client-server pattern is formally defined by http://rfc.zeromq.org/spec:41.
  61. NOTE: Server-client is still in draft phase.
  62. ZMQ_CLIENT
  63. ^^^^^^^^^^
  64. A 'ZMQ_CLIENT' socket talks to a 'ZMQ_SERVER' socket. Either peer can connect,
  65. though the usual and recommended model is to bind the 'ZMQ_SERVER' and connect
  66. the 'ZMQ_CLIENT'.
  67. If the 'ZMQ_CLIENT' socket has established a connection, linkzmq:zmq_send[3]
  68. will accept messages, queue them, and send them as rapidly as the network
  69. allows. The outgoing buffer limit is defined by the high water mark for the
  70. socket. If the outgoing buffer is full, or, for connection-oriented transports,
  71. if the ZMQ_IMMEDIATE option is set and there is no connected peer,
  72. linkzmq:zmq_send[3] will block.
  73. The 'ZMQ_CLIENT' socket will not drop messages.
  74. When a 'ZMQ_CLIENT' socket is connected to multiple 'ZMQ_SERVER' sockets,
  75. outgoing messages are distributed between connected peers on a round-robin
  76. basis. Likewise, the 'ZMQ_CLIENT' socket receives messages fairly from each
  77. connected peer. This usage is sensible only for stateless protocols.
  78. 'ZMQ_CLIENT' sockets are threadsafe and can be used from multiple threads
  79. at the same time. Note that replies from a 'ZMQ_SERVER' socket will go to
  80. the first client thread that calls linkzmq:zmq_msg_recv[3]. If you need to get
  81. replies back to the originating thread, use one 'ZMQ_CLIENT' socket per
  82. thread.
  83. NOTE: 'ZMQ_CLIENT' sockets are threadsafe. They do not accept the ZMQ_SNDMORE
  84. option on sends not ZMQ_RCVMORE on receives. This limits them to single part
  85. data. The intention is to extend the API to allow scatter/gather of multi-part
  86. data.
  87. [horizontal]
  88. .Summary of ZMQ_CLIENT characteristics
  89. Compatible peer sockets:: 'ZMQ_SERVER'
  90. Direction:: Bidirectional
  91. Send/receive pattern:: Unrestricted
  92. Outgoing routing strategy:: Round-robin
  93. Incoming routing strategy:: Fair-queued
  94. Action in mute state:: Block
  95. ZMQ_SERVER
  96. ^^^^^^^^^^
  97. A 'ZMQ_SERVER' socket talks to a set of 'ZMQ_CLIENT' sockets. A 'ZMQ_SERVER'
  98. socket can only reply to an incoming message: the 'ZMQ_CLIENT' peer must
  99. always initiate a conversation.
  100. Each received message has a 'routing_id' that is a 32-bit unsigned integer.
  101. The application can fetch this with linkzmq:zmq_msg_routing_id[3]. To send
  102. a message to a given 'ZMQ_CLIENT' peer the application must set the peer's
  103. 'routing_id' on the message, using linkzmq:zmq_msg_set_routing_id[3].
  104. If the 'routing_id' is not specified, or does not refer to a connected client
  105. peer, the send call will fail with EHOSTUNREACH. If the outgoing buffer for
  106. the client peer is full, the send call shall block, unless ZMQ_DONTWAIT is
  107. used in the send, in which case it shall fail with EAGAIN. The 'ZMQ_SERVER'
  108. socket shall not drop messages in any case.
  109. NOTE: 'ZMQ_SERVER' sockets are threadsafe. They do not accept the ZMQ_SNDMORE
  110. option on sends not ZMQ_RCVMORE on receives. This limits them to single part
  111. data. The intention is to extend the API to allow scatter/gather of multi-part
  112. data.
  113. [horizontal]
  114. .Summary of ZMQ_SERVER characteristics
  115. Compatible peer sockets:: 'ZMQ_CLIENT'
  116. Direction:: Bidirectional
  117. Send/receive pattern:: Unrestricted
  118. Outgoing routing strategy:: See text
  119. Incoming routing strategy:: Fair-queued
  120. Action in mute state:: Return EAGAIN
  121. Radio-dish pattern
  122. ~~~~~~~~~~~~~~~~~~
  123. The radio-dish pattern is used for one-to-many distribution of data from
  124. a single _publisher_ to multiple _subscribers_ in a fan out fashion.
  125. Radio-dish is using groups (vs Pub-sub topics), Dish sockets can join a group
  126. and each message sent by Radio sockets belong to a group.
  127. Groups are null terminated strings limited to 16 chars length (including null).
  128. The intention is to increase the length to 40 chars (including null).
  129. The encoding of groups shall be UTF8.
  130. Groups are matched using exact matching (vs prefix matching of PubSub).
  131. NOTE: Radio-dish is still in draft phase.
  132. ZMQ_RADIO
  133. ^^^^^^^
  134. A socket of type 'ZMQ_RADIO' is used by a _publisher_ to distribute data.
  135. Each message belong to a group, a group is specified with linkzmq:zmq_msg_set_group[3].
  136. Messages are distributed to all members of a group.
  137. The linkzmq:zmq_recv[3] function is not implemented for this socket type.
  138. When a 'ZMQ_RADIO' socket enters the 'mute' state due to having reached the
  139. high water mark for a _subscriber_, then any messages that would be sent to the
  140. _subscriber_ in question shall instead be dropped until the mute state
  141. ends. The _zmq_send()_ function shall never block for this socket type.
  142. NOTE: 'ZMQ_RADIO' sockets are threadsafe. They do not accept the ZMQ_SNDMORE
  143. option on sends. This limits them to single part data.
  144. [horizontal]
  145. .Summary of ZMQ_RADIO characteristics
  146. Compatible peer sockets:: 'ZMQ_DISH'
  147. Direction:: Unidirectional
  148. Send/receive pattern:: Send only
  149. Incoming routing strategy:: N/A
  150. Outgoing routing strategy:: Fan out
  151. Action in mute state:: Drop
  152. ZMQ_DISH
  153. ^^^^^^^^
  154. A socket of type 'ZMQ_DISH' is used by a _subscriber_ to subscribe to groups
  155. distributed by a _radio_. Initially a 'ZMQ_DISH' socket is not subscribed to
  156. any groups, use linkzmq:zmq_join[3] to
  157. join a group.
  158. To get the group the message belong to call linkzmq:zmq_msg_group[3].
  159. The _zmq_send()_ function is not implemented for this socket type.
  160. NOTE: 'ZMQ_DISH' sockets are threadsafe. They do not accept ZMQ_RCVMORE on receives.
  161. This limits them to single part data.
  162. [horizontal]
  163. .Summary of ZMQ_DISH characteristics
  164. Compatible peer sockets:: 'ZMQ_RADIO'
  165. Direction:: Unidirectional
  166. Send/receive pattern:: Receive only
  167. Incoming routing strategy:: Fair-queued
  168. Outgoing routing strategy:: N/A
  169. Publish-subscribe pattern
  170. ~~~~~~~~~~~~~~~~~~~~~~~~~
  171. The publish-subscribe pattern is used for one-to-many distribution of data from
  172. a single _publisher_ to multiple _subscribers_ in a fan out fashion.
  173. The publish-subscribe pattern is formally defined by http://rfc.zeromq.org/spec:29.
  174. ZMQ_PUB
  175. ^^^^^^^
  176. A socket of type 'ZMQ_PUB' is used by a _publisher_ to distribute data.
  177. Messages sent are distributed in a fan out fashion to all connected peers.
  178. The linkzmq:zmq_recv[3] function is not implemented for this socket type.
  179. When a 'ZMQ_PUB' socket enters the 'mute' state due to having reached the
  180. high water mark for a _subscriber_, then any messages that would be sent to the
  181. _subscriber_ in question shall instead be dropped until the mute state
  182. ends. The _zmq_send()_ function shall never block for this socket type.
  183. [horizontal]
  184. .Summary of ZMQ_PUB characteristics
  185. Compatible peer sockets:: 'ZMQ_SUB', 'ZMQ_XSUB'
  186. Direction:: Unidirectional
  187. Send/receive pattern:: Send only
  188. Incoming routing strategy:: N/A
  189. Outgoing routing strategy:: Fan out
  190. Action in mute state:: Drop
  191. ZMQ_SUB
  192. ^^^^^^^
  193. A socket of type 'ZMQ_SUB' is used by a _subscriber_ to subscribe to data
  194. distributed by a _publisher_. Initially a 'ZMQ_SUB' socket is not subscribed to
  195. any messages, use the 'ZMQ_SUBSCRIBE' option of linkzmq:zmq_setsockopt[3] to
  196. specify which messages to subscribe to. The _zmq_send()_ function is not
  197. implemented for this socket type.
  198. [horizontal]
  199. .Summary of ZMQ_SUB characteristics
  200. Compatible peer sockets:: 'ZMQ_PUB', 'ZMQ_XPUB'
  201. Direction:: Unidirectional
  202. Send/receive pattern:: Receive only
  203. Incoming routing strategy:: Fair-queued
  204. Outgoing routing strategy:: N/A
  205. ZMQ_XPUB
  206. ^^^^^^^^
  207. Same as ZMQ_PUB except that you can receive subscriptions from the peers
  208. in form of incoming messages. Subscription message is a byte 1 (for
  209. subscriptions) or byte 0 (for unsubscriptions) followed by the subscription
  210. body. Messages without a sub/unsub prefix are also received, but have no
  211. effect on subscription status.
  212. [horizontal]
  213. .Summary of ZMQ_XPUB characteristics
  214. Compatible peer sockets:: 'ZMQ_SUB', 'ZMQ_XSUB'
  215. Direction:: Unidirectional
  216. Send/receive pattern:: Send messages, receive subscriptions
  217. Incoming routing strategy:: N/A
  218. Outgoing routing strategy:: Fan out
  219. Action in mute state:: Drop
  220. ZMQ_XSUB
  221. ^^^^^^^^
  222. Same as ZMQ_SUB except that you subscribe by sending subscription messages to
  223. the socket. Subscription message is a byte 1 (for subscriptions) or byte 0
  224. (for unsubscriptions) followed by the subscription body. Messages without a
  225. sub/unsub prefix may also be sent, but have no effect on subscription status.
  226. [horizontal]
  227. .Summary of ZMQ_XSUB characteristics
  228. Compatible peer sockets:: 'ZMQ_PUB', 'ZMQ_XPUB'
  229. Direction:: Unidirectional
  230. Send/receive pattern:: Receive messages, send subscriptions
  231. Incoming routing strategy:: Fair-queued
  232. Outgoing routing strategy:: N/A
  233. Action in mute state:: Drop
  234. Pipeline pattern
  235. ~~~~~~~~~~~~~~~~
  236. The pipeline pattern is used for distributing data to _nodes_ arranged in
  237. a pipeline. Data always flows down the pipeline, and each stage of the pipeline
  238. is connected to at least one _node_. When a pipeline stage is connected to
  239. multiple _nodes_ data is round-robined among all connected _nodes_.
  240. The pipeline pattern is formally defined by http://rfc.zeromq.org/spec:30.
  241. ZMQ_PUSH
  242. ^^^^^^^^
  243. A socket of type 'ZMQ_PUSH' is used by a pipeline _node_ to send messages
  244. to downstream pipeline _nodes_. Messages are round-robined to all connected
  245. downstream _nodes_. The _zmq_recv()_ function is not implemented for this
  246. socket type.
  247. When a 'ZMQ_PUSH' socket enters the 'mute' state due to having reached the
  248. high water mark for all downstream _nodes_, or, for connection-oriented transports,
  249. if the ZMQ_IMMEDIATE option is set and there are no downstream _nodes_ at all,
  250. then any linkzmq:zmq_send[3] operations on the socket shall block until the mute
  251. state ends or at least one downstream _node_ becomes available for sending;
  252. messages are not discarded.
  253. [horizontal]
  254. .Summary of ZMQ_PUSH characteristics
  255. Compatible peer sockets:: 'ZMQ_PULL'
  256. Direction:: Unidirectional
  257. Send/receive pattern:: Send only
  258. Incoming routing strategy:: N/A
  259. Outgoing routing strategy:: Round-robin
  260. Action in mute state:: Block
  261. ZMQ_PULL
  262. ^^^^^^^^
  263. A socket of type 'ZMQ_PULL' is used by a pipeline _node_ to receive messages
  264. from upstream pipeline _nodes_. Messages are fair-queued from among all
  265. connected upstream _nodes_. The _zmq_send()_ function is not implemented for
  266. this socket type.
  267. [horizontal]
  268. .Summary of ZMQ_PULL characteristics
  269. Compatible peer sockets:: 'ZMQ_PUSH'
  270. Direction:: Unidirectional
  271. Send/receive pattern:: Receive only
  272. Incoming routing strategy:: Fair-queued
  273. Outgoing routing strategy:: N/A
  274. Action in mute state:: Block
  275. Scatter-gather pattern
  276. ~~~~~~~~~~~~~~~~
  277. The scatter-gather pattern is the thread-safe version of the pipeline pattern.
  278. The scatter-gather pattern is used for distributing data to _nodes_ arranged in
  279. a pipeline. Data always flows down the pipeline, and each stage of the pipeline
  280. is connected to at least one _node_. When a pipeline stage is connected to
  281. multiple _nodes_ data is round-robined among all connected _nodes_.
  282. ZMQ_SCATTER
  283. ^^^^^^^^
  284. A socket of type 'ZMQ_SCATTER' is used by a scatter-gather _node_ to send messages
  285. to downstream scatter-gather _nodes_. Messages are round-robined to all connected
  286. downstream _nodes_. The _zmq_recv()_ function is not implemented for this
  287. socket type.
  288. When a 'ZMQ_SCATTER' socket enters the 'mute' state due to having reached the
  289. high water mark for all downstream _nodes_, or, for connection-oriented transports,
  290. if the ZMQ_IMMEDIATE option is set and there are no downstream _nodes_ at all,
  291. then any linkzmq:zmq_send[3] operations on the socket shall block until the mute
  292. state ends or at least one downstream _node_ becomes available for sending;
  293. messages are not discarded.
  294. NOTE: 'ZMQ_SCATTER' sockets are threadsafe. They do not accept ZMQ_RCVMORE on receives.
  295. This limits them to single part data.
  296. [horizontal]
  297. .Summary of ZMQ_SCATTER characteristics
  298. Compatible peer sockets:: 'ZMQ_SCATTER'
  299. Direction:: Unidirectional
  300. Send/receive pattern:: Send only
  301. Incoming routing strategy:: N/A
  302. Outgoing routing strategy:: Round-robin
  303. Action in mute state:: Block
  304. ZMQ_GATHER
  305. ^^^^^^^^
  306. A socket of type 'ZMQ_GATHER' is used by a scatter-gather _node_ to receive messages
  307. from upstream scatter-gather _nodes_. Messages are fair-queued from among all
  308. connected upstream _nodes_. The _zmq_send()_ function is not implemented for
  309. this socket type.
  310. NOTE: 'ZMQ_GATHER' sockets are threadsafe. They do not accept ZMQ_RCVMORE on receives.
  311. This limits them to single part data.
  312. [horizontal]
  313. .Summary of ZMQ_GATHER characteristics
  314. Compatible peer sockets:: 'ZMQ_GATHER'
  315. Direction:: Unidirectional
  316. Send/receive pattern:: Receive only
  317. Incoming routing strategy:: Fair-queued
  318. Outgoing routing strategy:: N/A
  319. Action in mute state:: Block
  320. Exclusive pair pattern
  321. ~~~~~~~~~~~~~~~~~~~~~~
  322. The exclusive pair pattern is used to connect a peer to precisely one other
  323. peer. This pattern is used for inter-thread communication across the inproc
  324. transport.
  325. The exclusive pair pattern is formally defined by http://rfc.zeromq.org/spec:31.
  326. ZMQ_PAIR
  327. ^^^^^^^^
  328. A socket of type 'ZMQ_PAIR' can only be connected to a single peer at any one
  329. time. No message routing or filtering is performed on messages sent over a
  330. 'ZMQ_PAIR' socket.
  331. When a 'ZMQ_PAIR' socket enters the 'mute' state due to having reached the
  332. high water mark for the connected peer, or, for connection-oriented transports,
  333. if the ZMQ_IMMEDIATE option is set and there is no connected peer, then
  334. any linkzmq:zmq_send[3] operations on the socket shall block until the peer
  335. becomes available for sending; messages are not discarded.
  336. While 'ZMQ_PAIR' sockets can be used over transports other than linkzmq:zmq_inproc[7],
  337. their inability to auto-reconnect coupled with the fact new incoming connections will
  338. be terminated while any previous connections (including ones in a closing state)
  339. exist makes them unsuitable for TCP in most cases.
  340. NOTE: 'ZMQ_PAIR' sockets are designed for inter-thread communication across
  341. the linkzmq:zmq_inproc[7] transport and do not implement functionality such
  342. as auto-reconnection.
  343. [horizontal]
  344. .Summary of ZMQ_PAIR characteristics
  345. Compatible peer sockets:: 'ZMQ_PAIR'
  346. Direction:: Bidirectional
  347. Send/receive pattern:: Unrestricted
  348. Incoming routing strategy:: N/A
  349. Outgoing routing strategy:: N/A
  350. Action in mute state:: Block
  351. Peer-to-peer pattern
  352. ~~~~~~~~~~~~~~~~~~~~~~
  353. The peer-to-peer pattern is used to connect a peer to multiple peers.
  354. Peer can both connect and bind and mix both of them with the same socket.
  355. The peer-to-peer pattern is useful to build peer-to-peer networks (e.g zyre, bitcoin, torrent)
  356. where a peer can both accept connections from other peers or connect to them.
  357. NOTE: Peer-to-peer is still in draft phase.
  358. ZMQ_PEER
  359. ^^^^^^^^
  360. A 'ZMQ_PEER' socket talks to a set of 'ZMQ_PEER' sockets.
  361. To connect and fetch the 'routing_id' of the peer use linkzmq:zmq_connect_peer[3].
  362. Each received message has a 'routing_id' that is a 32-bit unsigned integer.
  363. The application can fetch this with linkzmq:zmq_msg_routing_id[3].
  364. To send a message to a given 'ZMQ_PEER' peer the application must set the peer's
  365. 'routing_id' on the message, using linkzmq:zmq_msg_set_routing_id[3].
  366. If the 'routing_id' is not specified, or does not refer to a connected client
  367. peer, the send call will fail with EHOSTUNREACH. If the outgoing buffer for
  368. the peer is full, the send call shall block, unless ZMQ_DONTWAIT is
  369. used in the send, in which case it shall fail with EAGAIN. The 'ZMQ_PEER'
  370. socket shall not drop messages in any case.
  371. NOTE: 'ZMQ_PEER' sockets are threadsafe. They do not accept the ZMQ_SNDMORE
  372. option on sends not ZMQ_RCVMORE on receives. This limits them to single part
  373. data.
  374. [horizontal]
  375. .Summary of ZMQ_PEER characteristics
  376. Compatible peer sockets:: 'ZMQ_PEER'
  377. Direction:: Bidirectional
  378. Send/receive pattern:: Unrestricted
  379. Outgoing routing strategy:: See text
  380. Incoming routing strategy:: Fair-queued
  381. Action in mute state:: Return EAGAIN
  382. Channel pattern
  383. ~~~~~~~~~~~~~~~~~~~~~~
  384. The channel pattern is the thread-safe version of the exclusive pair pattern.
  385. The channel pattern is used to connect a peer to precisely one other
  386. peer. This pattern is used for inter-thread communication across the inproc
  387. transport.
  388. NOTE: Channel is still in draft phase.
  389. ZMQ_CHANNEL
  390. ^^^^^^^^
  391. A socket of type 'ZMQ_CHANNEL' can only be connected to a single peer at any one
  392. time. No message routing or filtering is performed on messages sent over a
  393. 'ZMQ_CHANNEL' socket.
  394. When a 'ZMQ_CHANNEL' socket enters the 'mute' state due to having reached the
  395. high water mark for the connected peer, or, for connection-oriented transports,
  396. if the ZMQ_IMMEDIATE option is set and there is no connected peer, then
  397. any linkzmq:zmq_send[3] operations on the socket shall block until the peer
  398. becomes available for sending; messages are not discarded.
  399. While 'ZMQ_CHANNEL' sockets can be used over transports other than linkzmq:zmq_inproc[7],
  400. their inability to auto-reconnect coupled with the fact new incoming connections will
  401. be terminated while any previous connections (including ones in a closing state)
  402. exist makes them unsuitable for TCP in most cases.
  403. NOTE: 'ZMQ_CHANNEL' sockets are designed for inter-thread communication across
  404. the linkzmq:zmq_inproc[7] transport and do not implement functionality such
  405. as auto-reconnection.
  406. NOTE: 'ZMQ_CHANNEL' sockets are threadsafe. They do not accept ZMQ_RCVMORE on receives.
  407. This limits them to single part data.
  408. [horizontal]
  409. .Summary of ZMQ_CHANNEL characteristics
  410. Compatible peer sockets:: 'ZMQ_CHANNEL'
  411. Direction:: Bidirectional
  412. Send/receive pattern:: Unrestricted
  413. Incoming routing strategy:: N/A
  414. Outgoing routing strategy:: N/A
  415. Action in mute state:: Block
  416. Native Pattern
  417. ~~~~~~~~~~~~~~
  418. The native pattern is used for communicating with TCP peers and allows
  419. asynchronous requests and replies in either direction.
  420. ZMQ_STREAM
  421. ^^^^^^^^^^
  422. A socket of type 'ZMQ_STREAM' is used to send and receive TCP data from a
  423. non-0MQ peer, when using the tcp:// transport. A 'ZMQ_STREAM' socket can
  424. act as client and/or server, sending and/or receiving TCP data asynchronously.
  425. When receiving TCP data, a 'ZMQ_STREAM' socket shall prepend a message part
  426. containing the _routing id_ of the originating peer to the message before passing
  427. it to the application. Messages received are fair-queued from among all
  428. connected peers.
  429. When sending TCP data, a 'ZMQ_STREAM' socket shall remove the first part of the
  430. message and use it to determine the _routing id_ of the peer the message shall be
  431. routed to, and unroutable messages shall cause an EHOSTUNREACH or EAGAIN error.
  432. To open a connection to a server, use the zmq_connect call, and then fetch the
  433. socket routing id using the zmq_getsockopt call with the ZMQ_ROUTING_ID option.
  434. To close a specific connection, send the routing id frame followed by a
  435. zero-length message (see EXAMPLE section).
  436. When a connection is made, a zero-length message will be received by the
  437. application. Similarly, when the peer disconnects (or the connection is lost),
  438. a zero-length message will be received by the application.
  439. You must send one routing id frame followed by one data frame. The ZMQ_SNDMORE
  440. flag is required for routing id frames but is ignored on data frames.
  441. [horizontal]
  442. .Summary of ZMQ_STREAM characteristics
  443. Compatible peer sockets:: none.
  444. Direction:: Bidirectional
  445. Send/receive pattern:: Unrestricted
  446. Outgoing routing strategy:: See text
  447. Incoming routing strategy:: Fair-queued
  448. Action in mute state:: EAGAIN
  449. Request-reply pattern
  450. ~~~~~~~~~~~~~~~~~~~~~
  451. The request-reply pattern is used for sending requests from a ZMQ_REQ _client_
  452. to one or more ZMQ_REP _services_, and receiving subsequent replies to each
  453. request sent.
  454. The request-reply pattern is formally defined by http://rfc.zeromq.org/spec:28.
  455. ZMQ_REQ
  456. ^^^^^^^
  457. A socket of type 'ZMQ_REQ' is used by a _client_ to send requests to and
  458. receive replies from a _service_. This socket type allows only an alternating
  459. sequence of _zmq_send(request)_ and subsequent _zmq_recv(reply)_ calls. Each
  460. request sent is round-robined among all _services_, and each reply received is
  461. matched with the last issued request.
  462. For connection-oriented transports, If the ZMQ_IMMEDIATE option is set and there
  463. is no service available, then any send operation on the socket shall block until
  464. at least one _service_ becomes available. The REQ socket shall not discard messages.
  465. [horizontal]
  466. .Summary of ZMQ_REQ characteristics
  467. Compatible peer sockets:: 'ZMQ_REP', 'ZMQ_ROUTER'
  468. Direction:: Bidirectional
  469. Send/receive pattern:: Send, Receive, Send, Receive, ...
  470. Outgoing routing strategy:: Round-robin
  471. Incoming routing strategy:: Last peer
  472. Action in mute state:: Block
  473. ZMQ_REP
  474. ^^^^^^^
  475. A socket of type 'ZMQ_REP' is used by a _service_ to receive requests from and
  476. send replies to a _client_. This socket type allows only an alternating
  477. sequence of _zmq_recv(request)_ and subsequent _zmq_send(reply)_ calls. Each
  478. request received is fair-queued from among all _clients_, and each reply sent
  479. is routed to the _client_ that issued the last request. If the original
  480. requester does not exist any more the reply is silently discarded.
  481. [horizontal]
  482. .Summary of ZMQ_REP characteristics
  483. Compatible peer sockets:: 'ZMQ_REQ', 'ZMQ_DEALER'
  484. Direction:: Bidirectional
  485. Send/receive pattern:: Receive, Send, Receive, Send, ...
  486. Incoming routing strategy:: Fair-queued
  487. Outgoing routing strategy:: Last peer
  488. ZMQ_DEALER
  489. ^^^^^^^^^^
  490. A socket of type 'ZMQ_DEALER' is an advanced pattern used for extending
  491. request/reply sockets. Each message sent is round-robined among all connected
  492. peers, and each message received is fair-queued from all connected peers.
  493. When a 'ZMQ_DEALER' socket enters the 'mute' state due to having reached the
  494. high water mark for all peers, or, for connection-oriented transports, if the
  495. ZMQ_IMMEDIATE option is set and there are no peers at all, then any
  496. linkzmq:zmq_send[3] operations on the socket shall block until the mute state
  497. ends or at least one peer becomes available for sending; messages are not discarded.
  498. When a 'ZMQ_DEALER' socket is connected to a 'ZMQ_REP' socket each message sent
  499. must consist of an empty message part, the _delimiter_, followed by one or more
  500. _body parts_.
  501. [horizontal]
  502. .Summary of ZMQ_DEALER characteristics
  503. Compatible peer sockets:: 'ZMQ_ROUTER', 'ZMQ_REP', 'ZMQ_DEALER'
  504. Direction:: Bidirectional
  505. Send/receive pattern:: Unrestricted
  506. Outgoing routing strategy:: Round-robin
  507. Incoming routing strategy:: Fair-queued
  508. Action in mute state:: Block
  509. ZMQ_ROUTER
  510. ^^^^^^^^^^
  511. A socket of type 'ZMQ_ROUTER' is an advanced socket type used for extending
  512. request/reply sockets. When receiving messages a 'ZMQ_ROUTER' socket shall
  513. prepend a message part containing the _routing id_ of the originating peer to the
  514. message before passing it to the application. Messages received are fair-queued
  515. from among all connected peers. When sending messages a 'ZMQ_ROUTER' socket shall
  516. remove the first part of the message and use it to determine the _routing id _ of
  517. the peer the message shall be routed to. If the peer does not exist anymore, or
  518. has never existed, the message shall be silently discarded. However, if
  519. 'ZMQ_ROUTER_MANDATORY' socket option is set to '1', the socket shall fail
  520. with EHOSTUNREACH in both cases.
  521. When a 'ZMQ_ROUTER' socket enters the 'mute' state due to having reached the
  522. high water mark for all peers, then any messages sent to the socket shall be dropped
  523. until the mute state ends. Likewise, any messages routed to a peer for which
  524. the individual high water mark has been reached shall also be dropped. If,
  525. 'ZMQ_ROUTER_MANDATORY' is set to '1', the socket shall block or return EAGAIN in
  526. both cases.
  527. When a 'ZMQ_ROUTER' socket has 'ZMQ_ROUTER_MANDATORY' flag set to '1', the
  528. socket shall generate 'ZMQ_POLLIN' events upon reception of messages from one
  529. or more peers. Likewise, the socket shall generate 'ZMQ_POLLOUT' events when
  530. at least one message can be sent to one or more peers.
  531. When a 'ZMQ_REQ' socket is connected to a 'ZMQ_ROUTER' socket, in addition to the
  532. _routing id_ of the originating peer each message received shall contain an empty
  533. _delimiter_ message part. Hence, the entire structure of each received message
  534. as seen by the application becomes: one or more _routing id_ parts, _delimiter_
  535. part, one or more _body parts_. When sending replies to a 'ZMQ_REQ' socket the
  536. application must include the _delimiter_ part.
  537. [horizontal]
  538. .Summary of ZMQ_ROUTER characteristics
  539. Compatible peer sockets:: 'ZMQ_DEALER', 'ZMQ_REQ', 'ZMQ_ROUTER'
  540. Direction:: Bidirectional
  541. Send/receive pattern:: Unrestricted
  542. Outgoing routing strategy:: See text
  543. Incoming routing strategy:: Fair-queued
  544. Action in mute state:: Drop (see text)
  545. RETURN VALUE
  546. ------------
  547. The _zmq_socket()_ function shall return an opaque handle to the newly created
  548. socket if successful. Otherwise, it shall return NULL and set 'errno' to one of
  549. the values defined below.
  550. ERRORS
  551. ------
  552. *EINVAL*::
  553. The requested socket 'type' is invalid.
  554. *EFAULT*::
  555. The provided 'context' is invalid.
  556. *EMFILE*::
  557. The limit on the total number of open 0MQ sockets has been reached.
  558. *ETERM*::
  559. The context specified was shutdown or terminated.
  560. EXAMPLE
  561. -------
  562. .Creating a simple HTTP server using ZMQ_STREAM
  563. ----
  564. void *ctx = zmq_ctx_new ();
  565. assert (ctx);
  566. /* Create ZMQ_STREAM socket */
  567. void *socket = zmq_socket (ctx, ZMQ_STREAM);
  568. assert (socket);
  569. int rc = zmq_bind (socket, "tcp://*:8080");
  570. assert (rc == 0);
  571. /* Data structure to hold the ZMQ_STREAM routing id */
  572. uint8_t routing_id [256];
  573. size_t routing_id_size = 256;
  574. /* Data structure to hold the ZMQ_STREAM received data */
  575. uint8_t raw [256];
  576. size_t raw_size = 256;
  577. while (1) {
  578. /* Get HTTP request; routing id frame and then request */
  579. routing_id_size = zmq_recv (socket, routing_id, 256, 0);
  580. assert (routing_id_size > 0);
  581. do {
  582. raw_size = zmq_recv (socket, raw, 256, 0);
  583. assert (raw_size >= 0);
  584. } while (raw_size == 256);
  585. /* Prepares the response */
  586. char http_response [] =
  587. "HTTP/1.0 200 OK\r\n"
  588. "Content-Type: text/plain\r\n"
  589. "\r\n"
  590. "Hello, World!";
  591. /* Sends the routing id frame followed by the response */
  592. zmq_send (socket, routing_id, routing_id_size, ZMQ_SNDMORE);
  593. zmq_send (socket, http_response, strlen (http_response), 0);
  594. /* Closes the connection by sending the routing id frame followed by a zero response */
  595. zmq_send (socket, routing_id, routing_id_size, ZMQ_SNDMORE);
  596. zmq_send (socket, 0, 0, 0);
  597. }
  598. zmq_close (socket);
  599. zmq_ctx_destroy (ctx);
  600. ----
  601. SEE ALSO
  602. --------
  603. linkzmq:zmq_init[3]
  604. linkzmq:zmq_setsockopt[3]
  605. linkzmq:zmq_bind[3]
  606. linkzmq:zmq_connect[3]
  607. linkzmq:zmq_send[3]
  608. linkzmq:zmq_recv[3]
  609. linkzmq:zmq_inproc[7]
  610. linkzmq:zmq[7]
  611. AUTHORS
  612. -------
  613. This page was written by the 0MQ community. To make a change please
  614. read the 0MQ Contribution Policy at <http://www.zeromq.org/docs:contributing>.