Simple comet example using php and jquery - php

Can anyone give me a good and simple example of the comet technique using PHP?
I just need an example that uses a persistent HTTP connection or something similar. I don't want to use a polling technique, because I have something like that set up and not only is it difficult to work with and manage its a big hog of resources. Also I am using IIS7 not Apache.
A good example would be really helpful so I can move on from this ugly polling technique.

You should use polling, or use a web server which is specially conceived for long requests and COMET, with a good JS backend:
function listen() {
$.get("/mylongrequestfile", {}, function(data) {
$("#mydiv").html(data);
listen(); // then launch again
}));
};
Remember that COMET is "wait for data, if there's data return and exit", so JS backend will have to parse the data and re-launch the process of asking the server.
In this example, if there is a server side problem or just a disconnection from the user side, the entire process will be broken (the function is only called if the request is successful)

Check this out: How to implement COMET with PHP.
This is not using JQuery. It is made using PHP and Prototype. It is very easy to understand. I think you can made JQuery script easily after viewing this.

I have a very simple example here that can get you started with comet. It covers compiling Nginx with the NHPM module and includes code for simple publisher/subscriber roles in jQuery, PHP, and Bash.
http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/
A working example (simple chat) can be found here:
http://cheetah.jamieisaacs.com/

Never having used this technique and studying the Wikipedia article on the topic, "Long Polling" seems like the only viable solution. It sounds pretty simple to implement by infinitely looping and sleeping a script on the server. There's some actual code in the HTTP Streaming page linked to from the Wikipedia article.
Have you tried any of this and stumbled on specific problems?

Check out this demo video for implementing Long Polling ( comet )..
It might help you all
http://www.screenr.com/SNH

You can take a look at this article, it's a really good start to understand comet programming concepts.
You will find two examples on it. The first one use the iframe technique whereas the second one use a persistent connection.

For IIS, there's WebSync. Since you're using PHP, however, you might be better off with WebSync On-Demand. Either one will give you the server-push you're looking for, and is simple to use. Check out this question as well, which is basically what you're after.
Here's a simple example of WebSync On-Demand in action using no scripting language. Simply open in two windows, and see the publish/subscribe in action.
To publish from the server, you can use the PHP api.

Related

jQuery and PHP - Comet

I've searched Google a lot. In all the examples having Comet with PHP and jQuery. They are doing:
setTimeout(function(){ check_new_data_function() }, 5000);
function check_new_data_function(){
$.ajax{
blah
}
}
And yes, it's pretty simple to use Comet with checks in every 5 seconds. But this could not be called "COMET" at all, isn't the purpose of Comet to not loop our requests? and instead get the data which is Pushed from server?
See this example.
Or another clip.
They are all having a function which gets the data from another file with $.ajax requests by looping. But are they really Long Polling/Comet?
As far as I knew when we didn't want to put the server under many $.ajax request which are looped, we would use Comet, in Comet the data is gonna Pushed from server to the client browser, was I wrong?
Could someone make this idea clear for me?
Can I Use on Web Sockets - Working Draft:
Partial support refers to the websockets implementation using an older
version of the protocol and/or the implementation being disabled by
default (due to security issues with the older protocol). Microsoft is
currently experimenting with the technology.
http://i.imgur.com/20X5z.png
The technology just isn't there yet. Watch this video from SymfonyLive if you want a better grasp on what the specs for HTTP and REST mean. It's interesting, and apparently Twitter goofed.
Also see:
http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations

RealTime live notification Jquery PHP

I'm tried to implementing a live notification which is like the facebook newsticker and the notifications(on top). I was wondering what's the good solution to archieve this. I implemented an chat application before which I fired AJAX GET request in certain time to archieve the realtime, but it seems not good. I checked facebook using firebug, there is no GET request fired(or it is hided?)
Now here is my scenarion:
In main.php
I have a live ticker and a notifications button.
In bulletion.php and User.php
When I perform an add user,it will go heading to the bulletin or user.php.
How can I get informed in main.php when bulletin.php,user.php successfully created in database?
I checked this question as well before I asked.
notification system in PHP/jQuery
Realtime and php?
I was wonder,what's is long poling Ajax and session-based notification.How it can be archieved? I know Node.js can be good in implementing realtime, can it combine with PHP? and memcached ?
Any can provide sources to refer or example might be good.Thank you.
As a side note, PHP isn't really the best language to use when it comes to push notifications, it's really built around typical get/response kinda flows.
I use PHP for all my page stuff, but when it comes to push notifications of any sort I really like http://www.nodejs.org/ and http://socket.io/ to go with it. They're very easy to get setup, and will play well with you using php for the majority of your work, then using node to deal with push notification kinda stuff.
Have you thought about using HTML5 WebSockets? Have a look at EventSockets and the kickstart project on Github.
I've had much success with the server-sent events standard. It's very simple and works perfectly but it's only supported in modern browsers.
Meteor is a comet server using PHP and JavaScript to push data to browsers. It's very slick and worth a look, although it may be a little hard to setup/implement.
Edit: Quick demo here

jQuery server side push with ajax

I am in the middle of making a social network, and i want it to be as smooth as facebook.
Like if you look in a console and look at logging, it doesn't update all the time with ajax calls.
on my site i have to load: notifications(the number of new notifs and the notifs themselves), friend requests(same as notifications), online friends(if there are any online it will load the pictures of the online users.) thats 6 ajax calls that is loaded every 10 second. and this causes a huge bandwidth waste and server requests.
Therefore i thought, what if the SERVER told the CLIENT when there was a new update instead of the CLIENT asking the SERVER every 10 seconds.
i have googled this problem and read about ajax push, and a framework called comet.
i just can't seem to find any info on how to implement this on jQuery.
I looked briefly into Comet. It appears to be ambitious, experimental and won't run on just any old server.
As I understand it, Comet doesn't really push as such but does something called "long polling", which I won't try to describe here. The web already has several good texts on the subject.
Personally, I would stick with the current plan (conventional AJAX) but make one general purpose call with all the necessary data bundled into an object and JSON encoded. This will reduce 6 requests down to one (every 10 seconds).
You can box-clever by returning nulls within the returned object for information that hasn't changed thereby minimising the length of each response.
As far as I know, you must make significant modifications on your webserver to get this thing to work. Also, server side php is not really a good option.
Somebody had already asked something similar here: Using comet with PHP?
You can try socket.io on node.js too. It works great for real time communication
http://socket.io/

PHP - jQuery - Comet chat

I am looking for a solution for PHP and comet. I would like to make a chat using Ajax to send the message. I call the function using jQuery and send data, it is simple.
But the problem is in retrieving new messages. How to retrieve messages using the new solution comet? I need a simple example.
I am looking for a solution for non-commercial use - for free.
In short, I would tell you to use Node.js as your backend technology.
Here's a more lengthy explanation: Creating a live checkers-like web app with PHP, JS, CSS and HTML?
if you're set on using php, you can use a comet server which will sit on top of apache (or whatever server you prefer) and proxy any non-comet requests to it. I've used one called streamhub which is written in java.

Keeping a live connection with php?

I am working on a project has me constantly pinging a php script for new data, so if I understand this correctly that means that the php script being pinged gets run over and over indefinitely. It works but i'm guessing its a huge strain on the server, and is probably considered ugly and bad practice. Am I right about that?
Is there any way I could keep the connection to the script alive and make use of php's built in output buffering to flush the contents I need, but keep the script running for infinity using some sort of loop so when new data is available it can be output. Is this a bad idea as well?
I'm just looking for input form developers out there with more experience.
One last thing...
Are there any other ways to keep a constant flow of data going (excluding technologies such as flash or silverlight)?
If what you have currently works and continues to work when tested against the kind of load you might expect in this application, it is not really considered bad practice. It is not a crime to keep it simple if it works. Anything that does what you are describing is going to go against the grain of the original model of the web, so you're venturing into shaky territory.
I do recommend you check out the Comet technique. It is mostly popular for the inverse of what you want - the server pushing information to a page continuously - but it can obviously work both ways. Although your mileage may vary, I've heard good things. As Wikipedia describes it:
In web development, Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Comet is an umbrella term for multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as Javascript, rather than on non-default plugins.
It almost seems like php wouldn't be the best choice of language for this. Possibly consider something like scala or erlang which are setup to handle this type of long lived messaging better.
You have to learn how to use sockets in php.
Start from here: http://php.net/manual/en/book.sockets.php
And afair here is useful manual about writing standalone php apps: Advanced PHP Programming
I'd say that depends. If you want the data transfers to be started by the client, your best choice here would be some ajax (like getxmlhttpobject or just iframes if you feel like cheating :P). If you want the transfers to be started by the server, then, perhaps php is not the language you want to use.
You can use ajax to have http-streaming. Take a look at ajaxpatterns.

Categories