This is what namespaces and rooms has in common (socket.io v0.9.8 - note that v1.0 has been completely rewritten , then some things may have changed):
- Both namespaces
io.of('/nsp')
and rooms socket.join('room')
are created server-side
- Multiple namespaces and multiple rooms share the same WebSocket
- The server will only send messages to the clients that have connected to an nsp / room, ie the filter is not just on the client
Differences :
-
Namespaces are connected to the client using
io.connect(urlAndNsp)
(the client will be added to the namespace only if it already exists on the server)
-
Rooms can only "enter" server-side (embedding by creating a server-side API allows the client to go directly)
- Namespaces may have secured authorization
-
Rooms does not support authorization , but a custom authorization can be added, easy-to-create API on the server, in case someone is in multiple rooms
-
Rooms are part of a namespace (global namespace default)
- Namespaces are always allocated in the global scope
In order not to confuse the concept with the name (room or namespace), I will use compartment to refer to the concept, and the other two names for imlpementación of the concept. Then:
- If you need authorization per compartment, namespaces should be the easiest way to go
- If you want to hierarchize the layers (2 layers max), use a namespace / room combo
- If your client-side app consists of different parts that (do not care about compartments, but) need to be separated from each other, use namespaces . li>
A final example would be a large client application where different modules, perhaps developed separately (for example, third parties), each using socket.io independently, are being used in the same application and want to share a single network connection .
Not really comparing this, it seems to me that if you just need simple compartments in your project to separate and group messages, either one will look good.
I'm not sure if this answers your question, but the research that led to this answer at least helped me see more clearly.
This answer is a free translation of Eugene Beresovsky's answer to a similar question in StackOverflow
sub>