简体   繁体   English

如何将实时数据从Node.js服务器推送到AngularJS?

[英]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. 我已成功打开服务器中与外部API的websocket连接,以获取交换行情自动收录器数据,但不确定如何将数据连续推送到客户端/ 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: 我想做的是让tickerEvent的数据实时更新前端的控制器:

 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 谢谢

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. 为了在客户端获得这些“实时更新”,仅进行ajax调用是不够的,您需要实时通信。

You should use websoket even here, with socket.io and angular socket.io for example you can handle this communication easily 即使在这里,您也应该使用websoket,例如,使用socket.ioangular socket.io即可轻松处理此通信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM