q-nick/npm-gui

message event listener registration error

emarteca opened this issue · 0 comments

Hi all, I've been working on a tool to identify instances of events registered to the wrong object in uses of some JavaScript event-driven APIs, as part of a research project.

The tool flagged line 27 in server/console.ts, on the registration of the “message” event.
The reason I believe this is indicative of an error is as follows (from looking at the npm ws API documentation).
The wss variable, created by the call to Ws.Server (on line 18) is an object of type ws.Server. However, the “message” event is emitter on ws.Socket.
My guess is that your listener registration for “message” should be on the ws variable inside the listener for “connection” on line 22, since ws is a ws.Socket.
Specifically, I think the code should be:

wss.on('connection', (ws) => {
    consoleSocket = ws;
    send('console connected \n', 'default');

    ws.on(“message”), ….); // place the code from the “message” registration on wss here 
  });

This corresponds to the example in the API documentation.

Thanks!