javascriptc#jquerytwiliotwilio-programmable-voice

Twilio Sync: connection closed by server, reason is TOKEN_EXPIRED


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?


Solution

  • Thanks, I tried all events so here I got it...

    syncClient.on('connectionStateChanged', (newState) => {
        console.log('Received a new connection state:', newState);
        if (newState === 'disconnecting') {
            $.getJSON("/dashboard/token", function (response) {
                var token = response.token;
                syncClient.updateToken(token);
            });
        }
    });