Categories
JavaScript Answers

How to broadcast messages on a namespace with Node socket.io?

Spread the love

To broadcast messages on a namespace with Node socket.io, we call broadcast.emit.

For instance, we write

chat.on("connection", (socket) => {
  socket.on("message", (msg) => {
    socket.emit(msg);
    socket.broadcast.emit(msg);
  });
});

to call broadcast.emit to broadcast the msg string to all clients.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *