Skip to content

Commit 493386d

Browse files
authored
Introduce React integration guide (#282)
1 parent d569758 commit 493386d

File tree

3 files changed

+862
-10
lines changed

3 files changed

+862
-10
lines changed

docs/getting-started/with-react.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,5 @@ function StandaloneTodoList() {
180180
181181
### For More Information
182182
183+
- [React Guide](/docs/js-sdk/react) - Comprehensive guide covering all providers, hooks, performance optimization, and advanced patterns
183184
- [React TodoMVC Example](https://github.com/yorkie-team/yorkie-js-sdk/tree/main/examples/react-todomvc)

docs/js-sdk.mdx

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -964,18 +964,30 @@ When using hierarchical channel keys, follow these rules:
964964
| Cannot end with a period |`room-1.`<br/>❌ `room-1.section-1.` |`room-1`<br/>✅ `room-1.section-1` |
965965
| Cannot contain consecutive periods |`room-1..section-1`<br/>❌ `room..section..subsection` |`room-1.section-1`<br/>✅ `room.section.subsection` |
966966

967-
**Distribute first-level channel keys:** Yorkie servers route channels based on the first-level key. Using the same first-level key (e.g., `app.*`) for all channels concentrates load on a single server, causing performance bottlenecks. Distribute traffic by using varied first-level keys:
967+
**Query channel information:** Use the [GetChannels API](/docs/web-api#post-yorkiev1adminservicegetchannels) to retrieve presence counts for specific channels and their sub-levels.
968+
969+
<Alert status="warning">
970+
**Important: Distribute First-Level Channel Keys**
971+
972+
Yorkie servers determine which server handles a channel based on a combination of your project and the first-level key of the channel. Using the same first-level key for all channels means all your channels will be managed by the same server, which can lead to performance bottlenecks and uneven load distribution.
973+
974+
Design your channel key structure to naturally distribute traffic across different first-level prefixes for optimal performance.
975+
968976
```javascript
969977
// ✅ Good: Distributed first-level keys
970978
new yorkie.Channel('game-1.room-a');
971979
new yorkie.Channel('chat-1.thread-1');
980+
new yorkie.Channel('chat-2.thread-1');
981+
new yorkie.Channel('notification-1.user-123');
972982

973983
// ❌ Bad: Same first-level key
984+
// All channels share 'app' as first level - avoid this!
974985
new yorkie.Channel('app.game-1');
986+
new yorkie.Channel('app.game-2');
975987
new yorkie.Channel('app.chat-1');
988+
new yorkie.Channel('app.notification-1');
976989
```
977-
978-
**Query channel information:** Use the [GetChannels API](/docs/web-api#post-yorkiev1adminservicegetchannels) to retrieve presence counts for specific channels and their sub-levels.
990+
</Alert>
979991

980992
#### Broadcast
981993

@@ -1018,22 +1030,26 @@ await channel.broadcast('notification', { type: 'user-joined', user: 'Alice' });
10181030
Broadcast messages are ephemeral and only delivered to clients currently connected to the channel. Messages are not persisted or stored, so clients that join later will not receive previous messages.
10191031
</Alert>
10201032

1021-
#### Tracking Online Presence
1033+
#### Tracking Online Sessions
10221034

1023-
Channels automatically track the number of connected clients, making it easy to display "users online" counters:
1035+
Channels automatically track the number of connected clients, making it easy to display "sessions" counters:
10241036

10251037
```javascript
10261038
// Subscribe to presence changes
10271039
const unsubscribe = channel.subscribe('presence', (event) => {
1028-
console.log(`Users online: ${event.count}`);
1040+
console.log(`Sessions: ${event.count}`);
10291041
});
10301042

1031-
// Get the current presence count
1032-
const count = channel.getPresenceCount();
1033-
console.log(`Currently ${count} users online`);
1043+
// Get the current session count
1044+
const count = channel.getSessionCount();
1045+
console.log(`Currently ${count} sessions`);
10341046
```
10351047

1036-
The presence count is automatically updated when clients connect or disconnect from the channel.
1048+
The session count is automatically updated when clients connect or disconnect from the channel.
1049+
1050+
<Alert status="warning">
1051+
Sessions are connection-based and managed for scalability. In abnormal cases (app crash, network partition), a client might not detach cleanly, so the server may keep the session counted until it is considered stale and cleaned up. This means the displayed count can be approximate and may lag briefly; avoid using it as a source of truth for critical business logic (billing, authorization, settlements).
1052+
</Alert>
10371053

10381054
#### Detaching a Channel
10391055

0 commit comments

Comments
 (0)