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);
Old Response
There are the following:
closeerrormessageopen
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Attributes
Okay I found one more event that actually triggers. Now if only I would find one that gets called once on open connection.
app.ws('/', function (ws, req) { ws.on('close', function() { console.log('close connection'); }); ws.on('message', function (textChunk) { //do stuff } });});