0

I have successfully opened a websocket connection in server to an external API to get exchange ticker data but not sure how to continuously push the data to client / AngularJS.

Here is the router code:

router.get('/live-ticker', function(req, res) {


  var autobahn = require('autobahn');
  var wsuri = "wss://api.poloniex.com";
  var connection = new autobahn.Connection({
    url: wsuri,
    realm: "realm1"
  });

  connection.onopen = function(session) {

    function tickerEvent(args, kwargs) {

      res.json(args);
      console.log(args);

    }


    session.subscribe('ticker', tickerEvent);

  }

  connection.onclose = function() {
    console.log("Websocket connection closed");
  }

  connection.open();

});

What I'm attemping to do is to have the data from tickerEvent live update the controller in the front end:

app.controller('liveTickerController', function($scope, $http) {

  $scope.ticker = [];

  var request = $http.get('/live-ticker');

  request.success(function(ticker) {
    console.log(ticker); //only logging data once

    $scope.liveTicker = ticker;
  });

  request.error(function(err) {
    console.log('Error: ' + err);
  });

});

How would get live updates pushed to client?

Thank you

u354356007
  • 3,024
  • 13
  • 24
chuckieDub
  • 1,487
  • 8
  • 22
  • 38

1 Answers1

2

you need to push data from the server periodically.

to get these "live updates" on the client side, a plain ajax call is not enough, you need a realtime communication.

You should use websoket even here, with socket.io and angular socket.io for example you can handle this communication easily

Karim
  • 7,479
  • 1
  • 18
  • 31