My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Monday, April 21, 2008

Phomet - PHP Comet tiny library

Comet is a particular technique to interact asyncronously with the client.
Instead of perform a lot of calls using Ajax (polling) the server is able to send to client, whenever it wants, a generic response.

Unfortunately in the PHP world there's no simple way to implement this kind of technique and I'll write more posts to explain better how to implement this library and what is generally possible to do, or not possible yet, with Comet idea inside a dedicated PHP environment.

At the moment, the only thing you can do is read documentation inside JavaScript and PHP files (truly few lines) and test the first basic example, a server side clock in less than 30 lines of mixed code.

As last information, Phomet comes with all necessary to be optimized on client, and its final result is about 1.57Kb on client, and ridiculously 4.52 Kb on server, comments included :)

Here is the download, unpack them into your localhost, and go into phomet folder to view the first demo.

Compatibility? I've tested them with IE6, 7, 8, Opera 9, FireFox 1.5, 2, 3, Safari 3 windows but I am sure there are other browsers compatible.

I am waiting for your suggestions, bugs, opinions, whatever :D

Cheers, and please stay tuned for next Comet appointments :geek:

20 comments:

Anonymous said...

Hallo Andrea,

I tried your Comet solution (Phomet) for PHP and it runs very nice, but after 60sec there is an error because max execution time is exceeded and so running script ends.

For a later release maybe you show a more detailed example of how to get data from an internal php-function (news, stats, ...) and how the php-function have to present data (xml,...?!)? It would be very nice to have such an example to try own scripts and functions in php with your comet implementation.

Ronny

Andrea Giammarchi said...

You're right Ronny, I just published the code but the example and my explanation about Phomet usage are really rude.

That's why next days I'll write more and I hope I'll find time to create a better example with a simple chat, for example, in Gtalk style (userX is writing a message)

So please, stay tuned :-)

Andrea Giammarchi said...

P.S. the singleton instance has these settings:
ini_set('max_execution_time', 0);
set_time_limit(0);

so if you have not privileges in your host there are few things you can do with Phomet :(

Unknown said...
This comment has been removed by the author.
Andrea Giammarchi said...

CTAPbIu_MABP, those hacks are absolutely necessary. Consider that Dojo and others add even more hacks for IE, and probably for Opera too.

When these browsers will react as expected on readyState === 3, those hacks will never be useful at all.

About the typeof, the constructor is a public parameter that you can overwrite while typeof something, without usage of brackets, and === "function" check is an immutable behavior, so this is the best way to know if something is a function (and it is the best way only with functions, other primitives could be objects - i.e. new Number(123))

I think there's no PHP Comet implementation in about 1.5 Kb of client, with Ajax possibilities, and less than 5Kb on server.

So please, do not complain about these "simple" things, I prefer bugs report instead of a lot of "how I would like to see you code".

Thank you :)

Andrea Giammarchi said...

P.S. if you cannot change timelimit, you cannot have Comet ... I wrote an XMLSocket Flash bridge many times ago, and it could be a much better alternative but it requires Flash Player.

Unknown said...
This comment has been removed by the author.
Andrea Giammarchi said...

> ajax via iframe is necessary only with cross domain request IMHO

Comet is not Ajax, you should better understand what is Comet before we could talk about other stuff.

Timeout doesn't make sense as well, but if you understand Comet, it's truly simple to avoid timeout problem.

As I said, this is the code with basic example, and you have to wait other posts to know more about Phomet and its implementation.

Finally, and again, the constructor is a public property that you can easily overwrite.
I wrote that typeof is the best way to know if a variable is a *function*, and I said only with functions because null, as new Number(123) or new Something are typeof objects.

So please CTAPbIu_MABP, do not generate flames or confusion and read more carefully my answers.

Cheers

Andrea Giammarchi said...

Guys, generally, please read my code carefully before you reply about them.

Please, do tests before you start with long and off topic comments or just "to complain" without understanding what or why are you complaining.

If you have a problem that is absolutely pointless for the topic and it does not regard presented code, please find a forum to solve your problems or ask me privately.

Thank you to respect these simple rules :geek:

Unknown said...
This comment has been removed by the author.
Andrea Giammarchi said...

CTAPbIu_MABP, can you prove what you're talking about?

It's just because if I use this inside the while loop, and I close the browser, it "magically" stop to create files.

while(true){
Phomet::send('GMT '.gmdate('Y-m-d H:i:s'));
$fp = fopen(gmdate('Ymd-His').'.txt', 'w+');
fwrite($fp, 1);
fclose($fp);
sleep(1);
}

maybe this is my magic configuration, or maybe you do not know so well PHP (and I did not comment your "horrors" in the other post) and maybe you did not try, again, your code before you wrote this comment.

Regards

Andrea Giammarchi said...

> This post has been removed by the author.

This confirms what I thought about your way to post :)

Kind Regards

Anonymous said...

You might want to consider posting this on sourceforge, that way you could handle bugs and feature requests more efficiently. I will be messing out with your solution in a couple of days and I'm pretty anxious. Hopefully this project will be on-going and kept alive.

Jorge

Andrea Giammarchi said...

Jorge, I am still working on my code to fix everything as perfectly as I can. Do not worry about this project, it's so tiny and useful that I cannot stop to develop them.

Anyway, I will consider sourceforge after the 0.2 version and thank you for your interest about them.

Regards

Anonymous said...

There is already a project called Phomet, just a heads up to help us both avoid confusion.

Andrea Giammarchi said...

Sorry Morgan, I searched the Phomet name in google before I chose that name, but I did not find your project.

Currently, I am the first link if you look for Phomet, but since you published your stuff, that is totally different from mine, the 11th of January, I guess I should better change the name to avoid confusion.

I am sorry for this inconvenient, and I'll update this post ASAP linking your project and explaining why I changed the name.

Kind Regards

Unknown said...

Hi, how would i modify Phomet to read data from a text file and delete the text. Another application will make the data available and this varies, it could either be once every hour or maybe 100 times in 1 hour. Is Phomet useful for this and are there any manuals available for using Phomet if not can you suggest other methods.

N.B: Restricted to using PHP and Apache 2.2.9 and JavaScript.

Tim Smart said...

How are you avoiding the issue of scalability as PHP has a threading nature?

As soon as several people start using the service, the CPU and RAM usage stacks up.

Andrea Giammarchi said...

Hi Tim, good point.
I guess with PHP we have few options, a non blocking socket via Flash XMLSocket or Phico just for administration areas (limited users)

nlac said...

Hi Andrea,

congrat, i like this Phico! Some suggestions:

- Considering the possible permission issue for extending the max.exec.time for the script, would be useful to extend both the serve-side and the client-side components to get the following:
1. The server-side component could provide an api what make sure that the main cycle stops after the *real* max.execution time (before generation timeout error).
2. The client-side callback automatically could call itself again after the php cycle finished so readyState 4 arrived.

- In case of the server-side script uses session, it is important to surround the sleep(or usleep) in the cycle core by session_write_close() and session_start() like this:

session_write_close();
sleep(500000);
session_start();

to avoid the locking the session file while the script runs. Otherwise there will be impossible to change the navigate away or reload the page in the browser while the cycle runs or properly send paralel ajax requests.

Regards
nlac