Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Replace 'express-ws' #1

Closed
Albin-Rohde opened this issue Feb 4, 2021 · 4 comments
Closed

Replace 'express-ws' #1

Albin-Rohde opened this issue Feb 4, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@Albin-Rohde
Copy link
Owner

The npm package express-ws does not seem to support broadcasting to a clients on a specific route or context.

For example Imagine Client A creates a game on the ws-route ../game/lobby/<unique_key_1>.
Client B joins the game via a private link ../game/<unique_key_1>.
We now want to broadcast to everyone connected to ../game/<unique_key_1>/join that a new client has joined, and thus update the UI on both ends. Telling them how many players are in the game.

The issue right now is that broadcasting on a specific route is not possible in express-ws. I.e if we where to broadcast the new Game state to all clients; A Client C would also receive that message. The message would contain data such as <unique_key_1>.

Client C should not have access to such information, but now has, and can now hijack the game, and join via ../game/<unique_key_1>/join. Even though Client C never received an invite.

This issue was discussed here: https://gist.github.com/hugosp/5eeb2a375157625e21d33d75d10574df

OP sugest a solution:

var express = require('express');
var expressWs = require('express-ws');
var expressWs = expressWs(express());
var app = expressWs.app;

app.use(express.static('public'));

var aWss = expressWs.getWss('/');

app.ws('/', function(ws, req) {
  console.log('Socket Connected');

  ws.onmessage = function(msg) {
    console.log(msg.data);
    aWss.clients.forEach(function (client) {
      client.send(msg.data);
    });
  };
});

app.listen(3444);

But as highlited in the issue ticket, getWss() does not support path/route as argument
https://github.com/HenningM/express-ws/blob/master/src/index.js#L80

A possible solution was discussed as follows:

app.ws('/', function(ws, req) {
  console.log('Socket Connected');

  ws.route = '/';  /* <- Your path */

  ws.onmessage = function(msg) {
    console.log(msg.data);
    
    Array.from(
      wss.getWss().clients
    ).filter((sock)=>{
      return sock.route == '/' /* <- Your path */
    }).forEach(function (client) {
      client.send(msg.data);
    });
  };
});

After investigation however; it does not seem like ws has any property route. Which renders this solution invalid for express-ws@latest

This issue was further discussed in a PR a while back
HenningM/express-ws#122

But has as of yet not been merged. The owner of the PR has a fork for the project, which might be worth looking into.
https://github.com/aral/express-ws

@Albin-Rohde
Copy link
Owner Author

express.ioa express version of socket.io seems like a more solid way forward. I will test how implementing that would work.

@Albin-Rohde Albin-Rohde added the bug Something isn't working label Feb 4, 2021
@Albin-Rohde Albin-Rohde mentioned this issue Feb 4, 2021
7 tasks
@Albin-Rohde
Copy link
Owner Author

express-ws will be replaced by socket.io and complimented by express-socket.io-session.

Why?

Issues with express-ws can be seen in this issue ticket.
socket.io is well maintained and has a lot of documentation.
express-socket.io-session the problem which could not be solved with express-ws; discussed in this ticket.

@Albin-Rohde
Copy link
Owner Author

Progess can now be tracked at #5

@Albin-Rohde
Copy link
Owner Author

solved with #5
closing.

Albin-Rohde added a commit that referenced this issue Mar 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant