Matrix Federation with Cloudflare Workers: Difference between revisions

Initial
 
source focus
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Return to [[server-guides.md|server-guides]] Return to [[matrix-server-guide.md|matrix-server-guide]]
= Setting Up Matrix on Cloudflare Workers =


Source:https://appelman.se/matrix-on-cloudflare/ Cloudflare Workers are a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.
Cloudflare Workers are a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.


Here’s a step-by-step guide:
Source for this guide: [https://appelman.se/matrix-on-cloudflare/ Matrix on Cloudflare]


<ol style="list-style-type: decimal;">
== Step-by-Step Guide ==
<li><p>'''Log in to your Cloudflare account.'''</p>
Follow these steps to configure Matrix using Cloudflare Workers:
<p>Navigate to the account and domain where you want to create the worker.</p></li>
<li><p>'''Navigate to the Workers tab.'''</p>
<p>This is located on the top menu. Clicking it will bring you to the Workers’ dashboard.</p></li>
<li><p>'''Click on the “Create a Worker” button.'''</p>
<p>This will open a new page with a script template for your worker.</p></li>
<li><p>'''Replace the default script with your script.'''</p>
<p>In the editor, delete the existing script and paste your script from my previous message.</p>
<p>Remember to replace <code>&quot;https://matrix.irregulars.io,&quot;</code>“https://vector.im,” and <code>&quot;matrix-fed.irregulars.io:8443&quot;</code> with your actual homeserver URL, identity server URL, and federation server, respectively.</p></li>
<li><p>'''Give your worker a name.'''</p>
<p>The name is used to identify your worker, and it’s also used in the worker’s URL.</p></li>
<li><p>'''Click “Save and Deploy”.'''</p>
<p>Your worker is now deployed and live. You can test it directly in the Workers dashboard by requesting your worker’s URL.</p></li></ol>


Now, your worker is ready. In the next step (which is the Step 2 from my previous message), you’ll need to set up routes to your worker so that the requests to <code>/.well-known/matrix/client</code> and <code>/.well-known/matrix/server</code> on your domain are handled by this worker.
# '''Log in to your Cloudflare account.''' 
Navigate to the account and domain where you want to create the worker.


<syntaxhighlight lang="javascript">const HOMESERVER_URL = "https://matrix.example.com";
# '''Navigate to the Workers tab.''' 
This is located on the top menu. Clicking it will bring you to the Workers’ dashboard.
 
# '''Click on the “Create a Worker” button.''' 
This will open a new page with a script template for your worker.
 
# '''Replace the default script with your script.''' 
In the editor, delete the existing script and paste the following script. 
Replace:
* `"https://matrix.example.com"` with your actual homeserver URL.
* `"https://vector.im"` with your identity server URL.
* `"matrix-fed.example.com:8443"` with your federation server.
 
<pre lang="javascript">
const HOMESERVER_URL = "https://matrix.example.com";
const IDENTITY_SERVER_URL = "https://vector.im";
const IDENTITY_SERVER_URL = "https://vector.im";
const FEDERATION_SERVER = "matrix-fed.example.com:8443";
const FEDERATION_SERVER = "matrix-fed.example.com:8443";
Line 32: Line 35:
       case "/.well-known/matrix/client":
       case "/.well-known/matrix/client":
         return new Response(
         return new Response(
           <code>{"m.homeserver": {"base_url": "${HOMESERVER_URL}"},"m.identity_server": {"base_url": "${IDENTITY_SERVER_URL}"}}</code>
           JSON.stringify({
            "m.homeserver": { "base_url": HOMESERVER_URL },
            "m.identity_server": { "base_url": IDENTITY_SERVER_URL }
          }),
          { headers: { "Content-Type": "application/json" } }
         );
         );
       case "/.well-known/matrix/server":
       case "/.well-known/matrix/server":
         return new Response(<code>{"m.server": "${FEDERATION_SERVER}"}</code>);
         return new Response(
          JSON.stringify({ "m.server": FEDERATION_SERVER }),
          { headers: { "Content-Type": "application/json" } }
        );
       default:
       default:
         return new Response("Invalid request");
         return new Response("Invalid request", { status: 404 });
     }
     }
   },
   },
};</syntaxhighlight>
};
</pre>
 
# '''Give your worker a name.''' 
The name identifies your worker and is used in the worker’s URL.
 
# '''Click “Save and Deploy”.''' 
Your worker is now deployed and live. You can test it directly in the Workers dashboard by requesting your worker’s URL.
 
== Setting Up Routes ==
Once your worker is deployed, set up routes to ensure requests to the following endpoints are handled by the worker:
* `/.well-known/matrix/client`
* `/.well-known/matrix/server`
 
You can configure these routes in your Cloudflare dashboard under the **Workers > Routes** section.
 
== Categories ==
[[Category:Server Management]] 
[[Category:Cloudflare]] 
[[Category:Matrix]] 
[[Category:Networking]] 
[[Category:Troubleshooting]] 
[[Category:Configuration]]