Create Bundle
pluv.io ships @pluv/react to leverage @pluv/client in a type-safe and React.js way.
Create a PluvIO instance
First, create a PluvIO
instance from the @pluv/io
package in your backend codebase.
1// backend/io.ts23import { yjs } from "@pluv/crdt-yjs";4import { createIO } from "@pluv/io";5import { platformNode } from "@pluv/platform-node";67export const io = createIO({8 /**9 * @optional10 * @description This is required if you intend to use storage. Specify crdt to use for storage11 */12 crdt: yjs,13 platform: platformNode(),14});1516// Export the websocket client io type, instead of the client itself17export type AppPluvIO = typeof io;
Create a Pluv React bundle
Then, import your PluvIO
type to the frontend, and create your type-safe React.js bundle using @pluv/react
.
1// frontend/io.ts23import { yjs } from "@pluv/crdt-yjs";4import { createBundle, createClient } from "@pluv/react";5import { z } from "zod";6// import your PluvIO instance from your backend codebase7import { type AppPluvIO } from "backend/io";89const client = clientClient<AppPluvIO>({10 authEndpoint: () => "{{your auth endpoint}}",11 wsEndpoint: () => "{{your ws endpoint}}",12});1314export const {15 // factories16 createRoomBundle,1718 // components19 PluvProvider,2021 // hooks22 useClient,23} = createBundle(client);2425export const {26 // components27 PluvRoomProvider,2829 // hooks30 useBroadcast,31 useConnection,32 useEvent,33 useMyPresence,34 useMyself,35 useOther,36 useOthers,37 useRoom,38 useStorage,39} = createRoomBundle({40 // Optional: Specify which CRDT you are using, as well as the initial storage41 initialStorage: yjs.doc(() => ({})),42});