javascript 数据库更改中的自动通知:类似于 facebook 好友请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8629083/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:02:39  来源:igfitidea点击:

automatic notification in database change :similar with facebook friend request

phpjavascriptjqueryfacebooksocial-networking

提问by Istiaque Ahmed

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.

我希望开发一个基于 php mysql 的社交网站。注册用户可以选择将其他用户添加为好友,就像在 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.

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

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.

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

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?

我的兴趣点是如何在 B 在线时让 B 知道新的好友请求,而不会让他/她刷新包含好友请求的页面?

回答by Vishnu Haridas

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

为了最终确定答案,我假设您很好地理解了数据库/通知 DATA 逻辑,现在您只关心如何将其传递给浏览器。我将在这里列出所有要点。

  1. HTTP is a PULLprotocol. You cannot pushdata from Server to Client.
  2. So, what you can do is - continuously poll the server for new notifications.
  1. HTTP 是一种PULL协议。您无法将数据从服务器送到客户端。
  2. 因此,您可以做的是 - 不断轮询服务器以获取新通知。

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.)
  1. 短轮询- 您向服务器发送 Ajax 请求以获取新通知。
    • 这是一个简短的请求,但您会每隔一段时间(比如 10 秒)连续执行一次
    • 服务器处理 PHP 并立即返回。
    • 您处理返回的数据(例如显示通知)
  2. 长轮询- 您发送与上述相同的请求。但...
    • 在这种情况下,请求的 PHP 文件进入无限循环,不断检查 DB。
    • 当数据库发生变化时,PHP 文件“回应”它并结束循环。现在请求完成,浏览器接收数据。
    • 您处理数据。
    • 启动另一个长轮询 Ajax 请求,步骤再次重复。
    • (额外:如果 PHP 没有在特定的超时时间内完成,则中止当前请求并开始新的请求。)

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, BeaconPushetc.
  1. 编写您自己的 Javascript 代码- 需要一些专业知识,但您可以学习!
  2. 使用一些库- 简单、省时。您可以使用PubNetBeaconPush等。

I hope that this makes a clear idea to you!

我希望这能让你有一个清晰的想法!

回答by Deept Raghav

you need to write javascript that sends request to server after each interval of time. and then on the server side you can query the database and respond to the client if there are any new friend requests.

您需要编写在每个时间间隔后向服务器发送请求的 javascript。然后在服务器端,如果有任何新的好友请求,您可以查询数据库并响应客户端。

use the javascript setinterval function

使用 javascript setinterval 函数

var refreshId = setInterval(function(){  
$.ajax({
type: "POST",
url: "request.php",
data: 'more_updates='+more_updates, // or any data you want to send
cache: false,
success: function(html){
$('.old_updates').prepend(html).fadeIn('fast'); // response from server side process
}
});
}

},10000);

here we are sending data every ten seconds. so on the server side if we have any new friend request pending that are new or not confirmed it updates the client side.

这里我们每十秒发送一次数据。所以在服务器端,如果我们有任何新的或未确认的新好友请求待处理,它会更新客户端。

回答by Vishnu Haridas

First, you don't need to keep separate databases/tables for each people. You can keep a single database table with the following columns:

首先,您不需要为每个人保留单独的数据库/表。您可以保留一个包含以下列的数据库表:

table_friendship

table_friendship

+------+---------------+-------------+------------+
|  id  |  friend_from  |  friend_to  |  approved  |
+------+---------------+-------------+------------+
|  1   |      A        |      B      |     NO     |
+------+---------------+-------------+------------+
|  2   |      C        |      B      |     NO     |
+------+---------------+-------------+------------+

Here in this table, the entry 1 means, A requests B for friendship, and NOT approved. Entry 2 means, C requests B for friendship, and NOT approved yet.

在此表中,条目 1 表示 A 请求 B 建立友谊,但未获批准。条目 2 表示,C 向 B 请求友谊,但尚未批准。

Next step, is to notify "B" that two requests have been arrived at the server. Sadly, the server cannot send messages to the client (Browser). So what we are doing is, continuously poll the server through AJAX request. The request works like any other page request, but the difference is, you can request a PHP page without reloading the browser.

下一步,是通知“B”两个请求已经到达服务器。遗憾的是,服务器无法向客户端(浏览器)发送消息。所以我们要做的是,通过AJAX请求不断轮询服务器。该请求的工作方式与任何其他页面请求类似,但不同之处在于,您无需重新加载浏览器即可请求 PHP 页面。

You can request the page friend_requests.phpin an interval of 10 seconds. The page should do the following:

您可以friend_requests.php每隔 10 秒请求一次页面。该页面应执行以下操作:

  1. Do the SQL "SELECT COUNT(*) FROM table_friendship WHERE friend_to = B AND approved = "NO"(Just a pseudo-SQL only!)
  2. Return the following data: Number of requests pending, and New requests etc.
  1. 执行 SQL "SELECT COUNT(*) FROM table_friendship WHERE friend_to = B AND approved = "NO"(只是伪 SQL!)
  2. 返回以下数据:待处理请求数和新请求数等。

At the Browser side, you can display this data in the "BOX" that you have specified.

在浏览器端,您可以在指定的“BOX”中显示这些数据。

When you approve the request, you again send the AJAX request to another PHP page, to change the approvedcolumn to "YES"

当您批准请求时,您再次将 AJAX 请求发送到另一个 PHP 页面,将approved列更改为“是”

More on Ajax - en.wikipedia.org/wiki/Ajax_(programming)

有关 Ajax 的更多信息 - en.wikipedia.org/wiki/Ajax_(编程)

回答by p0wl

I think you are searching for a push notification service.

我认为您正在寻找推送通知服务。

You can either implement your own (using Comet), or subscribe to a public service.

您可以实现自己的(使用 Comet),也可以订阅公共服务。

Examples:

例子:

PubNub, BeaconPush

PubNub, 信标推送

You will find a lot more with google.

你会发现更多的谷歌。

Edit

编辑

I think my answer was not clear enough. With my suggestion, you could do this (using pubnub):

我觉得我的回答还不够清楚。根据我的建议,您可以这样做(使用 pubnub):

User B (user ID 7) writes a friend request to user A (user ID 8). In your PHP handler you do:

用户 B(用户 ID 7)向用户 A(用户 ID 8)写入好友请求。在您的 PHP 处理程序中,您执行以下操作:

$pubnub->publish(array(
    'channel' => 'friend_requests_8',
    'message' => array( 'request_from' => '7' )
));

I'm not very used to php, but I hope you understand what I mean.

我不是很习惯 php,但我希望你明白我的意思。

On the Client page you can just register to your channel ('friend_request_') and then handle the requests:

在客户端页面上,您只需注册到您的频道('friend_request_'),然后处理请求:

// PUBNUB.subscribe() - LISTEN
PUBNUB.subscribe({
    channel  : "friend_request_<? echo $user_ID; ?>",
    callback : function(message) { alert('FRIEND REQUEST FROM USER ID: ' + message.request_from) }
})

So with this solution you will not have to handle any timings or loops, because pubnub handles this for you. Facebook does this (as far as I know) and BeaconPush is used by EA for the Battlelog, which, in my opinion, is a great website with a lot of interessting web techniques.

因此,使用此解决方案,您将不必处理任何时间或循环,因为 pubnub 会为您处理。Facebook 做到了这一点(据我所知),而 BeaconPush 被 EA 用于 Battlelog,在我看来,这是一个很棒的网站,有很多有趣的网络技术。