momentohq / client-sdk-javascript

Official JavaScript SDK for Momento Serverless Cache
Apache License 2.0
55 stars 20 forks source link

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

Closed anitarua closed 5 days ago

anitarua commented 1 week ago

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