---
name: "zmq_device"
section: "3"
platform: "linux"
package: "libzmq-dev"
version: "2.2.0+dfsg-6"
release: "Debian 8 (Jessie)"
platforms: ["linux"]
url: "https://new.carta.tech/zmq_device"
---

# zmq\_device(3)

start built-in 0MQ device

## 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**

bind frontend socket to an endpoint, and connect backend socket to downstream components. A proxy device model does not require changes to the downstream topology but that topology is static (any changes require reconfiguring the device).

**broker**

bind frontend socket to one endpoint and bind backend socket to a second endpoint. Downstream components must now connect into the device. A broker device model allows a dynamic downstream topology (components 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 [**zmq\_socket**(3)](/zmq_socket/3.md) 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 [**zmq\_socket**(3)](/zmq_socket/3.md) 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 [**zmq\_socket**(3)](/zmq_socket/3.md) for a description of these socket types.

## RETURN VALUE

The *zmq\_device()* function always returns -1 and *errno* set to **ETERM** (the 0MQ *context* associated with either of the specified sockets was terminated).

## 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);
```

## SEE ALSO

[**zmq\_bind**(3)](/zmq_bind/3.md) [**zmq\_connect**(3)](/zmq_connect/3.md) [**zmq\_socket**(3)](/zmq_socket/3.md) [**zmq**(7)](/zmq/7.md)

## AUTHORS

This manual page was written by the 0MQ community.
