Building Scalable Apps with Redis and Node.js
上QQ阅读APP看书,第一时间看更新

Using namespaces or rooms

We have now used both and can see that rooms and namespaces in Socket.IO are really similar. They are both ways of segmenting all the connections into groups. This leads us to the question, when do we use either of them?

Namespaces

JavaScript is a scripted language that executes in essentially one context. If you create a function in a different file (without using any module or closure system), it is created in the global scope. Identically named functions will overwrite each other. If you have used any strongly typed object-oriented language (C#.NET or Java are two examples), you will have seen and used namespaces. Namespaces in those languages allow you to create objects with the same name, but they would be separate as they will live in different namespaces, otherwise known as scopes.

This is the same thought process you should have with Socket.IO namespaces. If you are building a modular Node web application, you will want to namespace out the different modules. If you look back at our namespace code, you will see that we were able to listen for the same exact events in different namespaces. In Socket.IO, the connection event on the default connection and connection event on a /vip namespace are different. For example, if you had a chat and comment system on your site and wanted both to be real time, you could namespace each. This allows you to build an entire Socket.IO application that lives only in its own context.

This would also be true if you were building something to be packaged and installed. You cannot know if someone is already using certain events in the default namespace, so you should create your own and listen there. This allows you to not step on the toes of any developer who uses your package.

Finding namespaces

Now that we know how and why we use namespaces, let's look at namespaces so we can see what is going on. The following is the screenshot of the io.nsps object from our namespaces object:

We can see that each attribute of this object ties to a namespace. There is the default namespace and the /vip namespace. I have expanded the /vip, so you can see its attributes. The two attributes of note are the name, which is the string that we pass in when we first create it (io.of('/vip')), and the other is the connected object. This has all our sockets that are currently connected. The connections are mapped based on their socket ID.

When to use rooms

Namespaces allow us to carve up connections into different contexts. We can compare this to rooms, which allow us to group connections together. Everyone that connects joins the default '' (empty string) room by default. We can then have the same connection join other rooms, as well.

Finding rooms

We should think of rooms as hashes of connections because that is exactly what they are! There is a rooms object right off of io.sockets.adapter that has each room and the clients that are in that room.

The objects may look a little weird because they say they are an array of 0. This is correct, as each socket is attached as a property of the array and not as a member. A little confusing, but it does allow us to use the array like a hash.