-1

I want to read the App.net public stream. However when I use a simple $http.get() I only get one response.

$http
  .get('https://alpha-api.app.net/stream/0/posts/stream/global')
  .success(function (results) {
    console.log('received results', results);
  })
;

How do I connect to http streams in AngularJS?
Is there even a way or would I have to use something like WebSockets?

Jeremy
  • 1
  • 85
  • 340
  • 366
nocksock
  • 5,369
  • 6
  • 38
  • 63
  • 1
    *rtm*, the structure is: `{data: [...]}`, in your case: `results.data` will contain 20 entries. – Yoshi Mar 26 '13 at 11:33
  • @Yoshi that ist correct and I can see that easily in the console. However, I'd have to make another `$http.get();` in order to fetch new posts - but I want to access the http stream and rather have long polling instead. – nocksock Mar 26 '13 at 11:43
  • I think that's more of a question regarding the api you're using. In the docs, I don't see where it says that any long polling would be possible. Simply *Return the 20 most recent Posts from the Global stream.*. – Yoshi Mar 26 '13 at 11:51
  • @Yoshi oic. Seems like the docs use the term 'stream' ambiguously. I assume I was looking in the wrong spot. Will update my question if appropriate - or answer it. Thanks for pointing that out. – nocksock Mar 26 '13 at 12:01

1 Answers1

1

$http is a single request only. For long polling, call it periodically with $interval.

function MyCtrl($scope, $timeout) {
  function poll() {
    $http.get('...').success(function(data) {
      $scope.posts = data;
    });
    $timeout(poll, 10000);
  }

  poll();
}

You could also create a service to encapsulate this logic, but you would need a fixed data structure to be updated, instead of changing the result. Something like this.

Community
  • 1
  • 1
Caio Cunha
  • 23,326
  • 6
  • 78
  • 74