-
Notifications
You must be signed in to change notification settings - Fork 243
/
svelte.mdx
164 lines (122 loc) · 4.35 KB
/
svelte.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
---
meta:
title: "Get started with Liveblocks and Svelte"
parentTitle: "Get started"
description: "Learn how to get started with Liveblocks and Svelte"
---
Liveblocks is a realtime collaboration infrastructure for building performant
collaborative experiences. Follow the following steps to start adding
collaboration to your Svelte application using the APIs from the
[`@liveblocks/client`](/docs/api-reference/liveblocks-client) package.
<Banner title="Official package">
Liveblocks does not have a package for Svelte. If you would like to have one, or
even better if you have ideas about what kind of API you would like to use,
please let us know on
[GitHub](https://github.com/liveblocks/liveblocks/issues/1).
</Banner>
## Quickstart
<Steps>
<Step>
<StepTitle>Install Liveblocks</StepTitle>
<StepContent>
Every package should use the same version.
```bash
npm install @liveblocks/client
```
</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-client#Typing-your-data).
```bash
npx create-liveblocks-app@latest --init --framework javascript
```
</StepContent>
</Step>
<Step>
<StepTitle>Set up the Liveblocks client</StepTitle>
<StepContent>
The first step in connecting to Liveblocks is creating a client which
will be responsible for communicating with the back end.
```ts file="room.js"
const client = createClient({
publicApiKey: "{{PUBLIC_KEY}}",
});
```
</StepContent>
</Step>
<Step>
<StepTitle>Join a Liveblocks room</StepTitle>
<StepContent>
Liveblocks uses the concept of rooms, separate virtual spaces where people collaborate.
To create a realtime experience, multiple users must be connected to the same room.
```js
const { room, leave } = client.enterRoom("my-room", { initialPresence: {} });
```
</StepContent>
</Step>
<Step>
<StepTitle>Use the Liveblocks methods</StepTitle>
<StepContent>
Now that we’re connected to a room, we can start using Liveblocks subscriptions.
The first we’ll add is `others`, a subscription that provides information
about which other users are connected to the room.
```js highlight="7-9"
<script>
import { onDestroy } from "svelte";
import { room } from "./room.js";
let others = room.getOthers();
const unsubscribeOthers = room.subscribe("others", (updatedOthers) => {
others = updatedOthers;
});
onDestroy(() => {
unsubscribeOthers();
});
</script>
<div>There are {others.length} 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 Svelte application.
- [@liveblocks/client API Reference](/docs/api-reference/liveblocks-client)
---
## Examples using Svelte
<ListGrid columns={2}>
<ExampleCard
example={{
title: "Live Cursors",
slug: "live-cursors/sveltekit-live-cursors",
image: "/images/examples/thumbnails/live-cursors.jpg",
}}
technologies={["nextjs", "vuejs", "sveltekit", "solidjs", "javascript"]}
openInNewWindow
/>
<ExampleCard
example={{
title: "Live Avatar Stack",
slug: "live-avatar-stack/sveltekit-live-avatars",
image: "/images/examples/thumbnails/live-avatar-stack.jpg",
}}
technologies={["nextjs", "nuxtjs", "vuejs", "sveltekit", "solidjs"]}
openInNewWindow
/>
</ListGrid>