Node.js
pluv.io supports building real-time APIs with Node.js.
Using with Node.js
Let's step through how we'd put together a real-time API for Node.js.
Install dependencies
1# For the server2npm install @pluv/io @pluv/platform-node3# Server peer-dependencies4npm install ws yjs zod
Create PluvIO instance
Define an io (websocket client) instance on the server codebase:
1// backend/io.ts23import { createIO } from "@pluv/io";4import { platformNode } from "@pluv/platform-node";56export const io = createIO({ platform: platformNode() });78// Export the websocket client io type, instead of the client itself9export type AppPluvIO = typeof io;
Integrate PluvIO with ws
Integrate with ws on your Node.js server.
1// backend/server.ts23import express from "express";4import Http from "http";5import WebSocket from "ws";6import { io } from "./io";78const PORT = 3000;910const app = express();11const server = Http.createServer();12const wsServer = new WebSocket.Server({ server });1314const parseRoomId = (url: string): string => {15 /* get room from req.url */16};1718wsServer.on("connection", async (ws, req) => {19 const roomId = parseRoomId(req.url);20 const room = io.getRoom(roomId);2122 await room.register(ws);23});2425server.listen(PORT, () => {26 console.log(`Server is listening on port: ${port}`);27});
From this point on, you can integrate your frontend with @pluv/react
as you would normally.