PHP/Ajax 通知系统模型?
我正在制作一个类似于 Facebook 的网站 - 诸如通知之类的东西使其变得相似。我真的不明白如何让通知发挥作用。好吧,我想出了如何通过查询从数据库中获取它们。
我认为该网站将遵循的结构将是针对不同应用程序的多个表 - 即:照片将添加图片 ID、文件名和一些用户 ID(如果“标记”或其他内容),以及如何将该信息发送到实时用户......超出了我的范围。
因此,我必须每隔几秒钟运行几个查询,扫描数据库表以查找在所有状态为未读的应用程序表中找到的用户的 $_SESSION['id'] ?
另一种可能性是每个用户都有自己的表?那是..很多。哈哈。
或者只是一个通知表,其中最新的通知被推送到具有唯一 ID 和用户 ID 的表中?
我真的无法解决这个问题,哈哈。
另外,实时显示通知?我知道 Facebook 使用长轮询并实时获取通知,但我不认为我可以在多个客户端的长轮询上运行大约 5-10 个查询(针对每个应用程序),否则会完全崩溃我的服务器,对吧?
关于如何尝试为社交网站制作通知系统的任何建议/代码?如果没有,我想我会选择静态通知而不是任何类型的实时通知。
话又说回来,每隔几秒查询一次服务器以获取每个页面加载的新通知的负载会太大吗?使用ajax意味着长时间轮询,所以这是双输。
I'm making a website similar to Facebook - with things such as Notifications which make it similar. I don't really see how I can get notifications working. Well, I figured out how I could get them from the database, with a query.
The structure I think the site will follow would be multiple tables for different applications - ie: Photos would add in an ID for the picture, a filename, and a few user IDs if 'tagged' or something, and how to send that information to the user in real time.. is beyond me.
So I would have to run several queries every few seconds scanning the database tables for the $_SESSION['id'] of the user being found in all the applications tables with a status of unread?
Another possibility is that every user has their own table? That's .. a lot. lol.
Or just a notifications table with the most recent notification being pushed to the table with a unique id and a user id?
I really can't wrap my head around this, lol.
Also, displaying notifications in real time? I understand Facebook uses long-polling and gets the notifications in real time, but I don't think I could leave about 5-10 queries (for each app) running on a long poll for multiple clients, or that'd completely crash my server, right?
Any advice/code on how I could try and make a notification system for a social networking-ish site? If not, I think i'll go with static notifications rather than any sort of realtime.
Then again, that'd be too much load querying the server every few seconds for a new notification on every page load? Using ajax would mean long polling, so it's a lose-lose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说长轮询就是答案。 Gmail 和 Facebook 都使用这种方法进行实时通知。您唯一的其他选择是带有数据服务的 Flex,但这不是 PHP。
在性能方面,查询一次只会拉取0-5个通知,如果表索引正确,并且查询写得好,那么其中5个查询不会对您的服务器产生重大影响。
此外,如果 Gmail 和 facebook 正在这样做,那么按理说你也可以这样做。诚然,他们有大量的服务器来支持所有用户,但我要冒险说你没有他们那么多的用户,所以服务器技术现在可以工作。当您获得如此多的用户时,您当前的服务器无法处理负载,那么您就需要投资更新更强大的服务器。
I would say Long polling is the answer. Gmail and Facebook both use this method for real-time notifications. Your only other alternative is Flex with a dataservice, but that is not PHP.
In terms of performance, the query is only going to pull from 0-5 notifications at a time, and if the tables are indexed properly, and the query is written well, then 5 of these queries will not be a significant impact on your server.
Furthermore, if Gmail and facebook are doing it, then it stands to reason you can also do this. Granted, they have a ton of servers to support all their users, but I am going to go out on a limb and say you don't have as many users as they do, so as a result the server technology will work for now. And when you get so many users your current servers can't handle the load, then you invest in newer more powerful ones.
这是我的看法。
您可以为状态、照片和视频创建不同的表格。
每当有人对视频或某些内容发表评论时,您都可以执行查询来存储通知以及喜欢它的用户的信息,您也应该设置一个状态字段,这样您就可以根据已看到和未看到的内容进行查询被用户看到。
您可以输入照片或状态所在页面的 url,以便当用户登录时,您每五分钟执行一次查询,检查是否有未读通知,如果有,您将在左下角的小 toast 消息中显示它们像facebook一样在屏幕的一侧。
单击 toast 消息时,您可以执行 ajax 调用来更新通知的状态以进行读取,这样它就不会再次显示,并且在成功回调中,您可以将用户带到状态更新所在的页面。
Well here is my take on it.
You could create different tables for status, photos and videos.
Everytime somebody comments on a video or something you can do the query to store the notification along with the information of the user who liked it, you should set a status field too, so you can query based on which has been seen and which has not been seen by the user.
You can put the url of the page where the photo is or status is located so when the user is logged in you do a query every five minutes checking for unread notifications, if there are any you display them in a tiny toast message on left bottom side of the screen like facebook.
On click of the toast message you can do an ajax call to update that status of the notifcation to read so it does not show up again and in the success call back you can take the user to the page where the status update is.