使用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.

doutui6241
doutui6241 据我所知,每个请求都会对apache做一个新的过程......我怎么能摆脱这个?[对不起,我正在研究这个,信息可能不正确]
大约 8 年之前 回复
doumu1212
doumu1212 通过对解决方案的解释,它将为需要长时间轮询的每个用户打开一个新连接。实际上没有更好的选择,您不能拥有一个处理所有传入客户端连接的连接。
大约 8 年之前 回复

1个回答



node </ code>不会处理一个连接中的所有请求。 节点可以同时处理大量的并发连接,与node.js相比,其他人手中的Apache只能处理很少的并发连接</ p>

查看 websockets </ code > http://socket.io/ 。</ p>

Websockets允许全双工 客户端和服务器之间的连接。 HTTP协议为每个请求打开一个连接,并在客户端收到响应后连接结束。 Websockets允许我们保持连接打开。</ p>

如果在服务器端使用nodejs和websockets,则可以使用套接字将事件推送到客户端,而不是客户端轮询 服务器在一定的时间间隔内。</ p>

因此它可以帮助您避免长轮询。 </ p>

在您的情况下:
如果您决定使用nodejs和websocket,那么您将需要找到一种方法来触发服务器中的传入电子邮件事件并通知现有套接字有关该事件。 </ p>

因此节点还需要轮询POP以检查邮件,区别是什么</ strong> </ p>

想象一下,1000个用户登录到 使用轮询的应用程序。 每个用户每30秒轮询一次服务器。 所以每秒进行2000次POP民意调查。</ p>

使用nodejs,其次是2次POP轮询,如果有任何邮件,请通知套接字,客户端将处理该事件。</ p> \ n

但您应该考虑项目的总体要求。
如何决定何时使用Node.js? </ p>
</ div>

展开原文

原文

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?

dsa122870
dsa122870 +1 bronco,我现在正在陶醉但是如何将你的值存储在json文件中,然后检查该文件的时间戳,如果时间戳不同则发送新请求?
大约 8 年之前 回复
dovs36921
dovs36921 感谢您的信息...我将调查socket.io ..有任何codeignitor的例子吗?
大约 8 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问