Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

javascript - How to push live data from nodejs server to AngularJS?

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();

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...