API Reference
createClient
Returns a PluvClient
instance.
This is a re-export of createClient from @pluv/client.
createBundle
Returns a CreateBundle
instance.
Creates a bundle to be able to access the PluvClient
from a react hook.
CreateBundle
This is the bundle returned from createBundle
.
createRoomBundle
Returns a CreateRoomBundle
instance.
Creates a bundle to be able to access PluvRoom
capabilities from various react hooks.
PluvProvider
React provider component to allow the PluvClient
created by createClient
to be accessible in child components via usePluvClient
. This is identical to importing the created client, so this is primarily useful for dependency injection when testing.
1<PluvProvider>{children}</PluvProvider>
usePluvClient
Returns PluvClient
.
Returns the PluvClient
that was created via createClient
.
CreateRoomBundle
This is the bundle returned from createRoomBundle
.
MockedRoomProvider
React provider component to mock PluvRoomProvider
. Enables hooks from CreateRoomBundle
while completely offline. This can be useful as in Storybook within a decorator when mocking PluvRoomProvider
.
1import { y } from "@pluv/react";2import { MockedRoomProvider } from "./io";34<MockedRoomProvider5 // Mock events sent and received for hooks in child components.6 events={{7 EMIT_EMOJI: ({ emojiCode }) => ({8 EMOJI_RECEIVED: { emojiCode },9 }),10 }}11 // Set initial presence for the current user12 initialPresence={{13 selectionId: null,14 }}15 // Set initial storage for usePluvStorage16 initialStorage={() => ({17 messages: y.array([]),18 })}19 // Set the room id20 room="my-mock-room"21>22 {children}23</MockedRoomProvider>
PluvRoomProvider
React provider component to enable CreateRoomBundle
hooks within child components. Mounting this component connects to a real-time room with other connected users. Unmounting this component will disconnect the room. See Create Rooms for more details.
1import { y } from "@pluv/react";2import { MockedRoomProvider } from "./io";34<PluvRoomProvider5 // Set initial presence for the current user6 initialPresence={{7 selectionId: null,8 }}9 // Set initial storage for usePluvStorage10 initialStorage={() => ({11 messages: y.array([]),12 })}13 // Emits an error whenever authorization fails, for monitoring or14 // debugging purposes15 onAuthorizationFail={(error: Error) => {16 console.error(error);17 }}18 // Set the room id19 room="my-room-id"20>21 {children}22</PluvRoomProvider>
usePluvBroadcast
Returns (event, data) => void
.
Returns a broadcast function to emit an event and data to all connected clients. This is type-safe. The first parameter event
will be the name of the event specified when creating your backend PluvIO
instance and the second parameter data
will be the associated response type. See Define Events for more details.
1const broadcast = usePluvBroadcast();23broadcast("EMIT_RECEIVED", { emojiCode: 123 });
usePluvConnection
Returns WebSocketConnection
.
Returns the websocket connection start of the current user.
1const connection = usePluvConnection();2// ^? const connection: {3// id: string | null;4// state: ConnectionState;5// }
usePluvEvent
Returns void
.
Defines a listener for events broadcasted by connected clients. This is type-safe. The first parameter event
is the name of the event to listen to from your backend PluvIO
instance. The second parameter is a callback containing the data emitted from the broadcasted event.
1usePluvEvent("EMOJI_RECEIVED", ({ emojiCode }) => {2 console.log(emojiCode);3});
usePluvMyPresence
Returns [myPresence: TPresence | null, updatePresence: Function]
.
Returns the current user's presence, and a function to update the user's presence. Using this hook rerenders the component based on a deep-equality check on the user's presence value. This hook accepts a selector to return a modified presence to reduce rerenders.
1const { usePluvMyPresence } = createRoomBundle({2 presence: z.object({3 selectionId: z.nullable(z.string()),4 selectionColor: z.string(),5 }),6});78const [9 // the user's presence10 myPresence,11 // a presence updater function12 updateMyPresence13] = usePluvMyPresence();1415// update individual base properties16updateMyPresence({ selectionId: "name-input" });17updateMyPresence({ selectionColor: "#123456" });1819// update multiple base properties20updateMyPresence({21 selectionId: "name-input",22 selectionColor: "#123456",23});2425// if you only need a partial presence26const [myPresence] = usePluvMyPresence(({ selectionId }) => selectionId);2728// if you don't need presence, and just want the updater29const [, updateMyPresence] = usePluvMyPresence(30 // set selector to return a stable value31 () => true32);
usePluvMyself
Returns UserInfo | null
.
Returns the user-info object for the current user. This hook accepts a selector to return a modified user-info to reduce rerenders.
1const myself = usePluvMyself();23// if you don't need the entire user-info4const myself = usePluvMyself(({ connectionId }) => connectionId);
usePluvOther
Returns UserInfo | null
.
Returns the user-info object for a connected user by connectionId
string.
1const other = usePluvOther();23// if you don't need the entire user-info4const other = usePluvOther(({ connectionId }) => connectionId);
usePluvOthers
Returns readonly UserInfo[]
.
Returns all user-info objects for all connected users. This may trigger a lot of re-renders if presence updates frequently.
Note Consider using this hook only for deriving others'
connectionId
values to pass intousePluvOther
.
1const others = usePluvOthers();23// if you want to just pull out connectionId properties4const connectionIds = usePluvOthers((others) => {5 return others.map(({ connectionId }) => connectionId);6});
usePluvStorage
Returns [TData | null, Yjs.SharedType | null]
.
Returns a base-level property of our yjs storage as a serialized value and a Yjs SharedType. The component is re-rendered whenever the Yjs SharedType is updated.
1import { y } from "@pluv/react";23const { usePluvStorage } = createRoomBundle({4 initialStorage: () => ({5 messages: y.array(["hello"]),6 }),7});89const [messages, sharedType] = usePluvStorage("messages");1011sharedType?.push(["world~!"]);1213messages?.map((message, key) => <div key={key}>{message}</div>);