A chat application build on top of Remix and Cloudflare.

Overview

Remix + Cloudflare Workers starter with Turborepo 🚀

Starter to get going with Remix and Cloudflare Workers.

This template is based on the starter created by Jacob(remix-cloudflare-worker-template).

What's inside? 👀

This turborepo uses npm as a package manager. It includes the following packages/apps:

Apps and Packages 📦

  • user-do: a Durable Object class for managing users
  • remix-app: a Remix app (with Tailwind)
  • worker: a Worker handler function
  • cloudflare-env: types for Cloudflare environement variables
  • eslint-config-custom: eslint configurations (includes @remix-run/eslint-config and eslint-config-prettier)
  • tsconfig: tsconfig.json used throughout the monorepo

Getting Started 🪄

Let's setup wrangler CLI instalation (wrangler requires a minimum Node version of 16)

npm i @cloudflare/wrangler -g

After installation, log in to your Cloudflare account from the CLI.

wrangler login

Let's install the dependencies.

npm install

Now we can set up the project.

npm run setup

The setup script will ask for your worker's name. It can be any name. And then, we also need to add the Account ID, which you can find by visiting the Cloudflare Dashboard.

You can add the Worker name and the Account ID later to the `wrangler.toml.

That's all we're ready to push to prod! 🚀

Before pushing to the worker via GitHub action, we have to configure the CF_API_TOKEN secret in GitHub. We can generate an API Token from here. When presented with the list of templates to choose from, select the "Edit Cloudflare Workers" option. This template should have the necessary permissions to push a Worker from GitHub. Now we can commit the changes made to wrangler.toml and push the changes.

git commit -am "<message>"
git push

Recap 🌀

npm install @cloudflare/wrangler -g
wrangler login
npm install
npm run setup
# configure CF_API_TOKEN action secret
git add -A -m "<message>"
git push

Durable Objects 🔥

Durable Objects are only available with a Workers paid subscription. However, you can upgrade to the paid plan from the Dashboard.

This starter template comes with a simple DO implementation to keep track of the number of times the root loader is invoked.

If you're starting with DO and not sure what it is, go through the official docs on Durable Objects will be a good start! And checkout using durable objects for more applications of DO.

Defining a Durable Object

This template comes with a script to create the boilerplate for a new Durable Object class.

npm run new:do

The script will have instructions to initialise the DO with the worker. Don't forget to follow them!

More information on DO

You can skip this section if you have used the script to generate the DO class. Continue for more information on DO :)

To define a DO class, check out the docs.

To include the DO class into the worker, we have to add the new DO package as a dependendency of the worker. Then we need to create a binding for the DO in the configuration file wrangler.toml.

[durable_objects]
bindings = [
  {name = "<DO_BINDING_NAME>", class_name = "<DO_CLASS_NAME>"},
]

For development add the following to wrangler.dev.toml

[env.dev.durable_objects]
bindings = [
  {name = "<DO_BINDING_NAME>", class_name = "<DO_CLASS_NAME>"},
]

We must create a migration to register the DO in the configuration file.

[[migrations]]
tag = "v<RUNNING_TAG_ID>"
new_classes = ["<DO_CLASS_NAME>"]

More info about uploading a DO here.

The DO binding will be available in the data functions (loader/action) through the context argument. Types for the context can be added at cloudflare-env. To add a type for a newly created DO, we have to add the following to the .d.ts file

<DO_BINDING_NAME>: DurableObjectNamespace

Now we can access the DO binding from the data function through the context.

export let loader: LoaderFunction = ({ context }) => {
	context.MY_DO;
	//        ^ Will have proper type definitions
	return null;
};

Deleting a DO

It requires a migration to delete a DO. More info here

Worker KV 📒

This template does not come with a KV namespace attached to it. However, you can create one using the Wrangler CLI.

wrangler kv:namespace create "MY_KV"

The above command will create a KV namespace. Now we need to bind the created namespace with the worker. When we run the create command, the CLI will print the binding configuration we need to add to our wrangler.toml configuration file. It will look like

kv_namespaces = [
  { binding = "MY_KV", id = "xxxx" }
]

We must add this above the [site] block in the wrangler.toml file.

We have added the KV namespace binding for the production environment, but we also need a namespace for dev. We can do that by creating a new namespace for dev.

wrangler kv:namespace create "MY_KV" --preview

This will generate a namespace for the dev environment, and we must add this below the [env.dev] block in the dev configuration file wrangler.dev.toml.

[env.dev]
kv_namespaces = [
  { binding = "MY_KV", id = "xxxx", preview_id = "xxxx" }
]

Note: We need to add the preview_id key to the configuration file along with the id key with the same value (ref: stackoverflow).

The bounded KV will be available in the loader/action via the context argument passed to the functions. We define types for the context at cloudflare-env. To add types for a newly bounded KV, we have to add the following to the .d.ts file

MY_KV: KVNamespace;

Now we can access the KV namespace from the data function through the context.

export let loader: LoaderFunction = ({ context }) => {
	context.MY_KV;
	//        ^ Will have proper type definitions
	return null;
};

Environment Variables (Secrets) 🔐

Adding Worker Environment Variables

You must run wrangler commands from a directory which contains the wrangler.toml file. Either we can cd into the worker directory present at packages/worker, or we can specify the path to the configuration file in the CLI.

To set a worker secret, we can

cd packages/worker
wrangler secret put <SECRET_NAME>

or,

wrangler secret -c packages/worker/wrangler.toml

Like DO/KV binding, the Env variables will be passed to the data functions via the context argument. But we have only configured the production worker with the secret. So let's configure the local environment with the secret.

When we ran npm run setup, the CLI would have created packages/worker/wrangler.dev.toml. And the configuration file should have a vars key under the [env.dev] table. So we can add the new secret there.

[env.dev]
vars = {SECRET_KEY = "<value>"}

One last thing to do is add the type definition for the Env var at config/cloudflare-env/index.d.ts.

SECRET_KEY: string;

Now, we can access SESSION_SECRET via context.env.SESSION_SECRET in the data functions inside our Remix app.

Turbo Setup

This repository is used in the npx create-turbo@latest command and selected when choosing which package manager you wish to use with your monorepo (npm).

Build 🛠

To build all apps and packages, run the following command:

npm run build

Develop 💻

To develop all apps and packages, run the following command:

npm run dev

Remote Caching 💽

Turborepo can use a technique known as Remote Caching (Beta) to share cache artefacts across machines, enabling you to share build caches with your team and CI/CD pipelines.

By default, Turborepo will cache locally. To enable Remote Caching (Beta), you will need an account with Vercel. If you don't have an account, you can create one, then enter the following commands:

npx turbo login

This will authenticate the Turborepo CLI with your Vercel account.

Next, you can link your Turborepo to your Remote Cache by running the following command from the root of your turborepo:

npx turbo link

Useful Links 🔗

Remix + Cloudflare

Turborepo

Learn more about the power of Turborepo:

You might also like...

Remix + Cloudflare Workers + DO + Turborepo

Remix + Cloudflare Workers + DO + Turborepo A starter to get you up and going with Remix on Cloudflare with all the latest and greatest. What's inside

Dec 12, 2022

Remix + Cloudflare Workers + Wrangler2 + Tailwind + ESLint + Prettier + Vitest + Playwright

Welcome to Remix! Remix Docs Development You will be running two processes during development: The Miniflare server (miniflare is a local environment

Dec 19, 2022

A starter template for Remix + Cloudflare Workers + DO + KV + Turborepo

Remix + Cloudflare Workers starter with Turborepo 🚀 Starter to get going with Remix and Cloudflare Workers. This template is based on the starter cre

Jan 2, 2023

Chat View let's you quickly and easily create elegant Chat UIs in your Markdown Files.

Chat View let's you quickly and easily create elegant Chat UIs in your Markdown Files.

Obsidian Chat View Plugin Chat View let's you quickly and easily create elegant Chat UIs in your Markdown Files. Usage Every chat message must be pref

Dec 27, 2022

Omnichannel Live Chat Widget UI Components offers a re-usable component-based library to help create a custom chat widget that can be connected to the Dynamics 365 Customer Service experience.

Omnichannel Live Chat Widget UI Components @microsoft/omnichannel-chat-widget is a React-based UI component library which allows you to build your own

Dec 15, 2022

Replaces Youtube Chat with Destiny.gg chat.

Replaces Youtube Chat with Destiny.gg chat.

A lightweight extension that replaces the native Youtube Live chat with an embeded destiny.gg chat. Note: This is in no way affiliated with Destiny.gg

Jul 27, 2022

O Web-Chat é um projeto com o intuito de criar um chat de ajuda, que contém uma experiência dinâmica e salva as informações preenchidas pelo usuário usando um formulário.

O Web-Chat é um projeto com o intuito de criar um chat de ajuda, que contém uma experiência dinâmica e salva as informações preenchidas pelo usuário usando um formulário.

Web-Chat Introdução O Web-Chat é um projeto com o intuito de criar um chat de ajuda, que contém uma experiência dinâmica e salva as informações preenc

Oct 5, 2022

simple-remix-blog is a blog template built using Remix and TailwindCSS. Create your own blog in just a few minutes!

simple-remix-blog is a blog template built using Remix and TailwindCSS. Create your own blog in just a few minutes!

simple-remix-blog is a blog template built using remix.run and TailwindCSS. It supports markdown and MDX for the blog posts. You can clone it and star

Dec 8, 2022

The Remix version of the fakebooks app demonstrated on https://remix.run. Check out the CRA version: https://github.com/kentcdodds/fakebooks-cra

Remix Fakebooks App This is a (very) simple implementation of the fakebooks mock app demonstrated on remix.run. There is no database, but there is an

Dec 22, 2022
Remix enables you to build fantastic user experiences for the web and feel happy with the code that got you there. Get a jumpstart on Remix with this workshop.

?? Remix Fundamentals Build Better websites with Remix Remix enables you to build fantastic user experiences for the web and feel happy with the code

Frontend Masters 204 Dec 25, 2022
The Chat'Inn is a simple and minimal realtime chat application whose database is powered by firebase and firestore.

The Chat-in The Chat'Inn is a simple and minimal realtime chat application whose database is powered by firebase and firestore. The frontend part is c

Aswin Asok 11 Aug 8, 2022
This application provides the CDK project and a frontend that allows you to build a serverless chat application based on API Gateway's WebSocket-based API feature.

Serverless chat application using ApiGateway Websockets This project lets you provision a ready-to-use fully serverless real-time chat application usi

AWS Samples 60 Jan 3, 2023
Functional-style Cloudflare Durable Objects with direct API calls from Cloudflare Workers and TypeScript support.

durable-apis Simplifies usage of Cloudflare Durable Objects, allowing a functional programming style or class style, lightweight object definitions, a

Dabble 12 Jan 2, 2023
A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages (https://developers.cloudflare.com/pub-sub/)

pubsub A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages. This includes: A isValidBrokerRequest helper for au

Cloudflare 18 Dec 4, 2022
Connect to a Postgres database from a Cloudflare Worker, using Cloudflare Tunnel

Cloudflare Workers Postgres Client This is an experimental module. Heavily based on cloudflare/worker-template-postgres, but cleaned up and bundled in

BubblyDoo 17 Dec 22, 2022
A lightweight, performant, and simple-to-use wrapper component to stick section headers to the top when scrolling brings them to top

A lightweight, performant, and simple-to-use wrapper component to stick section headers to the top when scrolling brings them to top

Mayank 7 Jun 27, 2022
Kafka 0.8.0 broker implementation on top of Cloudflare Workers

Kafka Worker A Kafka 0.8.0 broker implementation on top of Cloudflare Workers and Durable Objects. This broker supports 4 client-facing APIs: Produce

Max Peterson 129 Dec 11, 2022
Remix run stack built for the edge (cloudflare pages and d1)

Remix Race Stack Learn more about Remix Stacks. npx create-remix@latest --template jose-donato/race-stack What's in the stack Cloudflare Pages for ho

null 120 Aug 10, 2023
Starting template for building a Remix site with CloudFlare Workers (ES Modules Syntax)

Starting template for building a Remix site with CloudFlare Workers (ES Modules Syntax)

null 12 May 20, 2022