javascript - How to send broadcast to all connected client in node js -


i'm newbie working application mean stack. iot based application , using nodejs backend.

i have scenario in have send broadcast each connected clients can open socket , can wait incoming data. unless web-browser can not perform event , till have gone through socket.io , express.io couldn't find can helpful achieve want send raw data open socket connections'

is there other node module achieve this. ?

here code using websocketserver,

    const express = require('express');     const http = require('http');     const url = require('url');     const websocket = require('ws');      const app = express();      app.use(function (req, res) {       res.send({ msg: "hello" });     });      const server = http.createserver(app);     const wss = new websocket.server({ server });     wss.on('connection', function connection(ws) {      ws.on('message', function(message) {        wss.broadcast(message);      }    }     wss.broadcast = function broadcast(msg) {      console.log(msg);      wss.clients.foreach(function each(client) {        client.send(msg);      });     };      server.listen(8080, function listening() {       console.log('listening on %d', server.address().port);     }); 

now, query when code executed,

wss.on('connection', function connection(ws) {     ws.on('message', function(message) {        wss.broadcast(message);     }  } 

var websocketserver = require("ws").server;  var wss = new websocketserver({port:8100});  wss.on('connection', function connection(ws) {     ws.on('message', function(message) {        wss.broadcast(message);     }  }  wss.broadcast = function broadcast(msg) {    console.log(msg);    wss.clients.foreach(function each(client) {        client.send(msg);     }); }; 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -