Home > misc > Facebook chat 跟踪报导 —— comet with mochiweb

Facebook chat 跟踪报导 —— comet with mochiweb

May 16th, 2008 :: jackyz

话说 facebook 推出了支持高并发的 web im 引起众“hacker”(与 linux hacker 中的 hacker 相同的那个词)的一片哗然,纷纷出手研究其秘密。互联网上迅速的出现了一批“拆解 facebook webim”的文章。这里就是其中相当有意思的一篇。其中的干货如下:

1 作者放出 firebug 发现,http header 的 server 字段赫然写着 MochiWeb/1.0 的字样。

2 作者开动脑筋,在 mochiweb 基础之上,写了一个“最简单”的 facebook webim 概念模型,mochi loop 的核心代码如下:

...
'GET' ->
 
case Path of
    
"chat" ->
      
% 1) subscribing
      
Room ! {self(), subscribe},
      
% 2) waiting
      
receive
        
Message ->
          
% 3) everything went right
          
Req:ok({"text/plain", Message})
      
after 10000
        
% 4) oOops, too long buddy.
        
room ! {self(), unsubscribe},
        
Req:not_found()
    
end;
...

简而言之就是:在 browser 和 mochiweb 之间保持 10 秒的长连接,这(10s的)期间收到的任何消息都会即时发送给 browser ,然后再由 browser 内的代码再次发起下一个连接。作者提供了完整例子代码的下载。PS.关于这一实现方式的更多说明也可以参考拙作“the google way”。

当然这只是一个“概念模型”,距离“实用价值”仍有一段距离。比如:这个 web 层如何与 ejabberd 接起来,如何识别同一个用户,如何增加更多的“聊天逻辑”。

有兴趣(有时间)深入研究的读者可以移步阅读原文。如果看不到原文,请留言,以便“进行盗版”。

misc

  1. boost
    May 16th, 2008 at 09:02 | #1

    erlang相关的应用越来越被人推崇了。
    呵呵。mochiweb我还在研究。希望楼主回头指点指点哦。

  2. May 18th, 2008 at 04:12 | #2

    I’m so sad that I cannot understand ;-)

    Thanks for the link though.

    – Yoan

  3. sofie
    June 9th, 2010 at 20:19 | #3

    \(^o^)/~

  1. October 21st, 2008 at 12:00 | #1