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
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 have a project that needs a sort of shopping cart that is always available at the top of the screen. Whenever someone hovers over the "cart" icon, a list of everything that's inside is shown.
However, if an user has two tabs of my site open, and in one of them something is added to the cart, the other one will be outdated and a refresh will be required.
My question is: if I use AJAX to constantly update the list of items (which will require sessions and database checks), will it be a big enough load on the server (or even on the browser) for it to be a problem, or is this common practice? If it is a problem, what other ways can I go so every tab an user opens is always updated?
If someone could show me the path so I can study more about it, even the name of what I should look up, I would be really grateful. Thanks.
there is no load on browser, just one more request in server ... maybe these requests are useless,
there is another way name server push
APE (Ajax Push Engine) :: Comet server :: Real time data streaming ->
http://www.ape-project.org/
nginx_http_push_module - Comet For The People -> http://pushmodule.slact.net/
node.js -> http://www.nodejs.org/
Socket.IO: the cross-browser WebSocket for realtime apps. ->
http://socket.io/
Comet with Nginx and jQuery | Coach J ->
http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/
if your request count is more than your server power use this way but if you have little request and not important you can use server pull frequently
Ajax is the solution here - it has a very light footprint.. as long as you don't use it indiscriminately, it is pretty much the same as reloading the page (much less if done correctly as its serving a smaller document). In fact, research has shown that ajax can cut the server bandwidth usage by over 60%.
You can read about the speed issue specifically here.
I learned Ajax over at Tizag, they have a brilliant Tutorial.
Hope this helped :)
you can make the ajax call on an interval to load the div with latest data. Something like this
(function getLatestCart(){
setTimeout(function(){
$("#latestCart").load("getCartDetails.php", getLatestCart);
}, 10000);
})();
this will call the method on every 10 sec(you can change the time interval) and load the latest cart to the div with id latestCart.
You can track locally this changes.
Use UserData (IE7 and down) or domStorage with updated data, and check every 10-20 seconds if the data was changed.
how can I automatically add the value of a database row (PHP), to the page, without refreshing the page itself, when the mysql database table changes?
So, it is a bit like this: Automatically refresh browser in response to file system changes? , but instead of refreshing the browser with the file system changes, update the content, without refreshing anything, when the databse changes.
Thanks. I have tried to make this as clear as possible.
Please note this is outdated answer. Recent ways of doing that is: websockets, server-send events. Nice example of that is Firebase. You can find simple code example in: https://github.com/laithshadeed/wsk-feedback. In this example you will see that updating firebase will send event to the browser via websocket, then the UI will update.
This is called Comet/Reverse Ajax/HTTP server push http://en.wikipedia.org/wiki/Comet_(programming). They are many techniques for doing this as well as many existing frameworks to do it for you.
There are many answers in SO about Comet https://stackoverflow.com/search?q=comet
Simple implementation would be javascript setTimeout and setInterval to check server status, with trigger/stored procedure on mysql.
For depth dive into Comet. There are two cool books about this:
Comet and Reverse Ajax 2008 By Dave Crane
Chapter 4 (River of Content) - Building the Realtime User Experience 2010 By Ted Roden
Update: You may look to the newer techniques in HTML5 like Websockets and Server-sent Events, although IE does not support them well, at the moment Server-sent events is not supported in IE and Web Sockets only supported in IE10
It's not a truly simple task, but it's not that bad. You need a few things working in concert:
A javascript routine on your page that checks with the server at specifiedintervals
A page on your server that reports changes when polled
A callback function on your page that inserts new elements (or updates/deletes existing elements) when changed data is reported by the server.
How you determine which data has been changed is something you will have to think about. The easiest way is probably to have a "modified" field maintained for each record. This way when your javascript polls the server it can include a "last time I checked" timestamp and the server only has to return changes that are more recent.
It's not quite so hard as it may at first appear. Take advantage of prebuilt libraries like jQuery and you can do things like:
$.ajax({
url: 'http://example.com/checkforupdates.php?last=' + (new Date().getTime()),
context: document.body,
success: function(data){
// do something here to add/update/remove elements on your page
// using the information returned in the data argument.
}
});
Manipulate the DOM with 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.