Home > database >  Twilio Sync: connection closed by server, reason is TOKEN_EXPIRED
Twilio Sync: connection closed by server, reason is TOKEN_EXPIRED

Time:10-12

Twilio Sync Time out is 172800

$.getJSON("/dashboard/token", function (response) {
    console.log(`Token Generated at ${new Date()}`);
    localStorage.setItem('syncToken', response.token);
    
    syncClient = new Twilio.Sync.Client(response.token, { logLevel: "info" });

    syncClient.on('tokenAboutToExpire', function () {
        console.log(`tokenAboutToExpire at: ${new Date()}`);
        var token = localStorage.getItem('syncToken');
        syncClient.updateToken(token);
    });
});

Here is the Twilio Sync browser console showing:

Twilsock I: socket opened
twilio-sync.js:25304 Twilsock I: refreshing all registrations
twilio-sync.js:25304 Twilsock I: update registration for context 8c430fb3-4353-4b06-9cfd-6bebc78582b0

But after some time this message will occurs in browser:

Twilsock I: connection has expired
Notify I: Transport ready false 
Twilsock I: connection closed by server, reason is TOKEN_EXPIRED
Twilsock I: socket closed CloseEvent

can anyone tell me how to resolve this on Twilio Sync Event?

CodePudding user response:

Twilio developer evangelist here.

The event you are receiving there is that the token is about to expire, however you are replacing the token with itself. Instead, you should request a new token from your back-end and update the client with that new token.

$.getJSON("/dashboard/token", function (response) {
    console.log(`Token Generated at ${new Date()}`);
    localStorage.setItem('syncToken', response.token);
    
    syncClient = new Twilio.Sync.Client(response.token, { logLevel: "info" });

    syncClient.on('tokenAboutToExpire', function () {
        console.log(`tokenAboutToExpire at: ${new Date()}`);
        $.getJSON("/dashboard/token", function (response) {
            syncClient.updateToken(response.token);
        });
    });
});
  • Related