Socket.io Namespaces and Rooms - mwilkin-401-advanced-javascript/bend-javascript-401d2 GitHub Wiki
Socket.io allows you to assign different endpoints or paths, which they refer to as to “namespace” your sockets.
Advantages:
- Minimizes the number of resources (TCP connections) and reduces the number of concerns which provides for separate between communication channels.
- Multiple namespaces share the same WebSockets connection thereby saving socket ports on the server.
The default namespace is /
which clients connect to by default and which the server listens to by default.
Each namespace emits a connection event that receives each Socket instance as a parameter.
Customized Namespaces can be configured as needed.
Rooms or arbitrary channels can be defined which sockets can joined and leave. Upon disconnection, all the sockets automatically leave the channels they were subscribed to; not special operations or teardown are needed.
To emit events to sockets in Socket.io from the outside world, a separate channel can be implemented to send messages into the process. These allow you to emit messages from any process to any channel.
There are two different modules are available to assist this process:
- Socket.io-redis
- Socket.io-emitter