Registry / devops / express-ws-routes

express-ws-routes

JSON →
library1.1.0jsnpmunverified

Add WebSocket support to Express routes using the `ws` library. v1.1.0 (stable, no recent updates). Unlike `express-ws`, it uses a fake HTTP method (`WEBSOCKET`) instead of URL prefixing, supports explicit accept/reject via `cb()`, and works with detached routers. Last updated in 2016; maintained in low-activity mode.

npm install express-ws-routes
INSTALL
IMPORT
SIG · EXPRESS-WS-ROUTES
E
express-ws-routes
devopsjavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import expressWs from 'express-ws-routes'
const expressWs = require('express-ws-routes')
Module is CommonJS; default import works in ESM environments but require() is more common.
extendExpress
import { extendExpress } from 'express-ws-routes'
const { extendExpress } = require('express-ws-routes')
Named export for manual setup; available as property of default export.
createWebSocketServer
import { createWebSocketServer } from 'express-ws-routes'
const { createWebSocketServer } = require('express-ws-routes')
Named export for manual setup; used when not using the convenience method.

Create an Express app with WebSocket support via express-ws-routes, define a WebSocket route, and handle connections.

const express = require('express'); const app = require('express-ws-routes')(); app.websocket('/ws', function(info, cb, next) { cb(function(socket) { socket.send('Hello via WebSocket!'); }); }); const server = app.listen(3000, () => { console.log('Server listening on port 3000'); }); server.wsServer.on('connection', function(socket) { console.log('WebSocket connection established'); });
Debug
Known issues
gotchaRequires Express 4.5.0+; older versions do not support the required internal routing.
fix
Upgrade Express to 4.5.0 or later.
affects: >=1.0.0
gotchaThe fake HTTP method defaults to 'WEBSOCKET'; do not use 'websocket' as a real HTTP method in routes.
fix
Avoid using 'WEBSOCKET' as an HTTP method in other middleware.
affects: >=1.0.0
gotchaWhen using detached routers, ensure the router is attached to the app before defining WebSocket routes; otherwise routes may not be registered.
fix
Attach the router to the app with app.use() before calling router.websocket().
affects: >=1.0.0
gotchaThe callback 'cb' must be called synchronously; calling it asynchronously may cause unexpected behavior.
fix
Call cb() immediately in the handler.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: app.websocket is not a function
express-ws-routes not properly initialized; app created without calling the module function.
fix
Replace const app = express(); with const app = require('express-ws-routes')();
Error: Cannot find module 'ws'
ws is a dependency but not installed; npm may not have installed it automatically.
fix
Run npm install ws in your project.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
expressrequiredPeer dependency; required for routing
wsrequiredRuntime dependency; WebSocket server implementation
Agent activity
4 hits · last 30 days
node
4
Resources
express-ws-routes — npm install express-ws-routes · libregistry