zmq_socket.txt 22.5 KB
Newer Older
1 2 3 4 5 6
zmq_socket(3)
=============


NAME
----
Martin Lucina's avatar
Martin Lucina committed
7
zmq_socket - create 0MQ socket
8 9 10 11


SYNOPSIS
--------
Martin Lucina's avatar
Martin Lucina committed
12
*void *zmq_socket (void '*context', int 'type');*
13 14 15 16


DESCRIPTION
-----------
Martin Lucina's avatar
Martin Lucina committed
17 18
The 'zmq_socket()' function shall create a 0MQ socket within the specified
'context' and return an opaque handle to the newly created socket. The 'type'
Martin Lucina's avatar
Martin Lucina committed
19
argument specifies the socket type, which determines the semantics of
Martin Lucina's avatar
Martin Lucina committed
20 21
communication over the socket.

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
The newly created socket is initially unbound, and not associated with any
endpoints. In order to establish a message flow a socket must first be
connected to at least one endpoint with linkzmq:zmq_connect[3], or at least one
endpoint must be created for accepting incoming connections with
linkzmq:zmq_bind[3].

.Key differences to conventional sockets
Generally speaking, conventional sockets present a _synchronous_ interface to
either connection-oriented reliable byte streams (SOCK_STREAM), or
connection-less unreliable datagrams (SOCK_DGRAM). In comparison, 0MQ sockets
present an abstraction of an asynchronous _message queue_, with the exact
queueing semantics depending on the socket type in use. Where conventional
sockets transfer streams of bytes or discrete datagrams, 0MQ sockets transfer
discrete _messages_.

0MQ sockets being _asynchronous_ means that the timings of the physical
38
connection setup and tear down, reconnect and effective delivery are transparent
39 40 41 42 43 44 45 46 47 48
to the user and organized by 0MQ itself. Further, messages may be _queued_ in
the event that a peer is unavailable to receive them.

Conventional sockets allow only strict one-to-one (two peers), many-to-one
(many clients, one server), or in some cases one-to-many (multicast)
relationships. With the exception of 'ZMQ_PAIR', 0MQ sockets may be connected
*to multiple endpoints* using _zmq_connect()_, while simultaneously accepting
incoming connections *from multiple endpoints* bound to the socket using
_zmq_bind()_, thus allowing many-to-many relationships.

49
.Thread safety
50 51
0MQ has both thread safe socket type and _not_ thread safe socket types.
Applications MUST NOT use a _not_ thread safe socket
52
from multiple threads except after migrating a socket from one thread to
53
another with a "full fence" memory barrier.
54

55 56 57 58 59 60 61 62
Following are the thread safe sockets:
* ZMQ_CLIENT
* ZMQ_SERVER
* ZMQ_DISH
* ZMQ_RADIO
* ZMQ_SCATTER
* ZMQ_GATHER

63
.Socket types
64 65
The following sections present the socket types defined by 0MQ, grouped by the
general _messaging pattern_ which is built from related socket types.
66 67


68 69 70 71 72 73 74 75 76
Client-server pattern
~~~~~~~~~~~~~~~~~~~~~

The client-server pattern is used to allow a single 'ZMQ_SERVER' _server_ talk
to one or more 'ZMQ_CLIENT' _clients_. The client always starts the conversation,
after which either peer can send messages asynchronously, to the other.

The client-server pattern is formally defined by http://rfc.zeromq.org/spec:41.

77 78 79
Note: this pattern is meant to eventually deprecate the use of 'ZMQ_DEALER' and
'ZMQ_ROUTER' to build client-server architectures, as well as 'ZMQ_REP' and
'ZMQ_REQ' for request-reply.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

ZMQ_CLIENT
^^^^^^^^^^
A 'ZMQ_CLIENT' socket talks to a 'ZMQ_SERVER' socket. Either peer can connect,
though the usual and recommended model is to bind the 'ZMQ_SERVER' and connect
the 'ZMQ_CLIENT'.

