-
Notifications
You must be signed in to change notification settings - Fork 243
/
nextjs.mdx
207 lines (168 loc) · 6.23 KB
/
nextjs.mdx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
---
meta:
title: "Get started with Liveblocks and Next.js"
parentTitle: "Get started"
description: "Learn how to get started with Liveblocks and Next.js"
---
Liveblocks is a realtime collaboration infrastructure for building performant
collaborative experiences. Follow the following steps to start adding
collaboration to your Next.js `/app` directory application using the hooks from
the [`@liveblocks/react`](/docs/api-reference/liveblocks-react) package.
## Quickstart
<Steps>
<Step>
<StepTitle>Install Liveblocks</StepTitle>
<StepContent>
Every package should use the same version.
```bash
npm install @liveblocks/client @liveblocks/react
```
</StepContent>
</Step>
<Step>
<StepTitle>Initialize the `liveblocks.config.ts` file</StepTitle>
<StepContent>
We can use this file later to [define types for our application](/docs/api-reference/liveblocks-react#Typing-your-data).
```bash
npx create-liveblocks-app@latest --init --framework react
```
</StepContent>
</Step>
<Step>
<StepTitle>Create a Liveblocks room</StepTitle>
<StepContent>
Liveblocks uses the concept of rooms, separate virtual spaces where people
collaborate, and to create a realtime experience, multiple users must
be connected to the same room. When using Next.js’ `/app` router,
we recommend creating your room in a `Room.tsx` file in the same directory
as your current route.
Set up a Liveblocks client with
[`LiveblocksProvider`](/docs/api-reference/liveblocks-react#LiveblocksProvider),
join a room with [`RoomProvider`](/docs/api-reference/liveblocks-react#RoomProvider),
and use [`ClientSideSuspense`](/docs/api-reference/liveblocks-react#ClientSideSuspense)
to add a loading spinner to your app.
```tsx file="app/Room.tsx" highlight="12-18"
"use client";
import { ReactNode } from "react";
import {
LiveblocksProvider,
RoomProvider,
ClientSideSuspense,
} from "@liveblocks/react/suspense";
export function Room({ children }: { children: ReactNode }) {
return (
<LiveblocksProvider publicApiKey={"{{PUBLIC_KEY}}"}>
<RoomProvider id="my-room" initialPresence={{}}>
<ClientSideSuspense fallback={<div>Loading…</div>}>
{children}
</ClientSideSuspense>
</RoomProvider>
</LiveblocksProvider>
);
}
```
</StepContent>
</Step>
<Step>
<StepTitle>Add the Liveblocks room to your page</StepTitle>
<StepContent>
After creating your room file, it’s time to join it. Import
your room into your `page.tsx` file, and place
your collaborative app components inside it.
```tsx file="app/page.tsx" highlight="6-8"
import { Room } from "./Room";
import { CollaborativeApp } from "./CollaborativeApp";
export default function Page() {
return (
<Room>
<CollaborativeApp />
</Room>
);
}
```
</StepContent>
</Step>
<Step>
<StepTitle>Use the Liveblocks hooks</StepTitle>
<StepContent>
Now that we’re connected to a room, we can start using the Liveblocks hooks.
The first we’ll add is [`useOthers`](/docs/api-reference/liveblocks-react#useOthers), a hook that provides information
about which other users are connected to the room.
```tsx file="app/CollaborativeApp.tsx" highlight="6"
"use client";
import { useOthers } from "@liveblocks/react/suspense";
export function CollaborativeApp() {
const others = useOthers();
const userCount = others.length;
return <div>There are {userCount} other user(s) online</div>;
}
```
</StepContent>
</Step>
<Step lastStep>
<StepTitle>Next: set up authentication</StepTitle>
<StepContent>
By default, Liveblocks is configured to work without an authentication endpoint
where everyone automatically has access to rooms. This approach is great for
prototyping and marketing pages where setting up your own security isn’t always
required. If you want to limit access to a room for certain users, you’ll need
to set up an authentication endpoint to enable permissions.
<Button asChild className="not-markdown">
<a href="/docs/authentication">
Set up authentication
</a>
</Button>
</StepContent>
</Step>
</Steps>
## What to read next
Congratulations! You now have set up the foundation to start building
collaborative experiences for your React application.
- [@liveblocks/react API Reference](/docs/api-reference/liveblocks-react)
- [Next.js and React guides](/docs/guides?technologies=nextjs%2Creact)
- [How to use Liveblocks Presence with React](/docs/guides/how-to-use-liveblocks-presence-with-react)
- [How to use Liveblocks Storage with React](/docs/guides/how-to-use-liveblocks-storage-with-react)
---
## Examples using Next.js
<ListGrid columns={2}>
<ExampleCard
example={{
title: "Collaborative To-do List",
slug: "collaborative-todo-list/nextjs-todo-list",
image: "/images/examples/thumbnails/collaborative-todo-list.jpg",
}}
technologies={["nextjs", "vuejs", "sveltekit", "solidjs", "javascript"]}
openInNewWindow
/>
<ExampleCard
example={{
title: "Live Avatar Stack",
slug: "live-avatar-stack/nextjs-live-avatars",
image: "/images/examples/thumbnails/live-avatar-stack.jpg",
}}
technologies={["nextjs", "nuxtjs", "vuejs", "sveltekit", "solidjs"]}
openInNewWindow
/>
<ExampleCard
example={{
title: "Collaborative Spreadsheet",
slug: "collaborative-spreadsheet-advanced/nextjs-spreadsheet-advanced",
image:
"/images/examples/thumbnails/collaborative-spreadsheet-advanced.jpg",
advanced: true,
}}
technologies={["nextjs"]}
openInNewWindow
/>
<ExampleCard
example={{
title: "Collaborative Whiteboard",
slug: "collaborative-whiteboard-advanced/nextjs-whiteboard-advanced",
image:
"/images/examples/thumbnails/collaborative-whiteboard-advanced.jpg",
advanced: true,
}}
technologies={["nextjs"]}
openInNewWindow
/>
</ListGrid>