O'Reilly logo

Socket.IO Cookbook by Tyson Cadenhead

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sending messages to all the sockets, except for the sender

When a socket sends a message, we don't necessarily want it to receive the message that it sent. We may want to display a different message to the sender than to the receivers. This can be accomplished using the socket.broadcast.emit() syntax.

How to do it…

To send messages to every socket, except for the sender, follow these steps:

  1. First, create a template for sockets to announce their presence. This will also include a "messages" container. Here, we will render incoming messages:
    <div class="container"> <hr /> <form id="form" class="row"> <div class="col-md-10"> <input class="form-control" id="name" placeholder="Who are you?" /> </div> <div class="col-md-2"> <button class="btn-primary form-control" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required