express-ws
express-ws copied to clipboard
Does express-ws create a single WS server per initiation ?
Websocket newbie here. I managed to set up express-ws in my already existing node/express project.
This is how I do it
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
const expressWs = require('express-ws')(app);
app.use(express.static(path.join(__dirname,'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.json());
app.use(express.urlencoded({extended:false}));
app.ws('/ws', function(ws, req) {
ws.send(JSON.stringify({ message: 'Gotcha' }));
ws.on('message', function(message) {
console.log('Received: ' + message);
});
});
app.use('/', require('./routes/vessel'));
const port = process.env.PORT || 3000;
app.listen(port, ()=>{
console.log('server runs on port ', port);
});
Everything works fine.
According to the docs, const expressWs = require('express-ws')(app);
means that expressWS will use the server that is created automatically when you call app.listen
.
So there is an http server for the expressWs to work with.
My question is, if express-ws creates multiple WS servers, then those will share the same http server. If this is true, then we run into the ws issue described here .
If express-ws does not create multiple WS servers, then there is no issue at all, since there is only one single WS server for an http server.
Which one is it ? Does express-ws creates by default one or multiple WS servers ?
Please excuse me if my question is out of place, I am trying to understand how express-ws works and avoid potential errors and issues.
Thanks
Not the developer, but did take a quick peek at the code and it looks like it only creates one websocket server.