哪个是使用Angular.js和Node.js构建实时应用程序的更好方法?

时间:2021-08-09 12:36:42

I'm beginner in Angular.js and Node.js, but I've realized that there are two possible ways to make real-time applications. The first is using Socket.io and the other is using RESTful with setInterval() function as a client-side solution. I built my application using both alternatives, but I don't know why it is better to use one instead the other.

我是Angular.js和Node.js的初学者,但我意识到有两种可能的方法来制作实时应用程序。第一个是使用Socket.io,另一个是使用RESTful和setInterval()函数作为客户端解决方案。我使用两种替代方案构建了我的应用程序,但我不知道为什么使用一个替代另一个更好。

My controller using Angular.js (Socket.io alternative):

我的控制器使用Angular.js(Socket.io替代):

function MyController($scope, socket) {

  socket.on('test', function(data){
    $scope.data = data;
    console.log($scope.data);
  });

}

My controller using Angular.js (RESTful alternative):

我的控制器使用Angular.js(RESTful替代):

function MyController($scope, $http) {

  setInterval(function() {
    $http.get('/test.json')
         .success(function(data, status, headers, config) {
           $scope.data = data;
           console.log($scope.data);
         });
  }, 1000);

}

What would be the differences between these ways of doing things? Thanks in advance!

这些做事方式之间会有什么不同?提前致谢!

5 个解决方案

#1


8  

If you want a fully real-time web application, then sockets are the way to go. Socket.io or SockJS are both extremely good clients. They have the ability to degrade gracefully when web sockets aren't supported, though, you may choose which transportation method you'd like to use.

如果您想要一个完全实时的Web应用程序,那么套接字是可行的方法。 Socket.io或SockJS都是非常好的客户。当不支持Web套接字时,它们可以优雅地降级,但是,您可以选择要使用的传输方法。

You'll have to build a data subscription service for changes to be propagated between all users. Tower.js and Meteor both use a reactive approach, and they use event listeners on model changes. Depending on how complex, or how powerful you want this feature, they'll be different implementations available.

您必须构建数据订阅服务,以便在所有用户之间传播更改。 Tower.js和Meteor都使用被动方法,他们在模型更改时使用事件监听器。根据您希望此功能的复杂程度或强大程度,它们将是不同的可用实现。

It does become increasingly more complex when trying to sync client-side and server-side data across many users connected at once. I'd suggest you take a look at those two frameworks, see how they work, and possibly replicate parts of it, or all of it's functionality.

尝试在一次连接的多个用户之间同步客户端和服务器端数据时,它确实变得越来越复杂。我建议你看看这两个框架,看看它们是如何工作的,并且可能复制它的一部分,或者它的所有功能。

#2


6  

Based on your use case, I think Socket.IO is the way to go. However, there are a few caveats to using WebSockets with Angular. I recommend you take a look at a blog post I wrote on the subject a while ago: http://briantford.com/blog/angular-socket-io.html

根据您的用例,我认为Socket.IO是要走的路。但是,使用带有Angular的WebSockets有一些注意事项。我建议你看看我刚才写的关于这个主题的博客文章:http://briantford.com/blog/angular-socket-io.html

#3


3  

We had to choose from an alternative between pusher (using Websocket) and Pubnub which uses Ajax for publish/subscribe real time events. Your Angular RESTful alternative is not enough when trying to do realtime communication across different users of the application. For example, you have a project management application used by a team. One team member might be adding/updating a task while another might be looking at the same time. The update needs to be published and all other user who are currently logged in will be subscribed for the changed event and can be notified.

我们不得不选择推送器(使用Websocket)和使用Ajax进行发布/订阅实时事件的Pubnub之间的替代方案。当尝试跨应用程序的不同用户进行实时通信时,您的Angular RESTful替代方案是不够的。例如,您有一个团队使用的项目管理应用程序。一个团队成员可能正在添加/更新任务,而另一个团队成员可能正在同时查看。需要发布更新,并且当前登录的所有其他用户将订阅已更改的事件并可以通知。

We've been using Pubnub and it works very fast although Pusher's technology is better but not supported by all browsers currently.

我们一直在使用Pubnub并且它的工作速度非常快,尽管Pusher的技术更好但目前并不是所有浏览器都支持。

I know the question is for AJ and NodeJS but i feel that using a third party subscription/publishing API makes it easier to manage because you'll not have to manage the nodejs server and the bigger load (when your app goes popular). Pusher/Pubnub is scalable and you can scale your app as far as you want.

我知道问题出在AJ和NodeJS上,但我觉得使用第三方订阅/发布API可以更容易管理,因为您不必管理nodejs服务器和更大的负载(当您的应用程序流行时)。 Pusher / Pubnub是可扩展的,您可以根据需要扩展您的应用程序。

#4


2  

It's better to use Socket.io in your case.

在你的情况下使用Socket.io会更好。

Because you seem to do lots of interaction with the backend.If it's like that instead of querying the api in intervals just use Socket.io.

因为你似乎与后端进行了大量的交互。如果它是这样的,而不是间隔查询api只需使用Socket.io。

Using socket will reduce the work on you both back end and client side and also will make it much more easier to control your event based stuff.

使用套接字将减少后端和客户端的工作,并且还可以更容易地控制基于事件的内容。

#5


2  

Socket.io has the following advantages:

Socket.io具有以下优点:

  • less unnecessary trafic and rendering
  • 减少不必要的交通和渲染
  • lower latency
  • 降低延迟
  • (arguably) cleaner code
  • (可以说)清洁代码

