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 init
Select
Empty Project
Set a project name.
Once it's done add a database by running the following command:
shrailway add
Select
postgressql
as the database.Once that's done run the following command to upload the project to railway:
shrailway up
got to the
railway
project's site and click on the projectSwitch to the
settings
tabUnder
Environment
click onGenerate Domain
Copy the
generated url
, you'll need it for NEXTAUTH_URL on step 14Switch to the
variables
tabClick on
+ New Variable
, and in theVARIABLE_NAME
clickAdd Reference
and selectDATABASE_URL
Add another variable called
SESSION_SECRET
and set it to a random string, you can use an online UUID generatorAdd another variable called
NEXTAUTH_URL
and set it to thegenerated url
which 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 app
Click on the
API Keys
button on top.Copy the first api key (with the many capabilities), create an entry in the
.env.local
file, name itABLY_API_KEY
and paste the api key there.Configure
ably
as thesubscriptionServer
ts// src/api.ts //... import ably from "ably/promises" import { AblySubscriptionServer } from "remult/ably" const api = remultNextApp({ 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 atoken
for a user that wants to subscribe to a channel - in thesrc/app/api
folder, createa folder calledgetAblyToken
and in it create a file calledroute.ts
ts// src/app/api/getAblyToken/route.ts import ably from 'ably/promises' 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-end
to use ably as it'ssubscriptionClient
by adding a newuseEffect
hook and configureably
to use theapi/getAblyToken
route we've created as it'sauthUrl
- we'll that in theAuth
componenttsx// src/components/auth.tsx import ably from "ably/promises" 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
remultNextApp
to store live-queries in thedataProvider
ts// src/api.ts //... import { DataProviderLiveQueryStorage } from "remult/server" const dataProvider = createPostgresDataProvider() const api = remultNextApp({ 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_URL
environment 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
github
repo and clickimport
- Configure the project's name and in the
> Environment Variables
section,NEXTAUTH_SECRET
andABLY_API_KEY
environment 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
Storage
tab - 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_URL
and 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
Deployments
tab - 3 dots at the end of the deployment line and select
Redeploy
and 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.⭐