Module: vertx/event_bus

vertx/event_bus

Represents a distributed lightweight event bus which can encompass multiple vert.x instances. It is very useful for otherwise isolated vert.x application instances to communicate with each other. Messages sent over the event bus are JSON objects.

The event bus implements a distributed publish / subscribe network. Messages are sent to an address. There can be multiple handlers registered against that address. Any handlers with a matching name will receive the message irrespective of what vert.x application instance and what vert.x instance they are located in.

All messages sent over the bus are transient. On event of failure of all or part of the event bus messages may be lost. Applications should be coded to cope with lost messages, e.g. by resending them, and making application services idempotent.

The order of messages received by any specific handler from a specific sender will match the order of messages sent from that sender.

When sending a message, a reply handler can be provided. If so, it will be called when the reply from the receiver has been received.

This module can be used individually, or through the top-level vertx module.

Source:

Example

Accessing the event bus

var vertx = require('vertx');

var eb1 = require('vertx/event_bus');
var eb2 = vertx.eventBus;

eb1.registerHandler('some-address', function(message) {
  print("Got a message! " + message);
}
eb2.publish('some-address', 'Hello world');

Methods

<static> getDefaultReplyTimeout() → {number}

Get the default reply timeout in milliseconds. Unless changed, the default will be -1 which means messages sent on the event bus will never timeout unless `sendWithTimeout()` is used explicitly. Any other value will cause the event bus to timeout reply hanlders in the given number of milliseconds.
Source:
Returns:
Type
number

<static> publish(address, message) → {module:vertx/event_bus}

Publish a message on the event bus. Message should be a JSON object It should have a property "address".
Parameters:
Name Type Description
address string The address to send the message to
message Message The message to send
Source:
Returns:
Type
module:vertx/event_bus

<static> registerHandler(address, handler, registrationHandler) → {module:vertx/event_bus}

Register a handler.
Parameters:
Name Type Argument Description
address string the address to register for. Any messages sent to that address will be received by the handler. A single handler can be registered against many addresses.
handler MessageHandler The handler
registrationHandler Handler <optional>
If supplied, this handler is called when all nodes have registered this address
Source:
Returns:
the event bus
Type
module:vertx/event_bus

<static> registerLocalHandler(address, handler) → {module:vertx/event_bus}

Register a handler which won't be propageted acress the cluster.
Parameters:
Name Type Description
address string the address to register for. Any messages sent to that address will be received by the handler. A single handler can be registered against many addresses.
handler MessageHandler The handler
Source:
Returns:
The event bus
Type
module:vertx/event_bus

<static> send(address, message, replyHandler) → {module:vertx/event_bus}

Sends a message on the event bus.
Parameters:
Name Type Argument Description
address string The address to send the message to
message Message The message to send
replyHandler MessageHandler <optional>
called when the message receives a reply
Source:
Returns:
Type
module:vertx/event_bus

<static> sendWithTimeout(address, message, timeout, resultHandler)

Sends a message on the event bus. If a reply is not received within `timeout` milliseconds, the `replyHandler` will be called with an error and empty message body, then discarded.
Parameters:
Name Type Description
address string The address to send the message to
message Message The message to send
timeout number The timeout period in milliseconds
resultHandler ResultHandler called when the message receives a reply, or when the timeout triggers
Source:

<static> setDefaultReplyTimeout(timeout) → {module:vertx/event_bus}

Set the default reply time in milliseconds. Unless changed, the default will be -1 which means messages sent on the event bus will never timeout unless `sendWithTimeout()` is used explicitly. Any other value will cause the event bus to timeout reply hanlders in the given number of milliseconds.
Parameters:
Name Type Description
timeout number the number of milliseconds to wait before timing out a handler
Source:
Returns:
Type
module:vertx/event_bus

<static> unregisterHandler(address, handler, registrationHandler) → {module:vertx/event_bus}

Unregisters a handler.
Parameters:
Name Type Argument Description
address string The address the handler is registered to
handler MessageHandler The handler to unregister
registrationHandler Handler <optional>
If supplied, this handler is called when all nodes have unregistered this address
Source:
Returns:
the event bus
Type
module:vertx/event_bus