Tutorial: Channel.dispatch

Channel.dispatch

Messages can be dispatched from the channelProvider to any channelClient or from any channelClient to the ChannelProvider. This messaging mechanism works exactly the same when messages are dispatched from the provider to a client.

Channel Provider

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

Because multiple clients can share the same name and uuid, when dispatching from a provider to a client, the identity you provide must include the client's unique endpointId property. This endpointId is passed to the provider in both the Provider.onConnection callback and in any registered action callbacks.

(async ()=> {
    const provider = await fin.InterApplicationBus.Channel.create('channelName');

    await provider.register('provider-action', async (payload, identity) => {
        console.log(payload, identity);
        return await provider.dispatch(identity, 'client-action', 'Hello, World!');
    });
})();

Channel Client

Dispatch the given action to the channel provider. Returns a promise that resolves with the response from the provider for that action.

(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);
})();