express-ws
express-ws copied to clipboard
This project seems to be dead
it's not working anymore.
This project works, your comment is useless.
It may still work, but it definitely isn't actively maintained. Last publish at least 2 years ago, last commit 2.5 years, more than 50 open issues. ws
evolves further (confer #121) but this library doesn't. Seems pretty dead when you poke it.
@xtianus79 thank you for the very useful warning :)
@Janaka-Steph it absolutely is not working. Try to use this with any new layer of express and it doesn't work without issue or warning or emitted errors.
@xtianus79 It works for me with Node 14, latest version of express, latest version of expressWs and Typescript.
Here is a quick example
import express, {NextFunction, Request, Response} from 'express'
import expressWs, {Application} from 'express-ws'
import WebSocket from 'ws' //This is the type package @types/ws
let wsConnections: {[x: string]: WebSocket}[] = []
const app: Application = expressWs(express(), undefined, {wsOptions: {clientTracking: true}}).app
app.ws('/ws', (ws: WebSocket) => {
const wsClientId: string = randomBytes(2).toString('hex')
log.info(`New websocket connection open by client ${wsClientId}`)
ws.send(JSON.stringify({data: 'hello'}))
// Store client connection
wsConnections.push({[wsClientId]: ws})
})
app.listen(env.SERVER_PORT, () => log.info(`API Server started on port ${env.SERVER_PORT}!`))
@Janaka-Steph Are you able to get it to work on with express.Router() routes? This isn't working for me on any apps that are updated to new versions - I haven't tried using {Application} from express-ws maybe this is what is going wrong? Where is that referenced in the docs? :-)
I didn't try with express.Router() sorry. The docs shows a CommonJS example but the idea is the same. https://github.com/HenningM/express-ws#full-example
var app = express();
var expressWs = require('express-ws')(app);
is the same as
import expressWs from 'express-ws'
const app = express()
const expressWs = expressWs(app)
is the same as
import expressWs from 'express-ws'
const app = expressWs(express()).app
with type script,
const wsInstance : expressWs.Instance = expressWs(eApp, server); wsInstance.app.use('/room', roomRouter);
roomRouter.ws( '/:id/:userId', RoomController );
const RoomController = (socket : ws, req : Express.Request) : void => {
something like this works for me.
still works but definitely not maintained.
Are there any good alternatives? I'm writing an express app and I need to implement regular rest API, but I want one endpoint to use websockets. I know it's out of spec and bad practice, but I need to post data with an esp32 several times a second, and regular HTTP just isn't cutting it.
@andraz213 I don't know about an other lib, but if your communication is one-way maybe you should consider Server-Sent Events instead.
@andraz213 Since you're in control of the client, you could always just open an HTTP request and continue to stream your data under the same request. No need for the web sockets layer.
In case it helps, I’m using my fork (https://github.com/aral/express-ws) in Site.js (https://sitejs.org).
sorry for shameless plug but in case someone is looking for another express + ws middleware I've just published tinyws. Might be useful for those who want to use something newer than express-ws.
this is how you can use it with express:
import express from 'express'
import { tinyws } from 'tinyws'
const app = express()
app.use('/', tinyws())
app.use('/hmr', async (req, res) => {
if (req.ws) {
const ws = await req.ws()
return ws.send('hello there')
} else {
res.send('Hello from HTTP!')
}
})
app.listen(3000)
I created a new project, the code looks simpler and has more useful features, I hope it helps you. https://github.com/wll8/express-ws
- [x] Use directly from app.ws
- [x] http and ws of the same route can exist at the same time
- [x] Support regexp routing
- [x] Support reading params, query parameters