node.js socket.io 房间或命名空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10930286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:50:44  来源:igfitidea点击:

socket.io rooms or namespacing?

node.jssocket.io

提问by Joseph

I am investigating nodejs/socket.io for real time chat, and I need some advice for implementing rooms.

我正在研究用于实时聊天的 nodejs/socket.io,我需要一些关于实现房间的建议。

Which is better, using namespace or using the room feature to completely isolate grops of chatters from each other?

哪个更好,使用命名空间或使用房间功能来完全隔离彼此之间的喋喋不休?

what is the real technical difference between rooms and namespace?

房间和命名空间之间的真正技术区别是什么?

Is there any resource usage difference?

是否有任何资源使用差异?

回答by Eugene Beresovsky

This is what namespaces and rooms have in common(socket.io v0.9.8 - please note that v1.0 involved a complete rewrite, so things might have changed):

这是命名空间和房间的共同点(socket.io v0.9.8 - 请注意 v1.0 涉及完全重写,所以事情可能已经改变):

  • Both namespaces (io.of('/nsp')) and rooms (socket.join('room')) are created on the server side
  • Multiple namespaces and multiple rooms share the same (WebSocket) connection
  • The server will transmit messages over the wire only to those clientsthat connected to / joined a nsp / room, i.e. it's not just client-side filtering
  • 命名空间(io.of('/nsp'))和房间(socket.join('room')都是在服务器端创建的
  • 多个命名空间和多个房间共享同一个 (WebSocket) 连接
  • 服务器将仅通过线路消息传输到连接到/加入 nsp/房间的那些客户端,即它不仅仅是客户端过滤

The differences:

差异

  • namespaces are connected to by the clientusing io.connect(urlAndNsp)(the client will be added to that namespace only if it already exists on the server)
  • rooms can be joined only on the server side(although creating an API on the server side to enable clients to join is straightforward)
  • namespaces can be authorization protected
  • authorization is not available with rooms, but custom authorization could be added to the aforementioned, easy-to-create API on the server, in case one is bent on using rooms
  • rooms are part of a namespace(defaulting to the 'global' namespace)
  • namespaces are always rooted in the global scope
  • 命名空间由客户端使用连接到io.connect(urlAndNsp)(只有当它已经存在于服务器上时,客户端才会被添加到该命名空间)
  • 房间只能在服务器端加入(尽管在服务器端创建一个 API 使客户端能够加入很简单)
  • 命名空间可以被授权保护
  • 授权不适用于房间,但可以将自定义授权添加到上述服务器上易于创建的 API 中,以防有人一心要使用房间
  • 房间是命名空间的一部分(默认为“全局”命名空间)
  • 命名空间总是植根于全局范围

To not confuse the concept with the name (room or namespace), I'll use compartmentto refer to the concept, and the other two names for the implementationsof the concept. So if you

为了不将概念与名称(房间或名称空间)混淆,我将使用隔间来指代该概念,另外两个名称用于指代该概念的实现。所以如果你

  • need per-compartment authorization, namespaces might be the easiest route to take
  • if you want hierarchically layered compartments(2 layers max), use a namespace/room combo
  • if your client-side app consists of different parts that (do not themselves care about compartments but) need to be separated from each other, use namespaces.
  • 需要每个隔间的授权,命名空间可能是最简单的方法
  • 如果您想要分层分层隔间(最多 2 层),请使用命名空间/房间组合
  • 如果您的客户端应用程序由不同的部分组成(它们本身并不关心隔间,但是)需要彼此分开,请使用命名空间。

An example for the latter would be a large client app where different modules, perhaps developed separately (e.g. third-party), each using socket.io independently, are being used in the same app and want to share a single network connection.

后者的一个例子是一个大型客户端应用程序,其中不同的模块,可能是单独开发的(例如第三方),每个模块都独立使用 socket.io,在同一个应用程序中使用并希望共享单个网络连接。

Not having actually benchmarked this, it seems to me if you just need simple compartments in your project to separate and group messages, either one is fine.

没有实际对此进行基准测试,在我看来,如果您只需要在项目中使用简单的隔间来分隔和分组消息,那么任何一个都可以。

Not sure if that answers your question, but the research leading up to this answer at least helped me see clearer.

不确定这是否能回答您的问题,但导致此答案的研究至少让我看得更清楚。

回答by Julio Garcia

It's an old question but after doing some research on the topic I find that the accepted answer is not clear on an important point. According to Guillermo Rauch himself (see link): although it is theoretically possible to create namespaces dynamically on a running app you use them mainly as predefined separate sections of you application. If, on the other hand you need to create ad hoc compartments, on the fly, to accommodate groups of users/connections, it is best to use rooms.

这是一个老问题,但在对该主题进行了一些研究后,我发现接受的答案在一个重要问题上并不清楚。根据 Guillermo Rauch 本人的说法(见链接):虽然理论上可以在正在运行的应用程序上动态创建命名空间,但您主要将它们用作应用程序的预定义单独部分。另一方面,如果您需要动态创建临时隔间以容纳用户/连接组,则最好使用房间。

回答by Julio Garcia

It depends what you wanna do.

这取决于你想做什么。

The main difference is that roomsare harder to implement. You must make a method for join the rooms with each page reload.

主要区别在于房间更难实施。您必须在每次重新加载页面时制定加入房间的方法。

With namespacesyou just need to write var example = io.connect('http://localhost/example');in your javascript clientand client are automatically added in the namespaces.

使用命名空间,您只需要var example = io.connect('http://localhost/example');javascript 客户端中编写 ,客户端就会自动添加到命名空间中。

Example of utilization:

使用示例:

  • rooms: private chat.
  • namespaces: the chat of the page.
  • 房间:私人聊天。
  • 命名空间:页面的聊天。

回答by zardilior

Rooms and namespaces segment communication and group individual sockets.

房间和命名空间将通信分段并将单个套接字分组。

A broadcast to a room or to a namespace will not reach everyone just the members.

对房间或命名空间的广播不会只到达成员。

The difference between namespaces and rooms is the following:

命名空间和房间之间的区别如下:

  • Namespaces: are managed in the frontend meaning the user, or an attacker, joins through the frontend and the joining and disconnecting is managed here.
  • Rooms: are managed in the backend, meaning the server assigns joining and leaving rooms.
  • 命名空间:在前端管理,这意味着用户或攻击者通过前端加入,加入和断开连接在这里管理。
  • 房间:在后端管理,意味着服务器分配加入和离开房间。

The difference is mainly who manages them

区别主要在于谁来管理它们

To decide what to use you must decide if the segmentation should be managed in the frontend or in the backend

要决定使用什么,您必须决定应该在前端还是在后端管理分段

回答by faridcs

Namespaces 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 /xxx 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.

这与 Socket.IO 命名空间应该有相同的思考过程。如果您正在构建模块化 Node Web 应用程序,您将需要命名不同的模块。如果您回顾一下我们的命名空间代码,您会发现我们能够在不同的命名空间中侦听完全相同的事件。在 Socket.IO 中,默认连接上的连接事件和 /xxx 命名空间上的连接事件是不同的。例如,如果您的站点上有一个聊天和评论系统,并且希望两者都是实时的,那么您可以为每个系统命名。这允许您构建仅存在于其自身上下文中的整个 Socket.IO 应用程序。

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.

如果您正在构建要打包和安装的东西,这也是正确的。您无法知道是否有人已经在使用默认命名空间中的某些事件,因此您应该创建自己的事件并在那里监听。这使您不会踩到使用您的软件包的任何开发人员的脚趾。

Namespaces allow us to carve up connections into different contexts. We can compare this to rooms, which allow us to group connections together.We can then have the same connection join other rooms, as well.

命名空间允许我们将连接划分到不同的上下文中。我们可以将其与房间进行比较,这允许我们将连接分组在一起。然后我们也可以将相同的连接加入其他房间。

Namespaces allow you to create different contexts for Socket.IO to work in. Rooms allow you to group client connections inside of those contexts.

命名空间允许您为 Socket.IO 工作创建不同的上下文。房间允许您在这些上下文中对客户端连接进行分组。