wss.on("connection", function(ws) { ws.on("message", function(message) { if (message === 'exit') { ws.close(); } else { wss.clients.forEach(function(client) { client.send(message); }); } }); ws.send("Welcome to cyber chat"); });
wss.clients.forEach((client) => { try { client.send(message, (error) => { if (typeof error !== "undefined") { app.logger.error(`Send Error: ${error.toString()}`) } }) } catch (e) { app.logger.error(`Broadcast Error: ${e.toString()}`) } })
function emitUpdate(eventData, options = {}) { if (options.excludeSocket) { return socketServer.clients.forEach(socket => { if (socket !== options.excludeSocket) socket.send(JSON.stringify(eventData)); }); } if (options.targetSocket) { return options.targetSocket.send(JSON.stringify(eventData)); } return socketServer.clients.forEach(socket => socket.send(JSON.stringify(eventData))); }
wss.on('connection', (client) => { console.log('SERVER:: client connected') client.on('message', (data) => { console.log('SERVER:: message recieved => ', data); console.log('SERVER:: broadcast message to all clients'); wss.clients.forEach((c) => { c.send(data); }); }); client.send('CONNECTED'); });
// Check for died connections at regular intervals. setInterval(function () { wsServer.clients.forEach(function (connection) { if (connection.isAlive === false) { console.log("Connection died", connection.id); return connection.terminate(); } // Request the client to respond with pong. Client does this automatically. connection.isAlive = false; connection.ping(function () {}); }); }, 30000);
// Broadcast stream to client (front-end). function clientBroadcast (data, settings) { stream_client_server.clients.forEach((client) => { if (client.readyState !== WebSocket.OPEN) { return; } // If this client's stream id and token match, broadcast. if (client.stream_id === settings.stream_id && client.stream_token === settings.stream_token) { client.send(data); } }); }
// Listens messages and broadcasts it to all clients redisSub.on("message", (channel, msg) => { wss.clients.forEach(client => { client.send(msg); }); });
function broadcast(type="log", data, filter=()=>true) { wss.clients.forEach(function (client) { if (client.readyState === WebSocket.OPEN && filter(client)) { client.send(obj(type, data)); } }); }
/** * Iterate through clients and send passed msg */ const broadcast = msg => { wss.clients.forEach(client => { client.send(msg); }); }
// Broadcast to all connected clients const broadcast = (data, ws) => { wss.clients.forEach(client => { if (client.readyState === WebSocket.OPEN && client !== ws) { client.send(JSON.stringify(data)); } }); }
wss.clients.forEach((client) => { try { client.send(data); } catch (e) { app.logger.error(`Broadcast Error: ${e.toString()}`) } })
socketServer.clients.forEach(function each(client) { if (client.readyState === WebSocket.OPEN) { client.send(data); } });
function broadcast(data) { wss.clients.forEach((client) => { if (client.readyState == WebSockets.OPEN) { client.send(JSON.stringify(data)) } }) }