1

Imagine we're having a message box system for our website users, what I want to do is that if a new message is sent to a member, while the targeted member is on his inbox, he could see a notification message right after he gets the new message.

For having this, we could have the "new_messages" div which is gonna refresh using jQuery with setInterval() and sends the query to see if there is a new message or not, but if we have 10.000 members, we can not send 10.000 queries, this could just kill the server!!! I want a notification to be shown for the targeted member only when hi gets a new message. something like that we could have a "listen" div in our page, it gets fired when the new message is triggered.

I've also read about the MySQL trigger, but it seems that's not gonna be a solution to this since that just could fire another SQL statement to the DB and not helping showing a notification to the user.

So what's the solution for such scenario?

Thanks in advance

behz4d
  • 1,886
  • 5
  • 33
  • 59

2 Answers2

2

You could look at PHP Comet system question for ideas on pushing live events from the server to the browser. Or try looking into the details of using Websockets as well. But these don't really go down well with PHP.

But if you looking to implement some kind of a chat try looking at http://www.phpfreechat.net/

Community
  • 1
  • 1
1

Or you could use Long-Polling, but PHP is not the best solution for this whole problem I think.

Best explained by a quote found here

I wouldn't do it for many reasons here are some :

PHP is made for fast execution (not for waiting) PHP will force you to do some kind of polling on the server side and relying on sleep() PHP will eat your RAM while so are spawning processes for each requests (Apache will do so)

But you can do it, using sleep, polling a database (or better a cache APC/Memcache).

If you want to do something like that jump into some technologies that can deal with events : Python (Tornado, gevent, eventlet, Twisted, …), Ruby (Eventmachine, …), Erlang, Scala, Server Side JavaScript (node.js, …), …

Sorry if it's not a direct answer to your question, I still encourage you to try doing it in pure PHP to see how it behaves (especially the web server).

Community
  • 1
  • 1
thomas
  • 2,442
  • 21
  • 28
  • hmmmm, I though it should be so easy to do, I don't believe that there is not a good solution for this in PHP!!! – behz4d May 20 '12 at 10:52
  • Believe it or not, PHP is not the first technology that comes to mind, when you speak about push/poll techniques. Not even the the fifth! – thomas May 20 '12 at 11:04