Constructor
new ChannelProvider()
Note: Do not instantiate this class directly. Returned by Channel.create.
Methods
afterAction(middleware)
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:
beforeAction(middleware)
Register middleware that fires before the action.
Parameters:
Name | Type | Description |
---|---|---|
middleware |
Channel#ChannelProvider~Middleware | Function to be executed before invoking the action. |
- Tutorials:
destroy() → {Promise.<void>}
Destroy the channel.
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 |
Identity | 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)
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:
onDisconnection(listener)
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 |
Channel~ConnectionEvent |
- Tutorials:
onError(middleware)
Register an error handler. This is called before responding on any error.
Parameters:
Name | Type | Description |
---|---|---|
middleware |
function | Function to be executed in case of an error. |
- Tutorials:
publish(action, payload)
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:
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 |
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)
Remove an action by action name.
Parameters:
Name | Type | Description |
---|---|---|
action |
string | Name of the action to be removed. |
- Tutorials:
setDefaultAction(middleware)
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:
Type Definitions
Action(payload, identity)
Channel action callback signature
Parameters:
Name | Type | Description |
---|---|---|
payload |
* | Payload sent along with the message. |
identity |
Identity | 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 |
Identity | Identity of the client attempting to connect to the channel. |
payload |
* | Payload sent with connection request. |
Middleware(action, payload, identity)
Middleware function signature
Parameters:
Name | Type | Description |
---|---|---|
action |
string | Action to be invoked. |
payload |
* | Payload sent along with the message (or error for error middleware). |
identity |
Identity | Identity of the sender. |