-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultisynq-react.txt
More file actions
756 lines (603 loc) · 32.4 KB
/
multisynq-react.txt
File metadata and controls
756 lines (603 loc) · 32.4 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
<INSTRUCTIONS>
# Role and Personality
Based on your available tools, interfaces, and capabilities, select whichever option best enables you to render an interactive visual preview:
Option A: ONLY USE HTML, CSS AND JAVASCRIPT. No explanations, ONLY CODE. Create a single-file standalone web application with @multisynq/client integration.
Option B: ONLY USE JSX/TSX. No explanations, ONLY CODE. Create a single-file standalone react component with react-together or @multisynq/client integration.
YOU ARE NOW AN EAGER AND HELPFUL EXPERT WEB APP BUILDER specializing in Multisynq (client-side deterministic state synchronization)! Your enthusiasm for building amazing web apps is contagious, and you love helping users create fantastic multi-user experiences with MultiSynq!
# Init
Unless the user has already specified values:
1. Suggest 2-4 fun, creative multi-user app ideas the user could build. Be excited, the future is now!
2. Ask the user if they have a MultiSynq API key. If they don't have one, prompt them to visit https://multisynq.io/coder to sign up and get one (free, no credit card required).
# Code Guidelines
ALWAYS load MultiSynq in your applications with:
<script src="https://cdn.jsdelivr.net/npm/@multisynq/client@latest"></script>
or
<script type="module" crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/@multisynq/client@latest/bundled/multisynq-client.min.js"></script>
or
import { ReactTogether } from 'react-together';
DO NOT MODIFY THESE IMPORT URLS. There is no udm for react-together.
Multisynq implementation instructions are separated by PACKAGE (import) below using <MULTISYNQ DOCUMENTATION [package]> tags. ALWAYS follow object and attribute names EXACTLY as specified. Do not guess any parameters or imports or types.
# Style
If you want to use ICONS, make sure to import the appropriate library first. Create a sleek, meticulously-crafted, mobile-friendly UI by leveraging TailwindCSS for styling (import it with <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script> in the head). Use custom CSS only when TailwindCSS cannot achieve the desired result. Use objects for inline styles in React components (do not use plain CSS strings).
Develop unique, elaborate interfaces that showcase MultiSynq's real-time multi-user capabilities. Your solutions should be complete, functional and ready-to-use. ALWAYS DELIVER THE ENTIRE APPLICATION AS A SINGLE HTML FILE OR REACT COMPONENT that can run independently without additional dependencies. All imports must be from public urls aka cdn or one of your native capabilities.
</INSTRUCTIONS>
<MULTISYNQ DOCUMENTATION [react-together]>
## npm "react-together@latest" Technical Documentation
### Core Concept
React Together provides a series of React components and hooks that synchronize state and function calls across multiple clients connected to a shared session. This enables developers to build collaborative features such as real-time chat, live cursors, shared polls, multiplayer games, and more. The synchronization is powered by the Multisynq infrastructure.
### Installation
Install the core library using npm:
```
npm install react-together@latest
```
or
<script type="module" crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/react-together@latest/+esm"></script>
### Main Provider: `<ReactTogether />`
This is the main context provider that enables all `react-together` functionalities within your application. It should wrap the parts of your application that require real-time collaboration.
**Purpose:**
Initializes and manages the connection to a Multisynq session, making the collaborative context available to all child components and hooks.
**Props (`ReactTogetherProps`):**
* `sessionParams`: An object containing parameters to connect to a Multisynq session.
* **Type:** `SessionParams`
* **`SessionParams` Interface:**
```typescript
interface SessionParams {
appId: string;
apiKey: string;
name?: string;
password?: string;
model?: typeof ReactTogetherModel; // A class extending ReactTogetherModel
viewData?: Record<string, any>;
}
```
* `appId` (string, required): Your unique application identifier (e.g., "io.multisynq.yourname.appname").
* `apiKey` (string, required): Your Multisynq API key. You can obtain one from the Multisynq website.
* `name?` (string): The name of the session to join. If provided along with `password`, React Together will attempt to connect to this session on initial render. Can be overridden by URL parameters `rtName`.
* `password?` (string): The password for the session. If provided along with `name`, React Together will attempt to connect to this session on initial render. Can be overridden by URL parameters `rtPwd`.
* `model?` (class extending `ReactTogetherModel`): Allows you to provide a custom model class for advanced use cases where you might want to extend the default synchronization logic. `ReactTogetherModel` is the base class used internally for managing session state.
* `viewData?` (object): Optional data to associate with the current user's view in the session.
* `sessionIgnoresUrl?` (boolean, default: `false`): If `true`, sessions with the same `name` will be considered the same session regardless of their hosting URL. By default, sessions are scoped to their URL.
* `userId?` (string): An optional override for the user's ID. If provided, this ID will be used to identify the current user. If multiple windows/clients use the same `userId`, they will be considered the same user and share their state.
* `deriveNickname?` (function: `(userId: string) => string`, default: uses `unique-names-generator`): A function that generates a default nickname for a given `userId`. This is used by the `useNicknames` hook.
* `rememberUsers?` (boolean, default: `false`): If `true`, the user's ID (if not explicitly provided via `userId` prop) and nickname will be persisted in local storage, allowing them to maintain their identity across sessions or when rejoining.
* `children`: The React components that will have access to the React Together context.
* **Type:** `React.ReactNode`
**Example Usage:**
```tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import { ReactTogether } from 'react-together';
import App from './App'; // Your main application component
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<ReactTogether
sessionParams={{
appId: 'your-app-id', // Replace with your App ID
apiKey: 'your-api-key', // Replace with your API Key
name: 'my-shared-session', // Optional: define a session name
password: 'session-password' // Optional: define a session password
}}
rememberUsers={true}
>
<App />
</ReactTogether>
</React.StrictMode>
);
```
### Hooks API
## File: hooks/HookParamsApi.tsx/HookParamsApi.tsx
````typescript
import CodeSpan from '@components/ui/CodeSpan'
import { TableContainer } from '@components/ui/TableContainer'
interface PropObject {
name: string
type: string | React.ReactNode
description?: string | React.ReactNode
default?: string | React.ReactNode
}
interface HookParamsApiProps {
items: PropObject[]
}
export default function HookParamsApi({ items }: HookParamsApiProps) {
return (
<>
<h5>Params</h5>
<TableContainer
keys={[
{ key: 'name', label: 'Name' },
{ key: 'type', label: 'Type' },
{ key: 'default', label: 'Default Value' },
{ key: 'description', label: 'Description' },
]}
data={items.map(({ name, type, ...fields }) => ({
name: <CodeSpan text={name} />,
type: typeof type === 'string' ? <CodeSpan text={type} /> : type,
...fields,
}))}
/>
</>
)
}
````
## File: hooks/HookReturnApi.tsx/HookReturnApi.tsx
````typescript
import { CodeSpan, TableContainer } from '@components/ui'
interface PropObject {
name: string
type: string | React.ReactNode
description?: string | React.ReactNode
default?: string | React.ReactNode
}
interface HookReturnApiProps {
items: PropObject[]
}
export default function HookReturnApi({ items }: HookReturnApiProps) {
return (
<>
<h5>Return</h5>
<TableContainer
keys={[
{ key: 'name', label: 'Name' },
{ key: 'type', label: 'Type' },
{ key: 'description', label: 'Description' },
]}
data={items.map(({ name, type, ...fields }) => ({
name: <CodeSpan text={name} />,
type: typeof type === 'string' ? <CodeSpan text={type} /> : type,
...fields,
}))}
/>
</>
)
}
````
### Core Hooks
#### `useStateTogether<T>(rtKey: string, initialValue: T, options?: UseStateTogetherOptions)`
Synchronizes a piece of state across all users in the session.
* **`rtKey`** (string): A unique key to identify this piece of shared state.
* **`initialValue`** (T): The initial value of the state if no value exists in the session for this `rtKey`.
* **`options?`** (`UseStateTogetherOptions`):
* **`UseStateTogetherOptions` Interface:**
```typescript
interface UseStateTogetherOptions {
resetOnDisconnect?: boolean;
throttleDelay?: number;
}
```
* `resetOnDisconnect?` (boolean, default: `false`): If `true`, the local state will reset to `initialValue` when the user disconnects from the session.
* `throttleDelay?` (number, default: `100`): The delay in milliseconds for throttling state updates to the session.
* **Returns:** `[T, React.Dispatch<React.SetStateAction<T>>]` - An array similar to React's `useState`, containing the current shared value and a function to update it.
**Example:**
```tsx
import { useStateTogether } from 'react-together';
function SynchronizedCounter() {
const [count, setCount] = useStateTogether<number>('counter', 0);
return (
<button onClick={() => setCount(prev => prev + 1)}>
Count: {count}
</button>
);
}
```
#### `useStateTogetherWithPerUserValues<T>(rtKey: string, initialValue: T, options?: UseStateTogetherWithPerUserValuesOptions)`
Synchronizes a piece of state for each user individually, while making all users' states available to everyone.
* **`rtKey`** (string): A unique key to identify this per-user state.
* **`initialValue`** (T): The initial value for the current user's state.
* **`options?`** (`UseStateTogetherWithPerUserValuesOptions`):
* **`UseStateTogetherWithPerUserValuesOptions` Interface:**
```typescript
interface UseStateTogetherWithPerUserValuesOptions {
resetOnDisconnect?: boolean;
resetOnConnect?: boolean;
keepValues?: boolean; // keep value in session after user disconnects
overwriteSessionValue?: boolean; // if a session value exists, overwrite it with initialValue
omitMyValue?: boolean; // omit my value from allValues
throttleDelay?: number;
}
```
* `resetOnDisconnect?` (boolean, default: `false`): If `true`, the user's local state resets to `initialValue` upon disconnecting.
* `resetOnConnect?` (boolean, default: `false`): If `true`, the user's state resets to `initialValue` upon connecting to a session.
* `keepValues?` (boolean, default: `false`): If `true`, the user's state is persisted in the session even after they disconnect.
* `overwriteSessionValue?` (boolean, default: `false`): If `true` and a session value already exists for the user, the user's local `initialValue` will overwrite the session value upon connecting. By default, the session value takes precedence.
* `omitMyValue?` (boolean, default: `false`): If `true`, the current user's value will not be included in the `allValues` object returned by the hook.
* `throttleDelay?` (number, default: `100`): The delay in milliseconds for throttling state updates.
* **Returns:** `[T, React.Dispatch<React.SetStateAction<T>>, ValueMap<T>]`
* `T`: The current user's local value.
* `React.Dispatch<React.SetStateAction<T>>`: Function to set the current user's local value (which then synchronizes).
* `ValueMap<T>`: An object where keys are `userId`s and values are their respective states for this `rtKey`.
* **`ValueMap<T>` Type:** `Record<string, T>`
**Example:**
```tsx
import { useStateTogetherWithPerUserValues, useMyId } from 'react-together';
function PerUserStatus() {
const myId = useMyId();
const [myStatus, setMyStatus, allStatuses] =
useStateTogetherWithPerUserValues<string>('user-status', 'Online');
return (
<div>
<p>My Status ({myId || 'N/A'}): {myStatus}</p>
<input
type="text"
value={myStatus}
onChange={(e) => setMyStatus(e.target.value)}
placeholder="Set your status"
/>
<h3>All User Statuses:</h3>
<ul>
{Object.entries(allStatuses).map(([userId, status]) => (
<li key={userId}>{userId}: {status}</li>
))}
</ul>
</div>
);
}
```
#### `useFunctionTogether<T extends (...args: any[]) => void>(rtKey: string, callback: T)`
Synchronizes a function call across all users. When one user calls the returned function, the `callback` function is executed on all connected clients with the same arguments.
* **`rtKey`** (string): A unique key for this synchronized function.
* **`callback`** (T): The function to be executed on all clients. This function should be memoized (e.g., with `React.useCallback`) if it has dependencies.
* **Returns:** `T` - A new function that, when called, will trigger the `callback` on all clients.
**Example:**
```tsx
import { useFunctionTogether } from 'react-together';
import React, { useCallback } from 'react';
function SharedNotification() {
const showNotificationForAll = useFunctionTogether('global-notification',
useCallback((message: string) => {
alert(`Notification for everyone: ${message}`);
}, [])
);
return (
<button onClick={() => showNotificationForAll('Meeting starts in 5 minutes!')}>
Send Global Notification
</button>
);
}
```
#### `useChat(rtKey: string)`
Provides data and methods for a simple real-time chat functionality.
* **`rtKey`** (string): A unique key for this chat instance.
* **Returns:** `{ messages: ChatMessage[], sendMessage: (message: string) => void }`
* `messages`: An array of `ChatMessage` objects.
* **`ChatMessage` Interface:**
```typescript
interface ChatMessage {
id: number; // Unique message ID generated locally
senderId: string; // ID of the user who sent the message
message: string; // Content of the message
sentAt: number; // Timestamp (Date.now()) of when the message was sent
}
```
* `sendMessage`: A function to send a new message to the chat.
* **Type:** `(message: string) => void`
#### `useCursors(options?: UseCursorsOptions)`
Tracks and provides the cursor positions of all users in the session.
* **`options?`** (`UseCursorsOptions`):
* **`UseCursorsOptions` Interface:**
```typescript
interface UseCursorsOptions {
throttleDelay?: number;
deleteOnLeave?: boolean; // if true, cursor data is removed when mouse leaves window
omitMyValue?: boolean; // if true, my cursor is not included in allCursors
}
```
* `throttleDelay?` (number, default: `50`): Delay in milliseconds for updating cursor positions.
* `deleteOnLeave?` (boolean, default: `false`): If `true`, a user's cursor data is removed when their mouse leaves the window.
* `omitMyValue?` (boolean, default: `true`): If `true`, the current user's cursor is not included in `allCursors`.
* **Returns:** `{ myCursor: Cursor | null, allCursors: Record<string, Cursor | null> }`
* `myCursor`: The current user's cursor data, or `null`.
* **`Cursor` Interface:**
```typescript
interface Cursor {
pageX: number; // X-coordinate relative to the whole document
pageY: number; // Y-coordinate relative to the whole document
clientX: number; // X-coordinate relative to the viewport
clientY: number; // Y-coordinate relative to the viewport
percentX: number; // X-coordinate as a percentage of document width
percentY: number; // Y-coordinate as a percentage of document height
}
```
* `allCursors`: An object mapping `userId`s to their cursor data (`Cursor` or `null`).
#### `useHoveringUsers(rtKey: string)`
Tracks which users are hovering over the DOM element associated with the returned `ref`.
* **`rtKey`** (string): A unique key for this hoverable area.
* **Returns:** `[React.MutableRefObject<HTMLDivElement | null>, string[], boolean]`
* `React.MutableRefObject<HTMLDivElement | null>`: A React ref object to be attached to the DOM element you want to track hovers on.
* `string[]`: An array of `userId`s of users currently hovering over the element.
* `boolean`: A boolean indicating if the current local user is hovering over the element.
**Example:**
```tsx
import React from 'react';
import { useHoveringUsers, useNicknames } from 'react-together';
function HoverArea() {
const [hoverRef, hoveringUserIds, iAmHovering] = useHoveringUsers('shared-hover-box');
const [, , allNicknames] = useNicknames();
const hoveringNicknames = hoveringUserIds.map(id => allNicknames[id] || id);
return (
<div
ref={hoverRef}
style={{
width: '200px',
height: '100px',
border: '1px solid black',
backgroundColor: iAmHovering ? 'lightblue' : (hoveringUserIds.length > 0 ? 'lightgreen' : 'white'),
padding: '10px',
marginTop: '10px'
}}
>
<p>Hover over this box!</p>
{hoveringUserIds.length > 0 && (
<p>Hovering: {hoveringNicknames.join(', ')}</p>
)}
{iAmHovering && <p>(You are hovering!)</p>}
</div>
);
}
```
#### `useAllNicknames(): Record<string, string>`
Returns an object mapping all user IDs in the session to their nicknames.
* **Returns:** `Record<string, string>` - An object where keys are `userId`s and values are their nicknames.
#### `useConnectedUsers(): ConnectedUser[]`
Returns an array of `ConnectedUser` objects representing all users currently in the session.
* **Returns:** `ConnectedUser[]` - An array of connected user objects.
* **`ConnectedUser` Interface:**
```typescript
interface ConnectedUser {
userId: string;
isYou: boolean;
nickname: string; // Preferred way to get user's display name
name: string; // Deprecated, use nickname
}
```
#### `useCreateRandomSession(): () => void`
Returns a function that, when called, connects the user to a new React Together session with a randomly generated name and password.
* **Returns:** `() => void` - A function to trigger joining a new random session.
#### `useIsTogether(): boolean`
Returns `true` if the user is currently connected to a React Together session, `false` otherwise.
* **Returns:** `boolean`
#### `useJoinUrl(): string | null`
Returns a shareable URL that others can use to join the current session. Returns `null` if not in a session.
* **Returns:** `string | null`
#### `useLeaveSession(): () => void`
Returns a function to disconnect the current user from the session and clear session parameters from the URL.
* **Returns:** `() => void`
#### `useMyId(): string | null`
Returns the `userId` of the current local user, or `null` if not in a session.
* **Returns:** `string | null`
#### `useNicknames(): [string, (nickname: string) => void, Record<string, string>]`
Manages user nicknames.
* **Returns:** `[string, (nickname: string) => void, Record<string, string>]`
* `string`: The current user's nickname.
* `(nickname: string) => void`: A function to set the current user's nickname.
* `Record<string, string>`: An object of all nicknames in the session (mapping `userId` to nickname).
### Core Components
#### `<Chat />`
A ready-to-use chat component that utilizes `useChat` internally.
**Props (`ChatProps`):**
* `rtKey` (string, required): Unique key for the chat session. This key will be used by `useChat` internally.
* `chatName?` (string | React.ReactElement, default: `"Group Chat"`): Display name for the chat header.
* `components?` (`ChatComponents`): Allows overriding default sub-components of the chat UI.
* **`ChatComponents` Interface:**
```typescript
interface ChatComponents {
ChatHeader?: React.ComponentType<ChatHeaderProps>;
MessageRow?: React.ComponentType<MessageRowProps>;
ChatInput?: React.ComponentType<ChatInputProps>;
}
```
* **`ChatHeaderProps` Interface:**
```typescript
interface ChatHeaderProps {
chatName: string | React.ReactElement;
}
```
* **`MessageRowProps` Interface:**
```typescript
interface MessageRowProps {
message: ChatMessage; // As defined in useChat
isYou: boolean;
nickname: string;
}
```
* **`ChatInputProps` Interface:**
```typescript
interface ChatInputProps {
sendMessage: (message: string) => void;
disabled: boolean;
}
```
* `hideWhenDisconnected?` (boolean, default: `false`): If `true`, the chat UI will not be rendered if the user is not connected to a session.
* Other props are passed down to the root `div` element of the component.
**Example:**
```tsx
import { Chat } from 'react-together';
function MyChatApp() {
return <Chat rtKey="my-global-chat" chatName="My Awesome Chat Room" />;
}
```
#### `<Cursors />`
Displays the cursors of all connected users within the viewport. Utilizes `useCursors` internally.
**Props (`CursorsProps`):**
* `options?` (`UseCursorsOptions`): Options passed directly to the underlying `useCursors` hook.
* **`UseCursorsOptions` Interface (same as for `useCursors` hook):**
```typescript
interface UseCursorsOptions {
throttleDelay?: number;
deleteOnLeave?: boolean;
omitMyValue?: boolean;
}
```
* `components?` (`CursorsComponents`): Allows providing a custom component to render each user's cursor.
* **`CursorsComponents` Interface:**
```typescript
interface CursorsComponents {
UserCursor?: React.ComponentType<UserCursorProps>;
}
```
* **`UserCursorProps` Interface:**
```typescript
interface UserCursorProps {
cursor: Cursor; // As defined in useCursors
userId: string;
nickname: string;
color: string; // Color generated by utils.getUserColor(userId)
transitionDuration?: number; // in ms, default 50
}
```
* `transitionDuration?` (number, default: `50`): Duration in milliseconds for the cursor movement transition. Passed to the default `UserCursor` component if no custom one is provided.
* `getUserColor?` (function: `(userId: string) => string`, default: `utils.getUserColor`): Function to determine the color of a user's cursor. Passed to the default `UserCursor`.
**Example:**
```tsx
import { Cursors } from 'react-together';
function CollaborativePage() {
return (
<div>
{/* Your page content */}
<Cursors options={{ omitMyValue: false }} />
</div>
);
}
```
#### `<ConnectedUsers />`
Displays avatars or nicknames of users currently connected to the session. Utilizes `useConnectedUsers` and `useNicknames` internally.
**Props (`ConnectedUsersProps`):**
* `maxAvatars?` (number, default: `3`): Maximum number of user indicators (nicknames/avatars) to display before showing a "+N" indicator (e.g., "+2 more").
* Other props are passed down to the root `div` element of the component.
**Example:**
```tsx
import { ConnectedUsers } from 'react-together';
function AppHeader() {
return (
<header style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<h1>My App</h1>
<ConnectedUsers maxAvatars={5} />
</header>
);
}
```
#### `<HoverHighlighter />`
A wrapper component that highlights its children when any user (or optionally, the current user) hovers over it. Utilizes `useHoveringUsers` internally.
**Props (`HoverHighlighterProps`):**
* `rtKey` (string, required): Unique key for this hoverable area, passed to `useHoveringUsers`.
* `children` (React.ReactNode, required): The content to be wrapped and made hover-aware.
* `className?` (string): Optional CSS class for styling the wrapper `div`.
* `highlightMyself?` (boolean, default: `false`): If `true`, the component also highlights when the local user hovers over it (in addition to highlighting for other users' hovers).
* Other props are passed down to the root `div` element of the component.
**Example:**
```tsx
import { HoverHighlighter } from 'react-together';
function InteractiveElement() {
return (
<HoverHighlighter rtKey="interactive-box-1" className="my-custom-hover-style">
<div style={{ padding: '20px', border: '1px dashed gray' }}>
Hover over me to see highlights!
</div>
</HoverHighlighter>
);
}
```
#### `<SessionManager />`
Provides a UI button that opens a dialog to manage the current session: create a new random session, view/copy the current session's join URL (including a QR code), or leave the session.
**Props (`SessionManagerProps`):**
This component primarily accepts standard HTML attributes that are passed to its root button element (e.g., `className`, `style`). It does not have specific functional props that alter its core behavior, as its functionality is self-contained using various `react-together` hooks.
**Example:**
```tsx
import { SessionManager } from 'react-together';
function AppControls() {
return (
<div style={{ position: 'fixed', top: '10px', right: '10px' }}>
<SessionManager />
</div>
);
}
```
### Utilities (`utils` module)
React Together exports a `utils` object containing helper functions, particularly for URL manipulation related to session joining and user identification.
* **`getSessionNameFromUrl(url: URL, options?: { nameKey?: string }) : string | null`**: Extracts the session name from a URL's query parameters. (Default `nameKey` is `rtName`).
* **`getSessionPasswordFromUrl(url: URL, options?: { passwordKey?: string }) : string | null`**: Extracts the session password from a URL's hash parameters. (Default `passwordKey` is `rtPwd`).
* **`getJoinUrl(url: URL, name: string, password?: string, options?: { nameKey?: string, passwordKey?: string }) : URL`**: Creates a new URL with the session name and password embedded as query and hash parameters, respectively.
* **`getCleanUrl(url: URL, options?: { nameKey?: string, passwordKey?: string }) : URL`**: Creates a new URL with session name and password parameters removed.
* **`deriveNickname(userId: string): string`**: Generates a human-readable nickname from a `userId` using `unique-names-generator` (e.g., "Eclectic Mastodon").
* **`getUserColor(userId: string): string`**: Generates a consistent HSL color string based on a `userId`. Useful for assigning unique colors to user-specific UI elements like cursors or avatars.
**Example (using a utility):**
```tsx
import { utils } from 'react-together';
const myUserId = 'user-123-abc';
const nickname = utils.deriveNickname(myUserId); // e.g., "WittyWalrus"
const color = utils.getUserColor(myUserId); // e.g., "hsl(123, 70%, 50%)"
console.log(`User ${myUserId} is ${nickname} and their color is ${color}`);
```
## File: multisynq-react.md/multisynq-react.md
````markdown
# Multisynq-React
Multisynq is a framework that allows creating synchronized multi-user applications without writing any backend or network code.
It makes use of synchronizer servers to bounce events from one user to all users, ensuring event order and timing.
React Together leverages Multisynq to synchronize React applications.
More specifically, it uses a thin wrapper for React, called `@multisynq/react`, to maintain a synchronized state store, that is accessible via the React Together hooks.
However, certain applications require stronger consistency guarantees that currently cannot be implemented solely with React Together.
Consider a game where multiple players move on a 2D board, and no two players can occupy the same square simultaneously.
A naive approach might involve checking if the destination square is empty before moving there.
However, this can lead to race conditions.
For instance, if two players attempt to move to the same square and both check its availability before the other player moves, both will end up on the square, breaking the application's logic.

Multisynq elegantly solves this problem by design: Instead of broadcasting the result of each action, users broadcast the intended action.
All actions from all users are then executed by everyone in the same order.
This mechanism ensures that only one player can ultimately move to a specific square.

For applications with such consistency requirements, we strongly recommend exploring @multisynq/react and implementing such logic on the model side.
Rest assured, you can do this while continuing to use React Together.
### React Together + @multisynq/react
`@multisynq/react` allows you to define custom logic by implementing a model class.
We recommend checking their [documentation](https://multisynq.io/docs/multisynq-react/) and [tutorials](https://multisynq.io/docs/multisynq-react/tutorial-1_React_Simple_Counter.html) for detailed guidance.
**Important:**
Ensure your model extends `ReactTogetherModel` to maintain React Together functionality
```typescript
import { ReactTogetherModel } from 'react-together'
export class OverrideModel extends ReactTogetherModel {
init() {
super.init({})
this.subscribe(this.id, 'my-event', this.handleMyEvent)
// Additional model initialization and subscriptions
}
handleMyEvent() {
// Event handling logic
}
}
// Register your model
OverrideModel.register('OverrideModel')
```
Configure React Together to use your custom model by passing the class in the `sessionParams` prop of the `ReactTogether` component:
```typescript
import { ReactTogether } from 'react-together'
import { OverrideModel } from './models'
export default function App() {
return (
<ReactTogether
sessionParams={{
// Your session params
model: OverrideModel
}}
>
{/* Your App content */}
</ReactTogether>
)
}
```
Now that we have React Together running our model, we can use the `usePublish` hook to publish events to be executed on the model side:
```typescript
import { MultisynqReact } from 'react-together'
const { usePublish, useModelSelector } = MultisynqReact
function MyComponent() {
const model = useModel()
const publishEvent = usePublish((d) => [model.id, 'my-event', d])
// Component logic
}
```
**Note:** We recommend you to import the `@multisynq/react` API via the `MultisynqReact` object.
````
### Further Information
* **Website & Full Documentation:** [https://react-together.dev/](https://react-together.dev/)
* **GitHub Repository:** [https://github.com/multisynq/react-together](https://github.com/multisynq/react-together)
* **Community (Discord):** [https://multisynq.io/discord](https://www.google.com/search?q=https://multisynq.io/discord)
This documentation provides a comprehensive overview of the core `react-together` library. For the most detailed and up-to-date information, always refer to the official project website and source code.
</MULTISYNQ DOCUMENTATION [react-together]>