with the JS livekit sdk, how would you catch an ex...
# ask-ai
c
with the JS livekit sdk, how would you catch an expired token and refresh it?
t
Thanks for reaching out, Guillaume. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Handling expired tokens and refreshing them using the JS LiveKit SDK is something we can definitely walk through together. You have a couple of great options here, either relying on LiveKit’s built-in features or setting up your own checks: • Automatic Token Refresh: LiveKit is designed to automatically refresh tokens during active sessions, so the SDK takes care of this behind the scenes. You can also tap into the
onTokenRefresh
callback to respond whenever a token gets updated:
Copy code
signalClient.onTokenRefresh = (newToken) => {
  console.log('Token refreshed:', newToken);
  // Optionally store or use the new token
};
• Manual Token Validation: If you’d prefer to be proactive, you can decode the JWT to check when it expires and then request a new token from your backend if needed. Here’s a simple example:
Copy code
const { connectionDetails, existingOrRefreshConnectionDetails } = useConnectionDetails(config);

// Before performing an action requiring a valid session:
await existingOrRefreshConnectionDetails();
const token = connectionDetails?.participantToken;
If you’d like, I’m here to help with more details or examples—just let me know! Sources: livekit/src/rtc_engine/mod.rs | Sources/LiveKit/Protos/livekit_rtc.pb.swift | Voice Assistants | livekit/livekit_composite | DeepWiki | Example Applications | livekit/livekit_composite | DeepWiki | Authentication | LiveKit Docs