making ajax requests in every 2 seconds is doable? - javascript

I have to build a results viewing page where I have to list results of a voting process in live. I am planning to check for DB changes in every 2 seconds ? Is it doable ? will the page crash or get stuck after 1 or 2 hours?

I think you can use settimeout to call the ajax function.
The second better way would be if its possible in your existing architectural setup, use setimeout for making an io connection and then checking for any event , this event is nothing else but a change in the db which is triggered by a node js server.

Related

Create content which updates asynchronously PHP

I'm trying to create a trading bot with PHP.
I would like to get the value of the currency and update it even without refreshing the page.
In order to do this I should execute this call every 5-10 seconds.
Is that correct?
$summ = $d->getMarketSummary("USDT-BTC");
Is there a way to do this asynchronously? Even if user doesn't reload the whole page.
I've heard of AJAX, but it's Javascript.
Thank you in advance.
You've heard it correctly, you need to do this in JavaScript, with AJAX. There are two parts for this:
1) You need to make an API in PHP, a route that will only respond with the data you want. So a page that when called:
<?php
$summ = $d->getMarketSummary("USDT-BTC");
echo $summ;
?>
And mapped to a url, let's say /data.
2) You need to make a JS in your page that calls that newly created route every-so-often; for that your need to use ajax (xmlhttprequest or Fetch API), and use the setInterval function to call it regularly and update the data in your page accordingly.
If you can't use javascript (AJAX) for this task, your only way is to create a CRON job that fires that PHP script every minute. Unfortunatelly, CRON jobs can't be configured to execute every X seconds, but you can fire it all minutes of the day.

Symfony not send response if previous process is work

I send few ajax requests to my site. First running creating document, others check him status. But symfony not respond while first process is working. All processes wait in queue while end first. If look in log i see:
request.INFO: Matched route "start creating". work first request.INFO:
Matched route "check" work first request.INFO: Matched route "check"
work first .... event.DEBUG: Notified event "kernel.response" to
listener for first process event.DEBUG: Notified event
"kernel.response" to listener for second process etc
Why symfony very strange processes requests?
Problem in php session mechanism.
session_write_close();
fix this problem
It is not symfony, but your browser which limits number of simultaneous connections to the same domain. See What's the maximum number of simultaneous connections a browser will make?

Is it bad idea to make an AJAX post call every 2 secs?

If I make an AJAX $.post call (with jQuery) to a php file for updating a certain parameter/number, does it considered bad practise, dangerous or similar?
$.post(file.php, {var:var}, function(data){
// something
}, json);
It would be a single user on a single page updating a number by clicking on an object. For example if user A is updating a certain number by clicking on an object user B should see this update immediately without reloading the page.
It depends on 3 main factors:
How many users will you have at any given time?
How much data is being sent per request on average?
Given 1 and 2, is your sever set up to handle that kind of action?
I have a webapp that's set up to handle up to 10-20k users simultaneously, makes a request each time the user changes a value on their page (could be more than 1 req per second), and it sends roughly 1000 bytes on each request. I get an average of 10ms response time, however that's with node js. Originally I started the project in PHP but it turned out to be too slow for my needs.
I don't think web-sockets is the right tool for what you're doing, since you don't need the server to send to the client, and a constant connection can be much more expensive than sending a request every few seconds.
Just be sure to do lots of testing and then you can make judgements on whether it'll work out or not for your specific needs.
tl;dr - It's not a good idea if your server can't handle it. Otherwise, there's nothing wrong with it.
Another solution could be, to cache user actions in local storage/variables, and send them all at once every 10-15 seconds or so, then clear the cache, when sending was successful.
In this case you should also validate the data in local storage to prevent tampering.

Track last ajax call of website

I know its a bit stupid question which is a bit meaningless but still I wanted to know is there any way to track last ajax call on page.
Actually the issue is I developed an application which works with ajax and many ajax calls happening with different different actions but if no ajax request call for 10 mins. it will expire session but I don't want it on just ajax call.
What I Want
if no activity happens in 10 mins. I don't wanna do anything but if even any click or keyboard activity happens then it will make 1 more ajax in after 10 mins. of click so session will not expire.
But that's not the issue to write script for it but the issue is I've lots of page and lots of different ajax call's so I just want to track last ajax call time on page.
I don't know if there's any possible way or not for it so I didn't write any code yet if any its possible then I'll start writing script otherwise I'll find another way but I appreciate if someone suggest me a way to do it.
Thanks
You can use ajaxsetup and increment a variable in ajaxsetup. Because ajaxsetup call in every ajax call
see the documentation of ajaxsetup http://api.jquery.com/jquery.ajaxsetup/
declare one variable and set 0 at the end of ajax request and change the value of variable as per time
i think this will help you
If you want your session not to expire, then increase its duration :
Adding this to your script will set php sessions lifetime to a month :
ini_set('session.gc_maxlifetime', 2678400);
ini_set('session.cookie_lifetime', 2678400);
EDIT for comment below :
To track last call add this at the beginning of your application entry point :
<?php
session_start();
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
$_SESSION['last_ajax_call'] = time();
}

Save only changed objects on server : Restful service + angularjs

I display a list of posts on my page with a like button that the user can toggle on or off.
In order to throttle the traffic with my server, I would like to:
send updates for all the posts where the like status has changed at once in the same request
send updates for the modified posts only
update the like status each N seconds, and on page exit only. Not each time a user toggles a like button.
Is this possible with angularjs?
I've written up an example that can be seen here:
http://plnkr.co/edit/imMGTJ75mKJZT7ispD9E?p=preview
I've spent some time on it with commenting, and providing useful information that gets displayed on the page itself when it runs. Change around the delays if you'd like it to run slower if the messages are moving too quickly.
From your question it looks like you want to save when a user makes any changes to a particular post. You proposed checking every x seconds for changes, but this isn't ideal (though it would be simple to implement with a setInterval). You also mentioned saving the changes on page exit, but it's impossible to guarantee that something happens on page exit (a user loosing power for example).
To avoid the above, I would fire the ajax call when the user clicks the "like" button, but throttle them after the first click & store their changes while the throttle timer is running and push all their changes at once after the timer ends.
Here is what my plunker code does in a nutshell:
User "likes" or "unlikes" a post and it will make an Ajax call to the server with the new information on the post. At this point, any new "likes" / "unlikes" gets thrown into a "queue" of posts that need to get updated.
When the first Ajax call is successful, the throttle timer starts. In the example I've provided it is 5 seconds. Any changes to a post ("likes", "unlikes") will be thrown in that same "queue".
After 5 seconds is up, it will check the "queue". If it's empty, no action is taken. If it has items in it (e.g. posts that have changed), then it will make a second ajax call and update the posts on the server.
My example won't mirror what you're working on exactly, but it's the concept that matters. You could modify the code so it doesn't throttle for such a long time, or have it only throttle after x number of ajax calls in a certain amount of time, etc.

Categories