How can I access appInsights.context from javascript code - javascript

I want to display the UserId on the website, so users can communicate it to us whenever they encounter a problem. However, in code, appInsights.context is undefined. How can I access this?
window.appInsights = appInsights;
appInsights.trackPageView();
console.log(appInsights.context);
is logged as undefined. However, I can access it in the developer console when I run the application. It seems like the context is being removed/not added yet.

I believe this is because the appInsights context is not necessarily created right away?
you may need to do this work "asynchronously" by adding events to the ai queue:
appInsights.queue.push(function () {
console.log(appInsights.context);
});

Related

JavaScript Websocket Own Close Code

I have run in to the following problem:
For a customer I have to be able to open websocket connection to different websockets, depending on the current view.
The Connection & Message Handling is no issue & is all working as intended.
However, I wanted to implement a function that closes the open websockets when switching views, so they don't stay running for no reason.
I do this through the following code :
$scope.closeOpenWebsockets = function () {    
var  socketLength  =  $scope.openWebsockets.length;    
while (socketLength--) {      
console.log($scope.openWebsockets[socketLength]);
 $scope.openWebsockets[socketLength].close(4999);    
}    
 $scope.openWebsockets  =   [];
};
$scope.addOpenWebsocket = function (socket) {    
 $scope.openWebsockets.push(socket);
};
This does work, the only thing I can't get working is changing the close code of the websocket.
Based on the list I found here :
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
I tried using various codes listed there, but none seem to work. everytime I call the .close() myself, the close code I get back is 1006. Above example is with 4999, a close code normally available for applications. But also did not work when trying to send for example 1000.
The reason I need this is because the connection can also be closed from the other side, & in this case I need to try and reconnect. (It's always a close event & not an error event).
I hoped to do this based on close code, but I wanted to choose the code I sent when closing manually so that I am sure I'm not blocking any other close codes I might receive (I can only do limited tests with the source system & thus I am not 100% sure of all possible codes I might receive)
As probably visible in the code, I'm doing this in AngularJS.
Am I missing something here? I have looked through numerous stackoverflow posts/websocket guides and so on. But I haven't found anything.
Edit : This was one of the guides I found:
https://www.tutorialspoint.com/websockets/websockets_closing_connection.htm

TypeError: Meteor.user(...).services is undefined

I'm trying to get a Meteor user's Facebook ID (who is signed in through Facebook). The following line is giving me trouble:
var userID = Meteor.user().services.facebook.id;
In the console it says
TypeError: Meteor.user(...).services is undefined
From my understanding this is because the user().services hasn't been initialized yet. I wrapped the code in if(Meteor.user()) to confirm the user has been initialized. The code is executed when a button is pressed, so it definitely has enough time to start. This started happening when I removed autopublish (although that could be unrelated). Is there anything additional I need to do to make it work without autopublish?
Solved it with the following:
Meteor.publish("getUserData", function () {
return Meteor.users.find({_id: this.userId});
});
and subscribing to that. Once that subscription is filled all of the Meteor.user.services data is available.
The services key is not included by default in the user's document on the client because it contains security details. You could fetch it via a method call or publish it specifically from the server and subscribe to it.

Facebook User Feed Subscriptions

I have what I'm sure will be a very easy question, I'm just confused.
I have successfully got my server subscribed, for real time user/feed but simply am not getting any updates.
I have logged myself in using the FB.login JavaScript SDK, using the scope "user_about_me,user_status,read_stream" - so I expected to see updates for my user, but not getting anything at all.
The app is in "Development Mode", so, can anyone confirm that since I have got a { success: true }, that the reason is simply because of this? Or perhaps I need to put it under review from Facebook?
Thanks.
Woo, it's fixed now!
On my research, I had come across this post:
How to subscribe to real-time updates for a Facebook page's wall
Not realizing that I can use the USER_ID in place of the PAGE_ID. Following the Real Time Subscription documentation, I thought that by using the APP_ID, that it would allow to make one subscription for all users that grant the application its scope.
On to the next hurdle...

How to alert user upon update of chrome app?

I have a chrome app that I am going to be updating and the update will break previous saves. Now I would like to alert the users of the app upon successful completion of the update. I do have an chrome api that I am trying to use which is:
chrome.runtime.onInstalled.addListener(function(){
});
Now I have read the chrome.runtime developer docs and it says that I can use:
chrome.runtime.onInstalled.addListener(function callback)
With a callback function of:
function(object details) {...};
The problem is that I can not figure out what exactly to do with the info as I have tried a couple of different set ups with this code but none work. any help with this issue would be appreciated
Please note that I am trying to display a window that would contain this message. Also my current code is in the background.js of my app although as said the code does not work.
Documentation mentions one property of details:
reason
enum of "install", "update", "chrome_update", or "shared_module_update"
The reason that this event is being dispatched.
So your code needs to check that:
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "update") {
// Inform the user of the sad news
}
});

Chrome.storage.sync - Will retain their data?

I am using the 'chrome.storage.sync.get/set' to save my data. My question is, will the data saved using this API will get reset when I close chrome browser window and open it again. Is there any method to clear the data saved when we exit from chrome? I know there is a method 'chrome.storage.sync.clear()'. But this should happen only when the user exits from chrome.
will the data saved using this API will get reset when I close chrome browser window and open it again
No, it's saved — not just locally, but also potentially synced to the user's Google account. That's the point of the sync storage area. The documentation says sync is like localStorage, but synced to the user's account.
If you're using it for transient data, don't. Use local instead (as there doesn't appear to be an extension equivalent of session storage) or perhaps just store the data in your extension's runtime environment (it's not clear what you're using this for).
I know there is a method chrome.storage.sync.clear(). But this should happen only when the user exits from chrome.
You'd have to trigger clear from an event. Unfortunately, there is not currently an official way for an extension to get notified when Chrome closes (there's an open issue on it). Hopefully there will be a way at some point, but there isn't currently.
You're right chrome.storage.sync.clear(), removes all storage from sync. However, if you want to use that function when the browser 'exists', you'll have to look at the Processes Api.
You would probably want to use this function (from the documentation) :
chrome.processes.onExited.addListener(function(){...});
It takes processId as one of the arguments. So you would just have to get hold of the processId for the browser process. I found a relevant answer for this on SO only. It suggests you do something like:
//Basically loop through all the processes to get the browser's Process Id
var browserId;
chrome.processes.getProcessInfo([], false, function(processes) {
processes.forEach(function(process) {
if (process.type === 'browser') {
browserId = process.id;
}
});
});
Once you have the browser's Process Id, you can add a listener to the exit event and do the needful. I hope it gets you started in the right direction.

Categories

Resources