Yves
Yves

Reputation: 12371

ZeroMQ: many to many PUB/SUB

I am new to ZeroMQ. I am programming a cpp project:

There are different types of Agent.
Each type of agent can have more than one agent at one time.
Each agent will send some message to my server.
The different types of agents use different ports.
The same type of agents use same port.

For example, we have two types of agent: A and B.
For type A, we have two agents: agentA1 and agentA2;
For type B, we have three agents: agentB1, agentB2 and agentB3.

Now, I run the five agents at the same time, so they will send messages to server.
agentA1 and agentA2 send messages through port 5552,
agentB1, agentB2 and agentB3 send messages through port 5553.

What I am willing to do is that to make each agent as a publisher and to make my server as a subscriber.
The subscriber will listen 5552 and 5553, he will do something as soon as some message comes through the two ports.

Now I know that there is a ZeroMQ pattern: PUB/SUB.

However, this pattern has ONLY ONE publisher and many subscribers.
For me, I think I need many publishers and many subscribers. Each port corresponds with one subscriber and it receives messages from many agents.

I don't know how to realize these needs.

Upvotes: 1

Views: 2787

Answers (1)

Jason
Jason

Reputation: 13766

What you're talking about will work just fine on ZMQ. As you've seen, each publisher can have many subscribers. But each subscriber can, itself, subscribe to many publishers. Additionally, typically you bind() on the publisher and connect() on the subscribers, but you can reverse that as well. The only conventional limitation is that each socket only binds on a single port. So, your set up would be something like the following:

  (CLIENTS - connect)                   (SERVER - bind)

AgentA1 (PUB) :5552 --------------
                                  |
AgentA2 (PUB) :5552 -------------- -  :5552 (SUB) Server Socket A


AgentB1 (PUB) :5553 -------------- -  :5553 (SUB) Server Socket B
                                  |
AgentB2 (PUB) :5553 --------------
                                  |
AgentB3 (PUB) :5553 --------------

... now, you don't strictly need to use separate ports for these two communication types, you could just use a multi-frame message and indicate the message/agent type with the first frame of your message, which has the side benefit of allowing you to subscribe only to the messages you're interested in, should your project grow to need that functionality.

Upvotes: 5

Related Questions