javascript - AngularJS websockets too fast -


i using angularjs power webapp. i've decided introduce stomp on websockets in mix rabbitmq. 1 problem keep hitting messages sent backend @ high frequency angular fails trigger events losing data in process.

the application rather complex message might pass through following chain

  1. stompjs angular service wrapper. uses $rootscope.$broadcast notify components when new messages have arrived
  2. controller registers messages being broadcast on $rootscope. controller updates bindings or sends event component perform data updates.

as mentioned sometimes scopes don't updated properly, though events being sent , values being populated view not show updated values. have used $scope.$apply, $timeout, $scope.$digest , nothing seems work... example if have 2 packets coming 1 after , there's little or no delay between them on socket nothing happens if there's 1 same piece of code functions properly. how overcome ?

to further exemplify after questions posted users:

i take simple example: progress reports job runs on backend. tells me how many rows have been written in cassandra database. notifications {wrote: 8000, total: 4000000}. "wrote" being increased workers write in database , since there multiple workers these notifications pushed fast. have custom grid component written listens events grid:updatecell (which allows me update cell progress report). every time packet comes on socket broadcast event ($scope.$broadcast because grid child of page controller). have noticed not updates socket reflected in ui although event caught , grid event triggered successful update of data model not ui

it seems me might have few things going on here.

1) using $scope.$broadcast bubbles down scope quite heavy if have lots of nested scopes. prefer attach event listeners $rootscope , use $emit broadcast current scope , quicker. have below service this

    /**      * @ngdoc service      * @name somemodule.eventbus      * @requires $rootscope      *      * @description      * provides eventing mechanism when user cna broadcast , subscribe application wide events.      */     angular.module('somemodule').factory('eventbus', [         '$rootscope',         function ($rootscope) {             /**              * @ngdoc function              * @name subscribe              * @methodof somemodule.eventbus              *              * @description              * subscribes callback given application wide event              *              * @param {string} eventname name of event subscribe to.              * @param {function} callback callback fire when event raised.              * @return {function} function tht can called unsubscrive event.              */             var subscribe = function (eventname, callback) {                     return $rootscope.$on(eventname, callback);                 },                  /**                  * @ngdoc function                  * @name broadcast                  * @methodof somemodule.eventbus                  *                  * @description                  * broadcasts given event , data.                  *                  * @param {string} eventname name of event broadcast.                  * @param {object} data data object passed along event.                  */                 broadcast = function (eventname, data) {                     $rootscope.$emit(eventname, data);                 };              return {                 subscribe: subscribe,                 broadcast: broadcast             };         }     ]); 
  1. you might not firing update within angular digest cycle explain why getting updates. try doing update digest cycle (put function within $timeout block recommended way on $scope.$apply

    $timeout(function () { doupdate(datafrommessagebus); });

  2. if alot of messages should use debounce function see lodash-debouce. person can process in circa 200ms chunks updating every 200ms can take in. every second or in trading app etc.

(using lodash - debounce)

function (messagebus) {   var debouncefn = _.debounce(function () {       $timeout(function() {         doupdate(data);       });     }, 1000);      // assuming here socket / message bus stuff     messagebus.onmessagerecieved(function () {       //call debounce function call inner func every 1000ms       debouncefn();    });  } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -

jquery - Keeping Kendo Datepicker in min/max range -