If the 'ZMQ_CLIENT' socket has established a connection, linkzmq:zmq_send[3]
will accept messages, queue them, and send them as rapidly as the network
allows. The outgoing buffer limit is defined by the high water mark for the
socket. If the outgoing buffer is full, or if there is no connected peer,
linkzmq:zmq_send[3] will block, by default. The 'ZMQ_CLIENT' socket will not
drop messages.

When a 'ZMQ_CLIENT' socket is connected to multiple 'ZMQ_SERVER' sockets,
outgoing messages are distributed between connected peers on a round-robin
basis. Likewise, the 'ZMQ_CLIENT' socket receives messages fairly from each
connected  peer. This usage is sensible only for stateless protocols.

'ZMQ_CLIENT' sockets are threadsafe and can be used from multiple threads
at the same time. Note that replies from a 'ZMQ_SERVER' socket will go to
the first client thread that calls libzmq:zmq_msg_recv. If you need to get
replies back to the originating thread, use one 'ZMQ_CLIENT' socket per
thread.

105 106 107 108 109
NOTE: 'ZMQ_CLIENT' sockets are threadsafe. They do not accept the ZMQ_SNDMORE
option on sends not ZMQ_RCVMORE on receives. This limits them to single part
data. The intention is to extend the API to allow scatter/gather of multi-part
data.

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
[horizontal]
.Summary of ZMQ_CLIENT characteristics
Compatible peer sockets:: 'ZMQ_SERVER'
Direction:: Bidirectional
Send/receive pattern:: Unrestricted
Outgoing routing strategy:: Round-robin
Incoming routing strategy:: Fair-queued
Action in mute state:: Block


ZMQ_SERVER
^^^^^^^^^^
A 'ZMQ_SERVER' socket talks to a set of 'ZMQ_CLIENT' sockets. A 'ZMQ_SERVER'
socket can only reply to an incoming message: the 'ZMQ_CLIENT' peer must
always initiate a conversation.

Each received message has a 'routing_id' that is a 32-bit unsigned integer.
The application can fetch this with linkzmq:zmq_msg_routing_id[3]. To send
a message to a given 'ZMQ_CLIENT' peer the application must set the peer's
'routing_id' on the message, using linkzmq:zmq_msg_set_routing_id[3].

If the 'routing_id' is not specified, or does not refer to a connected client
peer, the send call will fail with EHOSTUNREACH. If the outgoing buffer for
133 134 135
the client peer is full, the send call shall block, unless ZMQ_DONT_WAIT is
used in the send, in which case it shall fail with EAGAIN. The 'ZMQ_SERVER'
socket shall not drop messages in any case.
136

137 138 139 140 141
NOTE: 'ZMQ_SERVER' sockets are threadsafe. They do not accept the ZMQ_SNDMORE
option on sends not ZMQ_RCVMORE on receives. This limits them to single part
data. The intention is to extend the API to allow scatter/gather of multi-part
data.

142 143 144 145 146 147 148 149 150 151
[horizontal]
.Summary of ZMQ_SERVER characteristics
Compatible peer sockets:: 'ZMQ_CLIENT'
Direction:: Bidirectional
Send/receive pattern:: Unrestricted
Outgoing routing strategy:: See text
Incoming routing strategy:: Fair-queued
Action in mute state:: Return EAGAIN


152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
Radio-dish pattern
~~~~~~~~~~~~~~~~~~~~~~~~~
The radio-dish pattern is used for one-to-many distribution of data from
a single _publisher_ to multiple _subscribers_ in a fan out fashion.

Radio-dish is using groups (vs Pub-sub topics), Dish sockets can join a group
and each message sent by Radio sockets belong to a group.

Groups are null terminated strings limited to 16 chars length (including null).
The intention is to increase the length to 40 chars (including null).

Groups are matched using exact matching (vs prefix matching of PubSub).

NOTE: Radio-dish is still in draft phase.

Note: this pattern is meant to eventually deprecate the use of 'ZMQ_PUB' and
'ZMQ_SUB' to build pub-sub architectures.

ZMQ_RADIO
^^^^^^^
A socket of type 'ZMQ_RADIO' is used by a _publisher_ to distribute data.
Each message belong to a group, a group is specified with linkzmq_zmq_msg_set_group[3].
Messages are distributed to all members of a group.
The linkzmq:zmq_recv[3] function is not implemented for this socket type.

