Post json to server in regular intervals in plan javascript - javascript

I am building a javascript plugin that a client will include in his. The plugin will track data user activity like page scroll, button clicks mouse movements etc and then save to web storage. This data then need to be send to a back end server at regular intervals of say 5-10 seconds in json format.
If I use a simple ajax for sending data by using setInterval method
will that impact user browsing experience.?
Will using websockets make more sense in this case or is there any other way?
NOTE: The backend server application is based on Spring MVC and the javascript plugin will push data to server externallly from clients application.

If you really wanted minimal impact on the UI thread, you could spawn a WebWorker and do the AJAX requests from there.
Here is a good answer explaining AJAX requests in the context of a WebWorker.
Here is another short example of implementing AJAX in a WebWorker.

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.

should i use Web Workers or setInterval()

i am using phonegap to build apps. currently i need to control the concurrent user access using the same login id.
i need to send an ajax request to web api server to check whether there is user using the same login id. this ajax request need to be send every 30 second.
if api server return 'Y', means more than 1 person using the same login account, i need to close the apps and return back to login screen.
what i worry is, if i using setInterval(), the ajax call will have impact to the main UI. In this situation, should i use web workers ?
Use Ajax requests. They are asynchronous by default, and waiting for the server to return the response will not in any way impact the main UI.
Also note there are many techniques that may provide a better user experience than polling every 30 seconds. Check out web sockets if you are interested in an alternative implementation.

Real Time Functionlity with php

I am working on attendance system where project management system is also handling using Javascript\Jquery at client side and PHP\MySQL at server side.
A feature in my web app is user message to admin. For this I have applied an ajax request which is made by setTimeout function (after every 15 seconds) to check that is there any new message comes in database if yes then return it to the admin.
It working fine but the drawback is (as you know) continuous request to server which really poor and bad. As I know that real time functionality can solve this problem I have checked some link websocket.io and signlR but there are applicable with Nodejs and asp.net.
So, how can I apply real time functionality with php or I get that new data have reached in to database without continues request with ajax.
There are actually some PHP tools for doing this now. For instance, check out Ratchet.
It's also possible to do bi-directional sockets by creating a simple TCP/IP server. I've done this before for bi-directional communication between a PHP server and a desktop app.
http://php.net/manual/en/sockets.examples.php

reporting performance to the server with ajax and .net mvc 3

I am building a mobile site that works like a slideshow. There are a number of image slides and you can swipe left and right to traverse the slides.
I would like to monitor the download speed performance of the slides using javascript and report the times to the server.
I presume that ajax is the way to report the times, although I am new to ajax. My fist concern is that the report sent to the server should be as lean as possible. Also it is only really necassary for the communication with the server to be one way.
Can the flow go just in the direction of from the browser to the server without any responses being sent back to the browser? Or do the http post and put methods have to send a response back to the browser? Obviously notthing is actually needed to be sent back to the browser and to the mobile site it doesn't even really matter if the request is a success or failure.
If a response does have to be sent to the browser what should my MVC 3 controller return? Can just a head with success or failure be returned?
Finally which of the http POST and PUT methods is best for this and what will be the best data format to use?
Do a POST to an action on the server.
I would suggest you use JSON for the data as it is quite lean and makes it easy to decode. However, if you wanted to make it really lean you you could just use a parameter on the url, like ..../reportPerf?d=123 and then your action should take a parameter "d".
The server does need to respond, but you can just
return new HttpStatusCodeResult(200)
That will only take a few bytes.
Sorry for the brevity, on a mobile device.

Comet applications without using IFrames

I am just getting started creating an AJAX application using server side push. I am using Grizzly Comet on Glassfish V2. Almost all the sample applications use IFrames for updating content on the client side. I want to stick to using JavaScript only. Are there any such sample applications (preferably ones that do not use a JavaScript library)?
The point about the push is that the HTTP request never ends. Not all client implementations handle that correctly. As I see it, it's possible to do it with pure javascript in Firefox with its extensive API, but the XMLHTTPRequest object would timeout and would not be able to stream you the content.
IFrame is good, you could also try the object tag if its about standard compliance.
Before XMLHTTPRequest and Ajax had no name, we used to get data from the servers through IFrames.
This is how I figured out how to do a push with just javascript and php.
Have javascript do an initial call and load content into a div.
Then have the javascript call back to the php and have the php sleep until it see there is a new update.
Then send out the data to everyone and call back to php and sleep again.
This allows for long polling and fewer calls back to back. I personally put a time of 5 mins to 30 mins on the php script.

Categories