How facebook initializes recent news feed entries instantly? - facebook

I am developing a news feed module for my web project. News feed activities (post, filter, etc.) are nearly similar to Facebook. I ve used pagination pattern that initializes news page by page (eg: 20 post for every scroll) when user scrolls the page down (unlimited scrolling).
I wonder how Facebook initializes current news when one of your friends shares a post.
I guess it uses a trigger that sends an ajax request to get if new posts are exist. Using a timer trigger (with javascript timeout function which sends ajax requests every 10 seconds) would not be an effective solution for this problem.
Does anyone have any other trigger advices for me ?
Thanks in Advance.

If you were to monitor the network activity of the home page, you'd see calls to https://pct.channel.facebook.com/pull with some unique parameters attached to it. While watching it you'd see that the calls take a variable length of time, from 1 second to ~60 seconds.
This is referred to as Long-Polling, where the server waits until new information is available to send back a response. Meantime, the HTTP call is held in suspense as though the endpoint is loading. Once the server finds information available, it sends a response with the data, and closes the connection. Then the client re-opens the connection with another HTTP call once it receives data.

Related

Facebook Messenger Platform - Webhook Subscription

I have followed the steps to setup the Facebook Messenger platform. The verification GET web hook request work perfectly, as does the subscribe but when I submit the chat I keep getting the follow Developer Alert:
Hi Norah,
We've noticed that your Webhooks subscription for callback URL https://{domain}/v1/webhook has not been accepting updates for at least 16 minutes. Please verify that your callback server is functioning so you may continue to receive updates. If you need to update your callback URL, see https://developers.facebook.com/docs/messenger-platform/webhook-reference#webhook_setup
If your callback URL continues to fail to accept updates for 8 hours straight, we will disable your subscription. To reactivate the subscription, make a POST request with the same parameters, and it will be reactivated.
My post request works through POSTMAN.
Please can someone help me! This is driving me nuts!
Do you have logs on your server for that post requests?
Facebook requires you to return status code 200 for the post request, so they know that you successfully received it. When they havent, they try it again and if that still fails after several times, they will give you this alert.
Maybe facebook uses another content-type or message content than you used with postman.
Your server logs should give you more insights about that.
Depending on what Webhook events that you have subscribed for a page, there will be callbacks for those events, and more, on the url you have specified in the Web Hook set up.
If you had subscribed to the message_deliveries event, every time a message is sent, whether from a user to your page or from your page to a user, there is a, maybe more, calllback with a Message Delivered json object. The Webhook Reference has an example of the Message Delivered json object, but no specification or explanation on what the fields mean.
Occasionally I find that an undocumented Read callback is received, sometimes. The undocumented json data for this is like:
{"object":"page",
"entry":[
{
"id":"1722858134648129",
"time":1465407550812,
"messaging":[
{
"sender":{"id":"1131485883560113"},
"recipient":{"id":"1722858134642218"},
"timestamp":1465407550868,
"read":
{
"watermark":1465407548057,
"seq":428
}}]}]}
Essentially, you must code your callback to handle ALL types of json data gracefully, including unknowns, even though you may not be ready to process them further. For those that you are not ready to handle or uninterested in, return nothing with Http status code 204 (in fact every callback should return 204 as the type is void).
If you handle only those types of json data you are interested in, any unexpected json data will most likely raise an exception in whatever language your web callback code is written in and result in a 500 server error returned to Facebook. It is this 500 error that is causing Facebook to make that complaint in your question.

Facebook Graph API subscription to /pages/conversations does not update on outgoing messages

I'm working with Facebook pages and building an app that allows you to send/receive private messages for your page from an external app.
Everything works fine, I can import old messages, send new one. My issue comes from the real time update subscription.
As explained here and here, I have subscribed to the conversations field on the page object. I also set up my server to receive Facebook verification and the updates.
I do receive updates when someone sends a new message to my test page ( even if it's a bit slow ~30 sec delay ) but I never receive any updates when the page replies to a message.
Is there something else I need to subscribe to in order to receive these updates ? Do I need to look for another way to do it ? Or is it just not supported by Facebook real time API updates ?
Any help appreciated, let me know if you need more info and have a nice day.

SMS Facebook like count when reaching a threshold

I am looking for a way to automatically send SMS updates when a Facebook page is reaching a certain like count. I want to know when https://www.facebook.com/Foodler?ref=stream&fref=nf is nearing 100,000 likes automatically via SMS. Is this possible?
Ok, you have to break down your tasks. These are 2 separate concerns.
1 Track Facebook page constantly for likes
You can get this information by using Facebook's Graph API as a JSON record. A simple call (without requiring any API key will do the job)
http://graph.facebook.com/Foodler/
Right now, the "likes" key has value 97542
You can possibly run a cron-job or Scheduled Task (depending on your server type/configuration) to run a script (PHP/ASP/.NET, etc.) which further runs this API call every "X" minutes (or hours or days, whatever you wish) and parses the "likes" returned. Once they are >= 100,000, you can now send SMS using this script.
Your script can do so by now calling the SMS Gateway's API.
2 Sending SMS
You need an SMS Gateway (preferably a simple API?) for doing that. Just a simple google search for "sms gateway" returns many leading ones.

Is there a reliable way to record user initiated facebook apprequests?

It seems like the only way to do apprequests from user to user is via a dialogue. It also seem like I would need to do a ajax post with the invite ids to my server to record them happening. It's a bit unreliable.
Is there a way where I can get a list of the requests without needing to do a post?
You can only list pending requests by calling /me/apprequests on the user. Any other requests that were processed, you need to save them at the initial point of the request.

Facebook notification system: Is it polling?

Notification when the user first login, not so hard, just require a database scan, I can deal with that. However, when a friend send a request or comment on profile X, a notification is sent, and almost immediately receive on the other end even when the user X not making any request. Is it polling? Does not feel like it, since the page never refresh itself. It must be something else? Anyone have any idea? maybe Web Push?
Facebook uses long-polling.
While you're on their page, they have a script continually issue requests to a particular URL. Instead of immediately responding, the server handling that URL first waits for a message to come in to its queue, and then sends that message out to the user. If a certain timeout is reached, the server responds without sending a message, and the client-side script makes another request to that URL.
To see this in action, open up Firebug's Net tab while on a Facebook page and wait for a couple minutes. You should see requests that last for a minute and then are followed up with a new request.
I believe they use AJAX/Javascript for that. It would allow the page to get information from the server and display it without reloading the page. You should be able to do this with an AJAX library like JQuery or something similar. As for whether or not Facebook itself does push or poll, I have no idea, but you can get a similar behavior by polling with AJAX.