Generate and test App starter code
Root supplies a command-line utility named create-root that generates starter code. Here, you'll create your code, build it, and test that it works with your community.
1. Generate
-
Open a terminal in your preferred folder.
-
Run the following command:
npx create-root --app <your-project-name> -
Open the generated folder in an IDE.
-
(Optional) If you'd like to, take a few minutes to review the generated code. Here are a few things that might be of interest:
package.jsonfile- The necessary dependencies for the Root SDK are already included.
- The necessary dependencies for the generated networking code are already included.
- The
buildscript will compile your code in all three folders:client,networking, andserver. - The
cleanscript is provided as a convenience. It deletes the compiled code, thenode_modulesfolder, and a few generated config files. Feel free to edit theclean.jsfile to modify this script as needed.
clientfolder- There's an
Echocomponent in thesrc/Example.tsxfile. - The client uses a React UI and Vite as a local development server.
- The
clientscript inpackage.jsonruns your App's client; to execute it, you'll saynpm run client.
serverfolder- There's the implementation of the
Echoservice in thesrc/exampleService.tsfile. - The
serverscript inpackage.jsonruns your App's server; when you execute it, you'll saynpm run server.
networkingfolder- There's the declaration of the
Echoservice in thesrc/example.protofile. - The configuration information for the Root SDK tooling is in the
src/root-protoc.jsonfile. You shouldn't need to change it unless you want to customize the scope or folder structure.
2. Add DEV_TOKEN
-
Open the
.envfile in theserverfolder. -
Add your previously generated
DEV_TOKENto the file. The completed file should look something like this (but contain yourDEV_TOKENvalue rather than this sample):Complete content of a sample .env fileDEV_TOKEN=ACROZj4Ilajlkgjoilkjdljfb5l2jln426k42548fdlj4359204undlajf_lkaejrln3904280294FLWalPd35aoier82m_lkad4lkjas0kj-aeheccewqioclbaljer34akjwqpz_-lk-kO52jQjlaljbjlk5299elk2aklbwpcast67lka0as2lgxlmqJSO98absLa34s90gask2ac
3. Build
The build command in package.json will generate networking code from your .proto files, compile the generated networking code, compile your server, and compile your client.
-
Open a terminal in your App's project folder.
-
Install the necessary packages by running the following command:
npm i -
Build the App by running the following command:
npm run build
4. Run server
Root supports testing your App locally. Both your App's client and server are hosted on your local machine. Your App's server still needs a valid DEV_TOKEN because execution isn't fully local; your client-server communication goes through Root's servers. Your App is always running in the context of a community, even for local testing. The community ID is encoded as part of the DEV_TOKEN so you'll need to generate a new DEV_TOKEN for each community you'd like to test against.
-
Open a terminal in your App's
serverfolder. -
Execute the server side of your App by running the following command:
npm run server
4. Run client
Your App's client will run in a local web browser. The starter code created by Root's generator use Vite as the development server. Root automatically adds the DevBar to the top of your running App client. The DevBar is an HTML element that helps your testing. It lets you choose which member you want your client to run as. The selector is automatically populated with all the community members and one virtual user that's different every time you run your client.
-
Open a terminal in your App's
clientfolder. -
Run the following command:
npm run client -
If your browser doesn't launch automatically, you can open your browser manually and navigate to
http://localhost:5173/. You can even open multiple browser windows at that same address to test multiple members connected to your App simultaneously.
5. Test
Use the client running in the browser to interact with your App.