summaryrefslogtreecommitdiff
path: root/doc/zmq_device.txt
blob: d5d7a187e37c506b6b5f398253b136171ffb6b7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
zmq_device(3)
=============

NAME
----
zmq_device - start built-in 0MQ device


SYNOPSIS
--------
*int zmq_device (int 'device', const void '*frontend', const void '*backend');*


DESCRIPTION
-----------
The _zmq_device()_ function starts a built-in 0MQ device.  The 'device'
argument is one of:

'ZMQ_QUEUE'::
    starts a queue device
'ZMQ_FORWARDER'::
    starts a forwarder device
'ZMQ_STREAMER'::
    starts a streamer device

The device connects a frontend socket to a backend socket.  Conceptually, data
flows from frontend to backend.  Depending on the socket types, replies may
flow in the opposite direction.

Before calling _zmq_device()_ you must set any socket options, and connect or
bind both frontend and backend sockets.  The two conventional device models
are:

* proxy model - accept inward connections to frontend socket (by binding it to
  an endpoint), and make onward connections through backend socket (connecting
  to endpoints on other nodes). A proxy device model can fit well into an
  existing topology.
* broker model - accept connections on both frontend and backend sockets (by
  binding both to endpoints).  A broker device model creates a star topology
  where nodes can come and go at any time.

_zmq_device()_ runs in the current thread and returns only if/when the current
context is closed.


QUEUE DEVICE
------------
'ZMQ_QUEUE' creates a shared queue that collects requests from a set of
clients, and distributes these fairly among a set of services.  Requests are
fair-queued from frontend connections and load-balanced between backend
connections. Replies automatically return to the client that made the original
request.

This device is part of the 'request-reply' pattern.  The frontend speaks to
clients and the backend speaks to services.  You should use 'ZMQ_QUEUE' with a
'ZMQ_XREP' socket for the frontend and a 'ZMQ_XREQ' socket for the backend.
Other combinations are not documented.

Refer to linkzmq:zmq_socket[3] for a description of these socket types.


FORWARDER DEVICE
----------------
'ZMQ_FORWARDER' collects messages from a set of publishers and forwards these
to a set of subscribers.  You will generally use this to bridge networks, e.g.
read on TCP unicast and forward on multicast.

This device is part of the 'publish-subscribe' pattern.  The frontend speaks to
publishers and the backend speaks to subscribers.  You should use
'ZMQ_FORWARDER' with a 'ZMQ_SUB' socket for the frontend and a 'ZMQ_PUB' socket
for the backend.  Other combinations are not documented.

Refer to linkzmq:zmq_socket[3] for a description of these socket types.


STREAMER DEVICE
---------------
'ZMQ_STREAMER' collects tasks from a set of pushers and forwards these to a set
of pullers.  You will generally use this to bridge networks.  Messages are
fair-queued from pushers and load-balanced to pullers.

This device is part of the 'pipeline' pattern.  The frontend speaks to pushers
and the backend speaks to pullers.  You should use 'ZMQ_STREAMER' with a
'ZMQ_PULL' socket for the frontend and a 'ZMQ_PUSH' socket for the backend.
Other combinations are not documented.

Refer to linkzmq:zmq_socket[3] for a description of these socket types.


RETURN VALUE
------------
The _zmq_device()_ function shall not return if successful. Otherwise it shall
return `-1` and set 'errno' to one of the values defined below.


ERRORS
------
*ETERM*::
The 0MQ 'context' associated with the specified 'frontend' or 'backend' was 
terminated.
*EFAULT*::
The provided 'frontend' or 'backend' was not valid (NULL).


EXAMPLE
-------
.Creating a queue broker
----
//  Create frontend and backend sockets
void *frontend = zmq_socket (context, ZMQ_XREP);
assert (backend);
void *backend = zmq_socket (context, ZMQ_XREQ);
assert (frontend);
//  Bind both sockets to TCP ports
assert (zmq_bind (frontend, "tcp://*:5555") == 0);
assert (zmq_bind (backend, "tcp://*:5556") == 0);
//  Start a queue device
zmq_device (ZMQ_QUEUE, frontend, backend);
----

.Creating a pubsub proxy
----
//  Create frontend and backend sockets
void *frontend = zmq_socket (context, ZMQ_SUB);
assert (backend);
void *backend = zmq_socket (context, ZMQ_PUB);
assert (frontend);
//  Connect frontend to publisher
assert (zmq_bind (frontend, "tcp://192.68.55.112:4444") == 0);
//  Bind backend to TCP port
assert (zmq_bind (backend, "tcp://*:5556") == 0);
//  Start a forwarder device
zmq_device (ZMQ_FORWARDER, frontend, backend);
----


SEE ALSO
--------
linkzmq:zmq_bind[3]
linkzmq:zmq_connect[3]
linkzmq:zmq_socket[3]
linkzmq:zmq[7]