jQuery ajax chat reload - php

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!

Related

Real time notification for website

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>

jQuery .get() data from JSON formatted data and refreshing some DOM element to display it

I have heard about technologies like socket.io, but it is too advanced for me.
I created PHP file that displayes JSON formatted data in array.
I use the jQuery function .get() with the URL datafile.php to get the string and display it in the pages. I use timer loop for this .get() and it is run every few seconds. This is how I simulate updating texts without refreshing the page.
But I really think if it is the right way of doing this. is there some better approach?
Here is my current script (the highlights classes are only to make little flashing on the element to show that value has been changed):
setInterval(function() {
$('.total-xp .number').addClass('highlighted');
setTimeout(function() {
$('.total-xp .number').removeClass('highlighted');
}, 1500);
$.get("data.php", function(data) {
$(".total-xp .number")
.text(data.total_xp_data)
}, "json");
}, 10000);
Two comments. You should look at socket.io. Socket.ios allows your server to push data updates to subscribed clients when they happen, thereby saving on wasteful polling for updated data from every attached client, this is pretty much accepted practice. I've no personal experience on socket.io however I've use Signal R in .NET for these kind of problems and found it works well.
Second. I would be tempted to return json to your client, then your client can render the data as appropriate. Then if you need another interface to present the data you don't have to write another server side method, or if you need to change your UI you don't need to reploy the server side components.
If your really can't face the effort of socket.io then your solution will work, it will be hard to get anyone to agree it is best practice.

need to refresh the page content

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

Jquery AJAX get new mysql records in real time

Is there any twitter-like solution for retrieving new mysql records using PHP in real-time and having them slide down
Thanks,
Try like this
$(function() {
setInterval(function() {
$.ajax({
type: "GET",
url: "location/of/server/script.php",
success: function(html) {
// html is a string of all output of the server script.
$("#element").html(html);
}
});
}, 5000);
});
It will update on every 5 seconds.
This is only an example script,fadein effects can be added after the sucess .Hope it helps
i would recommend you to read about websockets if you really want real-time updating. otherwise you could use the jquery setInterval.
websockets:
probably the best one
you can find lots of tuts on the web:
results
If you really want real-time, you're going to want the notifications to be "pushed" to the client, not polled. To do this, on the client side you will probably want to use something like HTML5's "event source" to listen for events and update the UI.
On the server side, there isn't any way for mysql to notify PHP of new records, so you would likely want to use some kind of a message queue (like RabbitMQ or ActiveMQ). You would have to modify the code that inserts into your database so that it also notifies your message queue.

I need an example of comet refresh html when database is updated!

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.

Categories