Skip to content

SOCKET.IO Namespace & Room

Wei edited this page Apr 11, 2019 · 1 revision

Namespace

Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths.

Default namespace

We call the default namespace / and it’s the one Socket.IO clients connect to by default, and the one the server listens to by default.

  • This namespace is identified by io.sockets or simply io.
  • Each namespace emits a connection event that receives each Socket instance as a parameter

Custom namespaces

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

Rooms

Within each namespace, you can also define arbitrary channels that sockets can join and leave.

Joining and leaving

You can call join to subscribe the socket to a given channel. And then simply use to or in (they are the same) when broadcasting or emitting. To leave a channel you call leave in the same fashion as join.

Default room

Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id.

Disconnection

Upon disconnection, sockets leave all the channels they were part of automatically, and no special teardown is needed on your part.

Clone this wiki locally