Class: ChannelProvider

ChannelProvider

Instance created to enable use of a channel as a provider. Allows for communication with the ChannelClients by invoking an action on a single client via dispatch or all clients via publish and to listen for communication from clients by registering an action via register.

Constructor

Returned by Channel.create.

Synchronous Methods
Asynchronous Methods
Middleware

Middleware functions receive the following arguments: (action, payload, senderId). The return value of the middleware function will be passed on as the payload from beforeAction, to the action listener, to afterAction unless it is undefined, in which case the most recently defined payload is used. Middleware can be used for side effects.

Properties:
Name Type Description
connections Array.<InterApplicationBus.Channel~ClientIdentity>

a read-only array containing all the identities of connecting clients.

Methods

afterAction(middleware) → {void}

Register middleware that fires after the action. This is passed the return value of the action.

Parameters:
Name Type Description
middleware Channel#ChannelProvider~Middleware

Function to be executed after invoking the action.

Tutorials:
Returns:
Type
void

beforeAction(middleware) → {void}

Register middleware that fires before the action.

Parameters:
Name Type Description
middleware Channel#ChannelProvider~Middleware

Function to be executed before invoking the action.

Tutorials:
Returns:
Type
void

destroy() → {Promise.<void>}

Destroy the channel.

Tutorials:
Returns:
Type
Promise.<void>

dispatch(to, action, payload) → {Promise.<any>}

Dispatch an action to a specified client. Returns a promise for the result of executing that action on the client side.

Parameters:
Name Type Description
to InterApplicationBus.Channel~ClientIdentity

Identity of the target client.

action string

Name of the action to be invoked by the client.

payload *

Payload to be sent along with the action.

Tutorials:
Returns:
Type
Promise.<any>

onConnection(listener) → {void}

Register a listener that is called on every new client connection. It is passed the identity of the connecting client and a payload if it was provided to Channel.connect. If you wish to reject the connection, throw an error. Be sure to synchronously provide an onConnection upon receipt of the channelProvider to ensure all potential client connections are caught by the listener.

Parameters:
Name Type Description
listener Channel#ChannelProvider~ConnectionListener
Tutorials:
Returns:
Type
void

onDisconnection(listener) → {void}

Register a listener that is called on every new client disconnection. It is passed the disconnection event of the disconnecting client.

Parameters:
Name Type Description
listener InterApplicationBus.Channel~ConnectionEvent
Tutorials:
Returns:
Type
void

onError(middleware) → {void}

Register an error handler. This is called before responding on any error.

Parameters:
Name Type Description
middleware Channel#ChannelProvider~Middleware

Function to be executed in case of an error.

Tutorials:
Returns:
Type
void

publish(action, payload) → {Array.<Promise.<any>>}

Publish an action and payload to every connected client. Synchronously returns an array of promises for each action (see dispatch).

Parameters:
Name Type Description
action string
payload *
Tutorials:
Returns:
Type
Array.<Promise.<any>>

register(action, listener) → {boolean}

Register an action to be called

Parameters:
Name Type Description
action string

Name of the action to be registered for channel clients to later invoke.

listener Channel#ChannelProvider~Action

Function representing the action to be taken on a client dispatch.

Tutorials:
Returns:
  • Boolean representing the successful registration of the action.
Type
boolean

remove(action) → {void}

Remove an action by action name.

Parameters:
Name Type Description
action string

Name of the action to be removed.

Tutorials:
Returns:
Type
void

setDefaultAction(middleware) → {void}

Sets a default action. This is used any time an action that has not been registered is invoked. Default behavior if not set is to throw an error.

Parameters:
Name Type Description
middleware Channel#ChannelProvider~Middleware

Function to be executed when a client invokes an action name that has not been registered.

Tutorials:
Returns:
Type
void

Type Definitions

Action(payload, identity)

Channel action callback signature

Parameters:
Name Type Description
payload any

Payload sent along with the message.

identity InterApplicationBus.Channel~ClientIdentity

Identity of the sender.

ConnectionListener(identity, payload)

Callback for the channel onConnection or onDisconnection. If it errors connection will be rejected.

Parameters:
Name Type Description
identity InterApplicationBus.Channel~ClientIdentity

Identity of the client attempting to connect to the channel.

payload any

Payload sent with connection request.

Middleware(action, payload, identity)

Middleware function signature

Parameters:
Name Type Description
action string

Action to be invoked.

payload any

Payload sent along with the message (or error for error middleware).

identity Identity

Identity of the sender.