Skip to content
New issue

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

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

Already on GitHub? # to your account

Does express-ws create a single WS server per initiation ? #126

Open
spimou opened this issue Aug 28, 2019 · 1 comment
Open

Does express-ws create a single WS server per initiation ? #126

spimou opened this issue Aug 28, 2019 · 1 comment

Comments

@spimou
Copy link

spimou commented Aug 28, 2019

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

@AIVASteph
Copy link

Not the developer, but did take a quick peek at the code and it looks like it only creates one websocket server.

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

No branches or pull requests

2 participants