ZeroMQ - Socket Types



In ZeroMQ, the Socket is a standard API (Application Programming Interface) for the network programming. That's why the ZeroMQ presents the familiar socket-based API's. One of the most interesting characteristics of ZeroMQ for developers is its use of different socket types to implement any messaging pattern.

Before discussing the socket types, it's important to first understand the concept of Messaging patterns. Understanding these patterns will provide clarity on how different sockets operate and are used in ZeroMQ.

Messaging Patterns

A Messaging pattern refers to the specific design and behavior of how messages are exchanged between different components of a distributed system using ZeroMQ sockets. ZeroMQ provides several built-in messaging patterns that satisfy various communication needs.

Following are the built-in core messaging patterns −

  • Request-reply Messaging Pattern
  • Pub-sub (Publish-subscribe) Messaging Pattern
  • Pipeline Messaging Pattern
  • Exclusive Pair Messaging Pattern

There are more ZeroMQ patterns that are still in draft state as follows −

  • Client-server Messaging Pattern
  • Radio-dish Messaging Pattern

Socket Types

The ZeroMQ messaging library provides several socket type that defines the semantics of the socket, including how it routes messages, and queues. Following is the list of messaging patterns and the sockets that belong to these patterns −

Socket types

Request-reply pattern

  • REQ Socket
  • REP Socket
  • Dealer Socket
  • Router Socket

Publish-subscribe pattern

  • PUB Socket
  • SUB Socket
  • XPUB Socket
  • XSUB Socket

Pipeline pattern

  • PUSH Socket
  • PULL Socket

Exclusive pair pattern

  • PAIR Socket

REQ Socket

The REQ is one of the initial socket types of the ZeroMQ messaging library comes under the synchronous flavors of the request-reply pattern. The request-reply pattern is designed for various kinds of service-oriented architectures.

The REQ socket is used by the client to send requests to and receive replies from a service. This type of socket allows only an altering sequence of sends and subsequent receive calls. It may connect to any number of REP or Router socket types..

Following is the summary of characteristics of the "REQ" Socket:

Characteristic Description
Compatible peer sockets REQ, DEALER
Direction Bidirectional
Send/receive pattern Receive, Send, Receive, Send ...
Outgoing routing strategy Fair-robin
Incoming routing strategy Last peer
Action in mute state Block

REP Socket

The REP socket is used by the services to receive requests from send replies to a client. This socket type allows only an alternating sequence ofreceiveand subsequentsendcalls. If the original requester does not exist anymore the reply is silently discarded.

Following is the summary of characteristics of the "REP" Socket:

Characteristic Description
Compatible peer sockets REQ, DEALER
Direction Bidirectional
Send/receive pattern Receive, Send, Receive, Send ...
Outgoing routing strategy Fair-robin
Incoming routing strategy Last peer

Dealer Socket

The DEALER socket type is a powerful and flexible messaging pattern used for building advanced communication patterns like request-reply, publish-subscribe, or other custom messaging schemes. It uses the "Round-Robin" algorithm to send and receive messages.

This socket works as an asynchronous replacement forREQ, for clients that communicate toREPorROUTERservers, and the messages received by aDEALERare fair-queued from all connected peers.

Following is the summary of characteristics of the "Dealer" Socket:

Characteristic Description
Compatible peer sockets ROUTER, REP, DEALER
Direction Bidirectional
Send/receive pattern Unrestricted
Outgoing routing strategy Round-robin
Incoming routing strategy Fair-queued
Action in mute state Block

Router Socket

A ROUTER socket is one of the socket types used for advanced message routing patterns. It enables complex routing logic by allowing you to address messages to specific clients. ROUTERworks as an asynchronous replacement forREP, and is often used as the basis for servers that communicate toDEALERclients.

It can send messages to specific peers using an address, which is often an identity string.

This socket supports message framing, which means it can handle multi-part messages. Each message sent through a ROUTER socket can be composed of multiple parts, where the first part is often used as a routing address, and the following parts contain the actual data.

