What Express websocket events exist? What Express websocket events exist? express express

What Express websocket events exist?


The function provided to app.ws is the one executed when a new websocket is opened. So use it this way:

app.ws('/', function (ws, req) {    console.log("New connection has opened!");    ws.on('close', function() {        console.log('The connection was closed!');    });    ws.on('message', function (message) {        console.log('Message received: '+message);    });});


After looking at the source code for express-ws it looks like you can do the following.

var express = require('express');var app = express();var expressWs = require('express-ws')(app);// get the WebsocketServer instance with getWss()// https://github.com/HenningM/express-ws/blob/5b5cf93bb378a0e6dbe6ab33313bb442b0c25868/index.js#L72-L74expressWs.getWss().on('connection', function(ws) {  console.log('connection open');});// ... express middleware// ... websocket middle wareapp.ws('/', function(ws, req) {  ws.on('message', function(msg) {    console.log(msg);  });});app.listen(3000);