Sometimes, we want to broadcast messages on a namespace with socket.io and Node.js.
In this article, we’ll look at how to broadcast messages on a namespace with socket.io and Node.js.
How to broadcast messages on a namespace with socket.io and Node.js?
To broadcast messages on a namespace with socket.io and Node.js, we can use the of
method.
For instance, we write
const io = require("socket.io").listen(80);
io.of("/admins").emit("message", { message: "Hello admins!" });
to call io.of
with the namespace to broadcast to.
And then we call emit
to send the message to that namespace.
Conclusion
To broadcast messages on a namespace with socket.io and Node.js, we can use the of
method.