Automatic notification of a database change: similar to facebook friend request

I want to create a php mysql social networking site. Registered users will have the opportunity to add another user as a friend, as is done on Facebook.

If user A clicks the “add a friend” link in user B’s profile, friend request records will be made in databases A and B, respectively. When B visits the pending request-show-page (as such on the profile page), the request will be displayed with request B db. It is very simple to do, I think.

But while B is online, C can make a request to friend B. I want to notify B that C made such a request, even if B does not refresh his profile page (or any page with the ability to display pending friend requests). As for the type of notification, this could be a field showing the total number of pending friend requests. Clicking on the field will show the details. Or it can be in any other form.

My interest is how to make B aware of a friend’s new request while B is online, without forcing him to refresh a page containing friend requests?

+4
source share
4 answers

To complete the answers, I assume that you understand the logic of the DATA database / notifications well, and now you are only concerned with HOW to deliver it to the browser. I will list all the points here.

  • HTTP is the PULL protocol. You cannot push data from server to client.
  • So what you can do is constantly polling the server for new notifications.

You can do two types of polls: -

  • A short survey . You send an Ajax request to the server for new notifications.
    • This is a short request, but you do it continuously at intervals (say, 10 s)
    • The server processes PHP and returns immediately.
    • You process returned data (for example, displaying notifications)
  • Long-term survey . You are sending the same request as above. BUT...
    • In this, the requested PHP file goes into an infinite loop, constantly checking the DB.
    • When a database change occurs, the PHP file echoes it and ends the loop. Now the request is completed and the data is received in the browser.
    • You are processing data.
    • Run another long poll of the Ajax request and repeat the steps.
    • (Optional: if PHP is not completed in a specific timeout, cancel the current request and run it.)

Further you can implement any of them in two ways: -

  • Write your own Javascript code . You need some experience, but you can learn it.
  • Use some libraries . Easy and economical. You can use PubNet , BeaconPush , etc.

I hope this gives you a clear idea!

+9
source

you need to write javascript that sends a request to the server at every time interval. and then on the server side you can query the database and respond to the client if there are new queries to each other.

use setinterval javascript function

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 send data every ten seconds. so on the server side, if we have a new request for a new request, waiting for a new or unconfirmed, it updates the client side.

+4
source

First, you do not need to store separate databases / tables for each user. You can save one database table with the following columns:

table_friendship

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

Here in this table, entry 1 means that A is requesting B for friendship and is NOT approved. Entry 2 means C will request B for friendship and is NOT approved.

The next step is to notify “B” that two requests were received on the server. Unfortunately, the server cannot send messages to the client (browser). So what we do is constantly polling the server through an AJAX request. The request works like any other page request, but the difference is that you can request a PHP page without reloading the browser.

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

  • Make SQL "SELECT COUNT(*) FROM table_friendship WHERE friend_to = B AND approved = "NO" (only for pseudo-SQL!)
  • Return the following data: the number of pending requests and new requests, etc.

On the browser side, you can display this data in the "BOX" you specify.

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

Learn more about Ajax - en.wikipedia.org/wiki/Ajax_ (programming)

+2
source

I think you are looking for a push notification service.

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

Examples:

PubNub , BeaconPush

You will find much more with Google.

Edit

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

User B (user identifier 7) records a user request to user A (user identifier 8). In your PHP handler you do:

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

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

On the "Client" page, you can simply register on your channel ("friend_request_"), and then process the requests:

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

Thus, with this solution, you do not have to handle any timings or loops, because pubnub handles this for you. Facebook does this (as far as I know), and BeaconPush uses EA for Battlelog, which, in my opinion, is a great website with many interesting web technologies.

+1
source

Source: https://habr.com/ru/post/1387926/


All Articles