📢 Lesson 2: Broadcasting

Learn how to send messages to multiple clients simultaneously

🎯 What You'll Learn

In this lesson, you'll master broadcasting techniques:

  • How to send messages to all connected clients
  • Broadcasting to everyone except the sender
  • Understanding different broadcasting patterns
  • Real-time message distribution

🔑 Broadcasting Concepts

  • Broadcasting: Sending a message to multiple clients at once
  • io.emit(): Sends to ALL connected clients (including sender)
  • socket.broadcast.emit(): Sends to ALL clients EXCEPT the sender
  • Real-time: Messages are delivered instantly to all recipients

👥 Users Online

0

Open multiple browser tabs to see broadcasting in action!

📤 Broadcasting to Everyone

Learn how to send a message that everyone (including you) will receive:

// Client-side: Send a broadcast message socket.emit('broadcast message', { message: 'Hello everyone!' }); // Server-side: Broadcast to all clients socket.on('broadcast message', (data) => { // Send to ALL connected clients (including sender) io.emit('broadcast message', { message: data.message, sender: socket.id, timestamp: new Date().toISOString() }); });

Broadcast to Everyone

📤 Broadcasting to Others Only

Learn how to send a message to everyone EXCEPT yourself:

// Client-side: Send a message to others socket.emit('broadcast to others', { message: 'Hello others!' }); // Server-side: Broadcast to all clients except sender socket.on('broadcast to others', (data) => { // Send to ALL clients EXCEPT the sender socket.broadcast.emit('broadcast message', { message: data.message, sender: socket.id, timestamp: new Date().toISOString() }); });

Broadcast to Others Only

📢 io.emit()

Sends to ALL connected clients including the sender

io.emit('event', data);
📤 socket.broadcast.emit()

Sends to ALL clients EXCEPT the sender

socket.broadcast.emit('event', data);

🔍 Understanding the Difference

Let's compare the two broadcasting methods:

// Method 1: Send to everyone (including sender) io.emit('message', data); // Result: All clients receive the message // Method 2: Send to everyone except sender socket.broadcast.emit('message', data); // Result: All clients EXCEPT the sender receive the message

💡 When to Use Each Method

  • io.emit(): Use when you want everyone (including sender) to see the message
  • socket.broadcast.emit(): Use when you want to notify others but not the sender
  • Example: Chat messages vs. "user joined" notifications

🎯 Practice Exercise

Try both broadcasting methods and observe the differences:

Interactive Broadcasting Test

Open multiple browser tabs and try these scenarios:

  1. Send a broadcast message and see it appear in all tabs
  2. Send a "to others" message and notice it doesn't appear in the sender's tab
  3. Observe how the user count changes as you open/close tabs

✅ What You've Learned

  • ✅ How to broadcast messages to all connected clients
  • ✅ How to send messages to everyone except the sender
  • ✅ Understanding the difference between io.emit() and socket.broadcast.emit()
  • ✅ Real-time message distribution patterns
  • ✅ When to use each broadcasting method
Next Lesson: User Management →