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

[ASK] How to use broadcast server API #46

Open
prakasa1904 opened this issue Nov 12, 2018 · 2 comments
Open

[ASK] How to use broadcast server API #46

prakasa1904 opened this issue Nov 12, 2018 · 2 comments

Comments

@prakasa1904
Copy link

I cant using broadcast API from here. Any one know how to use brodcast to send message to all clients connected ?

@noru
Copy link

noru commented Jan 3, 2019

seems this lib is not exposing related api from ws

@Joey-Wong
Copy link

we can write like this:

app.ws.use((ctx) => {

  /*
   * `ctx` is the regular koa context created from the `ws`
   * onConnection `socket.upgradeReq` object.
   */
  ctx.websocket.broadcast = function(data) {
    app.ws.server.clients.forEach(function each(client) {
      client.send(data);
    });
  };

  // the websocket is added to the context on `ctx.websocket`.
  ctx.websocket.on('message', function(message) {
    // do something with the message from client
    ctx.websocket.broadcast('something');
  });
});

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

No branches or pull requests

3 participants