When a 'ZMQ_RADIO' socket enters the 'mute' state due to having reached the
high water mark for a _subscriber_, then any messages that would be sent to the
_subscriber_ in question shall instead be dropped until the mute state
ends. The _zmq_send()_ function shall never block for this socket type.

NOTE: 'ZMQ_RADIO' sockets are threadsafe. They do not accept the ZMQ_SNDMORE
option on sends. This limits them to single part data.

[horizontal]
.Summary of ZMQ_RADIO characteristics
Compatible peer sockets:: 'ZMQ_DISH'
Direction:: Unidirectional
Send/receive pattern:: Send only
Incoming routing strategy:: N/A
Outgoing routing strategy:: Fan out
Action in mute state:: Drop


ZMQ_DISH
^^^^^^^
A socket of type 'ZMQ_DISH' is used by a _subscriber_ to subscribe to groups
distributed by a _radio_. Initially a 'ZMQ_DISH' socket is not subscribed to
any groups, use linkzmq:zmq_join[3] to
join a group.
To get the group the message belong to call linkzmq:zmq_msg_group[3].
The _zmq_send()_ function is not implemented for this socket type.

NOTE: 'ZMQ_DISH' sockets are threadsafe. They do not accept ZMQ_RCVMORE on receives.
This limits them to single part data.

[horizontal]
.Summary of ZMQ_DISH characteristics
Compatible peer sockets:: 'ZMQ_RADIO'
Direction:: Unidirectional
Send/receive pattern:: Receive only
Incoming routing strategy:: Fair-queued
Outgoing routing strategy:: N/A


Martin Lucina's avatar
Martin Lucina committed
216 217 218
Publish-subscribe pattern
~~~~~~~~~~~~~~~~~~~~~~~~~
The publish-subscribe pattern is used for one-to-many distribution of data from
219
a single _publisher_ to multiple _subscribers_ in a fan out fashion.
Martin Lucina's avatar
Martin Lucina committed
220

221
The publish-subscribe pattern is formally defined by http://rfc.zeromq.org/spec:29.
Martin Lucina's avatar
Martin Lucina committed
222

223 224
ZMQ_PUB
^^^^^^^
Martin Lucina's avatar
Martin Lucina committed
225
A socket of type 'ZMQ_PUB' is used by a _publisher_ to distribute data.
226
Messages sent are distributed in a fan out fashion to all connected peers.
227
The linkzmq:zmq_recv[3] function is not implemented for this socket type.
Martin Lucina's avatar
Martin Lucina committed
228

Pieter Hintjens's avatar
Pieter Hintjens committed
229
When a 'ZMQ_PUB' socket enters the 'mute' state due to having reached the
230
high water mark for a _subscriber_, then any messages that would be sent to the
Pieter Hintjens's avatar
Pieter Hintjens committed
231
_subscriber_ in question shall instead be dropped until the mute state
232
ends. The _zmq_send()_ function shall never block for this socket type.
Martin Lucina's avatar
Martin Lucina committed
233

234 235
[horizontal]
.Summary of ZMQ_PUB characteristics
236
Compatible peer sockets:: 'ZMQ_SUB', 'ZMQ_XSUB'
237
Direction:: Unidirectional
238 239
Send/receive pattern:: Send only
Incoming routing strategy:: N/A
240
Outgoing routing strategy:: Fan out
Pieter Hintjens's avatar
Pieter Hintjens committed
241
Action in mute state:: Drop
242 243 244 245


ZMQ_SUB
^^^^^^^
Martin Lucina's avatar
Martin Lucina committed
246 247
A socket of type 'ZMQ_SUB' is used by a _subscriber_ to subscribe to data
distributed by a _publisher_. Initially a 'ZMQ_SUB' socket is not subscribed to
248 249 250 251 252 253
any messages, use the 'ZMQ_SUBSCRIBE' option of linkzmq:zmq_setsockopt[3] to
specify which messages to subscribe to. The _zmq_send()_ function is not
implemented for this socket type.

