26

Can anyone tell what are the types of zmq.sockets?

In what situation one can use these sockets?

The main difference I need is zmq.DEALER and zmq.ROUTER in python zeroMQ?

Which type of socket can use these sockets?

user3666197
  • 1
  • 6
  • 50
  • 92
user27
  • 1,557
  • 4
  • 16
  • 16

1 Answers1

44

DEALER and ROUTER are sockets, which can allow easy scaling of REQ / REP pairs.

In direct communication, REQ and REP are talking in blocking manner.

ROUTER - accept requests - from REQ side

A router is able to accepts requests, adds an envelope with information about that requestee, and makes this new message available for further processing by interconnecting code). When the response comes back (in an envelop), it can pass the response back to the requestee.

DEALER - talks to workers - on REP side

Dealer cares about workers. Note, that to make the whole solution usable, workers have to connect to the dealer, not the other way around.

DEALER also allows non-blocking connection with REP.

Some connecting code passes a request in an envelope to the DEALER. The dealer manages the distribution of such requests to workers (without the envelope) and later responds back to the interconnecting code (again in an envelope).

Interconnecting code

An interconnecting code is to shuffle messages between ROUTER and DEALER sockets.

The simplest version is here: http://zguide.zeromq.org/py:rrbroker

# Simple request-reply broker
#
# Author: Lev Givon <lev(at)columbia(dot)edu>

import zmq

# Prepare our context and sockets
context = zmq.Context()
frontend = context.socket(zmq.ROUTER)
backend = context.socket(zmq.DEALER)
frontend.bind("tcp://*:5559")
backend.bind("tcp://*:5560")

# Initialize poll set
poller = zmq.Poller()
poller.register(frontend, zmq.POLLIN)
poller.register(backend, zmq.POLLIN)

# Switch messages between sockets
while True:
  socks = dict(poller.poll())

  if socks.get(frontend) == zmq.POLLIN:
    message = frontend.recv_multipart()
    backend.send_multipart(message)

  if socks.get(backend) == zmq.POLLIN:
    message = backend.recv_multipart()
    frontend.send_multipart(message)
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • 4
    Thanks this was a great simple explanation. I was reading the great book 'Node: the Right Way' but was a little stumped by their diagrams and explanation of DEALER/ROUTERs but yours cleared it up. – armyofda12mnkeys Jan 09 '15 at 08:37
  • Thank you. Is router also a non blocking? – Avv Sep 21 '22 at 13:53
  • 1
    @Avv, yes. Router is non-blocking. We could write a server with only the router that waits for requests from several REQ clients, process them and replies to each after that. – Ilia Gilmijarow Apr 11 '23 at 11:25
  • how it can handle multiple requests without blocking please? – Avv Apr 11 '23 at 14:55