开发者

Using Memcache to Build a PHP/Javascript Chat Room [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Using memcached as a database buffer for chat messages

A friend of mine was telling me how he used Memcache to build a PHP/Javascript realtime chat room, but I can't figure out how Memcache would help when the data is updated (possibly) every few seconds. Of course, he told me 开发者_StackOverflowto figure it out on my own.

Can anybody provide any hint as to what advantages Memcache would serve in such an application? (I don't want to know how to do it exactly, just how Memcache would speed things up in an application where the data is constantly being updated.)


Memcache is the PHP interface to memcached (memory object caching)

It really isn't the right tool for the job. He's obviously storing the data as a key - this probably would be faster than hitting a database, but it still sucks.

If you're looking to implement some sort of real-time chat solution I would recommend looking into the following techologies. Read up on them and you'll find a solution which may suit your needs.

  • Flash (AS3) sockets with a chat server OR
  • HTML5 Web Sockets OR
  • COMET (Facebook uses this.)

Flash (AS3) sockets: There are many ways to build an Actionscript 3 chat system. It's actually pretty easy if you use one of the many existing solutions out there. IE: Smartfox. You can even use the External Interface to trigger Javascript events every time a message is received.

HTML5 Web Sockets As support for this is pretty flakey I recommend you look into degrading gracefully with Flash. Example of a library you can use: https://github.com/gimite/web-socket-js/

COMET/Long polling This is quite an interesting approach. Effectively what you're going to be doing is "blocking" the HTTP request server-side by not returning any data until some is available EG:

while(!d = data()) {
    // no data...
}
return "{data : d}"

Your browser will in most cases just sit there happily waiting for something to happen. Once data is received the browser will close the connection - this is when you process the data, and then re-open a connection (fire off a new ajax request) so that you are listening for new events!

To accomplish this I recommend using a non-blocking server such as Tornado (http://www.tornadoweb.org/)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