views:

59

answers:

2

I have developed a Flash based chat client that displays messages posted by users belonging to a group along with their username and time of writing. The data is stored in a mysql database with the group id, user id and message so that it persists beyond chat sessions and allows users logging in at a later time to see the discussions, less of a chat, more of a notice board. When a user writes a message I want the chat clients to update instantly an appear to be real time chat. My solution so far has been to include an interval in my flash code that calls a PHP page which queries the database for new comments and return this to Flash.

I think from what Ive read that this approach is called long polling? is that right? Is this robust enough for volume? Would I be better looking at pushing the data to the client when there are changes? How do I detect these changes? I have looked at APE for example but I dont think that this stores the messages in a database.

Any suggestions?

+1  A: 

If you wanted to notify the application whenever a new message was received then you would have to implement some form of application on the server to monitor the database / be alerted of changes in it, and then pass updates out to clients.

This should be more efficient than just polling, because if you you think of a low traffic scenario, with polling you'll be knocking the hell out of the server regardless - this way you're only generating traffic when there is a reason to...

When you type in a comment, your client could notify this application directly (via a web service) of the change, and this helper app could then update the database...

It should be said though I'm no expert on chat programs - never been involved in one personally...

Martin Milan
+2  A: 

The bad thing about polling is, it can become expensive quite fast.

let's assume, you have 10000 users online, who want the chat to have a delay no bigger than 1 second. Then youn will bombard your server with 10000 requests per second.

for smaller things or things that needn't be up to date, polling is a good approach, since its simple and can't really go wrong.

if this is just for a small community with upto 100 people using the chat, then this should not be the problem.

apart from that, you could use APE or SmartFox, or Red5 or a lot of different things to create a persistent relay server, i.e. a server that has permanent connections to all clients and notifies them of any changes (e.g. new messages).

as always, my personal advice is to use haXe. You can use the chat tutorial to get started. haXe has quite a steep learning curve, but I think it's totally worth it. I'm happy to have left both ActionScript and PHP behind.

edit: what you describe is not long polling. Also, you can hardly do long polling with PHP, at least if classicaly used with Apache. Apache will create a certain number of PHP processes. Any time a request arrives, it'll look for a free PHP process and let it process the request. When the PHP process is done, it sends the response back to the client. If there are no free processes available, it will buffer the request until a process becomes available. Thus you could totally block your whole server if you tried doing long polling with PHP the classical way.

greetz

back2dos

back2dos
great thanks for this. :) best
undefined