Introduction to Cloudflare Workers
Hello today, I'm gonna be talking about Cloudflare Workers.
What are Cloudflare Workers?
They are Cloudflare's serverless platform.
And for those not very familiar with serverless, I always like to think about it in terms of abstractions.
So back in the day, if you wanted to deploy an API or some kind of Web application, you'd have to either purchase hardware or maybe you purchase a virtual private server.
And you'd have to think about the operating system that it runs, what languages it supports, how much memory, how much CPU.
And so in a large part, the serverless revolution was all about eliminating those abstractions, kind of hiding them from the user.
So you could just focus on your application and they would take care of the rest.
One of the things that makes us a little bit unique is that we deploy your code to edge by default.
Meaning that instead of picking a location, all of your code always deploys to our global cloud network.
We're incredibly affordable at the time of this presentation, we charge $5 for every 10 million requests.
And we also have a bunch of really cool things like edge storage and databases to make it possible for you to build your entire application within the Cloudflare ecosystem.
What do workers look like?
For those coming from a front end background, they should look pretty familiar.
We offer two different syntaxes for them.
One is the ESM or JavaScript module syntax pictured below.
This is a default export and then you listen on a fetch event and it takes in a request and returns a response much like any kind of node application.
Like if you've worked with Express or something like that before.
The other way is through a service worker syntax, where you can do document.addEventListener, and you can listen for a fetch event and it still takes in a request and returns a response.
Some of the benefits of using workers, you can write your functions or your application in a wide variety of languages.
Natively we support JavaScript, TypeScript, Rust, C and C++.
We already instantly deploy your code to over 270 different locations around the world.
And this also means that as your users go to use your application, they're always routed to the server closest to them.
Your code runs in V8 isolates, which is the JavaScript runtime that Google Chrome and Microsoft Edge use as opposed to traditional virtual machines, which means we can eliminate cold starts and have your application ready to run in zero milliseconds.
And we also offer a full range of tools for storage, databases, websockets, streaming video, and a lot more.
The Worker ecosystem at a high level Workers are the scripts that we saw earlier with the request/response.
We have a bunch of different ways to store state.
So we have our KV, our key value store, and durable objects, which are a single instance JavaScript object.
For a traditional object storage like Amazon S3 we have Cloudflare R2.
We offer SQLite at the edge with our product called D1.
We have great static site support for static site generators, like Hugo or Eleventy with Cloudflare pages.
We have opportunities for great deployment workflows, such as deploying to staging, and then promoting to production through service environments.
We allow scripts to communicate with each other.
For example, you can have five different workers that all talk to each other through service bindings.
And to top it off, we have a CLI to help you manage all of this called Wrangler.
How to create Workers?
You have three options.
You can use our CLI, Wrangler.
You can use the Cloudflare dashboard at Cloudflare dot com or you can use infrastructure as code services, such as Terraform.
In the image below, you can see how easy it is to get started with Wrangler.
Install it from NPM.
Do a Wrangler login in order to connect your Cloudflare credentials with the CLI.
`wrangler init` any new project, and then `wrangler publish` to deploy it to a unique URL.
A quick demo.
Hop over to my terminal and I'll do `wrangle init workers-example`.
It's gonna go ahead and make a new git repo, get NPM installed with a package JSON install and set up and configure TypeScript, and then generate our scaffolding for our project.
Now that that's done, we can go and head and `cd` into it and I'll do an `ls` and you can see a traditional node app with node modules, package lock, all of those things.
And then from here, I can go ahead and do a `wrangler publish`.
This will bundle it all up, deploy it to our global network and then return a nice URL for me to visit the site.
workers-example.jkup.workers.dev.
Heading back over, if you're looking to get started today, some resources that'll help you would be workers dot Cloudflare dot com, which is the project homepage, workers.new, which allows you to instantly build a new worker using our dashboard, developer dot Cloudflare dot com slash workers where our documentation lives.
The @cloudflare dev account on Twitter and discord dot gg slash Cloudflare, where we have a really active discord community.
Thank you so much.