{"id":2164,"date":"2022-08-30T15:22:35","date_gmt":"2022-08-30T15:22:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/angularjs-phonegap-android-using-websockets-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:35","modified_gmt":"2022-08-30T15:22:35","slug":"angularjs-phonegap-android-using-websockets-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/angularjs-phonegap-android-using-websockets-collection-of-common-programming-errors\/","title":{"rendered":"Angularjs Phonegap Android using Websockets-Collection of common programming errors"},"content":{"rendered":"<p>I don&#8217;t even know how to explain this directly, but I&#8217;ll try.<\/p>\n<p><strong>Intro<\/strong><\/p>\n<p>I&#8217;m building a Phonegap App with angularjs, and I&#8217;m trying to stitch up WebSocket messages to update my UI. I used to have services that communicate with the server periodically and change their data accordingly and it was working pretty well. An example:<\/p>\n<p><em>Service1.js<\/em>:<\/p>\n<pre><code>     var applyUpdate = function (\n         angular.extend(instance, data);\n         if (!$rootScope.$$phase) $rootScope.$apply();\n     };\n\n     this.update = function () {\n         DataProvider.get('getThermostatSettings', {}, applyUpdate, function(){}, true);\n     }\n<\/code><\/pre>\n<p>So basically I was simply calling the &#8220;update&#8221; function every 5 seconds, receiving data from the server and updating this service. The controllers would then simply access this service, and everything was working.<\/p>\n<p><strong>The problem<\/strong><\/p>\n<p>The problem now, is that I stitched up a WebSocket Interface written in java, that handles all the websocket implementation for me. I took it from: https:\/\/github.com\/ziadloo\/PhoneGap-Java-WebSocket . It basically registers an Javascript Interface accessible from Javascript to communicate with java.<\/p>\n<p>Everytime I have a change now, I simply push a string from the server via the WebSocket saying that it should update, and then I call the &#8220;update&#8221; function from the service in my javascript, instead of querying for data periodically, which is just stupid.<\/p>\n<p>The WebSockets are working well. I can see the message coming, I call the update, it fetches everything correctly from the server, the &#8220;update&#8221; function calls then the &#8220;applyUpdate&#8221; with the correct values and etc, even the &#8220;$rootScope.$apply&#8221; gets called.<\/p>\n<p><strong>But all updated data inside the angular Service is not visible! It seems that all these changes are being run in a different thread!?!?<\/strong>. I know javascript is single threaded, but it just seems so.<\/p>\n<p>Let me put this in a better way: <strong>I have the impression that as soon as the WebView calls the javascript callback function, I have a different javascript &#8220;thread&#8221; running, and nothing outside it can be accessed<\/strong><\/p>\n<p><strong>More about it<\/strong><\/p>\n<p>I wrote a function that simply outputs a number every <strong>5 seconds<\/strong>. The websocket updates this number. And my output is the following:<\/p>\n<pre><code>N: 1\nN: 1\n<\/code><\/pre>\n<p>Then after the WebSocket pushes the data with a new Number (2), I get TWO prints:<\/p>\n<pre><code>N: 1\nN: 2\n\nN: 1\nN: 2\n\nN: 1\nN: 2\n<\/code><\/pre>\n<p>Anyone has any pointers about this? Anyone tried doing something similar? I&#8217;m no angular pro, but it just seems that everything gets messed up as soon as I get a callback from the java interface.<\/p>\n<p>I have already looked at: Angularjs model changes after websocket data push from server and my code looks very similar. The only problem I think is this callback from Java.<\/p>\n<p>Thank you<\/p>\n<p><strong>Update<\/strong> : It&#8217;s a problem with AngularJS. If I set this variable in a window global variable, everything get&#8217;s assigned normally. Is it possible that Angular is somehow creating two different $scopes?<\/p>\n<p><strong>Update[2]<\/strong>: Just to be a bit more clear: In the browser, everything works as expected. Only when I run it in the emulator that this thing gets messed up.<\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-04 02:50:22. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t even know how to explain this directly, but I&#8217;ll try. Intro I&#8217;m building a Phonegap App with angularjs, and I&#8217;m trying to stitch up WebSocket messages to update my UI. I used to have services that communicate with the server periodically and change their data accordingly and it was working pretty well. An [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2164","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2164"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2164\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}