পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Wednesday, February 8, 2017

Socket.IO - Broadcasting

Broadcasting means sending a message to all connected clients. Broadcasting can be done at multiple levels. We can send the message to all connected clients, to clients on a namespace and clients in a particular room. The latter 2 would be covered in their respective chapters.

To broadcast an event to all the clients, We can use the io.sockets.emit method. Note that this will emit the event to ALL the connected clients(event the socket that might have fired this event). In this example we will broadcast the number of connected clients to all the users. Update your app.js file to incorporate the folllowing:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

var clients = 0;

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

http.listen(3000, function(){
  console.log('listening on localhost:3000');
});
On the client 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 4 clients, you will get the following result:
This was to send an event to everyone. Now if we want to send an event to everyone but the client that caused(in previous example it was caused by new clients on connecting), we can use the socket.broadcast.emit. Lets send the new user a welcome message and update the other clients about him/her joining. So in your app.js file, on connection of client send him a welcome message and broadcast connected client 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('index.html');
});

var clients = 0;

io.on('connection', function(socket){
  clients++;
socket.emit('newclientconnect',{ description: 'Hey, welcome!'});
socket.broadcast.emit('newclientconnect',{ description: clients + ' clients connected!'})
 socket.on('disconnect', function () {
        clients--;
  socket.broadcast.emit('newclientconnect',{ description: clients + ' clients connected!'})
             });
});

http.listen(3000, function(){
  console.log('listening 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('newclientconnect',function(data){
     document.body.innerHTML = '';
     document.write(data.description);
   });
 </script>
 <body>Hello world</body>
</html>
Now the newest client gets a welcome message and others get how many clients are connected currently to the server.

No comments:

Post a Comment