Which is the better way to build real-time applica

2019-02-03 11:41发布

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.

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

function MyController($scope, socket) {

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

}

My controller using Angular.js (RESTful alternative):

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条回答
叼着烟拽天下
2楼-- · 2019-02-03 12:18

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.

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.

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.

查看更多
乱世女痞
3楼-- · 2019-02-03 12:20

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.

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

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.

查看更多
戒情不戒烟
4楼-- · 2019-02-03 12:22

Socket.io has the following advantages:

  • less unnecessary trafic and rendering
  • lower latency
  • (arguably) cleaner code

REST has these advantages:

  • 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.

查看更多
5楼-- · 2019-02-03 12:25

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

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.

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.

查看更多
贪生不怕死
6楼-- · 2019-02-03 12:26

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

查看更多
登录 后发表回答