GitHub Homepage React Spectrum Libraries Legal Notice

@wuespace/vertx-event-bus

Install using

npm install @wuespace/vertx-event-bus

Exported Members

You can access these members by importing the module:

import { member } from '@wuespace/vertx-event-bus';

EventBus

class EventBus

An eventbus connector for the Vert.x environment.

It implements the Javascript client-side of the SockJS event bus bridge based on the online documentation by the Vert.x-Team and their implementation:

Constructors

constructor

function (
   url : string ,
   options ? : Partial<Options>
) : any

Builds a new event bus that automatically tries to connect to the given server url.

It uses the given options to configure the event bus instance and falls back on default options if some of the given options are not set.

@param:

url - the server url

@param:

options - event bus configuration options

@see:

@see:

@example:

// some vertx event bus channel
const channel = 'EVENTBUS_CHANNEL';

// build a new event bus instance
const eventBus = new EventBus('http://localhost:9870/bridge');
// set an event listener when the the event bus is open
eventBus.onOpen = () => {
	// publish a simple message to the event bus
	eventBus.publish(channel, 'Hello World');
};

Properties

Methods

setAutoReconnect (...)

function setAutoReconnect (
   newState : boolean
) : void

Enable or disable automatic reconnect on unexpected connection loss.

If automatic reconnect is enabled, the event bus tries to reconnect to the backend server in increasing time steps until a connection is re-established.

Initially enabled.

@param:

newState - true to enable automatic reconnect, false to disable

@see:

@see:

@example:

const eventBus = new EventBus('http://localhost:9870/bridge');

eventBus.onReconnect = () => {
	console.log('Event bus has reconnected');
};

// disable automatic reconnect
eventBus.setAutoReconnect(false);
publish (...)

function publish (
   address : ChannelAddress ,
   content : JsonSerializable ,
   headers ? : Headers
) : void

Publishes a message to the specified address.

If the eventbus is currently closed, it stores the message and publishes it after the connection is open.

@param:

address - address to which the content gets published

@param:

content - the content that will be broadcasted to the specified address

@param:

headers - optional headers sent with the event bus message

@see:

@example:

// some vertx eventbus address to publish to
const address = 'MY_CHANNEL';

const eventBus = new EventBus('http://localhost:9870/bridge');

eventBus.onOpen = () => {
	eventBus.publish(address, 'Hey there!');
}
send (...)

function send < T : JsonSerializable = JsonSerializable > (
   address : ChannelAddress ,
   content : JsonSerializable ,
   callback : Callback<T> ,
   headers ? : Headers
) : void

Publishes a message to the specified address. It also registers a one-time event handler that gets called when the first answer to the message is received.

@param:

address - address the content is sent to

@param:

content - the content that will sent to the specified address

@param:

callback - the function that gets called when the first answer is received

@param:

headers - optional headers sent with the event bus message

@typeParam:

T - the argument type of the callback

@see:

@see:

@example:

// some vertx eventbus address to send to
const address = 'MY_CHANNEL';

const eventBus = new EventBus('http://localhost:9870/bridge');

eventBus.onOpen = () => {
	eventBus.send(address, 'Ping', message => {
		console.log('Pong:', message);
	});
};
register (...)

function register < T : JsonSerializable = JsonSerializable > (
   address : ChannelAddress ,
   callback : Callback<T> ,
   headers ? : Headers
) : void

Registers an event handler to the specified address.

The given callback is executed when the event bus receives a message on the specified address.

@param:

address - the address on which the event handler that executes the callback on incoming messages gets registered

@param:

callback - the function that gets executed when incoming messages are received on the specified address

@param:

headers - optional headers sent with the initial registration message to the backend on the first register event

@typeParam:

T - the argument type of the callback

@see:

@see:

@example:

// some vertx eventbus address to register to
const address = 'MY_CHANNEL';

// define a handler
const handler: Callback = message => {
	console.log(message);
};

const eventBus = new EventBus('https://localhost:9870/bridge');

eventBus.onOpen = () => {
	eventBus.register(address, handler);
};

// later
// unregister handler
eventBus.unregister(address, handler);
unregister (...)

function unregister < T : JsonSerializable = JsonSerializable > (
   address : ChannelAddress ,
   callback : Callback<T> ,
   headers : Headers = {}
) : void

Unregisters an event handler from the specified address.

The callback argument must be the same as the one that was registered using registerHandler().

The specified headers are sent with the unregister message that goes to the backend if no other event handlers are registered to this address anymore.

@param:

address - the address the event handler unregisters from

@param:

callback - the callback to unregister from the address

@param:

headers - optional headers sent with the last unregister message

@typeParam:

T - the argument type of the callback

@see:

@example:

// some vertx eventbus address to register to
const address = 'MY_CHANNEL';

// define a handler
const handler: Callback = message => {
	console.log(message);
};

const eventBus = new EventBus('https://localhost:9870/bridge');

eventBus.onOpen = () => {
	eventBus.register(address, handler);
};

// later
// unregister handler
eventBus.unregister(address, handler);
close (...)

function close ( ) : void

Close the event bus and stop receiving any further event bus messages. The connection will not be re-established automatically, even if automatic reconnect is enabled.

@see:

@see:

@example:

const eventBus = new EventBus('http://localhost:9870/bridge');
// cleanup
eventBus.close();

defaultOptions

const : Options

the default options for the event bus that are merged with the custom options in the constructor

@see:

[(EventBus:constructor) | EventBus Constructor](#(EventBus:constructor) | EventBus Constructor)

@example:

constructor(url: string, options?: Partial<Options>) {
	this.options = { ...defaultOptions, ...options };
	...
}