Thanks Derek Hill and Normunds Kalnberzins
Works with routers is better
First import express-ws
import expressWs from 'express-ws';
To use the ws method, you need to strengthen express (I understand it this way)
const { app, getWss, applyTo } = expressWs(express());
after that
express.Router contains the ws method, but the type of the typescript is wrong, you only need to convert the type.
const router = express.Router() as expressWs.Router;
Define the routing logic
router.ws('/echo', (ws, req) => {
ws.on('message', (msg: String) => {
ws.send(msg);
});
});
Finally add router to the express app
app.use("/ws-stuff", router);