类似 facebook 的 php 通知系统

发布于 2024-09-16 13:40:25 字数 143 浏览 5 评论 0原文

我如何建立一个类似 facebook 的通知系统: - 用户A向用户B写入消息 - 数据库上的侦听器将消息路由到 userB - 在 userB 界面上,消息立即出现

我如何在 php 中做到这一点?

非常感谢,

问候

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

How can I do that in php?

Thank you very much,

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

独留℉清风醉 2024-09-23 13:40:27

您可以通过以下两种方式之一执行此操作。在 PHP 中,最简单的方法是“轮询”。更新、更简洁的方法是使用带有 Comet 服务器的 HTTP Push;但这对于 PHP 来说并不是那么好。

要使用轮询,只需更新数据库中的表,并将用户 A 的消息标记为要传递给用户 b。您有一个脚本通过 ajax 每隔几秒运行一次,该脚本轮询数据库以获取 userb 客户端上的消息。如果该消息存在,则填充它。这是非常低效的。

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.

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.

送舟行 2024-09-23 13:40:27

用 PHP 编写 Comet 服务器并不那么简单,但可以使用 sleep 来实现。
还有一些有用的套接字函数:

请注意,您应该注意时间限制。也许您应该在一段时间后停止挂起并发出新请求,除非您可以禁用这些请求的时间限制。

解决方案还可能取决于环境。您可能无法在通常的共享虚拟主机上做很多有用的事情。

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:

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.

有木有妳兜一样 2024-09-23 13:40:27

正如 akellehe 所提到的,如果您的用户可能长时间停留在同一页面上,您可以简单地使用 Ajax 进行轮询来查找新内容。如果绝对实时传递不是必需的,则在页面重新加载时检查新通知会更有效 - 特别是如果您缓存(例如 memcached)等待用户的总事件并且仅使事件中的计数无效有些东西被添加到他们的通知队列中。这将为您节省大量空手进行数据库查询的操作。

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.

何必那么矫情 2024-09-23 13:40:27

如今很有可能。我知道这个答案来得很晚,但它会对那些查看此页面寻求帮助的人有所帮助。请查看下面的链接,获取精彩教程像 Facebook 一样的实时通知

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

木格 2024-09-23 13:40:27

现在最好的解决方案是使用socket.io (http://socket.io/)。它会自动选择最佳(且可用!)的数据传输方式,包括:WebSockets、Adobe Flash Socket、Ajax 轮询。
您确实不必担心客户端通信方法的可用性。

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.

青巷忧颜 2024-09-23 13:40:26

如果您将拥有相当大的流量,我不建议使用轮询或尝试使用 PHP 实现基于推送的解决方案。所发生的情况是,最终所有 PHP 进程都会被阻止,然后您将无法再为 Web 请求提供服务。

研究node.js、cometd 或其他基于推送的解决方案。如果您需要一些简单的东西并且需要快速启动和运行,我建议您http://pusherapp.com/ 。他们有可用的 PHP 客户端和超级简单的 API。

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.

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.

谁的新欢旧爱 2024-09-23 13:40:26

我有一个运行良好的系统。它基本上有这样的设置:

notification_id
用户 ID
模块
动作类型
添加者
阅读
读取日期
添加

这是一种简单的方法。 user_id 是它的对象,added_by 是执行该操作的人的用户 ID。 Module 是执行的位置,action_type 是发生的情况(评论、删除、添加等)。 Read 表示是否已被查看,read_date 表示被查看的时间。

然后我有一个类根据上面的值构建措辞。

然后我设置了一个 crontab,它将在这么多天后清理数据库中的旧通知。

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文