Mmm fresh tokens!
TLDR
We improved APIs for refreshing sessions, make syncing more efficient across clients, and start load testing Triplit Cloud.
An improved session API
This week we’ve streamlined the process of refreshing the authentication token (JWT) used in the sync connection between the Triplit Client and Triplit Server.JWTs have gained in popularity over time as tooling for authentication and Triplit uses them to identify who you are as you begin a “Sync Session” with the server. For security best practices, it is recommended however that JWTs have an expiration so they cannot be used ad infinitum, and maybe by someone that isnt supposed to have the JWT!
Although updating the JWT used in the Triplit Client has always been possible, it required closing the web socket connection with the server and starting a new one along with some other cleanup of state related to your Sync Session. Although that’s mostly desirable for actions like signing in / out users, it makes less sense for refreshing an expired authentication where preferably you’d like to keep the connection open and just gracefully let the server know you still are who you say you are.
Well now you can keep your connection alive with our new session management APIs. We’re particularly excited because they have significantly less room for error than our current APIs and they encapsulate how you should conceptualize what’s happening when you assign a token on the client - which is starting a “sync session”.
At the 3 most common token lifecycle moments of your app, you can: Start a session when a user signs in:
async function onSignIn(token: string) {
await client.startSession(token);
}
Refresh a session when a token expires:
function onTokenRefresh(newToken: string) {
client.updateSessionToken(newToken);
}
End a session when a user signs out:
async function onSignOut() {
await client.endSession();
// Additionally clear the client's application data
await client.clear();
}
Improved syncing efficiency with multiple connections
We've made improvements to how subscriptions are managed on the server when multiple applications are connected to the same user account. Specifically, when multiple clients subscribe to the same query (after applying permissions) the server will share more resources. Currently, the main improvement is increased efficiency when data changes and query subscriptions need to be updated. In the future, this new design will allow for further optimizations to improve querying speed and update latency.
Load testing with K6:
We wanted to give a shoutout to the team at Grafana authoring the fantastic load testing library K6. It’s a neat project that actually has its own JS runtime written in Go. Although it took a few tries for us to set up the load tests in a way that satisfies our needs, we’re excited to see it in our testing to make Triplit even more reliable.
Bug fixes and improvements
- bug in subscriptions when using OR filters
- Types fixes for useInfiniteQuery and usePaginatedQuery
- Fixup bug erroneously changing the your cached schema data