Node.js
pluv.io supports building real-time APIs with Node.js. You can define your handler and websocket server manually if you need more control, but if you'd like to get started quicky, check out createPluvHandler.
Using with Node.js (manual)
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-node34# Server peer-dependencies5npm install ws 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({7 platform: platformNode(),8});910// Export the websocket client io type, instead of the client itself11export 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});
createPluvHandler
If you don't need to modify your websocket server or handler too specifically, @pluv/platform-node also provides a function createPluvHandler
to create a websocket server and handler for your automatically.
1import { createIO } from "@pluv/io";2import { createPluvHandler, platformNode } from "@pluv/platform-node";3import bodyParser from "body-parser";4import cors from "cors";5import express from "express";6import Http from "http";7import WS from "ws";89const io = createIO({10 platform: platformNode(),11});1213const app = express();14const server = Http.createServer(app);1516const Pluv = createPluvHandler({17 // Your PluvIO instance18 io,19 // Optional: Specify the base path from which endpoints are defined20 endpoint: "/api/pluv", // defaults to "/api/pluv"21 // Your Http.Server instance22 server,23 // If your PluvIO instance defines authorization, add your authorization24 // logic here. Return a user if authorized, return null or throw an error25 // if not authorized.26 async authorize({ req, res, roomId }) {27 return {28 id: "abc123",29 name: "leedavidcs",30 };31 },32});3334// Create your WS.Server instance, which listens to "connection" events35const wsServer = Pluv.createWsServer();3637// Alternatively, define your own websocket server38const wsServer = new WS.Server({ server });3940wsServer.on("connection", async (ws, req) => {41 const { matched } = await Pluv.wsHandler(ws, req);4243 if (matched) return;4445 // didn't match with the Pluv handler, add your own logic46 // ...47});4849app.use(bodyParser.json());50app.use(cors({ origin: "*" }));51app.use(Pluv.handler);5253server.listen(3000);