Adds WebSocket endpoints to Express applications using the ws library. Version 5.0.2 (latest stable) is maintained with updates as needed. Express-ws enables defining WebSocket routes with standard Express middleware support, working seamlessly with Express routers. It modifies the global Router prototype by default and supports custom server instances and WebSocket options. The package requires Express peer dependency (express ^4.0.0 || ^5.0.0-alpha.1) and Node >=4.5.0. Key differentiator: minimal syntax compared to raw ws usage, but suffers from prototype monkey-patching and thread-unsafe design.
npm install express-wsNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates an Express app, adds WebSocket support, and defines an echo WebSocket endpoint at /echo.
Use option `leaveRouterUntouched: true` and manually call `expressWs.applyTo(router)` on each router you want WebSocket support. Or consider using ws directly.
Initialize express-ws immediately after creating the app, before any router definitions or app.use calls.
Clone the set before iterating: const clients = [...wss.clients]; clients.forEach(...).
Initialize with wsOptions: { maxPayload: 100 * 1024 } for global settings, or handle per-connection in the callback.Use const expressWs = await import('express-ws'); expressWs.default(app); but note it's still CJS under the hood.Ensure require('express-ws')(app) is called before any route definitions.Initialize express-ws first, or call expressWs.applyTo(router).
Use require('express-ws') or dynamic import with await import('express-ws').Configure the proxy to forward Upgrade headers, or use the 'server' option in expressWs to pass the raw http.Server.