...

/

Communicating with Event Bus

Communicating with Event Bus

Learn how to split all the routes into a separate file and organize the application using communication with Event Bus.

Event Bus and Message Passing

Event Bus is an implementation of the Observable design pattern.

We’ve already mentioned that Vert.x is based on the concept of verticles, which are isolated actors. Kotlin’s coroutines library provides the actor() and producer() coroutine generators, which create a coroutine bound to a channel.

Similarly, all the verticles in the Vert.x framework are bound by Event Bus and can pass messages to one another using it. Now, let’s extract the code from our ServerVerticle class into a new class, which we’ll call CatVerticle.

Any verticle can send a message over Event Bus by choosing between the following methods:

  • request() will send a message to only one subscriber and wait for a response.
  • send() will send a message to only one subscriber, without waiting for a response.
  • publish() will send a message to all subscribers, without waiting for a response.

No matter which method is used to send the message, we subscribe to it using the consumer() method on Event Bus.

Now, let’s subscribe to an event in our ...