express-ws icon indicating copy to clipboard operation
express-ws copied to clipboard

This project seems to be dead

Open xtianus79 opened this issue 4 years ago • 14 comments

it's not working anymore.

xtianus79 avatar Jun 12 '20 22:06 xtianus79

This project works, your comment is useless.

Janaka-Steph avatar Jun 22 '20 17:06 Janaka-Steph

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 :)

KnorpelSenf avatar Jun 24 '20 08:06 KnorpelSenf

@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 avatar Jun 24 '20 16:06 xtianus79

@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 avatar Jun 24 '20 17:06 Janaka-Steph

@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? :-)

EoinFalconer avatar Jun 25 '20 07:06 EoinFalconer

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

Janaka-Steph avatar Jun 25 '20 10:06 Janaka-Steph

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.

nicholasbulka avatar Jul 11 '20 14:07 nicholasbulka

still works but definitely not maintained.

Portur avatar Jul 24 '20 19:07 Portur

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 avatar Sep 07 '20 17:09 andraz213

@andraz213 I don't know about an other lib, but if your communication is one-way maybe you should consider Server-Sent Events instead.

Janaka-Steph avatar Sep 07 '20 17:09 Janaka-Steph

@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.

bradisbell avatar Dec 03 '20 18:12 bradisbell

In case it helps, I’m using my fork (https://github.com/aral/express-ws) in Site.js (https://sitejs.org).

aral avatar Jan 08 '21 20:01 aral

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)

talentlessguy avatar May 10 '21 18:05 talentlessguy

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

wll8 avatar May 19 '22 06:05 wll8