Agent Class
Primary client-side abstraction used to establish and manage communication with a central dispatcher
Constructor
Agent
-
cfg
Parameters:
-
cfg
Object
Item Index
Methods
Methods
broadcast
-
event
-
payload
-
options
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
StringThe key identifying the event
-
payload
ObjectThe event data
-
options
ObjectOptions modifying the behavior of the broadcast.
Returns:
A promise resolved as soon as the event has been broadcasted. Nothing is returned (fire and forget)
connect
-
options
Parameters:
-
options
ObjectSupported options are :
- waitForDispatcher: Indicate if the agent will exit of retry connection with the dispatcher indefinitely. Default to false.
Returns:
execute
-
key
-
payload
-
options
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
ObjectThe key of the command to execute. It must be registered by the dispatcher or an agent somewhere to receive a response.
-
payload
ObjectThe data to send with the command.
-
options
ObjectOptions 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:
A promise for the command result.
executeOn
-
key
-
payload
-
options
Execute a remote command on a specific agent. The command will be forward directly to this agent.
Parameters:
-
key
ObjectThe key of the command to execute. It must be registered by the dispatcher or an agent somewhere to receive a response.
-
payload
ObjectThe data to send with the command.
-
options
ObjectOptions 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:
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
ObjectThe command key to attach this handler
-
fn
ObjectThe 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~!-
Objectdelay: 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
StringThe event key we wish to subscribe to remotely
-
fn
FunctionThe handler that will be called when the event is triggered remotely
-
options
ObjectOptions 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
StringThe name of the action that triggered the error
-
error
Error | StringThe 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
StringThe name of the action that triggered the error
-
error
Error | StringThe 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
StringThe name of the action that triggered the error
-
error
Error | StringThe actual error that was thrown