[horizontal]
.Summary of ZMQ_SUB characteristics
254
Compatible peer sockets:: 'ZMQ_PUB', 'ZMQ_XPUB'
255
Direction:: Unidirectional
256 257 258
Send/receive pattern:: Receive only
Incoming routing strategy:: Fair-queued
Outgoing routing strategy:: N/A
Martin Lucina's avatar
Martin Lucina committed
259 260


261 262 263 264 265
ZMQ_XPUB
^^^^^^^^
Same as ZMQ_PUB except that you can receive subscriptions from the peers
in form of incoming messages. Subscription message is a byte 1 (for
subscriptions) or byte 0 (for unsubscriptions) followed by the subscription
266 267
body. Messages without a sub/unsub prefix are also received, but have no
effect on subscription status.
268 269 270 271

[horizontal]
.Summary of ZMQ_XPUB characteristics
Compatible peer sockets:: 'ZMQ_SUB', 'ZMQ_XSUB'
272
Direction:: Unidirectional
273 274 275
Send/receive pattern:: Send messages, receive subscriptions
Incoming routing strategy:: N/A
Outgoing routing strategy:: Fan out
Pieter Hintjens's avatar
Pieter Hintjens committed
276
Action in mute state:: Drop
277 278


279 280
ZMQ_XSUB
^^^^^^^^
281 282
Same as ZMQ_SUB except that you subscribe by sending subscription messages to
the socket. Subscription message is a byte 1 (for subscriptions) or byte 0
283 284
(for unsubscriptions) followed by the subscription body. Messages without a
sub/unsub prefix may also be sent, but have no effect on subscription status.
285 286 287 288

[horizontal]
.Summary of ZMQ_XSUB characteristics
Compatible peer sockets:: 'ZMQ_PUB', 'ZMQ_XPUB'
289
Direction:: Unidirectional
290 291 292
Send/receive pattern:: Receive messages, send subscriptions
Incoming routing strategy:: Fair-queued
Outgoing routing strategy:: N/A
Pieter Hintjens's avatar
Pieter Hintjens committed
293
Action in mute state:: Drop
294 295


296 297 298
Pipeline pattern
~~~~~~~~~~~~~~~~
The pipeline pattern is used for distributing data to _nodes_ arranged in
Martin Lucina's avatar
Martin Lucina committed
299 300
a pipeline. Data always flows down the pipeline, and each stage of the pipeline
is connected to at least one _node_. When a pipeline stage is connected to
301
multiple _nodes_ data is round-robined among all connected _nodes_.
Martin Lucina's avatar
Martin Lucina committed
302

303
The pipeline pattern is formally defined by http://rfc.zeromq.org/spec:30.
Martin Lucina's avatar
Martin Lucina committed
304

305 306 307
ZMQ_PUSH
^^^^^^^^
A socket of type 'ZMQ_PUSH' is used by a pipeline _node_ to send messages
308
to downstream pipeline _nodes_. Messages are round-robined to all connected
309 310
downstream _nodes_. The _zmq_recv()_ function is not implemented for this
socket type.
Martin Lucina's avatar
Martin Lucina committed
311

Pieter Hintjens's avatar
Pieter Hintjens committed
312
When a 'ZMQ_PUSH' socket enters the 'mute' state due to having reached the
313 314
high water mark for all downstream _nodes_, or if there are no downstream
_nodes_ at all, then any linkzmq:zmq_send[3] operations on the socket shall
Pieter Hintjens's avatar
Pieter Hintjens committed
315
block until the mute state ends or at least one downstream _node_
316 317
becomes available for sending; messages are not discarded.

318
[horizontal]
319 320
.Summary of ZMQ_PUSH characteristics
Compatible peer sockets:: 'ZMQ_PULL'
321 322 323
Direction:: Unidirectional
Send/receive pattern:: Send only
Incoming routing strategy:: N/A
324
Outgoing routing strategy:: Round-robin
Pieter Hintjens's avatar
Pieter Hintjens committed
325
Action in mute state:: Block
326

Martin Lucina's avatar
Martin Lucina committed
327

