Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This project seems to be dead #135

Open
xtianus79 opened this issue Jun 12, 2020 · 14 comments
Open

This project seems to be dead #135

xtianus79 opened this issue Jun 12, 2020 · 14 comments

Comments

@xtianus79
Copy link

it's not working anymore.

@Janaka-Steph
Copy link

This project works, your comment is useless.

@KnorpelSenf
Copy link

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

@xtianus79
Copy link
Author

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

@Janaka-Steph
Copy link

@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}!`))

@EoinFalconer
Copy link

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

@Janaka-Steph
Copy link

Janaka-Steph commented Jun 25, 2020

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

@nicholasbulka
Copy link

nicholasbulka commented Jul 11, 2020

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.

@Portur
Copy link

Portur commented Jul 24, 2020

still works but definitely not maintained.

@andraz213
Copy link

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.

@Janaka-Steph
Copy link

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

@bradisbell
Copy link

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

@aral
Copy link

aral commented Jan 8, 2021

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

@talentlessguy
Copy link

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)

@wll8
Copy link

wll8 commented May 19, 2022

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

  • Use directly from app.ws
  • http and ws of the same route can exist at the same time
  • Support regexp routing
  • Support reading params, query parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests