Real time notification for website - php

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>

Related

jQuery ajax chat reload

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!

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

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.

Constantly Refresh the Page

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

Retrieve Database Value Without Page Load AJAX/PHP/Facebook Style

I am wondering if anyone could show, or explain with examples, how facebook checks its database for new messages? It seems to do it periodically, without loading the page. What would be the best way to achieve this in a PHP/MySQL/Jquery script?
Any help is always appreciated!
Cheers, Lea
you can do this: usign periodical updater
<span id="inbox-title"></span>
<script>
$.PeriodicalUpdater('/path/to/service', {
method: 'get', // method; get or post
minTimeout: 1000, // starting value for the timeout in milliseconds
maxTimeout: 8000, // maximum length of time between requests
}, function(data) {
$('#inbox-title').html('you have ' + data + 'new messages');
});
</script>
another option is to bind the onmousemove event and make the ajax call when than happes
There is actually a "page load", but it's a hidden request that doesn't reload the displayed page. Take a look at the jQuery Ajax command documentation for more details on one of the simplest ways to accomplish this (especially since you already mentioned using jQuery).
Have a look into reverse ajax with the COMET technique, this is a perfect use for it.
The idea behind it is to start an ajax request and let it timeout which could be 60 seconds, when it times out, start it again, here the browser has a (nearly) persistent connection to the server, if (for a simple example) a message gets created for a user. the server can reply to one of the hanging ajax requests that have been made (in this case by the recipient of the message).
No data is transfered while the xmlhttprequest and the server are waiting, but closing and reopening connections might be a burden on your server.

Categories