Subscribe to an event

Updated 

sprChat('subscribeToUpdate')

This is our common notifier API which can be used to either receive proactive updates from the application about the current state or listen to the status of the previously fired SDK requests. For example, it can be used to listen to the success or failure notification of a “sendExternalEvent” SDK request. It can also notify when the chat changes its availability status (if availability conditions are specified in the builder causing the chat to change state dynamically).
Parameters:

(every parameter is optional unless marked *):

Name

Typescript type

Description

Default Value

topic*

string

Update to listen to. Explanation of each topic can be found below in the examples.

-

subscriber*

(params: {

    // the additional parameters that were

sent in the SDK invocation where the SDK

is the one for which this subscriber is notifying

    request: StringAnyMap;

    response: {

      //any data to be shared after the SDK

invocation was successful or data

to be shared for chat application state,       

      data: StringAnyMap;

      //error that occurred while

executing the SDK

      error: StringAnyMap;

    };

}) => void;


Callback that will be invoked when a matching update is available.

Callback gets a “request” object only when this notifier is for the response of an SDK invocation. It contains all the params that were passed when making the SDK call.

It also gets a “response” object containing “data” whose structure depends on the topic. You can find the structure for each topic in the examples below. “response” can also carry “error” which are pre-defined set of errors. You can find the error list below as well.


-


Examples 

Listens to live chat service availability (which can change on the basis of conditions defined in the builder)

function onAvailabilityChange({ response: { data: { isChatVisible } }}) {
// isChatVisible: true/false means live chat is available/unavailable 
}

window.sprChat('subscribeToUpdate', {
    topic: 'availabilityChanged',
    subscriber: onAvailabilityChange,
});


Listens to the unread notification count across all the conversations

function onUnreadCountChanged({ response: { data: { count } }}) {
// count: number
}

window.sprChat('subscribeToUpdate', {
    topic: 'unreadCountChanged',
    subscriber: onUnreadCountChanged,
});

Listens to the external events triggered by bot/rule for the brand to handle where the type and externalParams have been defined in the bot/rule itself.

function onExternalEvent({ response: { data }}) {
// data contains the different externalEvent depending on configuration.
  // data: { type: string; payload: { externalParams: any, contextId: string }},
}

window.sprChat('subscribeToUpdate', {
    topic: 'externalEventTriggered',
    subscriber: onExternalEvent,
});


  1. Listens to the load signal of live chat

function onLoadCompleted({ response: { error }}) {
  //error can be true/false  if (error) {
// live chat could not be loaded
  } else {
// live chat is loaded
  }
}

window.sprChat('subscribeToUpdate', {
    topic: 'loadCompleted',
    subscriber: onLoadCompleted,
});


Listens to the telemetry/analytics events.

function onEventTriggered({ response: { data }}) {
// data: { 

       eventType: "CONVERSATION_WINDOW_OPENED"|

    "CONVERSATION_WINDOW_CLOSED"|

    "CONVERSATION_EXPORTED"|

    "CONVERSATION_DELETED"|

    "CONVERSATION_CLOSED"|

    "CONVERSATIONS_DELETED"|

    "CONVERSATIONS_CLOSED"|

    "NEW_CONVERSATION_CREATED"|

    "EXISTING_CONVERSATION_OPENED"|

    "TRIGGER_VISIBLE"|

    "BUTTON_CLICKED"|

    "AGENT_ASSIGNED"|

    "VIDEO_CALL_OUTGOING_STARTING"|

    "AUDIO_CALL_OUTGOING_STARTING"|

    "VIDEO_CALL_CARD_CTA_CLICKED"|

    "AUDIO_CALL_CARD_CTA_CLICKED"|

    "VIDEO_CALL_LAUNCH_CLICKED"|

    "AUDIO_CALL_LAUNCH_CLICKED"|

    "VIDEO_CALL_LAUNCHED"|

    "AUDIO_CALL_LAUNCHED"|

    "CONVERSATION_BACK_PRESSED"|

    "CONVERSATION_LIST_BACK_PRESSED"|

    "SURVEY_BACK_PRESSED"|

    "VIDEO_CALL_JOIN_CTA_CLICKED"|

    "VIDEO_CALL_JOIN_BACK_CTA_CLICKED"|

    "AUDIO_CALL_JOIN_CTA_CLICKED"|

    "AUDIO_CALL_JOIN_BACK_CTA_CLICKED"|

    "VIDEO_CALL_END_CLICKED"|

    "AUDIO_CALL_END_CLICKED"|

    "INFO_CARD_OPENED"|

    "INFO_CARD_CLOSED"|

    "USER_PROFILE_UPDATED"|

    "WIDGET_MAXIMIZED"|

    "WIDGET_MINIMIZED"|

    "PROACTIVE_PROMPT_TRIGGERED"|

    "PROACTIVE_PROMPT_ENGAGED"|

    "PROACTIVE_PROMPT_CLOSED"|

"SEND_BUTTON_CLICKED"|

"ATTACHMENT_BUTTON_CLICKED"|

    "PAGE_OPENED", 

       additional?: { conversationId?: string }

     }
}

window.sprChat('subscribeToUpdate', {
    topic: 'eventTriggered',
    subscriber: onEventTriggered,
});


Listens to see if the requested new conversation was created or not when window.sprChat(‘openNewConversation’) was fired.

const sdkPayload = {
conversationContext: {
'_c_abcdef123': ['123']
}
};


window.sprChat('openNewConversation', sdkPayload);


function onNewConversation({ request, data?: { conversationId }, error?: 'LIMIT_REACHED' }) {

      // request is same as sdkPayload
if (error) {
// open new conversation limit reached
} else {
// conversation is created successfully
}
}

window.sprChat('subscribeToUpdate', {
    topic: 'openNewConversation',
    subscriber: onNewConversation,
});


  1. Listens to see if the message was published or not when window.sprChat(‘sendExternalEvent’) was fired.

const sdkPayload = {
    type: 'MSG_PUBLISH',
    payload: {
        id: '123',
        message: {
          text: 'Hello world',
        },
        messageContext: {
'_m_asfd12': ['abc'],
        },
  },
};

window.sprChat('sendExternalEvent', sdkPayload);


function onSendExternalEvent({ request, data?: any; error?: 'INCORRECT_PAYLOAD' }) {
// request is same as sdkPayload
if (error) {
// sdk was called with incorrect payload
} else {
// data: {
      type: 'MSG_PUBLISHED',
      payload: {
        id: string;  // same as sdk.payload.id
        contextId: string;
        sprMsgId?: string;
      }
}
}

window.sprChat('subscribeToUpdate', {
    topic: 'sendExternalEvent',
    subscriber: onSendExternalEvent,
});