Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automatic notification in database change :similar with facebook friend request

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?

like image 284
Istiaque Ahmed Avatar asked Dec 05 '22 18:12

Istiaque Ahmed


1 Answers

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!

like image 154
Vishnu Haridas Avatar answered Dec 10 '22 11:12

Vishnu Haridas