Deployment
You can deploy the application to a standard node.js server, or a server-less server.
We'll review both options.
Deploy to a node.js server
Let's deploy the todo app to railway.app.
In order to deploy the todo app to railway you'll need a railway account. You'll also need Railway CLI installed, and you'll need to login to railway from the cli, using railway login.
Click enter multiple times to answer all its questions with the default answer
Create a Railway
project.From the terminal in your project folder run:
shrailway initSelect
Empty ProjectSet a project name.
Once it's done add a database by running the following command:
shrailway addSelect
postgressqlas the database.Once that's done run the following command to upload the project to railway:
shrailway upgot to the
railwayproject's site and click on the projectSwitch to the
settingstabUnder
Environmentclick onGenerate DomainCopy the
generated url, you'll need it for NEXTAUTH_URL on step 14Switch to the
variablestabClick on
+ New Variable, and in theVARIABLE_NAMEclickAdd Referenceand selectDATABASE_URLAdd another variable called
SESSION_SECRETand set it to a random string, you can use an online UUID generatorAdd another variable called
NEXTAUTH_URLand set it to thegenerated urlwhich was created on step 10.Wait for railway to finish deploying your changes and Click on the newly generated url to open the app in the browser and you'll see the app live in production. (it may take a few minutes to go live)
Note
If you run into trouble deploying the app to Railway, try using Railway's documentation.
That's it - our application is deployed to production, on a node js server
Next we'll explore deployment to a server-less environment.
Deploying to a serverless environment
LiveQuery Serverless Support
Any serverless platform can't be used to maintain an active subscription channel for our live query, we'll need to use a 3rd party provider for that.
If you're not using liveQuery you can skip to the next step.
In this demo, we'll use ably.com Follow these steps only if you want to use liveQuery in the app
- sh
npm i ably Goto ably.com create a user and click on the "Create new app" button
Select a name and click
create appClick on the
API Keysbutton on top.Copy the first api key (with the many capabilities), create an entry in the
.env.localfile, name itABLY_API_KEYand paste the api key there.Configure
ablyas thesubscriptionServerts// src/api.ts //... import ably from "ably" import { AblySubscriptionServer } from "remult/ably" const api = remultApi({ subscriptionServer: new AblySubscriptionServer( new ably.Rest(process.env["ABLY_API_KEY"]!) ) //... })Next, we'll need to create a route that
ably's client on the front-end will use to get atokenfor a user that wants to subscribe to a channel - in thesrc/app/apifolder, createa folder calledgetAblyTokenand in it create a file calledroute.tsts// src/app/api/getAblyToken/route.ts import ably from 'ably' import { NextResponse } from 'next/server' export async function POST() { const token = await new ably.Rest( process.env['ABLY_API_KEY']!, ).auth.createTokenRequest({ capability: { '*': ['subscribe'] } }) return NextResponse.json(token) }
Configure the
front-endto use ably as it'ssubscriptionClientby adding a newuseEffecthook and configureablyto use theapi/getAblyTokenroute we've created as it'sauthUrl- we'll that in theAuthcomponenttsx// src/components/auth.tsx import ably from "ably" import { AblySubscriptionClient } from "remult/ably" export default function Auth() { const session = useSession() remult.user = session.data?.user as UserInfo useEffect(() => { if (session.status === "unauthenticated") signIn() else if (session.status === "authenticated") remult.apiClient.subscriptionClient = new AblySubscriptionClient( new ably.Realtime({ authUrl: "/api/getAblyToken", authMethod: "POST" }) ) }, [session])Configure
remultApito store live-queries in thedataProviderts// src/api.ts //... import { DataProviderLiveQueryStorage } from "remult/server" const dataProvider = createPostgresDataProvider() const api = remultApi({ dataProvider, liveQueryStorage: new DataProviderLiveQueryStorage(dataProvider) //... })
Let's deploy the todo app to vercel.
Postgres
We'll use vercel's postgres as out database, and that requires the following changes to the createPostgresDataProvider options.
// src/api.ts
const dataProvider = createPostgresDataProvider({
connectionString: process.env["POSTGRES_URL"] || process.env["DATABASE_URL"],
configuration: {
ssl: Boolean(process.env["POSTGRES_URL"]),
},
})- Vercel sends the connection string using the
POSTGRES_URLenvironment variable, other providers use theDATABASE_URL- this code supports them both. - Ssl is required with vercel - by my local pg doesn't, so we condition ssl based on the environment variable.
Create a github repo
Vercel deploys automatically whenever you push to github, so the first step of deployment is to create a github repo and push all your changes to it.
Create a vercel project
- Create a vercel account if you don't already have one.
- Goto https://vercel.com/new
- Select your
githubrepo and clickimport - Configure the project's name and in the
> Environment Variablessection,NEXTAUTH_SECRETandABLY_API_KEYenvironment variables - Click
Deploy - Now we need to define the postgres database.
- Wait for vercel to complete it's deployment
- Click on
Continue to Dashboard - Select the
Storagetab - Create new Database and select Postgres
- Accept the terms
- Select region and click Create & continue
- Click Connect
- Click on Settings, Environment Variables and see that the
POSTGRES_URLand other environment variables were added. - At the time of this article, vercel did not yet automatically redeploy once you configure a database, so in order to redeploy, click on the
Deploymentstab - 3 dots at the end of the deployment line and select
Redeployand clickRedeploy - Once completed click on 'Visit'.
That's it - our application is deployed to production on vercel, play with it and enjoy.
To see a larger more complex code base, visit our CRM example project
Love Remult? Give our repo a star.⭐