Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the better way to build real-time applications using Angular.js and Node.js?

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!

like image 583
Marco Godínez Avatar asked Dec 30 '12 08:12

Marco Godínez


People also ask

Why Angular is better than js?

JavaScript is both a server-side and client-side scripting language for building web applications. On the other hand, AngularJS makes web applications quick and straightforward from the start. JavaScript takes less time to patch bugs and defects on a wide scale.

Is NodeJS good for real-time applications?

Node. js is best suited for handling concurrent connections, making it an excellent framework to develop real-time as well as multi-user web apps.

What is difference between AngularJS and NodeJS?

AngularJS is a client-side framework. Node. js is a cross-platform runtime environment. AngularJS supports two-way data binding but cannot support database query writing features.

Which is better AngularJS or NodeJS?

Node. JS is a useful tool to build fast and scalable server-side networking applications while AngularJS is best suited for building single-page client-side web applications. Node. JS is an ideal language for developing small size projects, and AngularJS is an ideal language for creating highly interactive web apps.


1 Answers

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.

like image 188
Daniel Avatar answered Oct 14 '22 05:10

Daniel