I have a chat box using jQuery and php.
setInterval(
function(){
if($('#session_container').children().length > 0){
if(localStorage['group_id']){
var group_id = localStorage['group_id'];
var session_id = localStorage['session_id'];
var gsid = 'GSID' + group_id + session_id;
$('#chatbox').load('php/chatbox.php', {'group_id' : group_id, 'session_id' : session_id},
function(){
$('#chatbox').find('tr:even').addClass('alt');
if($('#chatbox').children().length == 0){
localStorage.clear();
window.location.replace('chatro_main.php');
}
}
);
}else{
}
}
}, 1000);
As you can see from the code above, the script runs every one second which I think is very aggressive and I think it's also wasting power(my laptop's battery seems to be draining faster when I was running this). Is there a better way of doing this. Something like not reloading the content while the user is on another tab on the browser window. Or something like updating only the content if another user sent a message?
If you know of another technology that could do this in a more optimal way, you could also mention it.
There are several possible answers to this question:
Don't use setInterval, but repeatedly setTimeout and adjust your timeout. The idea is, that you first try like every 10 seconds, and then increase the time, if there is no action. So the intervals would be: 10, 20, 40, message received, 10, 20, message received, 10, ... You have to adjust the factor you will actually increase the timeout to fit your needs.
Implement it using WebSockets (see comment of #Christian Varga)
Use node.js as server side language, which interacts with client side javascript pretty well. You can have node.js broadcast messages to all clients, so you actually have a two-way connection there (I think they use WebSockets and as fallback something like comet.js, but I am not sure, I just saw it done once)
Actually this is not an answer but i can't make a comment cause of my rep. I had a chat like this, but i had a var named chatState, when chatState is 1(or true) fires interval for every second when no msg received for a while chatSate goes for 0(false) and clears that interval and starts a new one checks for every 10 seconds if there is someone type anything to chat? Not sure this is a good way but i tought that would be better for server cause less request, but as i said not sure is this a good way so this is would be a comment instead of answer.
Related
I have been looking for several answers around the web and here, but I could not find one that solved my problem.
I am making several JQuery ajax calls to the same PHP script. In a first place, I was seeing each call beeing executed only after the previous was done. I changed this by adding session_write_close() to the beginning of the script, to prevent PHP from locking the session to the other ajax calls. I am not editing the $_SESSION variable in the script, only reading from it.
Now the behaviour is better, but instead of having all my requests starting simultaneously, they go by block, as you can see on the image:
What should I do to get all my requests starting at the same moment and actually beeing executed without any link with the other requests ?
For better clarity, here is my js code:
var promises = [];
listMenu.forEach(function(menu) {
var res = sendMenu(menu);//AJAX CALL
promises.push(res);
});
$.when.apply(null, promises).done(function() {
$('#ajaxSpinner').hide();
listMenu = null;
});
My PHP script is just inserting/updating data, and start with:
<?php
session_start();
session_write_close();
//execution
I guess I am doing things the wrong way. Thank you in advance for you precious help !!
Thomas
This is probably a browser limitation, there is a maximum number of concurrent connections to a single server per browser instance. In Chrome this has been 6, which reflects the size of the blocks shown in your screenshot. Though this is from 09, I believe it's still relevant: https://bugs.chromium.org/p/chromium/issues/detail?id=12066
Context :
I'm making a PHP websocket server (here) running as a DAEMON in which there is obviously a main loop listening for sockets connections and incoming data so i can't just create an other loop with a sleep(x_number_of_seconds); in it because it'll freeze my whole server.
I can't execute an external script with a CRON job or fork a new process too (i guess) because I have to be in the scope of my server class to send data to connected client sockets.
Does anyone knows a magic trick to achieve this in PHP ? :/
Some crazy ideas :
Keeping track of the last loop execution time with microtime(true), and compare it with the current time on each loop, if it's about my desired X seconds interval, execute the method... which would result in a very drunk and inconsistent interval loop.
Run a JavaScript setInterval() in a browser that will communicate with my server trough a websocket and tell it to execute my method... i said they where crazy ideas !
Additional infos about what i'm trying to achieve :
I'm making a little online game (RPG like) in which I would like to add some NPCs that updates their behaviours every X seconds.
Is there an other ways of achieving this ? Am I missing something ? Should I rewrite my server in Node.js ??
Thanks a lot for the help !
A perfect alternative doesn't seams to exists so I'll use my crazy solution #1 :
$this->last_tick_time = microtime(true);
$this->tick_interval = 1;
$this->tick_counter = 0;
while(true)
{
//loop code here...
$t= microtime(true) - $this->last_tick_time;
if($t>= $this->tick_interval)
{
$this->on_server_tick(++$this->tick_counter);
$this->last_tick_time = microtime(true) - ($t- $this->tick_interval);
}
}
Basically, if the time elapsed since the last server tick is greater or equal to my desired tick interval, execute on_server_tick() method. And most importantly : we subtract the time overflow to make the next tick happen faster if this one happened too late. This way we fill the gaps and at the end, if the socket_select timeout is set to 1 second, we will never have a gap greater than 1.99999999+ second.
I also keep track of the tick counter, this way I can use modulo (%) to execute code on multiple intervals like this :
protected function on_server_tick($counter)
{
if($counter%5 == 0)
{
// 5 seconds interval
}
if($counter%10 == 0)
{
// 10 seconds interval
}
}
which covers all my needs ! :D
Don't worry PHP, I won't replace you with Node.js, you still my friend.
It looks to me like the websocket-framework you are using is too primitive to allow your server to do other useful things while waiting for connections from clients. The only call to PHP's socket_select() function is hard-coded to a one second timeout, and it does nothing when the time runs out. It really ought to allow a callback or an outside loop.
Look at the http://php.net/manual/en/function.socket-select.php manual page. The last parameter is a timeout time. socket_select() waits for incoming data on a socket or until the timeout time is up, which sounds like what you want to do, but the library has no provision for it. Then look at how the library uses it in core/classes/SocketServer.php.
I'm assuming you call run() and then it just never returns to your calling code until it gets a message on the socket, which prevents you from doing anything.
I'm using an Ajax function with a call to itself to update the information continuously. But I let the script run for a while and then the server blocked my IP because it thought I was flooding it or something like that, I don't know. Anyway, I wonder if there's another way to do this more properly. Here's my code:
Ajax function:
function update_cart()
{
if (window.XMLHttpRequest)
var http = new XMLHttpRequest();
else
var http = new ActiveXObject('Microsoft.XMLHTTP');
http.onreadystatechange=function()
{
if ((http.readyState == 4) && (http.status == 200))
{
id('cart_quantity').innerHTML = parseInt(http.responseText);
setTimeout('update_cart()', 1000);
}
}
http.open('GET', actual_path+'fetch_cart_quantity.php', true);
http.setRequestHeader("X-Requested-With", "XMLHttpRequest");
http.send();
}
PHP script:
<?php
if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest')
{
header('Location: ./');
exit();
}
session_start();
include '../include/config.php';
include '../include/db_handler.php';
include '../include/cart_handler.php';
$cart = get_cart_quantity($_SESSION['cart_id']);
if ($cart == NULL) $cart = 0;
echo $cart;
?>
Thanks in advance for your help. Sorry that my English is not very good.
You have a couple of options here as I stated in my comment.
Basically the first is to chill out with the querying. You don't need to long poll such a thing. Turn down the querying to once every 5 mins or just whenever there is an action.
You can also build a simple comet server to do a push pull type thing when ever updates are "pushed" down from the server. There is a pre-built one called APE: http://www.ape-project.org/
Also node.js can handle this sort of thing for you.
Also you should probably look into your server setup, sounds kinda weird that your sever is blocking it's own IP address/domain...
Probaly, but i seriously think that querying your server every second is totally innecesary and also a waste of resources (unless you have very compulsive customers), making your script to query your server every minute or so is better and may work even better in the long term if you have several customers using this application from the same server.
If you really think is necessary to have this feature, a good approach will be using Push notifications, more info can be found here: PHP - Push Notifications, here: Push notification to the client browser and here: Push notifications from server to user with PHP/JavaScript.
Why don't you just update the cart when the user performs an action.
Such as 'Add item to shopping cart'?
That way you'd only call the server when it's actually needed.
Well, i was trying to reach a solution and i thought this might work:
On the PHP file:
$liguem = getdate();
$liguemoff = $_COOKIE['liguemoff'];
$liguemon = $_COOKIE['liguemon'];
if(empty($liguemoff)){
setcookie('liguemoff',$liguem[0],time() + (50000));
}
setcookie('liguemon',$liguem[0],time() + (20000));
$body->assign("COOKIE2", $liguemoff);
$body->assign("COOKIE3", $liguemon);
This has some body assign because I'm working with XTemplate, but the PHP is just PHP.
Now on the index file, some JavaScript:
var cookie2 = {COOKIE2};
var cookie3 = {COOKIE3};
if( cookie3-cookie2 > 60){
alert('alerta');
};
Truth is that it works! People might not be navigating, but it is what i want, the pop up will only open after the visitor sees at least 2 pages (Server-side thing).
The main problem is, that i CAN'T make the function popup(); to trigger where i have the ALERT displaying. The ALERT is working alright though.... Any hints?
PS:
This is the popthat(); function:
function popthat(){
$("#darkside").css('opacity','0.3').fadeIn('slow');
$("#darkside").click(function () {
$(this).css('opacity','1').fadeIn('fast');
$("#liguem").hide();
});
$("#liguem").corner();
$("#liguem").hide();
$("#liguem").delay(200).css('visibility','visible');
$("#liguem").fadeIn('fast');
}
You can set a timeout to display your popup after a specified amount of time. This amount of time can be dicated by your PHP since the server-side code will be able to track the amount of time on the site through page-views. This way the popup can display after 60 seconds on the site even if the user is not navigating to another page.
Something like:
setTimeout(popthat, <?php echo $_COOKIE[...]; ?>);
Your PHP would echo the number of milliseconds until the popup should display.
A note: when you replace your alert() with the popthat() function the DOM may not be ready and popthat() won't be able to work because it won't find any elements that match your selectors. Try running your code on document.ready ($(function() {});).
Browsers automatically block popups initialized on page load, because nobody likes these sorts of popups.
When you do an alert(), execution of your script stops. alert() is a blocking function, and nothing will happen until it has moved on.
I don't know if you just made a typo, but your function is called popthat(), and in your statement you said you called the function popup(). You need to change popup(); to popthat(); for this to work, unless as I said that was a mistake.
I have a javascript setInterval that checks an external page every 5 seconds for mail, I am finding sometimes that if I login or click a form submit at the same time as the request goes out, I sometimes find myself looking at a Y or a N (what my JS was to intercept) instead of the real link I wanted to go to.
How does one debug this? I am using firefox with firebug, my app is using PHP with javascript.
EDIT: it's almost as if the onComplete is being missed by java, and it just dumps it as the user is signing in.... it only happens when someone is changing pages and the java is running at the same time.
EDIT 2: If you want to see this for yourself, you'll need visit my site and create an account and go through the signup process (2-3 mins to do tops), the website is http://mikesandmegs.com and the beta password is goldfish. What you want to do is login just as the check mail sends its request off. Its like I need to cancel something or tell java to throw the callback out or something. You should see the requests every 5 seconds, (well it adds 5 seconds each request) but you'll see. It may take a couple try's or some luck, but it is reproducible.
This is the javascript that is running (i think I have it all posted) If I seem to be missing anything, let me know. I also posted an htnl input html that the javascript checks...
<input id="hasMail" type="hidden" value="y">
<script type='text/javascript'>
mailTimer = setInterval("checkMail();", 10000);
function checkMail()
{
// should we check the mail now?
if ($('hasMail').value == "y")
{
// remove mail new mail alert (mail-check.php returns y or n
new Ajax.Request('mail-check.php',
{
method: 'post',
postBody: '',
onComplete: checkMailNotify
});
}
}
function checkMailNotify(req)
{
if (req.responseText.length > 5)
{
$('hasMail').value = "n";
clearInterval (mailTimer);
return;
}
if (req.responseText == "y")
{
$('hasMail').value = "n";
$('topMessage').update('You have new mail...');
$('alertBox').appear();
clearInterval (mailTimer);
}
else
{
clearInterval (mailTimer);
mailInterval = mailInterval + 5000;
mailTimer = setInterval("checkMail();", mailInterval);
}
}
</script>
I know this is nowhere near a solution, but it WILL help to increase the 5 second interval, even to something like 30 seconds. I've done work with mailservers before, and we often came across problems where people would have e.g their iphone as well as their desktop mail client ping the server at very short intervals. This would result in confusing (to them) failures because of locks.
So yeah, 5 seconds for messages is very quick (it doesn't look like chat but rather just messages, is that right?). At best if you do that then the problem will happen a lot less if it all. You will however have the horrible knowledge that it can happen.
Please don't take this as an attempt at a solution to your problem. just a suggestion.
I think what's happening is that while changing pages, the data from the mail-check.php is clashing with the new request that is coming back from the network at the same time. I think a possible solution is to disable the setInterval whenever you change a page or submit a form, then re-enable it after loading the new data.
Something like:
<input type="button" onClick="clearInterval('mailTimer'); this.submit()" />
...