Skip to content

Latest commit

 

History

History
103 lines (55 loc) · 5 KB

INSTALL.md

File metadata and controls

103 lines (55 loc) · 5 KB

Installing the P2PCF Cloudflare Worker

Setting up the worker is a few simple steps.

Create a Cloudflare account

Go to https://www.cloudflare.com/ and create an account.

Set up the R2 bucket

  1. In the Cloudflare console, go to R2:

r2dialog

  1. Click Create Bucket

image

  1. Enter any bucket name you want (we use p2pcf)

image

  1. Click Create Bucket

Create the Cloudflare Worker

  1. In the Cloudflare console, go to Workers in the main navigation:

image

  1. Then, Create a Service

  2. Enter any service name you want (we use p2pcf). Leave the starter type as HTTP Handler. Press Create service.

image

Bind the worker to R2

  1. In your Worker's view, click the Settings tab:

image

  1. Click Variables on the left:

image

  1. Scroll down to R2 Bucket Bindings and click Add Binding:

image

  1. Choose any binding name you want (we use BUCKET), and choose the bucket you created earlier. Click Save.

image

Deploy the worker code

  1. Go back to your worker's dashboard. To get there Workers in the main navigation:

image

  1. Click on your worker:

image

  1. Click on Quick Edit. This will open the code editor.

image

  1. 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.)

  2. Paste it into the editor, click Save and Deploy, and click to confirm the deployment.

image

  1. Your worker is now deployed and ready to be used. Hit the URL to your worker (under Routes) to make sure it's working:

image

  1. 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 in Settings.

image

  1. (Optional) You can add two other optional variables in the Environment Variables in Settings 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 on mysite.com or app.mysite.com.
  • ORIGIN_QUOTA: Number of requests per month to allow for any origin not specified in ALLOWED_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 via ALLOWED_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.

Use your worker in your code

The URL to your worker can be found at the top of the console view of your worker:

image

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.