Polling the server in background - javascript

I am busy developing a Firefox extension and I have a Widget that opens a Panel. How do I have a background script that is constantly running/polling the server to fetch updates. These updates would then need to be injected into the DOM of the Panel.

All of your extension scripts are "background scripts". What you are asking about seems to be a way to poll the server regularly - you use the timers module for that, method setInterval(). The callback would than use the request module to send a request to the server.
As to communicating information to a widget/panel - you use the usual approach to send messages to the content scripts running there.

Related

Client access vs broadcast data from web server

I'm looking for technique or skils to fix the ways for new web site.
This site show the read time data which located on server as file or data on memory.
I'll use Node.js for server-side. But I can't fix how to get the data and show that to web site user.
Because this data have to update per 1 second at least.
I think it is similar to the stock price page.
I know there are a lot of ways to access data like AJAX, Angular.js, Socket.io..
Also each has pros and cons.
Which platform or framework is good in this situation?
This ultimately depends on how much control you have over the server side. For data that needs to be refreshed every second, doing the polling on client side would place quite the load on the browser.
For instance, you could do it by simply using one of the many available frameworks to make http requests inside some form of interval. The downsides to this approach include:
the interval needs to be run in the background all the time while the user is on the page
the http request needs to be made for every interval to check if the data has changed
comparison of data also needs to be performed by the browser, which can be quite heavy at 1 sec intervals
If you have some server control, it would be advisable to poll the data source on the server, i.e. using a proxying microservice, and use the server to perform change checking and only send data to clients when it has changed.
You could use Websockets to communicate those changes via a "push" style message instead of making the client browser do the heavy lifting. The flow would go something like:
server starts polling when a new client starts listening on its socket
server makes http requests for each polling interval, runs comparison for each result
when result has changed, server broadcasts a socket message to all connected clients with new data
The main advantage to this is that all the client needs to do is "connect and listen". This even works with data sources you don't control – the server you provide can perform any data manipulation needed before it sends a message to the client, the source just needs to provide data when requested.
EDIT: just published a small library that accomplishes this goal: Mighty Polling ⚡️ Socket Server. Still young, examine for your use if using.

How to get Time to interact to a webpage via App Insights

I am using App Insights from Azure to get the analytic data for a website.
From the browserTimings and Pageviews settings I am able to get the Load time and receiveDuration.
My website has certain ajax requests loading in (async=true) manner. Due to which in chrome network capture, I am able to see Finish:2.15 Sec , DOMContentLoaded :1.05 s, Load: 1.57 s.
Problem statement is How to get the actual time (which attribute) by which the html (DOM) is ready for a user for interaction in App Insights analytics report.
Answer is Client Processing Time. You can understand better from this pic.
MS disclaimer:
The time is measured from when the browser sends the initial HTTP request until all synchronous load events have been processed, including layout and running scripts. It doesn't include asynchronous tasks such as loading web parts from AJAX calls.
For more details: MS documentation

Get custom protocol response

I was learning about custom protocols for few days, and there's one thing that I don't understand.
I know how to start an app with custom protocol, but My question is,
Is it possible to get apps response and print it in Web Browser using javascript?
For example If I will send request to protocol myapp:// , that will open an app written in C#, and that app will return string "This is response" can to print it in Web Browser?
If so, can you help me to accomplish it?
Thanks in advance.
Internet protocols aren't all about browsers.
mailto: causes an action in an email program (e.g. start a new email)
ftp: causes an action in an FTP program (which might be integrated into a web browser or Windows Explorer)
gopher: (well, that's not really prevalent anymore)
myapp:// will cause your (C#) app to start running. At that point, it can do anything a C# app can do. One thing it could choose to do is create a .html file on disk, and use
Process.Start("file://Path/To/My.html")
to cause the default web browser to open the document it just created.
UPDATE
You can certainly have your myapp:// protocol handler send an update to the web server that hosts the page in question. My assumption here is that the myapp:// handler is running on a client machine, and there is a web server on a different URL http://mydomain.com serving a page that includes a myapp:// reference.
Web server renders a page that includes both a myapp:// URL and Ajax code to periodically query the web server for updates to part of the HTML body.
User clicks the myapp:// URL
Protocol handler runs
Protocol handler sends an update to the web server, e.g. http://mydomain.com?user=joe&result=123
Web server uses ?user=joe&result=123 to update response next time the Ajax callback is initiated
Ajax callback gets updated data for page from web server, updates page.

Need to cancel external javascript load because of socket pool limit

I have a system where I inject a script tag into the dom.
The browser tries to load the script url, which is actually a long-post to a Tornado server.
I'm trying to implement an "abort" feature, where I stop the long poll. I need to do this because I am long-polling multiple different URL's which eventually exhausts the browser's socket pool for my server.
I have tried removing the specific script node from the DOM, but the browser is stubborn and continues waiting for a response from the server.
Is it possible to tell the browser to stop trying to load a resource (specifically a javascript file) once I've included it into the DOM?
You could try an asynchoronous abort "mission" and kill the pid of the request. Thsi kind of tehnique is very similar to this post Cancel PHP script with XHR? so you could run an extra ajax call for an abort which handles it like in the post above. Happy new year!

Can I receive a stream over HTTP with JavaScript, Flash, or Silverlight?

I'm prototyping a realtime notification mechanism using http over port 80. The aim of the project is to allow a Flash application to respond to an event on a remote server quickly (specifically an inbound phone call being connected to a phone next to the computer.) Polling is one approach, but is too slow. Currently I use a socket connection to get low latency notification of the events on the server, which works well but isn't firewall friendly. I don't want to install anything except Flash, or Silverlight on the client. Cross compatibility of browsers isn't a concern - in this application I can specify what browser the client uses but IE is preferred.
I've made a server HttpHandler in .NET which never closes the connection and sends the "events" to the client by writing out bytes to the http response stream (ConnectedClientContext.Response.OutputStream.Write etc) and I have a .NET client application which can read these messages okay.
My Question:
Can I receive the bytes from the server over HTTP as they arrive using JavaScript, Flash or Silverlight? So far I can only find a way to get notified of the "download progress" and don't get the actual bytes until the response is closed - I want them as they arrive.
Best Regards,
Daniel
I don't know about Flash but in Javascript (by which you mean in browser) and Silverlight you are limited pretty much to the http protocol.
You can use the AJAX Http Streaming pattern. The basic ideas which is different from what you are trying is that as soon as data is available outstanding request ends and a new is immediately initiated asychronously, mean while your client process the newly arrived data.
Silverlight gives you more options since is HTTP stack is purely asynchronous but you can get your hands on the stream to you as soon as data starts to arive by setting the HttpWebRequest.AllowReadStreamBuffering to false. (Unlike XmlHttpRequest which always buffers).
it's very easy to use the Comet ideas for notifications. you don't even have to use a comet-specific framework. simply do an ajax request with a callback on answer, wrap this on a loop and you have an event loop, just like a GUI app. on the server side, don't bother answering the request until there's either an event, or a timeout (which is just a 'null' event).
Flex and Flash have several AMF/XML remoting libraries available that support data pushing. I would certainly look into that.
http://raghuonflex.wordpress.com/2008/04/17/data-push-in-flex-with-backend/
These libraries use a Comet - like implementation to achieve this.

Categories