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!
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.
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.
Socket.io has the following advantages:
REST has these advantages:
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.
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.
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