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!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With