HenningM / express-ws

WebSocket endpoints for express applications
BSD 2-Clause "Simplified" License
877 stars 142 forks source link

Async/await prevents webSocket connection #136

Closed yarnball closed 4 years ago

yarnball commented 4 years ago

Hi all,

I was recommended to use express-ws to better handle cookies with my websocket (even though it seems a little while since the last update).

The below code works fine WITHOUT using the AWAIT

When I add something async to await, my client is continually in the "connecting" phase.

Not sure if this is express-ws or the lib I'm using, but either way I'm open to any suggestions here?

Is there somewhere else I could/should be doing this async call?


  var app = express();
  var server = http.createServer(app);
  var backend = new ShareDB();

  expressWs(app, server)
  app.ws('/', async (innerWs, req) => {
    await setThingsToRequest(req)
    var stream = new WebSocketJSONStream();
    backend.listen(stream);
  })

Same issue persists with this:

const handleAsync = async (res, req, next) => {
  await fakeDelay()
  next()
}

const connectToSocket = (innerWs, req, next) => {
  var stream = new WebSocketJSONStream(innerWs);
  backend.listen(stream);
  next()
}

app.ws('/', handleAsync, connectToSocket)
anraiki commented 4 years ago

My code runs fine with async/await.

app.ws('/watcher/', async function (ws, req) {

    await sleep(5000);

    ws.on("message", (data) => {
        ...
    });

});
yarnball commented 4 years ago

Hmm, looks like this isn't the cause then. I'll close. Feel free to re-open if anyone see's a similar issue