Deployment
Let's deploy the todo app to vercel.
LiveQuery Serverless Support
Vercel is a serverless
platform, as such we can't use it to maintain an active subscription channel for our live query, and we'll need to use a 3rd party provider for that.
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/[...remult].ts //... import ably from "ably/promises" import { AblySubscriptionServer } from "remult/ably" export default remultNext({ 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/pages/api
folder, create a file calledgetAblyToken.ts
ts// src/pages/api/getAblyToken.ts import { NextApiRequest, NextApiResponse } from "next" import ably from "ably/promises" export default async function handler( req: NextApiRequest, res: NextApiResponse ) { const token = await new ably.Rest( process.env["ABLY_API_KEY"]! ).auth.createTokenRequest({ capability: { "*": ["subscribe"] } }) res.status(200).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
tsx// src/pages/index.tsx import ably from "ably/promises" import { AblySubscriptionClient } from "remult/ably" //... useEffect(() => { remult.apiClient.subscriptionClient = new AblySubscriptionClient( new ably.Realtime({ authUrl: "/api/getAblyToken" }) ) }, [])
Configure
remultNext
to store live-queries in thedataProvider
ts// src/api/[...remult].ts //... import { DataProviderLiveQueryStorage } from "remult/server" const dataProvider = createPostgresConnection() export default remultNext({ dataProvider, liveQueryStorage: new DataProviderLiveQueryStorage(dataProvider) //... })
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, set theDATABASE_URL
,NEXTAUTH_SECRET
andABLY_API_KEY
environment variables - Click
Deploy
That's it - our application is deployed to production, 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.⭐