A Countdown powered by the Resonate Typescript SDK and Cloudflare Workers. The countdown sends periodic notifications to ntfy.sh at configurable intervals.
The Countdown is implemented with Resonate's Durable Execution framework, Distributed Async Await. The Countdown is a simple loop that can sleep for hours, days, or weeks. On yield ctx.sleep the countdown function suspends (terminates), immediately completing the Cloudflare Worker execution. After the specified delay, Resonate will resume (restart) the countdown function by triggering a new Cloudflare Worker execution.
export function* countdown(
ctx: Context,
count: number,
delay: number,
url: string,
) {
for (let i = count; i > 0; i--) {
// send notification to ntfy.sh
yield* ctx.run(notify, url, `Countdown: ${i}`);
// sleep creates a suspension point causing
// the Cloudflare Worker execution to terminate
yield* ctx.sleep(delay * 60 * 1000);
}
// send the last notification to ntfy.sh
yield* ctx.run(notify, url, `Done`);
}Key Concepts:
- Suspension and Resumption: Executions can be suspended for any amount of time
- Stateful executions on stateless infrastructure: Short-lived function instances executing one step of a long-lived execution coordinated by the Resonate Server.
You can run the Countdown locally on your machine with Cloudflares's Wrangler Framework or you can deploy the Countdown to Cloudflare Platform.
Install the Resonate Server & CLI with Homebrew or download the latest release from Github.
brew install resonatehq/tap/resonate
Start the Resonate Server. By default, the Resonate Server will listen at http://localhost:8001.
resonate dev
Clone the repository
git clone https://github.com/resonatehq-examples/example-countdown-cloudflare-ts
cd example-countdown-cloudflare-ts
Install dependencies
npm install
Start the Cloudflare Worker. By default, the Cloudflare Worker will listen at http://localhost:8787.
npm run dev
The examples use ntfy.sh to send notifications. Create a unique channel name (to avoid receiving notifications from other users) and open the ntfy.sh channel in your browser.
echo https://ntfy.sh/resonatehq-$RANDOM
Start a countdown
resonate invoke <promise-id> --func countdown --arg <count> --arg <delay-in-minutes> --arg https://ntfy.sh/<channel> --target <function-url>
Example
resonate invoke countdown.1 --func countdown --arg 5 --arg 1 --arg https://ntfy.sh/resonatehq-17905 --target http://localhost:8787
Use the resonate tree command to visualize the countdown execution.
resonate tree countdown.1
Example output (while waiting on the second sleep):
countdown.1
├── countdown.1.0 🟢 (run)
├── countdown.1.1 🟢 (sleep)
├── countdown.1.2 🟢 (run)
└── countdown.1.3 🟡 (sleep)
This section guides you through deploying the countdown example to Cloudflare Platform using Workers for the countdown function.
Install the Resonate CLI with Homebrew or download the latest release from Github.
brew install resonatehq/tap/resonate
Ensure you have a Cloudflare Platform account.
Warning
Cloudflare Platform offers extensive configuration options. The instructions in this guide provide a baseline setup that you will need to adapt for your specific requirements, organizational policies, or security constraints.
Expose the Resonate server running locally to the cloud
Step 1: ngrok url
ngrok http 8001
Step 2: Run the server locally
Configure the Resonate Server with its URL.
resonate dev --system-url <ngrok-url>
Example
resonate dev --system-url https://3d6a08125e17.ngrok-free.app
Paste the wrangler.toml at the root of this directory (replace the name with the name of your newly created worker)
name = "XXXXX-XXXXX-XXXX"
main = "src/index.ts"
compatibility_date = "2024-01-01"Deploy it
npm run deploy
The examples use ntfy.sh to send notifications. Create a unique channel name (to avoid receiving notifications from other users) and open the ntfy.sh channel in your browser.
echo https://ntfy.sh/resonatehq-$RANDOM
Start a countdown
resonate invoke <promise-id> --func countdown --arg <count> --arg <delay-in-minutes> --arg https://ntfy.sh/<channel> --target $WORKER_URL
Example
resonate invoke countdown.1 --func countdown --arg 5 --arg 1 --arg https://ntfy.sh/resonatehq-22012 --target $WORKER_URL
Use the resonate tree command to visualize the countdown execution.
resonate tree countdown.1
Example output (while waiting on the second sleep):
countdown.1
├── countdown.1.0 🟢 (run)
├── countdown.1.1 🟢 (sleep)
├── countdown.1.2 🟢 (run)
└── countdown.1.3 🟡 (sleep)
If everything is configured correctly, you will see notifications in your ntfy.sh workspace.
If you are still having trouble please open an issue.


