Setting up the worker is a few simple steps.
Go to https://www.cloudflare.com/ and create an account.
- In the Cloudflare console, go to R2:
- Click Create Bucket
- Enter any bucket name you want (we use
p2pcf
)
- Click
Create Bucket
- In the Cloudflare console, go to
Workers
in the main navigation:
-
Then,
Create a Service
-
Enter any service name you want (we use
p2pcf
). Leave the starter type asHTTP Handler
. PressCreate service
.
- In your Worker's view, click the
Settings
tab:
- Click
Variables
on the left:
- Scroll down to
R2 Bucket Bindings
and clickAdd Binding
:
- Choose any binding name you want (we use
BUCKET
), and choose the bucket you created earlier. ClickSave
.
- Go back to your worker's dashboard. To get there
Workers
in the main navigation:
- Click on your worker:
- Click on
Quick Edit
. This will open the code editor.
-
Now, in a separate tab, open the worker source from https://github.com/gfodor/p2pcf/blob/master/src/worker.js and copy it. (Click
Raw
to get the raw text.) -
Paste it into the editor, click
Save and Deploy
, and click to confirm the deployment.
- Your worker is now deployed and ready to be used. Hit the URL to your worker (under
Routes
) to make sure it's working:
- If you are on a paid plan, it is highly recommended you ensure your worker is in the
Bundled
usage model, which will increase your unmetered requests by 10x. The worker is very CPU efficient so should work fine in this usage model. You can find it inSettings
.
- (Optional) You can add two other optional variables in the
Environment Variables
inSettings
to increase the security of your worker.
ALLOWED_ORIGINS
: A comma-separated list of origins that will be allowed to access the worker. If you're not offering a public worker, this is recommended.- Example:
https://mysite.com,https://app.mysite.com
would limit use of the worker to secured sites running onmysite.com
orapp.mysite.com
.
- Example:
ORIGIN_QUOTA
: Number of requests per month to allow for any origin not specified inALLOWED_ORIGINS
. If you're offering a public worker, this is recommended to rate limit public usage. The default is 10000 if you have not restricted origins viaALLOWED_ORIGINS
and zero if origins are restricted (so if you restrict origins, other origins will have no access by default.)- Example:
100
would limit use of the worker to 100 requests per month from a given origin.
- Example:
The URL to your worker can be found at the top of the console view of your worker:
To use your worker in your client code, specify it as the workerUrl
in the options passed to the P2PCF
constructor:
import P2PCF from 'p2pcf'
const p2pcf = new P2PCF('MyUsername', 'MyRoom', { workerUrl: "https://p2pcf.minddrop.workers.dev" })
That's it! You now have a free (or cheap) WebRTC signalling server that will stay up as long as Cloudflare is working.