Deploy a real-time chat application
In this tutorial, you will deploy a serverless, real-time chat application that runs using Durable Objects.
This chat application uses a Durable Object to control each chat room. Users connect to the Object using WebSockets. Messages from one user are broadcast to all the other users. The chat history is also stored in durable storage. Real-time messages are relayed directly from one user to others without going through the storage layer.
To continue with this tutorial, you must purchase the Workers Paid plan and enable Durable Objects by logging into the Cloudflare dashboard ↗ > Workers & Pages > select your Worker > Durable Objects.
All of the tutorials assume you have already completed the Get started guide, which gets you set up with a Cloudflare Workers account, C3 ↗, and Wrangler.
Open your terminal and clone the workers-chat-demo ↗ repository:
After you have cloned the repository, authenticate Wrangler by running:
When you are ready to deploy your application, run:
Your application will be deployed to your *.workers.dev
subdomain.
To deploy your application to a custom domain within the Cloudflare dashboard, go to your Worker > Triggers > Add Custom Domain.
To deploy your application to a custom domain using Wrangler, open your project's wrangler.toml
.
To configure a route in your wrangler.toml
, add the following to your environment:
If you have specified your zone ID in the environment of your wrangler.toml
, you will not need to write it again in object form.
To configure a subdomain in your wrangler.toml
, add the following to your environment:
To test your live application:
- Open your
edge-chat-demo.<SUBDOMAIN>.workers.dev
subdomain. Your subdomain can be found in the Cloudflare dashboard ↗ > Workers & Pages > your Worker > Triggers > Routes > select theedge-chat-demo.<SUBDOMAIN>.workers.dev
route. - Enter a name in the your name field.
- Choose whether to enter a public room or create a private room.
- Send the link to other participants. You will be able to view room participants on the right side of the screen.
To uninstall your chat application, modify your wrangler.toml
file to remove the durable_objects
bindings and add a deleted_classes
migration:
Then run npx wrangler deploy
.
To delete your Worker:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- In Overview, select your Worker.
- Select Manage Service > Delete. For complete instructions on set up and deletion, refer to the
README.md
in your cloned repository.
By completing this tutorial, you have deployed a real-time chat application with Durable Objects and Cloudflare Workers.
Continue building with other Cloudflare Workers tutorials below.
- Build a Slackbot
- Create SMS notifications for your GitHub repository using Twilio
- Build a QR code generator