Following is the summary of characteristics of the "Dealer" Socket:

Characteristic Description
Compatible peer sockets DEALER, REQ, ROUTER
Direction Bidirectional
Send/receive pattern Unrestricted
Outgoing routing strategy See text
Incoming routing strategy Fair-queued
Action in mute state Drop (see text)

PUB Socket

A PUB (publish) is one of the core socket types used by the publisher to distribute data referring to implementing the publish-subscribe messaging pattern. It is designed to send messages to multiple subscribers. This socket type is not able to receive any messages from the server.

Following is the summary of characteristics of the "PUB" Socket:

Characteristic Description
Compatible peer sockets SUB, XSUB
Direction Unidirectional
Send/receive pattern Send only
Outgoing routing strategy N/A
Incoming routing strategy Fan out
Action in mute state Drop

SUB Socket

A SUB (Subscribe) is one of the socket types used by the subscriber to subscribe to the data distributed by the publisher. Initially, aSUBsocket is not subscribed to any messages.

Following is the summary of characteristics of the "SUB" Socket:

Characteristic Description
Compatible peer sockets PUB, XPUB
Direction Unidirectional
Send/receive pattern Receive only
Outgoing routing strategy Fair-queued
Incoming routing strategy N/A

XPUB Socket

An XPUB (Extended Publish) socket is an advanced version of the PUB (Publish) socket used in the publish-subscribe messaging pattern. The XPUB socket provides additional features compared to a standard PUB socket.

Same asPUB, except that you can receive subscriptions from the peers in form of incoming messages. The subscription message is a byte 1 (for subscriptions) or byte 0 (for unsubscriptions) followed by the subscription body.

Following is the summary of characteristics of the "XPUB" Socket:

Characteristic Description
Compatible peer sockets ZMQ_SUB, ZMQ_XSUB
Direction Unidirectional
Send/receive pattern Send messages, receive subscriptions
Outgoing routing strategy N/A
Incoming routing strategy Fan out
Action in mute state Drop

XSUB Socket

An XSUB (Extended Subscribe) socket is an advanced version of the SUB (Subscribe) socket used in the publish-subscribe messaging pattern. The XSUB socket provides additional features compared to a standard SUB socket.

Same asSUBexcept that you subscribe by sending subscription messages to the socket. The subscription message is a byte 1 (for subscriptions) or byte 0 (for non-subscriptions) followed by the subscription body.

Following is the summary of characteristics of the "XSUB" Socket:

Characteristic Description
Compatible peer sockets ZMQ_PUB, ZMQ_XPUB
Direction Unidirectional
Send/receive pattern Receive messages, send subscriptions
Outgoing routing strategy Fair-queued
Incoming routing strategy N/A
Action in mute state Drop

PUSH Socket

APUSH socket is a type of socket that is designed for sending messages to one or more PULL sockets using a "round-robin" algorithm in a simple and scalable manner. Each connected PULL socket receives messages from the PUSH socket in a round-robin technique, ensuring that the workload is balanced among multiple receivers.

Following is the summary of characteristics of the "PUSH" Socket:

Characteristic Description
Compatible peer sockets PULL
Direction Unidirectional
Send/receive pattern Send only
Outgoing routing strategy N/A
Incoming routing strategy Round-robin
Action in mute state Block

PULL Socket

APULLsocket is a type of socket that is designed for receiving messages using a "fair-queuing" algorithm in a simple and scalable manner. It is often used in conjunction with a PUSH socket, which sends messages to one or more PULL sockets.

Following is the summary of characteristics of the "PULL" Socket:

Characteristic Description
Compatible peer sockets PUSH
Direction Unidirectional
Send/receive pattern Receive only
Outgoing routing strategy Fair-queued
Incoming routing strategy N/A
Action in mute state Block

PAIR Socket

A PAIR socket is a type of socket that belongs to the "Exclusive pair pattern". It is designed for a simple, one-to-one communication pattern and allows for a direct connection between two peers, providing a way for them to send messages back and onwards.

Advertisements