Asghar
Asghar

Reputation: 2346

fetching all facebook messages with FQL

I need to fetch all users threads and messages inside thread for a facebook user with FQL.

When i run the below FQL it returns me all thread ids

select thread_id,message_count from thread where viewer_id = me() and folder_id = 0

showing threads and message count in the inbox folder,

    {
  "data": [
    {
      "thread_id": "196940283688620", 
      "message_count": 99
    }, 
    {
      "thread_id": "283904071679203", 
      "message_count": 1
    }, 

But when i run the below query to fetch all messages in the given thread_id suppose "thread_id": "196940283688620",

select message_id,body,viewer_id , thread_id from message where thread_id = 196940283688620 and viewer_id = me()

As the first query result is showing that i have 99 Messages in "thread_id": "196940283688620", but the query returned only 20 messages.

1- What i am going wrong? what i need to fetch all messages in a thread in a single FQL, or i need multiple FQL? 2- What is the best way to fetch all threads and messsages inside thread for a facebook authenticated user.

Upvotes: 3

Views: 4634

Answers (2)

Rafał Guźniczak
Rafał Guźniczak

Reputation: 103

My solution for this is like this:

SELECT message_id,body,viewer_id, thread_id FROM message 
  WHERE thread_id = 196940283688620 AND viewer_id = me() LIMIT 0,30 

Get first 30 results and then

SELECT message_id,body,viewer_id, thread_id FROM message 
      WHERE thread_id = 196940283688620 AND viewer_id = me() LIMIT 30,60

next 30 elements.

And so on ... I haven't found better solution yet.

Upvotes: 4

cpilko
cpilko

Reputation: 11852

For the problem where Facebook isn't returning all the messages shown in a thread, you are probably bumping up against a default internal limit. You can bypass this by using a LIMIT statement in FQL:

SELECT message_id,body,viewer_id, thread_id FROM message 
  WHERE thread_id = 196940283688620 AND viewer_id = me() LIMIT 200

In my experience, Facebook sets a maximum limit of 500 or 5000 items, depending on the object. When you exceed their maximum, you will start getting many fewer items than your limit returned. At that point, you'll need to find a way to paginate through the data with multiple queries.

As for the most efficient way to get all the threads and messages, it's an FQL multiquery.

You could restructure your queries into one like this: (white space added for readability)

 {'msg_threads':
    'SELECT thread_id,message_count FROM thread WHERE viewer_id = me() AND folder_id = 0',
  'msg_messages':
    'SELECT message_id,body,viewer_id, thread_id FROM message 
       WHERE thread_id IN (SELECT thread_id FROM #msg_threads)
       AND viewer_id = me() LIMIT 200'
  }

Upvotes: 3

Related Questions