328 329 330 331 332 333 334
ZMQ_PULL
^^^^^^^^
A socket of type 'ZMQ_PULL' is used by a pipeline _node_ to receive messages
from upstream pipeline _nodes_. Messages are fair-queued from among all
connected upstream _nodes_. The _zmq_send()_ function is not implemented for
this socket type.

335
[horizontal]
336 337
.Summary of ZMQ_PULL characteristics
Compatible peer sockets:: 'ZMQ_PUSH'
338 339 340 341
Direction:: Unidirectional
Send/receive pattern:: Receive only
Incoming routing strategy:: Fair-queued
Outgoing routing strategy:: N/A
Pieter Hintjens's avatar
Pieter Hintjens committed
342
Action in mute state:: Block
343

Martin Lucina's avatar
Martin Lucina committed
344

345 346
Exclusive pair pattern
~~~~~~~~~~~~~~~~~~~~~~
347 348 349
The exclusive pair pattern is used to connect a peer to precisely one other
peer. This pattern is used for inter-thread communication across the inproc
transport.
Martin Lucina's avatar
Martin Lucina committed
350

351
The exclusive pair pattern is formally defined by http://rfc.zeromq.org/spec:31.
Martin Lucina's avatar
Martin Lucina committed
352

353 354
ZMQ_PAIR
^^^^^^^^
355 356 357
A socket of type 'ZMQ_PAIR' can only be connected to a single peer at any one
time.  No message routing or filtering is performed on messages sent over a
'ZMQ_PAIR' socket.
Martin Lucina's avatar
Martin Lucina committed
358

Pieter Hintjens's avatar
Pieter Hintjens committed
359
When a 'ZMQ_PAIR' socket enters the 'mute' state due to having reached the
360 361 362 363
high water mark for the connected peer, or if no peer is connected, then
any linkzmq:zmq_send[3] operations on the socket shall block until the peer
becomes available for sending; messages are not discarded.

364 365
NOTE: 'ZMQ_PAIR' sockets are designed for inter-thread communication across
the linkzmq:zmq_inproc[7] transport and do not implement functionality such
366
as auto-reconnection.
367

368 369 370 371 372 373 374
[horizontal]
.Summary of ZMQ_PAIR characteristics
Compatible peer sockets:: 'ZMQ_PAIR'
Direction:: Bidirectional
Send/receive pattern:: Unrestricted
Incoming routing strategy:: N/A
Outgoing routing strategy:: N/A
Pieter Hintjens's avatar
Pieter Hintjens committed
375
Action in mute state:: Block
376

377

378 379
Native Pattern
~~~~~~~~~~~~~~
380
The native pattern is used for communicating with TCP peers and allows
381 382 383 384 385
asynchronous requests and replies in either direction.


ZMQ_STREAM
^^^^^^^^^^
386 387
A socket of type 'ZMQ_STREAM' is used to send and receive TCP data from a
non-0MQ peer, when using the tcp:// transport. A 'ZMQ_STREAM' socket can
388 389 390
act as client and/or server, sending and/or receiving TCP data asynchronously.

When receiving TCP data, a 'ZMQ_STREAM' socket shall prepend a message part
391 392 393
containing the _identity_ of the originating peer to the message before passing
it to the application. Messages received are fair-queued from among all
connected peers.
394

395 396
When sending TCP data, a 'ZMQ_STREAM' socket shall remove the first part of the
message and use it to determine the _identity_ of the peer the message shall be
397 398
routed to, and unroutable messages shall cause an EHOSTUNREACH or EAGAIN error.

399
To open a connection to a server, use the zmq_connect call, and then fetch the
400 401
socket identity using the ZMQ_IDENTITY zmq_getsockopt call.

402 403 404 405 406 407
To close a specific connection, send the identity frame followed by a
zero-length message (see EXAMPLE section).

When a connection is made, a zero-length message will be received by the
application.  Similarly, when the peer disconnects (or the connection is lost),
a zero-length message will be received by the application.
408

409
The ZMQ_SNDMORE flag is ignored on data frames. You must send one identity frame
410 411
followed by one data frame.

