question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebChat using direct line unable to refresh token or reconnect

See original GitHub issue

Hi, on our project we’re using webchat that is connecting to our bot through direct line without web sockets so we are able to restore chat history. After 15-30 minutes of inactivity when user tries to send message it immediately shows couldn’t sent. I’ve tried to use reconnect API however looking into source code it seems that it’s working only with web sockets enabled. I’ve tried to implement following options but neither of them is working and token always expires resulting on 403 error on token refresh.

var botConnection = new BotChat.DirectLine({ 
     secret: model.directLineSecret, 
     webSocket: false, 
     conversationId: getPersistedConversationId(), 
     domain: model.chatUrl 
}); 

BotChat.App({ 
     botConnection: botConnection, 
     user: { 
          id: model.userId, 
          name: model.userName, 
     }, 
}, 
window.document.getElementById('chat')); 

botConnection.connectionStatus$ 
.subscribe(function(status){ 
     handleConnection(status) 
}); 

//try to refresh token every 50 seconds
window.setInterval(function (){ 
  console.log("About to refresh token"); 
  $.ajax({ 
     method: "POST", 
     url: "https://directline.botframework.com/v3/directline/tokens/refresh", 
     headers: { 
       "Authorization" : "Bearer " + botConnection.token 
     } 
     }).done(function(result){  
          botConnection = new BotChat.DirectLine({  
               token: result.token,  
               conversationId: getPersistedConversationId(), 
               webSocket: false, 
               domain: model.chatUrl 
          }); 

     BotChat.App({ 
     botConnection: botConnection, 
     user: { 
               id: model.userId, 
               name: model.userName, 
          }, 
     }, 
     window.document.getElementById('chat')); 

     botConnection.connectionStatus$
          .subscribe(function(status){ 
               handleConnection(status) 
          }); 
     }); 
},50000); 

function handleConnection(connectionStatus){ 
     switch(connectionStatus){ 
     case 0: 
          console.log("Uninitialized"); 
          break; 
     case 1: 
          console.log("Connecting"); 
          break; 
     case 2: 
          console.log("Online")
          saveConversationId(botConnection.conversationId);  
          break; 
     case 3: 
          console.log("ExpiredToken"); 
          botConnection = new BotChat.DirectLine({ 
               secret: model.directLineSecret, 
               webSocket: false, 
               conversationId: getPersistedConversationId(), 
               domain: model.chatUrl 
          }); 

          BotChat.App({ 
               botConnection: botConnection, 
               user: { 
                    id: model.userId, 
                    name: model.userName, 
               }, 
          }, 
          window.document.getElementById('chat')); 

          botConnection.connectionStatus$ 
          .subscribe(function(status){ 
               handleConnection(status) 
          });  
          break; 
     case 4: 
          console.log("FailedToConnect"); 
          break; 
     case 5: 
          console.log("Ended"); 
          break; 
     } 
}

Token refresh does not throw any error but after mentioned interval Expired token is triggered and even if new connection is created it fires request to refresh endpoint resulting 403 error - this method can be used only with existing token.

I’ve looked at the issue https://github.com/Microsoft/BotFramework-WebChat/issues/372

and used c# code to return new conversation object and reconnected, after that message seems to be sent even after timeout however no response from the bot is displayed.

Can anyone point me to the right direction on how to properly manage token of direct line?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
billbacommented, Feb 28, 2018

Secrets don’t expire.

Tokens do expire, but are automatically renewed by DirectLineJS (unless there’s a break in connection). This renewal is enabled by passing { token } when creating DirectLine.

0reactions
morrisond91commented, Jul 24, 2018

@billba

Secrets don't expire. Tokens do expire, but are automatically renewed by DirectLineJS (unless there's a break in connection). This renewal is enabled by passing { token } when creating DirectLine.

Is this DirectLine default behaviour, or will the token only be generated if we are using webchat?

We are building a custom interface using directlinejs and the token is not being refreshed, so thinking that we need to do this manually.


We are not using web-chat, we have built our own react front end and using directlineJs.

We have an API that retrieves a token through https://directline.botframework.com/v3/directline/conversations. Upon API response we connect to DirectLine.

Everything works fine, however the token dies in 30 minutes and the conversation locks up.

What we are now trying to do is send a refresh message to https://directline.botframework.com/v3/directline/tokens/refresh passing in our original token, we get a new token back but are unsure what to do next?

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - How to reconnect WebChat to old conversation with ...
If you want a user to be able to re-join a conversation, then the token should be refreshed before they expire by calling...
Read more >
Reconnect to a conversation in Direct Line API 3.0
Learn how to reconnect to a conversation after losing contact. See how to use Direct Line API version 3.0 to generate new WebSocket...
Read more >
botframework-directlinejs-tmp - NPM Package Overview - Socket
Storing the conversationid and your token (in a permanent place, like local storage); Calling the DirectLine reconnect API yourself to get a refreshed...
Read more >
ambit-directlinejs: Docs, Tutorials, Reviews | Openbase
JavaScript client library for Microsoft Bot Framework's Direct Line protocol ... If you're currently using WebChat, you don't need to make any changes...
Read more >
Bot Framework resume conversation, Https DirectLine botframework ...
Web Chat uses DirectLineJs internally, and you can see here that it refreshes the token every 15 minutes. Everything starts with a Direct...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found