AboutSupportDeveloper GuideVersion 36.122.80.26

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.

Hierarchy

  • ChannelBase
    • ChannelClient

Accessors

Methods

  • Register middleware that fires after the action.

    Parameters

    Returns void

    Remarks

    If the action does not return the payload, then the afterAction will not have access to the payload object.

    Example

    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.

    Parameters

    Returns void

    Example

    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);
    })();
  • Disconnects the client from the channel.

    Returns Promise<void>

    Example

    (async ()=> {
    const client = await fin.InterApplicationBus.Channel.connect('channelName');

    await client.disconnect();
    })();
  • Dispatch the given action to the channel provider. Returns a promise that resolves with the response from the provider for that action.

    Parameters

    • action: string
    • Optional payload: any

    Returns Promise<any>

    Example

    (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 on provider disconnection. It is passed the disconnection event of the disconnecting provider.

    Returns void

    Example

    (async ()=> {
    const client = await fin.InterApplicationBus.Channel.connect('channelName');

    await client.onDisconnection(evt => {
    console.log('Provider disconnected', `uuid: ${evt.uuid}, name: ${evt.name}`);
    });
    })();
  • Register an error handler. This is called before responding on any error.

    Parameters

    • func: ErrorMiddleware

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

    Returns void

  • Register an action to be called by dispatching from any channelClient or channelProvider.

    Parameters

    Returns boolean

    Remarks

    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.

    Example

    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.

    Parameters

    • action: string

    Returns void

    Example

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

    await provider.register('provider-action', (payload, identity) => {
    console.log(payload);
    return {
    echo: payload
    };
    });

    await provider.remove('provider-action');

    })();
  • Returns Promise<void>

  • Registers a default action. This is used any time an action that has not been registered is invoked.

    Parameters

    Returns void

    Example

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

    })();