Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: web sdk example with topic client that refreshes disposable tokens #1437

Merged
merged 13 commits into from
Sep 17, 2024

Conversation

anitarua
Copy link
Contributor

@anitarua anitarua commented Sep 12, 2024

Closes https://github.com/momentohq/dev-eco-issue-tracker/issues/1013

Implements a wrapper class TokenRefreshingTopicClient that automatically refreshes disposable tokens and active topics subscriptions before tokens expire.

A standalone example exists in a single file under examples/web/cache/refresh-disposable-tokens.ts and can be run completely locally by using a localTokenVendingMachine() method, or can be run against a deployed TVM by passing the endpoint to the example code.

Sample output:

Callback A: User code processing message 77
Callback B: User code processing message 77
Callback A: User code processing message 78
Callback B: User code processing message 78
Disposable token expiring soon, refreshing topic client with new token
Callback A: User code processing message 79
Generated a disposable token that will expire at 2024-09-12T23:21:27.000Z
Refreshing in 19586 ms
Callback B: User code processing message 79
Callback A: User code processing message 80
Callback B: User code processing message 80

The existing Vite chat app was also updated to use the TokenRefreshingTopicClient. Also fixed some bugs with the TVM and web app setup while testing and updated the CSS to be more Momento-themed.

Screenshot 2024-09-16 at 10 50 57 AM

@anitarua anitarua marked this pull request as ready for review September 12, 2024 23:55
@anitarua anitarua marked this pull request as draft September 14, 2024 00:52
@anitarua anitarua marked this pull request as ready for review September 16, 2024 17:54
@anitarua anitarua requested review from poppoerika and a team September 16, 2024 18:05
// In your own setup, the token vending machine would likely be a separate service.
// See https://github.com/momentohq/client-sdk-javascript/tree/main/examples/nodejs/token-vending-machine
async function tokenVendingMachine(): Promise<{token: string; expiresAt: ExpiresAt}> {
const authClient = new AuthClient({});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably I'm not understanding the whole picture here but don't we need a super user api key to call the getDisposableToken?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's used only in the localTokenVendingMachine example for anyone who wants to run the example completely locally (in case they don't have a deployed TVM). Then they can just run this example like any other one, export MOMENTO_API_KEY=<api key> npm run tokens.

If they want to run the example against their own deployed TVM, then they can pass in an endpoint instead and switch to using the tokenVendingMachine function in the example code. Running the example that way would look like: TVM_ENDPOINT=<their TVM url> npm run tokens

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anitarua
So localTokenVendingMachine is something they will integrate into their backend so that api can be called from each iPad?
But where is localTokenVendingMachine using MOMENTO_API_KEY?

Copy link
Contributor Author

@anitarua anitarua Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, their backend would call generateDisposableToken and return that token to the iPads (or other end user devices).

MOMENTO_API_KEY is used to create an AuthClient. This auth client in the example is using default configuration settings: CredentialProvider that expects a MOMENTO_API_KEY environment variable and default AuthClientConfiguration. These default settings are now optional, which is why the new AuthClient({}) constructor call doesn't contain anything.

Their backend component that creates the AuthClient and calls generateDisposableToken could be a Lambda function, like the TVM example is, or it can be something they write into their own server.

Copy link
Contributor

@poppoerika poppoerika left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the back and force due to my lack of my knowledge about the latest state of our SDK! But looks good to me. Thank you for adding this example 🚀

@anitarua anitarua merged commit 73d7f8f into main Sep 17, 2024
13 checks passed
@anitarua anitarua deleted the disposable-token-example branch September 17, 2024 21:48
return disposableToken;
}

async function tokenVendingMachine(): Promise<{token: string; expiresAt: ExpiresAt}> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@poppoerika I'm late reviewing this PR but this is the function that they would implement on their ipads. It will just call fetch with whatever URL, headers, query params are necessary to get a token from their server-side token vending endpoint.

Their server-side token-vending endpoint will be the thing that uses AuthClient to actually get the tokens.

// for each active subscription, unsubscribe from the old topic client and subscribe to the new one
for (const key in this.activeSubscriptions) {
const value = this.activeSubscriptions[key];
const newSubscription = await newTopicClient.subscribe(value.cacheName, value.topicName, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anitarua I think maybe you need to call your local subscribe function here so that you get the wrapped version, don't you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants