I'm wondering how it's implemented in Gmail, that every time you receive e-mail, the list of mails is automatically refreshed. It looks like the server is sending some kind of event to the browser, but how is it possible? Or maybe it’s simle: the browser ask the server for new messages every let’s say 2 seconds? But it would probably kill the performance…
我想知道它是如何在Gmail中实现的,每次收到电子邮件时,邮件列表都会自动刷新。看起来服务器正在向浏览器发送某种事件,但它怎么可能呢?或者也许它很简单:浏览器每次让我们说2秒钟就会询问服务器是否有新消息?但它可能会扼杀性能......
Anyone have some ideas?
谁有想法?
EDIT: OK, so if it's the simple answer, how do they manage performance? When I send an email from an other account to the gmail account the view is "refreshed" almost instantly. You were saying about a simple function that returns true / false, but it must have some logic (db connection or reads some files). How they manage it?
编辑:好的,如果这是简单的答案,他们如何管理性能?当我从其他帐户向Gmail帐户发送电子邮件时,视图几乎立即“刷新”。你说的是一个返回true / false的简单函数,但它必须有一些逻辑(db连接或读取一些文件)。他们如何管理它?
See also: How is GMail Chat able to make AJAX requests without client interaction?
4 个解决方案
#1
Dont know exactly which technoloy Gmail uses, but the concept is to open a channel - using reverse AJAX, comet or sprocket based techniques.
不知道Gmail使用哪种技术,但概念是打开一个频道 - 使用反向AJAX,彗星或链轮技术。
Think of it as the client requesting the server for data, but the server does not return for one minute unless it has new mail. Using this technique, the client can almost show the results in a real time manner and it does not have to poll every 2 secs. Makes sense?
可以将其视为请求服务器获取数据的客户端,但除非有新邮件,否则服务器不会返回一分钟。使用这种技术,客户端几乎可以以实时方式显示结果,并且不必每2秒轮询一次。说得通?
#2
gmail is, in fact, polling the server for updates. Not as often as every two seconds, though. That would be madness. A bit of testing with Tamper Data makes it look like maybe every 20 seconds, though there seem to be multiple events going through that confuse it a bit.
事实上,gmail轮询服务器以获取更新。但不是每两秒钟一次。那将是疯狂的。使用Tamper Data进行的一些测试使得它看起来可能每隔20秒,尽管似乎有多个事件会让人感到困惑。
Regarding your edit, I imagine they might have a last-activity timestamp on the account tracking in their database, with the client polling query retrieving that via Ajax and comparing with its last sync to determine whether it needs to do a full update.
关于您的编辑,我想他们可能在其数据库中的帐户跟踪上有最后活动时间戳,客户端轮询查询通过Ajax检索并与上次同步进行比较以确定是否需要进行完全更新。
#3
You have right with simple answer. Google Mail checking new messages on server via AJAX.
你有简单的答案。 Google Mail通过AJAX检查服务器上的新消息。
#4
It must be some kind of ajax listener that get informations every X seconds.
它必须是每隔X秒获取信息的某种ajax监听器。
I already set something like that for one of my projects. What I was doing is calling a function that was returning true or false. True if the page needed to be refreshed, false otherwise. Then if you have an update, you do another call to get the actual update. This way you don't have to refresh everything every time... but it's still intense on the server if you have a lot of users.
我已经为我的一个项目设置了类似的东西。我正在做的是调用一个返回true或false的函数。如果需要刷新页面,则为True,否则为false。然后,如果您有更新,则进行另一次调用以获取实际更新。这样你就不必每次都刷新一切......但如果你有很多用户,它在服务器上仍然很强烈。
In other words and like chaos said, it's polling the server.
换句话说,就像混乱说的那样,它正在对服务器进行轮询。
#1
Dont know exactly which technoloy Gmail uses, but the concept is to open a channel - using reverse AJAX, comet or sprocket based techniques.
不知道Gmail使用哪种技术,但概念是打开一个频道 - 使用反向AJAX,彗星或链轮技术。
Think of it as the client requesting the server for data, but the server does not return for one minute unless it has new mail. Using this technique, the client can almost show the results in a real time manner and it does not have to poll every 2 secs. Makes sense?
可以将其视为请求服务器获取数据的客户端,但除非有新邮件,否则服务器不会返回一分钟。使用这种技术,客户端几乎可以以实时方式显示结果,并且不必每2秒轮询一次。说得通?
#2
gmail is, in fact, polling the server for updates. Not as often as every two seconds, though. That would be madness. A bit of testing with Tamper Data makes it look like maybe every 20 seconds, though there seem to be multiple events going through that confuse it a bit.
事实上,gmail轮询服务器以获取更新。但不是每两秒钟一次。那将是疯狂的。使用Tamper Data进行的一些测试使得它看起来可能每隔20秒,尽管似乎有多个事件会让人感到困惑。
Regarding your edit, I imagine they might have a last-activity timestamp on the account tracking in their database, with the client polling query retrieving that via Ajax and comparing with its last sync to determine whether it needs to do a full update.
关于您的编辑,我想他们可能在其数据库中的帐户跟踪上有最后活动时间戳,客户端轮询查询通过Ajax检索并与上次同步进行比较以确定是否需要进行完全更新。
#3
You have right with simple answer. Google Mail checking new messages on server via AJAX.
你有简单的答案。 Google Mail通过AJAX检查服务器上的新消息。
#4
It must be some kind of ajax listener that get informations every X seconds.
它必须是每隔X秒获取信息的某种ajax监听器。
I already set something like that for one of my projects. What I was doing is calling a function that was returning true or false. True if the page needed to be refreshed, false otherwise. Then if you have an update, you do another call to get the actual update. This way you don't have to refresh everything every time... but it's still intense on the server if you have a lot of users.
我已经为我的一个项目设置了类似的东西。我正在做的是调用一个返回true或false的函数。如果需要刷新页面,则为True,否则为false。然后,如果您有更新,则进行另一次调用以获取实际更新。这样你就不必每次都刷新一切......但如果你有很多用户,它在服务器上仍然很强烈。
In other words and like chaos said, it's polling the server.
换句话说,就像混乱说的那样,它正在对服务器进行轮询。