
- ZeroMQ - Home
- ZeroMQ - Overview
- ZeroMQ - Installation
- ZeroMQ - Features
- ZeroMQ Messaging
- ZeroMQ - Socket Types
- ZeroMQ - Communication Patterns
- ZeroMQ - Transport Protocols
- ZeroMQ - Message Framing
- Scaling & Performance
- ZeroMQ - Load Balancing
- ZeroMQ - SMP
- ZeroMQ - Multithreading
- ZeroMQ - Performance Considerations
- ZeroMQ Useful Resources
- ZeroMQ - Quick Guide
- ZeroMQ - Useful Resources
- ZeroMQ - Discussion
ZeroMQ - Synchronous Message Processing
The ZeroMQ is a high-performance asynchronous universal messaging library used for building distributed or concurrent applications. It is also known as MQ. As we know that ZeroMQ is primarily designed for asynchronous message passing, you can also implement synchronous message processing patterns using various socket types and messaging patterns.
Key Concepts
The following are important topics that you should know while performing synchronous message processing in ZeroMQ −
Socket: In ZeroMQ, a socket is an endpoint (API) for sending or receiving data between client and server. It represents a communication channel between two components (such as processes or devices) that allows them to exchange data. ZeroMQ provides different types of sockets for various messaging patterns such as PUB/SUB, REQ/REP, PUSH/PULL, etc.
Messaging Queuing: The ZeroMQ sockets are used to send and receive messages. They dont require a dedicated message broker, as the messaging patterns are implemented in the library itself, which makes it "broker less".
Asynchronous Nature: The ZeroMQ is primarily designed as asynchronous, which means it is designed to handle multiple operations at once, which is why it is often used for high-performance, non-blocking communication.
Here is the diagram of Synchronous Message Processing −

Implementing Synchronous Processing
The synchronous process is a type of operation where tasks or operations are executed sequentially, with each step or action waiting for the previous one to complete before proceeding.
To achieve synchronous processing with ZeroMQ, which follows an asynchronous mechanism by default, you can use specific patterns and socket types. The most common approach for synchronous message processing is using the REQ (request) and REP (reply) sockets, which follow a request-reply pattern. In the REQ/REP request pattern −
- REQ Socket: It is used by client to send request or data to the server.
- REP Socket: It is used by server to respond to received request.
Synchronous Message Processing in Java
Here is a basic example of synchronous message processing using ZeroMQ in Java −
Server code (REP Socket)
import org.zeromq.ZContext; import org.zeromq.SocketType; import org.zeromq.ZMQ; import org.zeromq.ZContext; public class Response { public static void main(String[] args) { try (ZContext context = new ZContext()) { ZMQ.Socket socket = context.createSocket(ZMQ.REP); socket.bind("tcp://127.0.0.1:5555"); while (true) { // Wait for the next request from the client String message = socket.recvStr(); System.out.println("Received request: " + message); // Send a reply back to the client socket.send("Welcome, " + message); } } } }
Client code (REQ Socket)
import org.zeromq.ZContext; import org.zeromq.SocketType; import org.zeromq.ZMQ; import org.zeromq.ZContext; public class Request { public static void main(String[] args) { ZMQ.Context context = ZMQ.context(1); ZMQ.Socket socket = context.socket(ZMQ.REQ); socket.connect("tcp://127.0.0.1:5555"); // Send a request socket.send("to World".getBytes(ZMQ.CHARSET), 0); // Receive the reply byte[] reply = socket.recv(0); System.out.println("Received reply: "+new String(reply,ZMQ.CHARSET)); socket.close(); context.term(); } }
Output
After executing the above programs, the following output will be displayed −
Received reply: Welcome, to World
Synchronous Message Processing in Python
Here is a basic example of synchronous message processing using ZeroMQ in Python −
Server code (REP Socket)
import zmq def main(): context = zmq.Context() socket = context.socket(zmq.REP) socket.bind("tcp://127.0.0.1:5555") while True: # Wait for the next request from the client message = socket.recv_string() print(f"Received request: {message}") # Send a reply back to the client socket.send_string(f"Welcome, {message}") if __name__ == "__main__": main()
Client code (REQ Socket)
import zmq def main(): context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://127.0.0.1:5555") # Send a request socket.send_string("to World") # Receive the reply message = socket.recv_string() print(f"Received reply: {message}") if __name__ == "__main__": main()
Output
The above programs produces the following output −
Received reply: Welcome, to World