express-ws icon indicating copy to clipboard operation
express-ws copied to clipboard

improper handling of multiple sockets from same client

Open swantzter opened this issue 7 years ago • 2 comments

so, i have two sockets /errors and /db using the following configuration:

app.ws('/errors', function(ws, req) {})
app.ws('/db', function(ws, req) {
  ws.on('message', function(data) {
    dbBroadcast(ws)
  })
})
var errWs = expressWs.getWss('/errors');
var dbWs = expressWs.getWss('/db');

function broadcast(msg) {
  errWs.clients.forEach(function(client) {
    client.send(JSON.stringify(msg));
  });
}

function dbBroadcast(ws) {
  dbWs.clients.forEach(function(client) {
    if (client !== ws) {
      client.send('update');
    }
  });
}

however, if i, from a web browser connect to both with

var errorSocket = new WebSocket("ws://localhost:3333/errors");
errorSocket.onmessage = function(evt) {
  console.log('backend error:', JSON.parse(evt.data))
}

var dbSocket = new WebSocket("ws://localhost:3333/db");
dbSocket.onmessage = function(evt) {
  console.log('Update')
}

all messages sent to dbSocket (dbSocket.send('hello') for example) will send data to errorSocket, which will error in errorSocket.onmessage broadcast() will also trigger both errorSocket.onmessage and dbSocket.onmessage is this because of multiple connections to the same host? or how express-ws creates and handles websockets? or is it simply ment to not be this way?

swantzter avatar Jul 02 '17 13:07 swantzter

I am getting similar. Mine are from multiple clients (2 command line clients on different routes).

JaredSartin avatar Oct 18 '18 01:10 JaredSartin

These methods

var errWs = expressWs.getWss('/errors'); var dbWs = expressWs.getWss('/db');

are shown in the examples, but the getWss method does not take any arguments in the code. So that feature still needs to be implemented apparently.

fieldfoxWim avatar Feb 02 '19 13:02 fieldfoxWim