English 中文(简体)
Socket.IO - Broadcasting
  • 时间:2024-12-22

Socket.IO - Broadcasting


Previous Page Next Page  

Broadcasting means sending a message to all connected cpents. Broadcasting can be done at multiple levels. We can send the message to all the connected cpents, to cpents on a namespace and cpents in a particular room. To broadcast an event to all the cpents, we can use the io.sockets.emit method.

Note − This will emit the event to ALL the connected cpents (event the socket that might have fired this event).

In this example, we will broadcast the number of connected cpents to all the users. Update the app.js file to incorporate the following −


var app = require( express )();
var http = require( http ).Server(app);
var io = require( socket.io )(http);

app.get( / , function(req, res){
   res.sendFile( E:/test/index.html );
});
   
var cpents = 0;

io.on( connection , function(socket){
   cpents++;
   io.sockets.emit( broadcast ,{ description: cpents +   cpents connected! });
   socket.on( disconnect , function () {
      cpents--;
      io.sockets.emit( broadcast ,{ description: cpents +   cpents connected! });
   });
});

http.psten(3000, function(){
   console.log( pstening on localhost:3000 );
});

On the cpent side, we just need to handle the broadcast event −


<!DOCTYPE html>
<html>
   <head><title>Hello world</title></head>
   <script src="/socket.io/socket.io.js"></script>
   <script>
      var socket = io();
      socket.on( broadcast ,function(data){
         document.body.innerHTML =   ;
         document.write(data.description);
      });
   </script>
   <body>Hello world</body>
</html>

If you connect four cpents, you will get the following result −

Broadcast All

This was to send an event to everyone. Now, if we want to send an event to everyone, but the cpent that caused it (in the previous example, it was caused by new cpents on connecting), we can use the socket.broadcast.emit.

Let us send the new user a welcome message and update the other cpents about him/her joining. So, in your app.js file, on connection of cpent send him a welcome message and broadcast connected cpent number to all others.


var app = require( express )();
var http = require( http ).Server(app);
var io = require( socket.io )(http);

app.get( / , function(req, res){
   res.sendFile( E:/test/index.html );
});
var cpents = 0;

io.on( connection , function(socket){
   cpents++;
   socket.emit( newcpentconnect ,{ description:  Hey, welcome! });
   socket.broadcast.emit( newcpentconnect ,{ description: cpents +   cpents connected! })
   socket.on( disconnect , function () {
      cpents--;
      socket.broadcast.emit( newcpentconnect ,{ description: cpents +   cpents connected! })
   });
});
http.psten(3000, function(){
   console.log( pstening on localhost:3000 );
});

And your html to handle this event −


<!DOCTYPE html>
<html>
   <head><title>Hello world</title></head>
   <script src="/socket.io/socket.io.js"></script>
   <script>
      var socket = io();
      socket.on( newcpentconnect ,function(data){
               document.body.innerHTML =   ;
               document.write(data.description);
      });
   </script>
   <body>Hello world</body>
</html>

Now, the newest cpent gets a welcome message and others get how many cpents are connected currently to the server.

Advertisements