PHP-Lively is a php/javascript/ajax live chat script that allows users to chat with vistors on their site. It’s like that feature that nearly all large business sites have but not quite as fully fledged and somewhat buggy. It’s also free, not $99 a month like the most common chat service is.
I was installing PHP-lively into a site after testing it on my personal server and noticed it was not automatically refreshing the view. So if someone typed the other user would have know way of knowing unless they hit the “Envair” (enter, yes the scripting is in… spanish?). This of course was not the point of a live chat so I dug through the code (formatted in some strange language to me) to fix the problem.
1 2 3 4 5 |
httpRequest.open('POST', 'index.php', true); httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); httpRequest.onreadystatechange = function() { traer_contenido(httpRequest); }; vis_data = "modo=escribir_msg&sid=" + sid + "&nick=" + encodeURIComponent(chatnick.value) + "&email=" + encodeURIComponent(chatemail.value) + "&msg="; httpRequest.send(vis_data); |
Snipplr: http://snipplr.com/view/45224/phplively-auto-refresh-fix/
The code goes towards the end of the code.js
document (located at master/js/chat.js)
, specifically at the end of the UpdateTimer()
function.
What the code does is register a submission as if you entered text, but sends a blank set of data, wich tells the script to update without posting. This is very much a hack and should only be used if your on a server that will not allow the automatic refreshing of the script as written.