简体   繁体   中英

Twilio Programmable Chat - Connection dropped from JS client

I have a React app that uses the Twilio Programmable Chat library to provide chat functionality. The setup code generally looks like this, all wrapped in a try/catch block:

  this.accessManager = new AccessManager(twilioToken.token);
  const chatClientOptions = config.debugMode ? { logLevel: 'debug' } : {};
  this.chatClient = await TwilioChat.Client.create(twilioToken.token, chatClientOptions);

  // Regsiter chatClient to get new access tokens from AccessManager
  this.accessManager.on('tokenUpdated', function(am) {
    console.log("Chat client getting new token from AccessManager");
    // get new token from AccessManager and pass it to the library instance
    self.chatClient.updateToken(am.token);
  });

  // IF accesstoken expries, grab a new one from the server
  this.accessManager.on('tokenExpired', async function() {
    console.log("Chat access token expired. Requesting new one from server...");
    // generate new token here and set it to the accessManager
    const updatedToken = (await axios.get(`/users/${user.publicId}/twilioToken`)).data;
    self.accessManager.updateToken(updatedToken.token);
  });

  this.channel = await this.createOrGetChannel();

createOrGetChanne() gets the channel, then we can get messages and send messages properly.

90% of the time, everything works properly, but looks like sometimes Twilsock (I assume this is the underlying connection management lib) seems to drop my connection, after which the client cannot call this.channel.sendMessage() properly. The sendMessage() call times out, and there is no Promise rejection to catch on the client (so I can't reconnect and retry).

With debug mode on, the error I get on the client seems to be:

2018-08-23T22:02:21.425Z Twilsock T: closing socket

then the client doesn't work properly. I can reproduce the situation on my mobile phone (but not always) if I switch to another app or drop the wifi then return to the browser and chat page.

Here's what a failed message attempt looks like after the closed socket:

2018-08-23T22:02:28.214Z Sync D: POST https://cds.us1.twilio.com/v3/Services/some_id/Lists/some_id/Items ID: RQb01f6b1df35f4f7ead730d990fcc0ef8

2018-08-23T22:02:45.209Z Chat Messages D: Sending text message One more after another app {}

2018-08-23T22:02:45.209Z Chat Session I: Adding command: sendMessage 11707143-0933-429d-bce0-ac2d37b5f699

2018-08-23T22:02:45.210Z Chat Session D: command arguments: {"channelSid":"something","text":"One more after another app","attributes":"{}","action":"sendMessage"} true

2018-08-23T22:02:45.223Z Sync D: POST https://cds.us1.twilio.com/v3/Services/some_id/Lists/some_id/Items ID: RQd8cdea2425724ba8b1c70970c4d890ea

2018-08-23T22:02:48.234Z Twilsock D: request is timed out

Followed by this:

2018-08-23T22:04:04.480Z Chat Session E: Failed to add a command to the session r@https:[some stuff] promiseReactionJob@[native code]

and

Uncaught (in promise) scheduleAttempt@https://[some stuff] promiseReactionJob@[native code]

Refreshing the page (and reinitializing the client) fixes the problem.

Questions:

1) Since the sendMessage() promise isn't rejected, is there anything I can do to catch this and retry or re-initialize the connection?

2) Alternately, is there a callback I can hook into that where I might reconnect when there is a Twilsock closes the socket?

3) Are there any other connection management best practices I need to add to make my app as robust as possible?

Any help appreciated. Thanks, thanks, thanks!

Twilio just released version 3.1.0 of their chat sdk with a fix to this connectivity issue. Upgrading to this version should fix your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM