koa-websocket
koa-websocket copied to clipboard
[ASK] How to use broadcast server API
I cant using broadcast API from here. Any one know how to use brodcast to send message to all clients connected ?
seems this lib is not exposing related api from ws
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');
});
});