dongmi5177 2012-06-24 13:49
浏览 84
已采纳

使用codeigniter和node.js进行长轮询

I am in a position of researching new technologies , so I heard something about Long polling,node.js.

I need to create a web application that which use long polling.

On each and every page of this project i need to use polling , actually it checks if there is a new Email through POP.

So I think that I need to do the following

  1. Call a ajax request to the server
  2. Server receives the request and checks if there is a new Email
  3. If there is a new Mail server responds with its details
  4. If there is no new Email server started sleeping sometime and checking again until one new Email arrives.

so something like this

$(document).ready(function(){

is_there_new_mail();

function is_there_new_mail()
{

$.get(url,function(data){

if(data ==true)
{
//do some actions and call again
is_there_new_mail();
}


});


}


});

and in server something like this

   function check_mail()
    {

    //processing and checking is there a new mail on inbox 

    return $is_mail = $this->_new_mail()?true:false;

    }

    function receiver()
    {
    if($check_mail())
    {
     //send to client..
    }
    else

    {
    //sleep sometime and call mail function
    }

} 

I heard that doing something like this will open many connection on server, and if we use node.js we can manage it with in one connection.

I am using Codeigniter, and really new to node.js.

How can I implement node.js with codeigniter, or could you please suggest me something more about this scenario.

  • 写回答

1条回答 默认 最新

  • dqwn64004 2012-06-24 15:08
    关注

    Its not such that node will handle all the requests in one connection. Node can handle large number of concurrent connection at a time, where Apache in other hands can only handle very few concurrent connections as compared to node.js

    Look into websockets http://socket.io/ .

    Websockets allow full duplex connections between the client and the server. HTTP protocol opens up a connection for each request and the connection ends after the client receives the response. Websockets allows us to keep the connection open.

    If you use nodejs and websockets in the server end, you can push the events using the sockets to the clients, as opposed to the clients polling the server in certain intervals.

    So it will save you from long polling.

    In your case: If you decide on using nodejs and websocket then you will need to find a way to trigger an incoming email event in the server and notify the existing sockets about the event.

    So node will also need to poll POP to check mail, whats the difference

    Imagine 1000 users logged in to the app using polling. Each user will poll the server every 30 seconds. So 2000 POP polls per second.

    Using nodejs, 2 POP poll second, and if there is any mail, notify the sockets, and the clients will handle the event.

    But you should really consider the overall requirements of your project. How to decide when to use Node.js?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化