I have created simple Angular app that uses Yahoo Finance to pull some currency data. However, at present, it pulls the data, but it is not until i reload the page that the currency changes, which is fine. I can create a simple $timeout around the $http.get, however, i would prefer to use SocketIO.
Here is the standard working $http.get: http://plnkr.co/edit/oiZ7JOASUbtLDPAQkZj2?p=preview
Here's is the start of the SocketIO app: http://plnkr.co/edit/dUEekn6kIJwLYxikWT9H?p=preview
However i am stuck as to where to go from here so that the currency data constantly updates.
Controller:
app.controller('MainCtrl', function($scope, yahooService) {
$scope.name = 'World';
yahooService.getData()
.success(function(data, status, headers, config) {
$scope.currencies = data;
})
.error(function (data, status, headers, config) {
$scope.currencies = 'There has been an error';
});
});
Service:
app.factory('yahooService', function($http) {
return {
getData: function() {
var url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDMXN%22%2C%20%22USDCHF%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
return $http.get(url);
}
};
});
SocketService:
app.factory('socket', function ($rootScope) {
var socket = io.connect('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDMXN%22%2C%20%22USDCHF%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=');
//var socket = io.connect();
return {
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
})
}
};
});
Any help would be appreciated.