Skip to content

resonatehq-examples/example-browser-worker-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Resonate Workers in the Browser

** Currently only works in chrome **

A fun demo showing how Resonate Workers can be hosted in the browser using the Resonate TypeScript SDK.

What it does

This demo implements a recursive factorial function that:

  • Executes in the browser as a Resonate worker
  • Can be triggered by creating promises via the Resonate CLI

Running the Example

1. Start Resonate Server

Install the Resonate Server:

brew install resonatehq/tap/resonate

Start the Resonate Server without persistence and with CORS enabled:

resonate serve --aio-store-sqlite-path :memory: --api-http-cors-allow-origin "*" --aio-sender-plugin-poll-cors-allow-origin "*"

2. Start the Resonate Wroker(s)

npm run dev

2. Run Factorial

Create a durable promise representing the durable invocation of factorial(5)

resonate promises create factorial.5 \
  --idempotency-key factorial.5 \
  --timeout 1h \
  --data '{"func":"factorial","args":[5]}' \
  --tag resonate:invoke=poll://any@default

Fetch the result

resonate promises get factorial.5

You should see:

  • Recursive calls displayed in the browser: factorial(5) called, factorial(4) called, etc.
  • Final result: 120 (5! = 5×4×3×2×1)

Scripts

  • npm run build - Build the browser bundle
  • npm run dev - Build and serve with auto-refresh
  • npm run serve - Serve the built files

Architecture

  • Frontend: Browser worker polling for tasks via Server-Sent Events
  • Backend: Resonate server managing distributed state and task routing
  • Communication: HTTP API for task claiming, SSE for real-time updates

About

A Resonate Worker running in a browser window or browser tab

Topics

Resources

Stars

Watchers

Forks