⛅ Getting Started with Serverless Cloudflare Workers (Lesson 1)

Search for a command to run...

No comments yet. Be the first to comment.
Turn any website into an Installable App How I make web apps that install to the home screen (Android and iOS), launch full-screen (not in-browser), work offline, and offer a first-class in-app Instal

Type Narrowing allows the IDE (development environment) to narrow down the object type, from a union type, to a more specific type. Bullsh*t Example In this bullsh*t example, value is the union type string | number, meaning it can either be a string ...
There is still a lot of work to be done, but I finally have something "playable" 🔥 I would describe it as a mashup of Mine Sweeper and a rogue-like dungeon crawler RPG. That's right! The first playable demo is up! The game is still in active develo...

I recently added some dialog options for my indie game, Dungeon Sweeper: A Knights Adventure and wanted to create a TypeWriter effect for the dialog text. This technique works well for monospaced fonts and may not work for variable-width fonts. Fail...

TL;DR: I discuss adding intuitive features, dealing with Phaser quirks such as a tween memory leak and scaling issues, managing screen size, and creating an executable for a demo. I express my struggle with the less enjoyable parts of game developmen...

Cloudflare Workers are a similar to other serverless platform offerings, but at the Edge. Edge meaning your code is deployed and runs globally across many regions in the same way a CDN works for static content.
Server Side Rendering - Better SEO compared to a static site host.
Automatic Scaling - Scaling is not an easy problem to solve and can be a PITA.
Fast - Cloudflare Workers are geographically distributed, so they're fast wherever your users happen to be.
0ms Cold Starts - Unlike AWS Lambdas, Azure Functions, and Google Cloud Functions, Cloudflare Workers have no startup time.
Price - The price is amazing and the FREE plan might even be enough for your needs.
CPU Runtime - 10ms (free) and 50ms (paid). You won't be running any CPU intensive tasks using Cloudflare Workers. So those will have to be offloaded to another server or service. Idle time isn't counted against this, so wait time for long API calls won't count against this.
Simultaneous Outgoing Requests - 6. If you make a lot of API calls from your workers, you could easily hit this limit. So you'll have to be smart about caching.
It's not Node.js - Cloudflare Workers do not run in a Node.js environment. There may be some Node API's or NPM modules that don't work in the Cloudlfare environment. But I have found that if it run inside a web browser, it's likely that it will also run in a Worker.
Cloudflare Workers are not a replacement for Lambdas, or Cloud Functions. You likely won't be able to port your code from one to a worker. You will also need to design your application around the strengths and weaknesses of Workers.
With that being said, I am incredibly excited about the Cloudflare Worker platform. I have been working on some PoC projects on Cloudflare Workers and have been pretty impressed with the experience so far.
Install the wrangler CLI.
$ npm install -g @cloudflare/wrangler
Use wrangler to generate a new site.
$ wrangler generate my-worker
$ cd my-worker
Make sure it works
$ wrangler preview --watch
If all goes well, a window should popup looking a bit like this:

Congratulations, your Cloudflare Worker is running locally!
Sign into Cloudflare and go to the api-tokens page and click Create Token then select Use Cloudflare Workers template.

My permissions looks like this:

Copy your Token and keep it somewhere safe, like a password manager. It will not be shown again.
Now launch wrangler config and enter in your API Key.
$ wrangler config
There are other ways to authenticate, read the documentation for more details, but an API Token is my preference.
Open your wrangler.toml file and populate your account_id. Then use the publish subcommand.
$ wrangler publish
After publishing, I get a message with the URL of my new Worker.

I will be creating many more articles and videos talking about Cloudflare Workers. Please subscribe to my 📰 Newsletter to be notified when new articles come out.
Read further: Cloudflare Docs - Getting Started
Cheers 🍻
Photo by Rakicevic Nenad from Pexels