Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send broadcast to all connected client in node js

I'm a newbie working with an application with MEAN stack. It is an IoT based application and using nodejs as a backend.

I have a scenario in which I have to send a broadcast to each connected clients which can only open the Socket and can wait for any incoming data. unless like a web-browser they can not perform any event and till now I have already gone through the Socket.IO and Express.IO but couldn't find anything which can be helpful to achieve what I want send raw data to open socket connections'

Is there any other Node module to achieve this. ?

Here is the 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, my query is when this code will be executed,

wss.on('connection', function connection(ws) {
    ws.on('message', function(message) {
       wss.broadcast(message);
    }
 }
like image 661
Darshan Soni Avatar asked Jul 25 '17 11:07

Darshan Soni


People also ask

How do you emit to all clients in socket IO?

To broadcast an event to all the clients, we can use the io. sockets. emit method. Note − This will emit the event to ALL the connected clients (event the socket that might have fired this event).

How do I broadcast a message in Socketio?

To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it. var io = require('socket.io'). listen(80); io.

How many concurrent connections can node js handle?

JS uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads.

How do you send a header in node JS?

We will use request. setHeader() to set header of our request. The header tells the server details about the request such as what type of data the client, user, or request wants in the response. Type can be html , text , JSON , cookies or others.


1 Answers

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);
    });
};
like image 143
raj peer Avatar answered Oct 14 '22 04:10

raj peer