Getting started with Next.js
Live updates, chat, who is online, calls, group calls and live streams in a Next.js app (App Router), through WordPlus Cloud. @wordplus/realtime is both halves:
- Your server (
@wordplus/realtime/server) declares your rooms once, says who may join them and call whom, and sends events to rooms and to users: from route handlers, server components and server actions. - The page (
@wordplus/realtime/react) connects to WordPlus Cloud through a provider, joins rooms with hooks, and sends and gets events:emit,emitWithAck,volatile.
What goes through WordPlus Cloud is sealed with keys only your server and the browsers of your users hold. Ready-made parts come with it, all optional: the Better Messages chat, its list of who is online and its call screen, and the Easy Stream player. Without them, your own components draw everything on the same API (your own interface). The example app is a whole shop built this way.
A Next.js app cannot hold a websocket open on a serverless host. Why WordPlus Cloud says what the cloud runs for you, and what it adds.
Try it live at nextjs.docs.wordplus.cloud: the example app of this guide as a demo, a shop of your own for a day, with Ben and Cara opening in windows of their own.

1. Your app in WordPlus Cloud
Apps come with a subscription. Choose your connections, starting from 1,000, and your WordPlus Cloud account gets its Apps page.
In your WordPlus Cloud account, go to Apps → Add app. Name it, and list the addresses its pages are served from: https://shop.example.com, and http://localhost:3000 while you develop. Only pages on those addresses may connect.
You get the app key, public because every page carries it, and its secret, for your server only.
2. Install and set up
npm install @wordplus/realtime @wordplus/realtime-ui # the second for the ready-made screens, all optional
npx wordplus-realtime init
init asks for the key and the secret, and writes four things. It changes nothing already there.
.env.local, with the key, the secret and a local key it makes for you: everything sealed is sealed with keys derived from it, and it never leaves your servers. Keep it as long as the app, the same on every server of the app.lib/realtime.ts, your server's part, with rooms to change into yours (step 3).app/api/realtime/[...path]/route.ts, the route your pages ask (step 3).app/api/call-token/route.ts, a route of your own that answers a call token after your own check, for your first call (calls).
# .env.local
WORDPLUS_SITE=s1500000007
WORDPLUS_SECRET=…
WORDPLUS_LOCAL_KEY=…
None of them takes NEXT_PUBLIC_: the secret and the local key never reach a page, and the key reaches it through your layout (step 4).
That is all there is to install: the browser files of the SDK come from your realtime route, which serves them on GET. The worker lets the tabs of a user share one connection, the room client runs group calls, and realtime-ui.min.js and realtime-ui-streams.min.js are the ready-made parts, the second for live streams. Nothing to copy into public/ and nothing to keep out of git: every deployment serves the ones its package came with, from a folder named after their version, kept by browsers and CDNs for a year.
3. Your server
Each room is named after what it carries: order:1042 for the updates of order 1042. A page that joins it asks your server first, and a rule answers: a function your server declares once for every room whose name fits the pattern order:{id}. WordPlus Cloud then holds the page to the answer (permissions). This rule says:
- Visitors who are not signed in stay out.
- The customer of the order joins, and nobody else.
ownerOfis yours, from your data.
And the room keeps its last 20 updates, for a page that reconnects.
// lib/realtime.ts
import { createRealtime } from '@wordplus/realtime/server';
import { currentUserId, profileOf } from '@/lib/auth'; // your sign-in: Auth.js, Clerk or your own sessions
export const realtime = createRealtime( {
// Who is asking: their id, or null for nobody.
user: () => currentUserId(),
// What others see of them: in presence, calls and the directory.
profile: ( user ) => profileOf( user ),
} );
// The room of an order, order:1042, the last 20 updates kept.
realtime.room( 'order:{id}', async ( user, { id } ) => {
// 1. Visitors stay out.
if ( user === null ) return false;
// 2. The customer of the order, and nobody else.
return ( await ownerOf( id ) ) === user;
}, { history: 20 } );
// app/api/realtime/[...path]/route.ts: POST /api/realtime/auth, /calls, /keys and /webhook, and GET
// /api/realtime/files/…, the browser files
import { realtime } from '@/lib/realtime';
export const { GET, POST } = realtime.handlers;
- The user is whatever id your app knows them by, a number or a string (the ids of Clerk and Supabase work as they are), or
null.userruns inside the route handler and your layout's provider, socookies()andauth()work there. It is also given theRequest. Read it from a session or a token your server checks, never from a name or an id the page sends: whoever sends one reads that person's rooms and calls as them. - The pattern of a room names its parts:
order:{id}matchesorder:1042, and the rule gets{ id: '1042' }. Your editor knows them, and a part the pattern does not have is an error. - A name no rule matches is refused, and your server's log says so once, with the rule to declare.
- The route folder is a catch-all,
[...path]: the browser files are in folders under/api/realtime/files/, which a one-segment[action]folder never reaches. A file that does not load says so in the console. - The routes take JSON POSTs, and GETs for the browser files alone. If your middleware guards
/api, let/api/realtime/*through with the session. - Joining and calling as routes of your own:
app/api/realtime/auth/route.tsandapp/api/realtime/calls/route.tsanswer before the catch-all does, withrealtime.authorize( rooms, user )andrealtime.authorizeCall( ask, user ), so your code shows where the rules run. The dating site does that.
4. The page
Connect once, in the layout of your signed-in pages:
// app/(app)/layout.tsx
import '@wordplus/realtime-ui/styles.css';
import { RealtimeProvider } from '@wordplus/realtime/next';
import { CallScreen } from '@wordplus/realtime-ui/react';
import { realtime } from '@/lib/realtime';
export default function AppLayout( { children }: { children: React.ReactNode } ) {
return (
<RealtimeProvider realtime={ realtime }>
<CallScreen />
{ children }
</RealtimeProvider>
);
}
The provider asks your user who this is, signs the page's settings for them on your server, and connects the page with no request first. Your page shows at once and never waits for the connection, which comes up beside it. Signing in or out draws the layout again, and the connection moves on to the new user with no reload.
The ready-made screens are the call screen, the chat and the list of who is online from Better Messages, drawn in your own tree by @wordplus/realtime-ui/react: your CSS reaches them, and each finds the page connection in the provider. The call screen takes no place in your layout, its window and rings over the page, on every page under the layout. All of them are optional: your own components draw everything on the same API (your own interface).
- A user your
useroption can't find from a request, such as one your layout already knows:<RealtimeProvider realtime={ realtime } user={ userId }>. - It builds with Cache Components on or off, as a new app from
create-next-apphas them on. The settings belong to one request, so they wait for it in a<Suspense>of their own, and the rest of your layout stays prerendered.fallbackshows what you like for those few milliseconds. - When WordPlus Cloud refuses the page, such as from an address your app does not list (
wrong_origin), the console says why and what to do, once. In a client component of yours,useConnection()gives{ connected, error }, for a "Reconnecting…" of your own.
// app/(app)/orders/[id]/status.tsx
'use client';
import { useState } from 'react';
import { useRoom } from '@wordplus/realtime/react';
export function Status( { id, initial }: { id: string; initial: string } ) {
const [ status, setStatus ] = useState( initial );
const room = useRoom( `order:${ id }`, { updated: ( data: { status: string } ) => setStatus( data.status ) } );
return <p>{ status } { room.state === 'failed' && room.error }</p>;
}
Every page under the layout shares its connection, which stays up across client navigations.
See that it all works, with your app running. This checks your three values, asks WordPlus Cloud whether it knows your app, and asks your routes what a page asks them, and it says what to fix:
npx wordplus-realtime check http://localhost:3000
5. Send
From a server action, a route handler, or anything else on the server, after the change is saved:
// app/(app)/orders/[id]/actions.ts
'use server';
import { realtime } from '@/lib/realtime';
export async function markShipped( id: string ) {
await saveStatus( id, 'shipped' );
await realtime.to( `order:${ id }` ).emit( 'updated', { status: 'shipped' } );
}
Everyone in the room gets { status }, sealed with the room key on the way, and a page that reconnects later gets it from the room history.
Next
- Rooms, presence and who is online, sending from your server and events from the page, with the ready-made conversation window (
<Chat>) - Calls and group calls, with the call screen, and live streams
- Encryption and your users
- Customizing the screens: their look, a piece of your own, or their whole code
- Deploying (Vercel, or any Node host), and the example app
- The React reference, the server reference, errors and limits
What to build next
A whole site, run from the package and gone through a part at a time, with its realtime code:
- Build a dating site: who is online, chat, and video dates in the chat
- Build a live lessons site: teachers live in their lessons, students on stage, and a notice when a lesson goes live
- Build a team intranet: a meeting room and channels for each team, who is online, calls between colleagues, and a notice when a meeting starts
- Build a private consultations site: times booked with experts, a waiting room, and a visit by video encrypted end to end, with messages and files beside it
- Build a community site: channels with photos, lounges by voice or camera, live events with members on stage, and what is on now
- Build a marketplace: a buyer talks to the seller about an item, sees it on a call and makes an offer, and every page shows who is looking and what changed
One feature at a time, each use case a page of its own, with the rules of your server and the code of the page: chat, who is online, live updates and notifications, calls from a button, team meetings, live streaming and the rest in the use cases.