Skip to main content

Getting started with Next.js

Markdown

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.

Two browsers side by side: Ann marks order 1042 as shipped on the staff page of the shop, and the order page of Ben shows Shipped at once, without a reload

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. Make a local key of your own too: everything sealed is sealed with keys derived from it, and it never leaves your servers.

# .env.local
WORDPLUS_SITE=s1500000007
WORDPLUS_SECRET=…
WORDPLUS_LOCAL_KEY=… # openssl rand -hex 32; the same on every server of the app, kept as long as the app

None of them takes NEXT_PUBLIC_: the secret and the local key never reach a page, and the key reaches it through config().

2. Install​

npm install @wordplus/realtime @wordplus/realtime-ui      # the second for the ready-made screens, all optional

That is all: the browser files of the SDK come from your realtime route (step 3), 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:

  1. Visitors who are not signed in stay out.
  2. The customer of the order joins, and nobody else. ownerOf is 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( {
site: process.env.WORDPLUS_SITE!,
secret: process.env.WORDPLUS_SECRET!,
localKey: process.env.WORDPLUS_LOCAL_KEY!,
// 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}', { history: 20 }, 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;
} );
// 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 = realtime.route;
export const POST = realtime.route;
  • 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. user runs inside the route handler, so cookies() and auth() work there. It is also given the Request.
  • The pattern of a room names its parts: order:{id} matches order:1042, and the rule gets { id: '1042' }.
  • 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.ts and app/api/realtime/calls/route.ts answer before the catch-all does, with realtime.authorize( rooms, user ) and realtime.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, with the user settings rendered in, so it connects with no request first:

// app/(app)/layout.tsx
import { Suspense } from 'react';
import { connection } from 'next/server';
import { RealtimeProvider } from '@wordplus/realtime/react';
import { currentUserId } from '@/lib/auth';
import { realtime } from '@/lib/realtime';
import { Screens } from './screens';

export default function AppLayout( { children }: { children: React.ReactNode } ) {
return (
<Suspense>
<Connected>{ children }</Connected>
</Suspense>
);
}

// The settings are signed for this user, at this moment: made for each request, never prerendered.
async function Connected( { children }: { children: React.ReactNode } ) {
await connection();
return (
<RealtimeProvider options={ await realtime.config( await currentUserId() ) }>
<Screens />
{ children }
</RealtimeProvider>
);
}
// app/(app)/screens.tsx: the look of the ready-made screens, and the call screen, ringing on every page under the layout
'use client';

import '@wordplus/realtime-ui/calls.css';
import '@wordplus/realtime-ui/presence.css';
import '@wordplus/realtime-ui/chat.css';
import { useRealtime } from '@wordplus/realtime/react';
import { CallScreen } from '@wordplus/realtime-ui/react';

export function Screens() {
return <CallScreen rt={ useRealtime() } />;
}

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 takes the page connection, rt={ useRealtime() }. The call screen takes no place in your layout, its window and rings over the page. All of them are optional: your own components draw everything on the same API (your own interface).

  • Why <Suspense> and connection(). config() reads the session and signs with the time, so it belongs to one request. A new app from create-next-app has Cache Components on, and then Next.js refuses to build a layout that reads the session outside <Suspense>, and warns about the time while it prerenders. This layout builds with Cache Components on or off.
  • 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. For your own handling, onError on the provider hears it: as a function, it goes on the provider from a client component of yours ('use client'), not from this server layout.
// 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>;
}

The React entry is a client module, so a server component can render the provider. Every page under the layout shares its connection, which stays up across client navigations.

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​

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.