a read-only provider identity
Register middleware that fires after the action.
If the action does not return the payload, then the afterAction will not have access to the payload object.
Channel Provider:
(async ()=> {
const provider = await fin.InterApplicationBus.Channel.create('channelName');
await provider.register('provider-action', (payload, identity) => {
return {
echo: payload
};
});
await provider.afterAction((action, payload, identity) => {
//the payload can be altered here after handling the action but before sending an acknowledgement.
payload.sent = date.now();
return payload;
});
})();
Channel Client:
(async ()=> {
const client = await fin.InterApplicationBus.Channel.connect('channelName');
await client.register('client-action', (payload, identity) => {
return {
echo: payload
};
});
await client.afterAction((action, payload, identity) => {
//the payload can be altered here after handling the action but before sending an acknowledgement.
payload.sent = date.now();
return payload;
});
})();
Register middleware that fires before the action.
Channel Provider:
(async ()=> {
const provider = await fin.InterApplicationBus.Channel.create('channelName');
provider.register('provider-action', (payload, identity) => {
console.log(payload, identity);
return {
echo: payload
};
});
provider.beforeAction((action, payload, identity) => {
//The payload can be altered here before handling the action.
payload.received = Date.now();
return payload;
});
})();
Channel Client:
(async ()=> {
const client = await fin.InterApplicationBus.Channel.connect('channelName');
client.register('client-action', (payload, identity) => {
console.log(payload, identity);
return {
echo: payload
};
});
client.beforeAction((action, payload, identity) => {
//The payload can be altered here before handling the action.
payload.received = Date.now();
return payload;
});
const providerResponse = await client.dispatch('provider-action', { message: 'Hello From the client' });
console.log(providerResponse);
})();
Dispatch the given action to the channel provider. Returns a promise that resolves with the response from the provider for that action.
Optionalpayload: any(async ()=> {
const client = await fin.InterApplicationBus.Channel.connect('channelName');
await client.register('client-action', (payload, identity) => {
console.log(payload, identity);
return {
echo: payload
};
});
const providerResponse = await client.dispatch('provider-action', { message: 'Hello From the client'});
console.log(providerResponse);
})();
Register a listener that is called when this client loses its channel: the provider disconnects, the endpoint drops, or a self-heal fails. It is passed the provider identity.
Register an error handler. This is called before responding on any error.
Channel Provider:
(async ()=> {
const provider = await fin.InterApplicationBus.Channel.create('channelName');
provider.register('provider-action', (payload, identity) => {
console.log(payload);
throw new Error('Action error');
return {
echo: payload
};
});
provider.onError((action, error, identity) => {
console.log('uncaught Exception in action:', action);
console.error(error);
});
})();
Channel Client:
(async ()=> {
const client = await fin.InterApplicationBus.Channel.connect('channelName');
client.register('client-action', (payload, identity) => {
console.log(payload);
throw new Error('Action error');
return {
echo: payload
};
});
client.onError((action, error, identity) => {
console.log('uncaught Exception in action:', action);
console.error(error);
});
})();
Register a listener that is called each time this client transparently self-heals its connection to the provider.
A channel client's underlying endpoint is torn down while its document sits in the back/forward cache
(bfcache). If the back/forward cache is supported and the document is restored from it, the client
automatically re-handshakes with the provider and swaps in a fresh endpoint; all of this client's
register/middleware handlers are preserved, so no re-registration is required.
A restore is not guaranteed: Chromium may evict a cached document at any time, in which case navigating back reloads the page and a new client must connect.
This listener fires once per successful reconnect, after the new endpoint is live, and can fire multiple
times over the client's lifetime. It is triggered when the document is restored from the back/forward
cache (a bfcache pageshow). It does not fire for the initial connection, nor after
disconnect. onDisconnection fires
when the provider or endpoint goes away.
The info provides the reconnected provider identity along with the previous and new endpoint ids.
Register an action to be called by dispatching from any channelClient or channelProvider.
The return value will be sent back as an acknowledgement to the original caller. You can throw an error to send a negative-acknowledgement and the error will reject the promise returned to the sender by the dispatch call. Once a listener is registered for a particular action, it stays in place receiving and responding to incoming messages until it is removed. This messaging mechanism works exactly the same when messages are dispatched from the provider to a client. However, the provider has an additional publish method that sends messages to all connected clients.
Because multiple clients can share the same name and uuid, in order to distinguish between individual clients,
the identity argument in a provider's registered action callback contains an endpointId property. When dispatching
from a provider to a client, the endpointId property must be provided in order to send an action to a specific client.
Channel Provider:
(async ()=> {
const provider = await fin.InterApplicationBus.Channel.create('channelName');
await provider.register('provider-action', (payload, identity) => {
console.log('Action dispatched by client: ', identity);
console.log('Payload sent in dispatch: ', payload);
return { echo: payload };
});
})();
Channel Client:
(async ()=> {
const client = await fin.InterApplicationBus.Channel.connect('channelName');
await client.register('client-action', (payload, identity) => {
console.log('Action dispatched by client: ', identity);
console.log('Payload sent in dispatch: ', payload);
return { echo: payload };
});
})();
Remove an action by action name.
Registers a default action. This is used any time an action that has not been registered is invoked.
Channel Provider:
(async ()=> {
const provider = await fin.InterApplicationBus.Channel.create('channelName');
await provider.setDefaultAction((action, payload, identity) => {
console.log(`Client with identity ${JSON.stringify(identity)} has attempted to dispatch unregistered action: ${action}.`);
return {
echo: payload
};
});
})();
Channel Client:
(async ()=> {
const client = await fin.InterApplicationBus.Channel.connect('channelName');
await client.setDefaultAction((action, payload, identity) => {
console.log(`Provider with identity ${JSON.stringify(identity)} has attempted to dispatch unregistered action: ${action}.`);
return {
echo: payload
};
});
})();
Instance created to enable use of a channel as a client. Allows for communication with the ChannelProvider by invoking an action on the provider via dispatch and to listen for communication from the provider by registering an action via register.
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 original payload is used. Middleware can be used for side effects.