数据库更改中的自动通知:类似于Facebook的朋友请求 [英] automatic notification in database change :similar with facebook friend request

查看:221
本文介绍了数据库更改中的自动通知:类似于Facebook的朋友请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个基于php的mysql社交网站。注册用户可以选择将其他用户添加为朋友,就像在Facebook中一样。



如果用户A点击用户B的个人资料上的添加好友链接朋友请求记录将在A和B的数据库中进行。当B访问等待的朋友请求 - 显示页面(这样的个人资料页面)时,将显示该请求查询B的数据库。这很简单,我认为。



但是当B在线时,C可以向B发送一个朋友请求B.要通知B,C已经提出了这样的请求,即使B没有刷新他/她的个人资料页面(或任何页面)可以选择显示等待的朋友请求)。对于通知类型,它可以是一个显示朋友请求等待总数的框。单击该框将显示详细信息。或者它可以是任何其他形式。



我的兴趣是如何让B知道一个新的朋友请求,而B在线,而不使他/她刷新包含朋友请求的页面

解决方案

为了完成答案,我假设你理解数据库/通知DATA逻辑,现在您只关心如何将其传送到浏览器。我将在这里列出所有要点。


  1. HTTP是一个 PULL 协议。

  2. 所以,您可以做的是不断轮询服务器的新通知。

您可以进行两种类型的投票: -


  1. 短轮询 - 您向服务器发送一个Ajax请求以获取新的通知。

    • 这是一个简短的请求,但是您会在一段时间内持续执行(例如10秒)

    • 服务器会立即处理PHP

    • 您处理返回的数据(例如显示通知)


  2. 长轮询 - 您发送与上述相同的请求。 BUT ...

    • 在这里,请求的PHP文件进入无限循环,连续检查数据库。

    • 在那里,PHP文件回显它并结束循环。现在,请求完成,并在浏览器中收到数据。

    • 您处理数据。

    • 启动另一个长时间轮询的Ajax请求,步骤重复。

    • (额外:如果PHP没有在特定的超时时间内完成,则中止当前请求并启动新的请求。)


接下来,您可以通过两种不同的方式实现以下任一方法: -


  1. 编写自己的Javascript代码 - 需要一些专业知识,但您可以了解它!

  2. 使用一些图书馆 - 方便,省时。您可以使用 PubNet BeaconPush 等。

我希望这能给您一个清晰的想法!


i wish to develeop a php mysql based social networking site. Registered users will have the option to add another user as a friend just as is done in Facebook.

If user A clicks on the 'add friend' link on user B's profile, friend-request records will be made in A's and B's databases accordingly. When B visits the waiting friend-requests- showing-page ( as such the profile page), the request will be shown querying B's db.This much is pretty simple to do i think.

But while B is online, C can make a friend-request to B. I want to make a notification to B that C has made such a request even if B does not refresh his/her profile page(or any page with the option of showing the waiting friend-requests ). As to the type of notification, it can be a box showing the total number of friend-requests waiting. Clicking on the box will show the details. Or it could be in any other form.

My point of interest is how to make B aware of a new friend request while B is online without making him/her refresh the page containing the friend-requests?

解决方案

To finalize the answers, I assume that you understand the database/notification DATA logic well, and now you are concerned only about HOW TO deliver it to the browser. I will list all the points here.

  1. HTTP is a PULL protocol. You cannot push data from Server to Client.
  2. So, what you can do is - continuously poll the server for new notifications.

You can do two types of polling :-

  1. Short-polling - You send an Ajax request to the server for new notifications.
    • This is a short request, but you do it continuously in an interval (say 10s)
    • The server process the PHP and immediately returns.
    • You process the returned data (eg. Show notifications)
  2. Long-polling - You send the same request as above. BUT...
    • In this, the requested PHP file goes into an infinite loop, checking DB continuously.
    • When a DB change is there, the PHP file "echo" it and ends the loop. Now the request is completed, and data is received at Browser.
    • You process the data.
    • Start another long-poll Ajax request, and the steps repeat again.
    • (Extra: If the PHP is not completed in a particular timeout, then abort the current request and start new one.)

Next, you can implement any of these in two different ways:-

  1. Write your own Javascript code - Needs some expertise, but you can learn it!
  2. Use some libraries - Easy, and time-saving. You can use PubNet, BeaconPush etc.

I hope that this makes a clear idea to you!

这篇关于数据库更改中的自动通知:类似于Facebook的朋友请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