i need create web chat.
So i need constantly refresh page.
I think use this jQuery function:
<script type="text/javascript">
$(document).ready(function(){
var sec = setInterval(function()
{
$('#chat').load('chat_r.php?id=' + Math.random());
}, 1000);
});
</script>
But I found out that this function is very slow.
Maybe you know a better option?
You should look into AJAX long polling. It will allow you to update the chat without refreshing the page, and will respond much more quickly than standard polling.
why don't you try to use webSockets?
They're kind'of new and powerful.
Of course, there still are the old browsers which do not support many of the html5 new features, but, when I had to make a web app that used a web-chat, I ended up implementing both ajax and webSokcet based chat.
It's just a suggestion.
See documentation here.
For your original question I would use
<meta http-equiv="refresh" content="1;url=http://example.com/" />
Though its a discouraged method it works fine for me. Basically just refreshes the page every second.
BUT for what your making(web chat) I would definitely use AJAX
Here is a snippet from one of my scripts
Ok stackoverflow won't work when I try to put the code in so I put the code on pastebin. http://pastebin.com/569zpbLe
Related
I'm not very good in php since I was making mobile app at first. So the problem is how could I make a real time notification like Facebook nowadays? I having quick much of researching regarding comet, polling , web socket, etc.
For web socket my server proxy do not allow me to do that.
For comet is that really using much resource? And I always have the problem with 500 server interval error even though i using set_time_limit(0). How what is the solution for this?
For polling no comments.
So what is the best solution?
I think the best solution for you is yousing ajax. You can create function which with ajax get all notifications from php and then with setInterval repeat this function maybe ebry second!
For real-time notification system you might want to use Web Sockets
This link might help you for PHP.
For any simple real time notifiaction you can use ajax auto refresh DIV
your include.php file will run every 5 second
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_tweets').load('include.php').fadeIn("slow");
}, 5000); // refresh every 50000 milliseconds
</script>
<div id="load_tweets" > </div>
I'm working on a simple webchat application for my website. However, for me, it feels wrong using interval and requesting the chat with ajax every second?
What I'm doing today is this,
<script>
$(function() {
$('#chat_form').submit(function() {
$.post('chat/post', $('#chat_form').serialize(), function(data){
var message = $("#message").val();
$("#message").val('');
$("#chat_main").append('<li><b>Bruker: <?php echo " ".$this->session->userdata("chat_username") ?></b>: ' + message + '<br/></li>');
});
return false;
});
function loadchat()
{
$('#chat_main').load('chat/load');
}
setInterval(loadchat, 500);
loadchat();
});
</script>
Is there any other better way?
you could use Server Side Events which should allow the same thing but send the data to the browser when it has changed so the user isnt constantly requesting to see if a change has been made.
This is really cool article regarding chat application and best way to implement comet using jQuery.
Its works for me.
There is few hack needed in order to make it perfect because its work in timestamp so when you change your machine date its fail to perform but on server its work fine.
Replace text file implementation using xml that is good practice.
I wrote this couple of times. Ajax chat with polling from DB is not really a chat in real time. And you are right, using interval to poll data sucks. Anyway this is solution (one and only real solution).
You need to read this book. If you do not have jabber server I will give you acces to mine (user register update etc). Read the book and then contact me. It is XMPP + Strophe library chat (that what google and facebook are using)! So it is better to start over and learn something new!
I am using codeigniter frame work and currently working on social networking kind of stuff. My problem is, i need to notify the user of their friends activity with out reloading the page when the user clicks on the feeds link.
I have tried the following script,
<script>
setInterval(function() {
$('#reload').fadeOut("slow").load('user_feeds').fadeIn("slow");
}, 10000);
</script>
It increases the load to my server. any other idea for accomplishing this.
Thanks in advance.
Then try to decrease the time interval
<script>
setInterval(function() {
$('#reload').fadeOut("slow").load('user_feeds').fadeIn("slow");
}, 1000);
</script>
Manoj what you are trying to achieve here is notifications and the data source residing on the server, so there is no way you can do that from the browser either by a pull or push from the server.
pull you can achieve by constant polling / frequent polling - usually by setting a timeout and getting the notifications.
push - ideally not really push you can achieve this by combining AJAX+Comet, this can also be achieved by long polling too where you maintain a constant connection with the server for any new updates and return data to server when there is an update.
let me know if this helps or you need more information
This is actually a very interesting problem, what you need is an implementation of COMET .There is an event routing bus using comet called COMETD. COMETD also has javascript implementation and has bindings for Jquery. http://cometd.org/documentation/2.x/cometd-javascript
People,
I am developing a web page that need to be refresh everytime that the data base gets an update. I already have the checkDatabaseUpdate() done in my PHP code.
But now I reaaly need some help to develop a simple comet to wait for a response, and another to check for update.
Is there anybody with any simple example to help me?
Is comet the right solution for that?
Thanks,
What you want to say is that on the database are executed querys (INSERT, UPDATE, DELETE) in the backend and you want to refresh the front page of a user when that query`s are executed ?
Hmm .. use a jQuery (looping) to "Ajax check" for database update in the frontcontroller and then refresh.
function refreshPage () {
$.load('checkModifDb.php', function(response, status) {
if .... { do the trick here - check jquery load.api }
}
});
and then use setInterval( "refreshPage()", 10000 ); to run the function every 10 seconds and
refresh only if it founds that db was modified.
I can't think of anything right now but i guess with little modification you shoul do the trick. This is how twitter.com do it.
Is comet the right solution for that?
Because of the way that PHP works (having a web server daemon process incoming requests), combining it with long-polling techniques can make for an unhappy server. Each connected user is going to hold open a connection to the web server daemon. Depending on that daemon's configuration, you may find that comet is an effective denial of service attack against your own server.
You'd probably be better off with plain old short-lived ajax polling here.
I want to mess around with realtime information, and there is a pretty standard functionality that I want to duplicate:
It occurs here on SO when you're on a single-question view, typing your answer, and an alert pops up top saying "there are 3 news answers, click to show"
It also occurs on Twitter "There are 5 new tweets in this search: click to update"
I'm pretty versed in server and client side code, and what I'm looking for is the basic outline (not even psuedo code, but perhaps plain english) of how something like this happens.
Is there a CRON job on the server running every minute, that shoots a signal to a long-polled AJAX bit on the page?
Does the page itself poll a server?
Any and all solutions are welcome. Thanks!
You can implement that using an AJAX call that runs on the client side at a regular interval using the Javascript setTimeout method. You'll have a Javascript function that calls your server side method that checks if an update has occurred, displays any update, then calls setTimeout to call itself.
pseudo code:
function updateCheck()
{
//make ajax call
//do something if any update has occurred
setTimeout("updateCheck()", 10000); //second param is in milliseconds
}
From the top of my head, I'd make it via javascript - setting timeouts to question the server. That's only an educated guess though.
Looks like SO uses a periodical updater to make an ajax request to a url like:
https://stackoverflow.com/posts/2307584/answer-activity-heartbeat
This returns a JSON result:
{"Result":false,"Count":0}
Heres an example of the result when a new answer exists:
{"Result":true,"Count":1}