Cross-platform WebSocket server toolkit supporting Node.js, Deno, Bun, and Cloudflare Workers. Version 0.4.5 provides a unified hooks API with typed events, prebundled ws for Node.js with optional uWebSockets adapter, and tree-shakable ESM exports. It is maintained by the h3js organization and follows a regular release cadence. Key differentiators: zero per-connection callback overhead, conditional exports for each platform, and TypeScript-first developer experience.
npm install crosswsNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates a WebSocket echo server using crossws with typed adapter hooks and listens on port 3000.
Use import statements and ensure your project is configured for ESM (e.g., type: 'module' in package.json).
Access peer properties via methods (e.g., peer.url, peer.headers) instead of the underlying raw connection.
Use new TextEncoder().encode(msg) for string-to-binary conversion; avoid plain ArrayBuffer.
Always await or catch send() errors, e.g., peer.send(data).catch(console.error).
Explicitly return a Response from the worker's fetch handler after upgrading.
Upgrade hooks: replace 'connection' with 'open' and update peer usage per documentation.
Switch to ESM by adding type: 'module' to package.json or use dynamic import().
Ensure you are using the peer object provided in hook callbacks, not the original raw WebSocket.
Use the named import: import { defineWebSocketAdapter } from 'crossws'.Check peer.readyState === 1 before sending, or catch send() rejections.