REST has these advantages:

REST具有以下优点:

  • Supported on all browsers and clients
  • 支持所有浏览器和客户端
  • Less open connections
  • 较少开放的连接
  • Works better in clustered, proxied and otherwise complex network topologies
  • 在群集,代理和其他复杂的网络拓扑中更好地工作

Each of these points deserves a long discussion on it's own, some depend a lot on the application characteristics. But if you tag them by priority, you'll see what is probably best for you.

这些要点中的每一点都值得长期讨论,有些依赖于应用程序的特性。但如果你按优先级标记它们,你会看到最适合你的东西。

#1


8  

If you want a fully real-time web application, then sockets are the way to go. Socket.io or SockJS are both extremely good clients. They have the ability to degrade gracefully when web sockets aren't supported, though, you may choose which transportation method you'd like to use.

如果您想要一个完全实时的Web应用程序,那么套接字是可行的方法。 Socket.io或SockJS都是非常好的客户。当不支持Web套接字时,它们可以优雅地降级,但是,您可以选择要使用的传输方法。

You'll have to build a data subscription service for changes to be propagated between all users. Tower.js and Meteor both use a reactive approach, and they use event listeners on model changes. Depending on how complex, or how powerful you want this feature, they'll be different implementations available.

您必须构建数据订阅服务,以便在所有用户之间传播更改。 Tower.js和Meteor都使用被动方法,他们在模型更改时使用事件监听器。根据您希望此功能的复杂程度或强大程度,它们将是不同的可用实现。

It does become increasingly more complex when trying to sync client-side and server-side data across many users connected at once. I'd suggest you take a look at those two frameworks, see how they work, and possibly replicate parts of it, or all of it's functionality.

尝试在一次连接的多个用户之间同步客户端和服务器端数据时,它确实变得越来越复杂。我建议你看看这两个框架,看看它们是如何工作的,并且可能复制它的一部分,或者它的所有功能。

#2


6  

Based on your use case, I think Socket.IO is the way to go. However, there are a few caveats to using WebSockets with Angular. I recommend you take a look at a blog post I wrote on the subject a while ago: http://briantford.com/blog/angular-socket-io.html

根据您的用例,我认为Socket.IO是要走的路。但是,使用带有Angular的WebSockets有一些注意事项。我建议你看看我刚才写的关于这个主题的博客文章:http://briantford.com/blog/angular-socket-io.html

#3


3  

We had to choose from an alternative between pusher (using Websocket) and Pubnub which uses Ajax for publish/subscribe real time events. Your Angular RESTful alternative is not enough when trying to do realtime communication across different users of the application. For example, you have a project management application used by a team. One team member might be adding/updating a task while another might be looking at the same time. The update needs to be published and all other user who are currently logged in will be subscribed for the changed event and can be notified.

我们不得不选择推送器(使用Websocket)和使用Ajax进行发布/订阅实时事件的Pubnub之间的替代方案。当尝试跨应用程序的不同用户进行实时通信时,您的Angular RESTful替代方案是不够的。例如,您有一个团队使用的项目管理应用程序。一个团队成员可能正在添加/更新任务,而另一个团队成员可能正在同时查看。需要发布更新,并且当前登录的所有其他用户将订阅已更改的事件并可以通知。

We've been using Pubnub and it works very fast although Pusher's technology is better but not supported by all browsers currently.

我们一直在使用Pubnub并且它的工作速度非常快,尽管Pusher的技术更好但目前并不是所有浏览器都支持。

I know the question is for AJ and NodeJS but i feel that using a third party subscription/publishing API makes it easier to manage because you'll not have to manage the nodejs server and the bigger load (when your app goes popular). Pusher/Pubnub is scalable and you can scale your app as far as you want.

我知道问题出在AJ和NodeJS上,但我觉得使用第三方订阅/发布API可以更容易管理,因为您不必管理nodejs服务器和更大的负载(当您的应用程序流行时)。 Pusher / Pubnub是可扩展的,您可以根据需要扩展您的应用程序。

#4


2  

It's better to use Socket.io in your case.

在你的情况下使用Socket.io会更好。

Because you seem to do lots of interaction with the backend.If it's like that instead of querying the api in intervals just use Socket.io.

因为你似乎与后端进行了大量的交互。如果它是这样的,而不是间隔查询api只需使用Socket.io。

Using socket will reduce the work on you both back end and client side and also will make it much more easier to control your event based stuff.

使用套接字将减少后端和客户端的工作,并且还可以更容易地控制基于事件的内容。

#5


2  

Socket.io has the following advantages:

Socket.io具有以下优点:

  • less unnecessary trafic and rendering
  • 减少不必要的交通和渲染
  • lower latency
  • 降低延迟
  • (arguably) cleaner code
  • (可以说)清洁代码

REST has these advantages:

REST具有以下优点:

  • Supported on all browsers and clients
  • 支持所有浏览器和客户端
  • Less open connections
  • 较少开放的连接
  • Works better in clustered, proxied and otherwise complex network topologies
  • 在群集,代理和其他复杂的网络拓扑中更好地工作

Each of these points deserves a long discussion on it's own, some depend a lot on the application characteristics. But if you tag them by priority, you'll see what is probably best for you.

这些要点中的每一点都值得长期讨论,有些依赖于应用程序的特性。但如果你按优先级标记它们,你会看到最适合你的东西。