Javascript 如何实现 facebook 之类的通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5359773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to implement facebook like notification?
提问by Vin
I am trying to implement a Facebook-like live notifications system to notify users whenever someone adds them as friend, like their post or posts replies to their comments.
我正在尝试实现一个类似 Facebook 的实时通知系统,以便在有人将他们添加为朋友时通知用户,例如他们的帖子或帖子回复他们的评论。
All the database and PHP part is done, but I can't figure out how to implement it like Facebook has.
所有的数据库和 PHP 部分都完成了,但我不知道如何像 Facebook 那样实现它。
Whenever someone likes/comments on your post in Facebook the light blue box appears at the bottom left corner of the screen. This happens as soon as someone hits like button or posts comment in Facebook. I would like to know what I should do to implement this.
每当有人喜欢/评论您在 Facebook 中的帖子时,浅蓝色框就会出现在屏幕的左下角。一旦有人点击“赞”按钮或在 Facebook 发表评论,就会发生这种情况。我想知道我应该怎么做才能实现这一点。
Using YUI or any JavaScript framework I can query a database table after n
seconds to check for notifications. This method is too heavy.
使用 YUI 或任何 JavaScript 框架,我可以在n
几秒钟后查询数据库表以检查通知。这个方法太重了。
I was wondering if there is any server side mod or scripting can be done so that whenever there is new notification entry in my database table the server will tell that particular client. That way unnecessary request calls from client to server will be avoided completely and system can work efficiently for website with more than 50,000 users online at a time.
我想知道是否有任何服务器端 mod 或脚本可以完成,以便每当我的数据库表中有新的通知条目时,服务器都会告诉那个特定的客户端。这样就完全避免了客户端到服务器的不必要的请求调用,系统可以有效地处理同时在线超过50,000个用户的网站。
How can I achieve this?
我怎样才能做到这一点?
采纳答案by Matthew Flaschen
回答by Samudra
回答by Dead Programmer
回答by emre kocyigit
You basically set a request up, like callng the service that asks your server/db for the notifications of that user. You may do a while loop that retries if theres no notification (maybe Thread.Sleep
in between searches). Your js request will timeout, then you can call the function again in timeout. This means long polling afaik
您基本上设置了一个请求,例如调用向您的服务器/数据库询问该用户通知的服务。如果没有通知(可能Thread.Sleep
在搜索之间),您可以执行 while 循环重试。您的 js 请求会超时,然后您可以在超时时再次调用该函数。这意味着长时间的轮询 afaik
回答by Bleaourgh
The only way to do it is to have some sort of mechanism (e.g. Javascript) to repeatedly poll the server for updates. Doing server pushes to web browsers isn't possible.
唯一的方法是使用某种机制(例如 Javascript)重复轮询服务器以获取更新。将服务器推送到 Web 浏览器是不可能的。