javascript php中类似facebook的通知系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3604210/
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
A facebook-like-notification system in php
提问by Zakaria
How can I put in place a facebook-like-notification system: - A userA writes a message to the userB - A listener on the database routes the message to the userB - On the userB interface, the message appears instantly
我怎样才能建立一个类似 facebook 的通知系统: - 用户 A 向用户 B 写一条消息 - 数据库上的侦听器将消息路由到用户 B - 在用户 B 界面上,消息立即出现
How can I do that in php?
我怎样才能在 php 中做到这一点?
Thank you very much,
非常感谢你,
Regards
问候
回答by Johnathan Leppert
I wouldn't advise using either polling or trying to implement a push based solution using PHP, if you are going to have any sizable traffic. What happens is that eventually all the PHP processes get blocked, and then you can't serve anymore web requests.
如果您有任何可观的流量,我不建议使用轮询或尝试使用 PHP 实现基于推送的解决方案。发生的情况是,最终所有 PHP 进程都被阻止,然后您就无法再处理 Web 请求。
Look into node.js, cometd or another push based solution. If you need something simple and need to get up and running quickly, I'd recommend http://pusherapp.com/. They have a PHP client available and super simple API.
查看 node.js、cometd 或其他基于推送的解决方案。如果您需要一些简单的东西并且需要快速启动和运行,我会推荐http://pusherapp.com/。他们有一个可用的 PHP 客户端和超级简单的 API。
回答by bradenkeith
I have a system that works well set in place. It basically has the set up like this:
我有一个运行良好的系统。它基本上有这样的设置:
notification_id
user_id
module
action_type
added_by
read
read_date
added
Notification_id
user_id
模块
action_type
added_by
read
read_date
添加
This is a simple way of doing it. user_id is who it's for, added_by is the user id of whomever performed the action. Module is where it was performed at, and action_type is what happened (comment, deletion, added, etc). Read is if it's already been viewed, and read_date is when it was viewed.
这是一个简单的方法。user_id 是它的对象, added_by 是执行该操作的人的用户 ID。Module 是执行的地方,action_type 是发生的事情(评论、删除、添加等)。Read 是已经被查看过的,而 read_date 是它被查看的时间。
I then have a class that builds the verbiage based on what the values above are.
然后我有一个类,它根据上面的值来构建措辞。
I have then set up a crontab that will clean up old notifications in the database after so many days.
然后我设置了一个 crontab,它将在这么多天后清理数据库中的旧通知。
回答by v6ak
Comet server is not so simple to write in PHP, but it is possible to do so using sleeps. There are also socket functions that can be useful:
Comet 服务器用 PHP 编写并不是那么简单,但是可以使用 sleeps 来实现。还有一些有用的套接字函数:
- http://php.net/manual/en/function.stream-socket-client.php
- http://php.net/manual/en/function.stream-socket-server.php
- http://php.net/manual/en/function.stream-socket-client.php
- http://php.net/manual/en/function.stream-socket-server.php
Note that you should be aware of time limit. Maybe you should stop hanging after some time and make a new request, unless you can disable time limit for these requests.
请注意,您应该了解时间限制。也许您应该在一段时间后停止挂起并提出新请求,除非您可以禁用这些请求的时间限制。
The solution may also depend on the environment. You probably can't do many useful things on usual shared webhosting.
解决方案也可能取决于环境。您可能无法在通常的共享虚拟主机上做很多有用的事情。
回答by Jeff Standen
As mentioned by akellehe, you could simply poll using Ajax to look for new content if your users are likely to remain on the same page for long periods of time. If absolute real-time delivery isn't a necessity, it's a lot more efficient to check for new notifications when the page reloads -- especially if you cache (e.g. memcached) the total events awaiting a user and only invalidate that count in the event something is added to their notification queue. This will save you a lot of empty-handed database queries.
正如 akellehe 所提到的,如果您的用户可能长时间停留在同一页面上,您可以简单地使用 Ajax 进行轮询以查找新内容。如果绝对实时交付不是必需的,那么在页面重新加载时检查新通知会更有效率 - 特别是如果您缓存(例如 memcached)等待用户的总事件并且仅使事件中的计数无效一些东西被添加到他们的通知队列中。这将为您节省大量空手的数据库查询。
回答by KeatsKelleher
You could do this one of two ways. In PHP the easiest way would be 'polling'. The newer neater method is using HTTP Push with a Comet server; but that's not so great for PHP.
您可以通过以下两种方式之一执行此操作。在 PHP 中,最简单的方法是“轮询”。更新更简洁的方法是使用带有 Comet 服务器的 HTTP 推送;但这对 PHP 来说不是很好。
To use polling ou simply update a table in the database with UserA's message flagged as to-be delivered to user b. You have a script running every few seconds via ajax that polls the database for the message on userb's client. If the message is there you populate it. This is very inefficient.
要使用轮询,您只需更新数据库中的一个表,将用户 A 的消息标记为要传递给用户 b。您有一个脚本通过 ajax 每隔几秒运行一次,该脚本轮询数据库以获取 userb 客户端上的消息。如果消息在那里,则填充它。这是非常低效的。
回答by Diamond
Very much possible nowadays. I know this answer is coming very late but it will help those who will view this page for help. Check the link below for a great tutorial Real Time Notification Like Facebook
现在很有可能。我知道这个答案来的很晚,但它会帮助那些查看此页面寻求帮助的人。检查下面的链接以获得一个很棒的教程像 Facebook 一样的实时通知
回答by Webowiec
Nowadays the best solution is to use socket.io (http://socket.io/). It automatically chooses the best (and availible!) way to transfer data, including: WebSockets, Adobe Flash Socket, Ajax polling. You really don't worry about availability of communication methods on the client side.
现在最好的解决方案是使用 socket.io ( http://socket.io/)。它会自动选择最佳(且可用!)传输数据的方式,包括:WebSockets、Adobe Flash Socket、Ajax 轮询。您真的不必担心客户端通信方法的可用性。

