DEV Community

Cover image for Host your own web-based collaborative IDE
James Murdza
James Murdza

Posted on • Updated on

Host your own web-based collaborative IDE

Intro

I recently got to try Ishaan Dey's Sandbox, (ishaan1013/sandbox) which is an open source web-based editor similar to Replit that lets you write and run code in your browser.

Screenshot

In this post I write down the steps I followed to get the project running locally.

Quick noteβ€”in some of the text below you may see the use of emojis like 🍎 in example code. It should be very obvious what you need to put in place of the emojisβ€”if not, leave a comment!

Requirements

The application uses NodeJS and can be run with Docker.

Needed accounts to set up:

  • Clerk:Β Used for user authentication.
  • Liveblocks:Β Used for collaborative editing.
  • Cloudflare:Β Used for relational data storage (D2) and file storage (R2).

A quick overview of the tech before we start: The deployment uses a NextJS app for the frontend and an ExpressJS server on the backend. Presumably that's because NextJS integrates well with Clerk middleware but not with Socket.io.

Initial setup

No surprise in the first step:

git clone https://github.com/ishaan1013/sandbox
cd sandbox
Enter fullscreen mode Exit fullscreen mode

Run npm install in:

/frontend
/backend/database
/backend/storage
/backend/server
Enter fullscreen mode Exit fullscreen mode

Adding Clerk

Setup the Clerk account.
Get the API keys from Clerk.

Update /frontend/.env:

NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY='πŸ”‘'
CLERK_SECRET_KEY='πŸ”‘'
Enter fullscreen mode Exit fullscreen mode

Deploying the storage bucket

Go to Cloudflare.
Create and name an R2 storage bucket in the control panel.
Copy the account ID of one domain.

Update /backend/storage/src/wrangler.toml:

account_id = 'πŸ”‘'
bucket_name = 'πŸ”‘'
key = 'SUPERDUPERSECRET'
Enter fullscreen mode Exit fullscreen mode

In the /backend/storage/src directory:

npx wrangler deploy
Enter fullscreen mode Exit fullscreen mode

Deploying the database

Create a database:

npx wrangler d1 create sandbox-database
Enter fullscreen mode Exit fullscreen mode

Use the output for the next setp.

Update /backend/database/src/wrangler.toml:

database_name = 'πŸ”‘'
database_id = 'πŸ”‘'
KEY = 'SUPERDUPERSECRET'
STORAGE_WORKER_URL = 'https://storage.🍎.workers.dev'
Enter fullscreen mode Exit fullscreen mode

In the /backend/database/src directory:

npx wrangler deploy
Enter fullscreen mode Exit fullscreen mode

Applying the database schema

Delete the /backend/database/drizzle/meta directory.

In the /backend/database/ directory:

npm run generate
npx wrangler d1 execute sandbox-database --remote --file=./drizzle/0000_🍏_🍐.sql
Enter fullscreen mode Exit fullscreen mode

Configuring the server

Update /server/.env:

DATABASE_WORKER_URL='https://database.🍎.workers.dev'
STORAGE_WORKER_URL='https://storage.🍎.workers.dev'
WORKERS_KEY='SUPERDUPERSECRET'
Enter fullscreen mode Exit fullscreen mode

Adding Liveblocks

Setup the Liveblocks account.

Update /frontend/.env:

NEXT_PUBLIC_LIVEBLOCKS_PUBLIC_KEY='πŸ”‘'
LIVEBLOCKS_SECRET_KEY='πŸ”‘'
Enter fullscreen mode Exit fullscreen mode

Configuring the frontend

Update /frontend/.env:

NEXT_PUBLIC_DATABASE_WORKER_URL='https://database.🍎.workers.dev'
NEXT_PUBLIC_STORAGE_WORKER_URL='https://storage.🍎.workers.dev'
NEXT_PUBLIC_WORKERS_KEY='SUPERDUPERSECRET'
Enter fullscreen mode Exit fullscreen mode

Running the IDE

Run npm run dev simultaneously in:

/frontend
/backend/server
Enter fullscreen mode Exit fullscreen mode

Additionally, here's a Dockerfile that can be used to run both components together.

Top comments (0)