Categories
JavaScript Answers

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

Spread the love

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.

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 *