Socket.io Namespaces and Rooms - 401-advanced-javascript-davetrost/alchemy-fsjs-fall-2019 GitHub Wiki

Socket.IO Namespaces and Rooms

Reading Materials

Rooms and Namespaces

Namespaces

  • By default, Socket.IO creates a namespace "/". This follows common conventions from the way url paths are set up, but it has no correlation with the url.
  • All connections happen in the "/" namespace by default. New (custom) namespaces can be created if desired.
    • The io.of('/my-namespace') function creates and returns a separate namespace with the specified, custom name. This namespace must be connected to explicitly by the client.

Rooms

  • By default, Socket.IO creates a generic room inside each namespace. This room is named according to the id of the socket.
  • Additional rooms can be specified within a namespace. The socket.join('room-name') and socket.leave('room-name') functions control in which room the socket is sending and receiving information.

Remote Connections

  • Socket.IO has 2 modules that facilitate sending events to sockets from outside the context of the socket.IO processes.
    • socket.io-emitter allows you to communicate with socket.io servers easily from non-socket.io processes.
    • socket.io-redis allows you to run multiple socket.io instances in different processes or servers that can all broadcast and emit events to and from each other.