Article: WebSdk- Reconnection of bot after token expiry

Problem Statement:

While upgrading the Kore Web SDK from version 8.1.0 to 10.1.13, a critical issue has surfaced for the users. The problem revolves around JWT token expiration after 60 minutes, rendering the system is incapable of sending messages to the bot.

Error:
A critical error related to jQuery ($) has been identified, specifically with the line
chatWindowInstance.bot.RtmClient.$ being undefined.

Workaround:
In the v2 implementation, it is possible to override SDK internal methods.
The below provided sample code offers a solution to address the token expiry issue.

let chatWindowInstance = new chatWindow();

chatWindowInstance.bot.on("rtm_client_initialized", function () {

chatWindowInstance.bot.KoreRTMClient.prototype._onStart = function _onStart(err, data) {

this.$=chatWindowInstance.$;
var CLIENT_EVENTS=this.CLIENT_EVENTS;

var errMsg;
var __reconnect__ = this._reconnecting?true:false;
this._connecting = false;
this._reconnecting = false;
try{
data = data;
}
catch(e){
console.log(e && e.stack);
}
if(data && data.errors && (data.errors[0].code === 'TOKEN_EXPIRED' || data.errors[0].code === 401 || data.errors[0].msg === 'token expired')){
var $=this.$;
$(".reload-btn").trigger('click',{isReconnect:true});
data.error='token_expired';
}
if (err || !data.url) {
debug("error or no url in response %s", err || "no url");
this.emit(CLIENT_EVENTS.UNABLE_TO_RTM_START, err || data.error);

// Any of these mean this client is unusable, so don't attempt to auto-reconnect
if (data && contains(UNRECOVERABLE_RTM_START_ERRS, data.error)) {
errMsg = 'unrecoverable failure connecting to the RTM API';
this.disconnect(errMsg, data.error);
} else {
this.authenticated = false;
if (this.autoReconnect) {
this.reconnect();
}
}
} else {
if(__reconnect__){
data.url = data.url + "&isReconnect=true";
}
this.authenticated = true;
//this.activeUserId = data.self.id;
this.emit(CLIENT_EVENTS.AUTHENTICATED, data);
this.connect(data.url);
}
};
});

chatWindowInstance.show(chatConfig);

Please Click Here for more information on using Kore WebSdk

1 Like