Search code examples
vert.xvertx-eventbus

What's the difference between the send and publish methods of Vertx's EventBus?


I'm having my first contact with Vertx's EventBus and I realized there are two ways to submit a message. Used the send or publish method. I ask: What is the practical difference between using these two methods and in what scenario do they use each one?


Solution

  • Both send and publish are used to send a message to an event bus address. However there are some differences between the two.

    By using publish:

    • A message is sent to one or multiple listeners
    • All handlers listening against the address will be notified
    • No answer is expected from handlers

    By using send:

    • A message is sent to one and only one handler registered against the event bus address.
    • If multiple handlers are registered, only one will be notified. The receiver will be selected by a "round-robin algorithm" as per the docs.
    • The receiver can answer the message, this answer can be empty or contain a response body. A response timeout can also be specified.

    In practical usage, publish is quite useful to inform that an event has occured, whereas send is quite handy for asking a treatment where the response matters.

    Conceptually, publish uses the publish/subscribe pattern whereas send uses the request/response pattern.