Class: VertxBus

eventbus~VertxBus

new VertxBus(options)

Construct an interface to the Vert.x bus. Use this as follows:

    var eventbus = require('eventbus');
    var bus = new eventbus.VertxBus();
    bus.subscribe('topic');
    bus.on('topic',
     function(msg) {
       print(msg);
     }
    );
    bus.publish('topic', {'hello':'world'});
 
This creates an interface to the event bus, subscribes to events with address 'topic', provides a handler for such events, and publishes a single event to that same address. The result should be to print:
   {'hello':'world'}
 
on the standard output.

This implementation uses the event emitter pattern common in JavaScript. Once you have subscribed to an address, you can specify any number of handlers as follows:

    bus.on(address, function);
 
To give a handler that reacts only to exactly one event with this address, use
    bus.once(address, function);
 
To unsubscribe to an address, use
    bus.unsubscribe(address);
 
To unsubscribe to all addresses, use
    bus.unsubscribe();
 
In addition, this module supports point-to-point communication, which sends an event to exactly one subscriber, chosen in a approximately round-robin fashion. To send to exactly one subscriber, instead of '''publish''' use '''send''', as follows:
    bus.send('topic', {'hello':'world'});
 
When sending a point-to-point message, it is possible to get a reply from the recipient. The recipient (which also uses this module) should set the reply message as in the following example:
    bus.setReply('confirmed');
 
where 'confirmed' can be replaced with any string or value that has a JSON string representation. The sender can then specify a handler to receive the reply as follows:
    bus.send('topic', {'hello':'world'}, handler);
 
where handler is a function that takes one argument, the reply message.

Parameters:
Name Type Description
options

A JSON record containing optional fields 'port' (an int) and 'host' (a string). These specify the network interface on the local host to use to connect to the Vert.x event bus cluster. This defaults to {'host':'localhost', 'port':0}, where a port value of 0 means "find an open port and use that. If no options parameter is given, then use the defaults.

Source:

Methods

notify(address, body)

Notify this object of a received message from the event bus. This function is called from the Nashorn Java helper for this module and should not be directly invoked by the user of the module. This method assumes that the body of the message is a string in JSON format. If it is not, then it will just emit the body as is.

Parameters:
Name Type Description
address

The address.

body

The message body

Source:

notifyReply(handler, message)

Notify this object of a received reply from the event bus This function is called from the Nashorn Java helper for this module and should not be directly invoked by the user of the module. confirming completion of a point-to-point send.

Parameters:
Name Type Description
handler

The callback function to invoke.

message

The message to send to the callback function.

Source:

publish(address, data)

Publish the specified data on the specified address. The data is first converted to a string representation in JSON format.

Parameters:
Name Type Description
address

The address (or topic) of the event bus channel. This is a string.

data

The data to publish. This can be any JavaScript object that has a JSON representation using JSON.stringify().

Source:
See:
  • this.send()

send(address, data, handler)

Send the specified data to exactly one receiver at the specified address. This implements a point-to-point send, vs. the broadcast realized by publish(). The data is first converted to a string representation in JSON format. According to the Vert.x documentation, the recipient will be chosen in a loosely round robin fashion.

Parameters:
Name Type Description
address

The address (or topic) of the event bus channel. This is a string.

data

The data to publish. This can be a string or any JavaScript object that has a JSON representation using JSON.stringify().

handler

A function to invoke with argument address and reply body when the recipient has received the message, or null to not provide a reply handler.

Source:
See:
  • publish()

setReply(reply)

Set the reply to send when events are received in the future via a point-to-point send.

Parameters:
Name Type Description
reply

The reply to respond with, or null to send no reply. this should be a string or any object that can be encoded as a JSON string.

Source:
See:
  • this.send(address, data)

subscribe()

Subscribe to events with the specified address. To react to those events, use on() or once() as explained above.

Source:

unsubscribe()

Unsubscribe to events with the specified address.

Source: