Show:

Primary client-side abstraction used to establish and manage communication with a central dispatcher

Constructor

Agent

(
  • cfg
)

Parameters:

  • cfg Object

Methods

broadcast

(
  • event
  • payload
  • options
)
Promise

Send a notification to whomever is listening to. This is not directed to a specific agent, but to anyone on the network, including the dispatcher.

Parameters:

  • event String

    The key identifying the event

  • payload Object

    The event data

  • options Object

    Options modifying the behavior of the broadcast.

Returns:

Promise:

A promise resolved as soon as the event has been broadcasted. Nothing is returned (fire and forget)

connect

(
  • options
)

Parameters:

  • options Object

    Supported options are :

    • waitForDispatcher: Indicate if the agent will exit of retry connection with the dispatcher indefinitely. Default to false.

Returns:

:

execute

(
  • key
  • payload
  • options
)
Promise

Execute a remote command. Depending on the dispatcher context, the command might be executed by the dispatcher itself or by another agent, having registered a remote command handler with the dispacher.

Parameters:

  • key Object

    The key of the command to execute. It must be registered by the dispatcher or an agent somewhere to receive a response.

  • payload Object

    The data to send with the command.

  • options Object

    Options that may change the way the command is executed. Supported options are:

    • timeout : The number of seconds to wait for a response. Default to 30 seconds.

Returns:

Promise:

A promise for the command result.

executeOn

(
  • key
  • payload
  • options
)
Promise

Execute a remote command on a specific agent. The command will be forward directly to this agent.

Parameters:

  • key Object

    The key of the command to execute. It must be registered by the dispatcher or an agent somewhere to receive a response.

  • payload Object

    The data to send with the command.

  • options Object

    Options that may change the way the command is executed. Supported options are:

    • timeout : The number of seconds to wait for a response. Default to 30 seconds.

Returns:

Promise:

A promise for the command result.

registerCommandHandler

(
  • key
  • fn
)

Add or replace a command handler. This handler will be execute each time a command of type key will be received.

Parameters:

  • key Object

    The command key to attach this handler

  • fn Object

    The handler that will be executed when a command of the specified key is received

retrieveCommands

()

Force the retrieval of any pending commands from the dispatcher. *

Returns:

:

retrieveEvents

(
  • options!~YUIDOC_LINE~!!~YUIDOC_LINE~!-
)

Retrieve all pending events for this agent.

Parameters:

  • options!~YUIDOC_LINE~!!~YUIDOC_LINE~!- Object

    delay: The number of seconds we have to wait before retrieving the events. Default to 0.

Returns:

:

subscribeTo

(
  • key
  • fn
  • options
)

Will connect this agent with remote events emitted by the dispatcher. These events may be triggered by the dispatcher or other agents connected on the network. For now, you can only subscribe to events that are sent to you, but in the future, we will add support for channels to receive 'room-like' messages.

Parameters:

  • key String

    The event key we wish to subscribe to remotely

  • fn Function

    The handler that will be called when the event is triggered remotely

  • options Object

    Options modifying the behavior to the function.

    • force : Avoid reusing a subscription if already in place and send the request to the dispatcher anyway. This is used when the dispatcher just failed and we are instructed to reconnect our event handlers.

unsubscribeFrom

(
  • key
)

Parameters:

  • key Object

Events

network-error

Emitted when there is a network problem and we were unable to communicate with the dispatcher.

Event Payload:

  • action String

    The name of the action that triggered the error

  • error Error | String

    The actual error that was thrown

network-error

Emitted when there is a network problem and we were unable to communicate with the dispatcher.

Event Payload:

  • action String

    The name of the action that triggered the error

  • error Error | String

    The actual error that was thrown

transport-error

Emitted when there is a network problem and we were unable to communicate with the dispatcher.

Event Payload:

  • action String

    The name of the action that triggered the error

  • error Error | String

    The actual error that was thrown