should i use Web Workers or setInterval() - javascript

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.

Related

manage session after refresh page in WebRTC based audio calls

I am currently using a Kandy WebRTC library to make WebRTC based audio calls. As this JavaScript library internally using the WebSocket the problem I am facing is to keep the session alive if the user refreshes the page.
For example, when a user A connects via Kandy library and makes a call to user B and during the call if the user refreshes the page the connection lost at user A but user B is still on call as it is not properly ended by user A. The hurdle is Kandy library does not provide a way to re-connect to an ongoing call.
Hence, I want to know is there anything that we could do on a client-side to manage this refresh page issue. One of the approaches that came into my mind is to shift the JavaScript Client + Kandy logic to the Node.js server and access the make call and receive calls using HTTP services. But I am really not sure how the audio would work in this case because this Kandy library requires the rendering media or <audio> tag to establish the media channel.
Here is the Codepen link just to understand the code, due to security reason I won't be able to provide the credentials but you can have the jest of code that we have used for the sample.
Check the kandy.media.renderTracks(call.remoteTracks, "#remote-container") which requires the div tag <div id="remote-container"></div> to render the media.
That seems like a bug in the Kandy SDK. Typically the signalling server (in this case a websocket) can detect if a client that is in a call closes the websocket and notify the other end of the call.
Using window.onbeforeunload to send a clientside "bye" might help too.

Post json to server in regular intervals in plan 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.

Asynchronus javascript service call to reduce turn around time

I have a scenario in an enterprise application which is partially based on angular js. We have some ReSTful service to access data from different domain. The problem is that when the service fails the server side cannot posibly know the reason of failure as it is happening on client side (browser).
The possible solution to the problem will be that in the error block make a service call to server to log the response received from the third party service. But if I do that it will increase the turn around time for given service call which in turn result in slower user experience.
My thought on this is to implement some asynchronus queue which sends the service failure periodically to server. Could any one suggest the how to achive the same using javascript?
you can implement an pinging AJAX call to your server sending the required data of service failure.
you can create an cache for the data the you need to communicate to server and clear it when you do a successful ping AJAX.
you can select any conformable time interval based on application performance for ping interval duration.

How to Implement a Class/Database Listener?

I am using the Parse.com database service for a PhoneGap app we are creating. We have users that can mark themselves (First User) "Available" for their friends (Second User), and I need a way to listen for that toggle in availability on the second user's side, so their friends list can update without having the refresh the page.
With Parse, your interaction with the database is monitored by # API calls and Burst Limit (Number of API calls per second) so I need to only call the database for the change in status when it is actually changed, I can't keep a setInterval on otherwise it will make the burst limit too small for other user, or it will cause to many API calls for no reason if the status isn't changing.
How can I got about this?
You should try socket.io with intercom.js. The first one does the pushing to the client, the second one ensures that only a single socket is open by multiple tabs. Socket.io has multiple fallbacks on client side, it can use websockets, flash, maybe even long-polling I guess...
The server side should support sockets. By nodejs it is very easy. By classical http languages, for example php+apache just the long-polling will work I think.
Be aware that your data pushing application will be socket based and not request-response based, so it won't be part of you webservice. Probably you should only push, that the user list must be refreshed. So there won't be any duplicated code...

Rails, Ajax, OAuth and client-side API querying -- should it be done, and how?

I used the OAuth gem to acquire an access token. In my code, I can write:
access_token.get('/1#{path}')
where path is some api query. But I want to do these queries asynchronously, client-side -- with no page refreshing.
I would like to know the best way to pass the API querying to the AJAX after authenticating with OAuth, and examples or an explanation of how to do so.
For example, I wish to display 20 followers per page, but when I click 'next page', it will just refresh the 20 on screen.
You're biggest problem will probably be the Same Origin Policy, i.e you will not be able to access data on the API providers domain.
You have two options.
First is to make your own server side dispatcher that will do your API calls for you. Call this from your client code. If you need to do any POST requests, then this is actually the only solution.
Second option depends on whether your API provider accepts JSONP requests. If it does, then you can at least do GET requests directly to the API end point without going via your own dispatcher.

Categories