412
Also, please note that omitting the ZMQ_SNDMORE flag will prevent sending further
413 414
data (from any client) on the same socket.

415 416 417 418 419 420 421 422 423 424
[horizontal]
.Summary of ZMQ_STREAM characteristics
Compatible peer sockets:: none.
Direction:: Bidirectional
Send/receive pattern:: Unrestricted
Outgoing routing strategy:: See text
Incoming routing strategy:: Fair-queued
Action in mute state:: EAGAIN


425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
Request-reply pattern
~~~~~~~~~~~~~~~~~~~~~
The request-reply pattern is used for sending requests from a ZMQ_REQ _client_
to one or more ZMQ_REP _services_, and receiving subsequent replies to each
request sent.

The request-reply pattern is formally defined by http://rfc.zeromq.org/spec:28.

Note: this pattern will be deprecated in favor of the client-server pattern.

ZMQ_REQ
^^^^^^^
A socket of type 'ZMQ_REQ' is used by a _client_ to send requests to and
receive replies from a _service_. This socket type allows only an alternating
sequence of _zmq_send(request)_ and subsequent _zmq_recv(reply)_ calls. Each
request sent is round-robined among all _services_, and each reply received is
matched with the last issued request.

If no services are available, then any send operation on the socket shall
block until at least one _service_ becomes available. The REQ socket shall
not discard messages.

[horizontal]
.Summary of ZMQ_REQ characteristics
Compatible peer sockets:: 'ZMQ_REP', 'ZMQ_ROUTER'
Direction:: Bidirectional
Send/receive pattern:: Send, Receive, Send, Receive, ...
Outgoing routing strategy:: Round-robin
Incoming routing strategy:: Last peer
Action in mute state:: Block


ZMQ_REP
^^^^^^^
A socket of type 'ZMQ_REP' is used by a _service_ to receive requests from and
send replies to a _client_. This socket type allows only an alternating
sequence of _zmq_recv(request)_ and subsequent _zmq_send(reply)_ calls. Each
request received is fair-queued from among all _clients_, and each reply sent
is routed to the _client_ that issued the last request. If the original
requester does not exist any more the reply is silently discarded.

[horizontal]
.Summary of ZMQ_REP characteristics
Compatible peer sockets:: 'ZMQ_REQ', 'ZMQ_DEALER'
Direction:: Bidirectional
Send/receive pattern:: Receive, Send, Receive, Send, ...
Incoming routing strategy:: Fair-queued
Outgoing routing strategy:: Last peer


ZMQ_DEALER
^^^^^^^^^^
A socket of type 'ZMQ_DEALER' is an advanced pattern used for extending
request/reply sockets. Each message sent is round-robined among all connected
peers, and each message received is fair-queued from all connected peers.

When a 'ZMQ_DEALER' socket enters the 'mute' state due to having reached the
high water mark for all peers, or if there are no peers at all, then any
linkzmq:zmq_send[3] operations on the socket shall block until the mute
state ends or at least one peer becomes available for sending; messages are not
discarded.

When a 'ZMQ_DEALER' socket is connected to a 'ZMQ_REP' socket each message sent
must consist of an empty message part, the _delimiter_, followed by one or more
_body parts_.

[horizontal]
.Summary of ZMQ_DEALER characteristics
Compatible peer sockets:: 'ZMQ_ROUTER', 'ZMQ_REP', 'ZMQ_DEALER'
Direction:: Bidirectional
Send/receive pattern:: Unrestricted
Outgoing routing strategy:: Round-robin
Incoming routing strategy:: Fair-queued
Action in mute state:: Block


ZMQ_ROUTER
^^^^^^^^^^
A socket of type 'ZMQ_ROUTER' is an advanced socket type used for extending
request/reply sockets. When receiving messages a 'ZMQ_ROUTER' socket shall
prepend a message part containing the _identity_ of the originating peer to the
message before passing it to the application. Messages received are fair-queued
from among all connected peers. When sending messages a 'ZMQ_ROUTER' socket shall
remove the first part of the message and use it to determine the _identity_ of
the peer the message shall be routed to. If the peer does not exist anymore
the message shall be silently discarded by default, unless 'ZMQ_ROUTER_MANDATORY'
socket option is set to '1'.

