Java Reference
In-Depth Information
There's a different pattern of communication here as well. Instead of just having to send
messages to a single user, you now have the ability to publish to multiple users. Fortunately,
Vert.x's event bus also lets us publish a message to multiple handlers (see Figure 9-2 ). This
lets us use a similar overarching approach.
Figure 9-2. Event bus publishing
The only code difference is that we use the publish method on the event bus rather than the
send method. To avoid overlapping with the existing addresses whenever a user uses the !
command, it gets published to the user's name suffixed with .followers . So, for example,
when bob publishes a message it goes to any handler registered on bob.followers
( Example 9-5 ) .
Example 9-5. Broadcasting messages to followers
private
private void
void broadcastMessage ( String message ) {
String name = this
this . name . get ();
eventBus . publish ( name + ".followers" , name + '>' + message );
}
When it comes to the handler, we want to do the same operation that we performed earlier
when registering sends: pass that message along to the client ( Example 9-6 ) .
Example 9-6. Receiving the broadcast messages
private
private void
void followUser ( String user ) {
eventBus . registerHandler ( user + ".followers" , ( Message < String > message ) -> {
sendClient ( message . body ());
});
}
Search WWH ::




Custom Search