Namespaces

A Namespace is a communication channel that allows you to split the logic of your application over a single shared connection (also called “multiplexing”).

Namespace diagram

Introduction

Each namespace has its own:

  1. io.of("/orders").on("connection", (socket) => {
  2. socket.on("order:list", () => {});
  3. socket.on("order:create", () => {});
  4. });
  5. io.of("/users").on("connection", (socket) => {
  6. socket.on("user:list", () => {});
  7. });
  1. const orderNamespace = io.of("/orders");
  2. orderNamespace.on("connection", (socket) => {
  3. socket.join("room1");
  4. orderNamespace.to("room1").emit("hello");
  5. });
  6. const userNamespace = io.of("/users");
  7. userNamespace.on("connection", (socket) => {
  8. socket.join("room1"); // distinct from the room in the "orders" namespace
  9. userNamespace.to("room1").emit("holà");
  10. });
  1. const orderNamespace = io.of("/orders");
  2. orderNamespace.use((socket, next) => {
  3. // ensure the socket has access to the "orders" namespace, and then
  4. next();
  5. });
  6. const userNamespace = io.of("/users");
  7. userNamespace.use((socket, next) => {
  8. // ensure the socket has access to the "users" namespace, and then
  9. next();
  10. });

Possible use cases:

  • you want to create a special namespace that only authorized users have access to, so the logic related to those users is separated from the rest of the application
  1. const adminNamespace = io.of("/admin");
  2. adminNamespace.use((socket, next) => {
  3. // ensure the user has sufficient rights
  4. next();
  5. });
  6. adminNamespace.on("connection", socket => {
  7. socket.on("delete user", () => {
  8. // ...
  9. });
  10. });
  • your application has multiple tenants so you want to dynamically create one namespace per tenant
  1. const workspaces = io.of(/^\/\w+$/);
  2. workspaces.on("connection", socket => {
  3. const workspace = socket.nsp;
  4. workspace.emit("hello");
  5. });

Main namespace

Until now, you interacted with the main namespace, called /. The io instance inherits all of its methods:

  1. io.on("connection", (socket) => {});
  2. io.use((socket, next) => { next() });
  3. io.emit("hello");
  4. // are actually equivalent to
  5. io.of("/").on("connection", (socket) => {});
  6. io.of("/").use((socket, next) => { next() });
  7. io.of("/").emit("hello");

Some tutorials may also mention io.sockets, it’s simply an alias for io.of("/").

  1. io.sockets === io.of("/")

Custom namespaces

To set up a custom namespace, you can call the of function on the server-side:

  1. const nsp = io.of("/my-namespace");
  2. nsp.on("connection", socket => {
  3. console.log("someone connected");
  4. });
  5. nsp.emit("hi", "everyone!");

Client initialization

Same-origin version:

  1. const socket = io(); // or io("/"), the main namespace
  2. const orderSocket = io("/orders"); // the "orders" namespace
  3. const userSocket = io("/users"); // the "users" namespace

Cross-origin/Node.js version:

  1. const socket = io("https://example.com"); // or io("https://example.com/"), the main namespace
  2. const orderSocket = io("https://example.com/orders"); // the "orders" namespace
  3. const userSocket = io("https://example.com/users"); // the "users" namespace

In the example above, only one WebSocket connection will be established, and the packets will automatically be routed to the right namespace.

Please note that multiplexing will be disabled in the following cases:

  • multiple creation for the same namespace
  1. const socket1 = io();
  2. const socket2 = io(); // no multiplexing, two distinct WebSocket connections
  • different domains
  1. const socket1 = io("https://first.example.com");
  2. const socket2 = io("https://second.example.com"); // no multiplexing, two distinct WebSocket connections
  1. const socket1 = io();
  2. const socket2 = io("/admin", { forceNew: true }); // no multiplexing, two distinct WebSocket connections

Dynamic namespaces

It is also possible to dynamically create namespaces, either with a regular expression:

  1. io.of(/^\/dynamic-\d+$/);

or with a function:

  1. io.of((name, auth, next) => {
  2. next(null, true); // or false, when the creation is denied
  3. });

You can have access to the new namespace in the connection event:

  1. io.of(/^\/dynamic-\d+$/).on("connection", (socket) => {
  2. const namespace = socket.nsp;
  3. });

The return value of the of() method is what we call the parent namespace, from which you can:

  1. const parentNamespace = io.of(/^\/dynamic-\d+$/);
  2. parentNamespace.use((socket, next) => { next() });

The middleware will automatically be registered on each child namespace.

  1. const parentNamespace = io.of(/^\/dynamic-\d+$/);
  2. parentNamespace.emit("hello"); // will be sent to users in /dynamic-1, /dynamic-2, ...

Complete API

The complete API exposed by the Namespace instance can be found here.