When a 'ZMQ_ROUTER' socket enters the 'mute' state due to having reached the
high water mark for all peers, then any messages sent to the socket shall be dropped
until the mute state ends. Likewise, any messages routed to a peer for which
the individual high water mark has been reached shall also be dropped.

When a 'ZMQ_REQ' socket is connected to a 'ZMQ_ROUTER' socket, in addition to the
_identity_ of the originating peer each message received shall contain an empty
_delimiter_ message part. Hence, the entire structure of each received message
as seen by the application becomes: one or more _identity_ parts, _delimiter_
part, one or more _body parts_. When sending replies to a 'ZMQ_REQ' socket the
application must include the _delimiter_ part.

[horizontal]
.Summary of ZMQ_ROUTER characteristics
Compatible peer sockets:: 'ZMQ_DEALER', 'ZMQ_REQ', 'ZMQ_ROUTER'
Direction:: Bidirectional
Send/receive pattern:: Unrestricted
Outgoing routing strategy:: See text
Incoming routing strategy:: Fair-queued
Action in mute state:: Drop


535 536
RETURN VALUE
------------
Martin Lucina's avatar
Martin Lucina committed
537 538 539
The _zmq_socket()_ function shall return an opaque handle to the newly created
socket if successful. Otherwise, it shall return NULL and set 'errno' to one of
the values defined below.
540 541 542 543 544


ERRORS
------
*EINVAL*::
Martin Lucina's avatar
Martin Lucina committed
545
The requested socket 'type' is invalid.
546
*EFAULT*::
547
The provided 'context' is invalid.
548 549
*EMFILE*::
The limit on the total number of open 0MQ sockets has been reached.
550 551
*ETERM*::
The context specified was terminated.
552

553 554 555 556 557 558 559 560 561 562 563 564 565 566
EXAMPLE
-------
.Creating a simple HTTP server using ZMQ_STREAM
----
void *ctx = zmq_ctx_new ();
assert (ctx);
/* Create ZMQ_STREAM socket */
void *socket = zmq_socket (ctx, ZMQ_STREAM);
assert (socket);
int rc = zmq_bind (socket, "tcp://*:8080");
assert (rc == 0);
/* Data structure to hold the ZMQ_STREAM ID */
uint8_t id [256];
size_t id_size = 256;
567 568 569
/* Data structure to hold the ZMQ_STREAM received data */
uint8_t raw [256];
size_t raw_size = 256;
570 571
while (1) {
	/*  Get HTTP request; ID frame and then request */
572
	id_size = zmq_recv (socket, id, 256, 0);
573
	assert (id_size > 0);
574 575 576 577
	do {
		raw_size = zmq_recv (socket, raw, 256, 0);
		assert (raw_size >= 0);
	} while (raw_size == 256);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
	/* Prepares the response */
	char http_response [] =
		"HTTP/1.0 200 OK\r\n"
		"Content-Type: text/plain\r\n"
		"\r\n"
		"Hello, World!";
	/* Sends the ID frame followed by the response */
	zmq_send (socket, id, id_size, ZMQ_SNDMORE);
	zmq_send (socket, http_response, strlen (http_response), ZMQ_SNDMORE);
	/* Closes the connection by sending the ID frame followed by a zero response */
	zmq_send (socket, id, id_size, ZMQ_SNDMORE);
	zmq_send (socket, 0, 0, ZMQ_SNDMORE);
	/* NOTE: If we don't use ZMQ_SNDMORE, then we won't be able to send more */
	/* message to any client */
}
zmq_close (socket);
zmq_ctx_destroy (ctx);
----


598 599 600 601 602 603 604 605
SEE ALSO
--------
linkzmq:zmq_init[3]
linkzmq:zmq_setsockopt[3]
linkzmq:zmq_bind[3]
linkzmq:zmq_connect[3]
linkzmq:zmq_send[3]
linkzmq:zmq_recv[3]
606
linkzmq:zmq_inproc[7]
607
linkzmq:zmq[7]
608

609 610 611

AUTHORS
-------
612 613
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>.