Update user's page in the same way Facebook do - httprequest

I am developing a web app for a little company. There, for example, we can have users A,B and C that are from a account team. We can have a users D,F,G,H that are form sales team.
Each user has his own private page, like Facebook and they are linked by team, that means in Facebook language, A is friend from B an C, not from D,F,G,H. So when B sends a new thought for the team the others team users (A,C) can see the message.
So my question is, lets say the user A is logged in at his page and user B send a message to the team, how to update the user A and C browser in the same way we see in facebook when we have new posts from our friends?
For the moment I could think about a script, trough AJAX, that could check in the database for the new posts from "myFriends" each 5seconds. But I dont think Facebook do this. Maybe Facebook use HTTP persistent conections http://en.wikipedia.org/wiki/HTTP_persistent_connection . I dont know.
In fact I dont know the best way (best practices) to achieve this results. Idont know even the tags I should use for this question.
Any thought?
Update: I am using php, AJAX, Bootsrap jQuery, MySQL

Estimated.
Whereas want similar functionality to Facebook, if you check the console (with Firebug in my case) you realize that always makes GET requests to the server, both for notifications to Chat. That is, do not use Http Persistent. You could use $.ajax() function of jQuery to make GET Requests, get a JSON and update the elements you want in your page.

Related

Query discord api from webiste

Is there a way to query the discord API without have a bot on a server.
I would like to retrieve my friends and my profile images and put it on a website I created.
I've done some research and only found stuff for discord bots.
Is what I want even possible ?
Yes, this is possible.
Take a look at this API route. You can just make a request there with the user id and get the whole user object.
If you need more information on the API itself and how to use it check out the Discord API Reference

Using Instagram's Graph API, how can I retrieve post data (media url, caption, poster username, etc)

I want to use Instagram's Graph API on my backend server to retrieve data about an Instagram post. On my frontend, users will submit a post URL (like https://www.instagram.com/p/CAvPIm2lszQ/). Then on my backend, I want to take the ID of the post from that URL (so in this case CAvPIm2lszQ) and then I'm hoping that I can pass that ID thru the Instagram Graph API and then retrieve the data that I need (media URL, caption, poster username, etc.).
So would that be possible? I did find documentation on "IG Media" for the Graph API, but under permissions, it says, "A Facebook User access token from a User who created the IG Media object, with the following permissions.."
Unless I'm misunderstanding it, I'm not sure if I'll be able to access posts from various public accounts. I think it's also worth mentioning that my users are not logging into their Instagram accounts to use my service so the only possible "User access token" would be my own.
Any ideas on how I can go about this? I was using the instagram.com/p/{post_id}/?__a=1 endpoint to meet my needs before but it doesn't work on my production server for some reason. So I'm kinda stuck.
Most probably you will not be able to achieve that using Instagram API. First of all the ID you are referring to CAvPIm2lszQ is not the ID that you will use for getting IG Media. The ID is different (it's numeric value like in the sample request from the page you've linked). The full URL that includes CAvPIm2lszQ is in the shortcode field.
At the moment it is not possible to look for the post detail using shortcode. If you want to use that endpoint you need to get the real post ID first, for instance by listing list of posts from given user.
But in order to do so - you need to use Facebook login authorization window to get token from given user. Alternatively you can try to request https://developers.facebook.com/docs/instagram-api/guides/business-discovery but it requires going through App review and having your own company to pass the Business Verification. Keep in mind that this endpoint returns information only about Instagram Professional accounts (Business/Creator account). You will not be able to get information about regular accounts.
And the last thing about ?__a=1 endpoint. This is not the official Instagram API. They use it only for their own purposes. Most probably your server IP address has been blocked due to sending too many requests.

For Twitter , how to create test user accounts?

Facebook allows you to create test user accounts that can only be used for testing purpose.
Does Twitter provide similar functionality ? I don't want to get my application blacklisted for creating fake user accounts; do I have to use my real user account for testing my application ? What strategies are your using for testing application with Twitter ?
As far as I know, there is nothing in Twitter's rules against creating account (unlike Facebook, where with the exception of test accounts, you're not allowed to create multiple accounts for testing purposes). So, you can just register the account like you normally would.
You might want to take a look at this post for some other tips for test accounts (hiding your tweets, deleting the account when you're done testing, etc.).
I'm currently creating a Twitter application and here are some of the strategies I'm using.
I create my accounts in combination with Gmail addresses. If I create a gmail account as user bob#gmail.com , I secure the Twitter name #bob on Twitter. That way it's kind of hard to forget where to email a lost password. I don't go crazy, as I don't need 100's of test accounts but I do have up to three.
I log on to my test accounts using Chrome because it will automatically recall your password as soon as you type in your Twitter name on the home page. That way it is easy to switch between them, but note that I find it hard sometimes to know which account is actually active because I'm constantly looking at other profiles. This gets confusing if I don't constantly look at the logged in user icon indicator.
Never, ever re-tweet anything unless you absolutely have to for a test case or use hash-tags unless for a test case. Unbelievably even on a completely un-publicized account, I had a few surprise Twitter users in my DB a few seconds after I re-tweeted a link.
on Localhost, close all your other browser windows while your testing. Especially if your calling the API through AJAX. You never know which sites you have open whom also call the Twitter API through AJAX, and this can seriously screw with your tests and rate limits. Especially when your developing live.
I would not recommend protecting your tweets. It's too limiting for most use cases.
For my site, I need to place a link in the tweets. Twitter will not
allow live links to http://localhost so you have to plan around
this and have a live test server sooner then you may anticipate.
Twitter has one of the easiest registration processes I have seen. You can quite easily create several test accounts; this is the only method I have used.
Here is a blog post about it.

Design for getting Twitter friends list for large user base and managing rate limiting

Assume there's a mobile app and a server.
I have question about rate limiting and hoping someone can give some advice on a design as I'm banging my head on how to navigate around rate limit. There must be something I"m missing because the 150 unauthenticated rate limit per IP per hour is extremely low.
Imagine the scenario I want to build is the following (simplified into a trivial example for this discusion). Assume user is signed into Twitter for this entire discussion to remove discussion about oAuth.
Mobile talks to our service to show users twitter friends list. Every time the mobile app is loaded, it will show the entire friends list, and highlighting the new friends that were added within the last 2 days.
That's it. But the trick is that I want to ensure that the friends list is always up to date in the client, which means our server has to have the most recent up to date friends list.
Periodically, I want my server to automatically scan the Twitter friends list for every user of my app to see if new friends have been added.
Our initial design was getting our server to do all the work with this flow:
New User signs in on client, gives access token to server
Server makes call to Twitter REST APIs to get initial friends lists
Server stores the Twitter Friends IDs and shows responds to the client with that list.
Periodically (e.g. every 48 hours), server checks Twitter REST APIs for friends list for each user and compares it to our cached Twitter friends list we have for them to see who is new and to highlight in the mobile app.
The good thing about this is that all the interaction with twitter to get friends list, compare and peridiocally refresh is on the server. Mobile client just makes a single call to my server and gets friends list.
The problem with this design is that it will work for a single user, but since the rate limit is 150 per hour on un-authenticated calls, I will hit my limit as soon as 151 users user my service (which has a fixed IP).
The only solution I can see is to have the client do the work for each user, then send me the friends list which my server caches. This takes care of Step #2 above. However, for Step #4, I'd have to build something into the client to auto refresh twitter friends and send back to the server.
This is super clumsy to have the client involved at all in this Twitter friends list operation.
At first I thought I was crazy and the public unauthenticated APIs like getting friends lists wouldn't be subject to rate limiting. However, according to their docs, it is.
Am I missing something obvious or is the only way to solve this is to put heavy logic into the client?
With whitelisting gone for those that aren't grandfathered or Twitter business partners, I don't think you have any alternative but to have your mobile app do the Twitter API calls from the handset.
Having the handset call Twitter isn't a bad thing by any means. Pretty much every Twitter client in the world does it. One benefit will be that the user will be authenticated to Twitter, and thus her full 350 calls per hour will be available to you. Keep in mind, however, that you should minimize your calls since the user may have other Twitter-aware applications installed on her handset eating into your call allotment, and vice versa.
Now to the solution. The way I would implement your use case would be to first fetch the complete list of friends for your user by calling the friends/ids method.
http://api.twitter.com/1/friends/ids.json?screen_name=yourUsersName
The above call will return the most recent 5,000 friend IDs, in order followed, for #yourUsersName. If you want to fetch more friend IDs than the first 5,000, you'll need to specify the cursor parameter to initiate paging.
Next, I would check the latest list of friends we just fetched against the list on the handset, syncing them by removing any IDs that are no longer present, while adding any that are new.
If we only need the friend IDs, then we're done at a cost of one API call per 5,000 friend IDs. If, however, we need to get user info for these new friends as well, then I would call users/lookup and pass in the list of all new users that we discovered while syncing friend IDs. You can request up to 100 user objects at a time.
http://api.twitter.com/1/users/lookup.json?user_id=123123,5235235,456243,4534563
You user must be authenticated in order to make the above request, but the call can fetch any Twitter user profiles you wish -- not just those that are friends of the authenticated user.
So, let's say for example that a user has 2,500 friends and has never used your app before. In that case, she would burn one call to fetch all of the friend IDs, and 25 calls for her friends' information. That's not too bad to get the app populated with data.
Subsequent calls should be more streamlined with probably only two calls burned (one for the IDs, and one to get the new friends).
Finally, once the data has been updated on the handset, the deltas for the IDs and user data can be gathered up and pushed to your server.
It may even be possible that your server application won't even have to interface with Twitter at all, and that should alleviate the 150 user limit you are encountering.
Some final notes:
Be sure to note in your app's privacy policy that you sync your user's friend list with your server.
I recommend specifying JSON as the return format for all Twitter API calls. It is a much more lightweight document format than XML, and you will typically transfer only about 1/3 to 1/2 as much data over the wire.
Pick a Twitter framework appropriate for your mobile device and your programming language. Twitter access is a commodity these days, and there's little to no reason to reinvent how to access the Twitter API.
I answered a similar question about an approach for efficiently fetching followers here.
Since you are making request on behalf of users you should make those requests be authenticated as those users. Then requests will count against each users own pool of 350 requests/hour.

Twitter API for searching a user's friends?

I'm working on an app that allows users to search for a particular friend on Twitter (and eventually Facebook) and then send them a message (sort of).
My problem is, the API limits me to only getting 100 friends per request. For a user with a lot of friends, this could take many requests (even if I cache it) and will make my app hit the rate limit pretty quickly.
Is there an official (or unofficial) Twitter API for searching for only your friends?
The solution I have implemented for now is this: whenever a user logs in, iterate through each 100 block of friends and put them in the Rails.cache. They stay there until the user logs out and logs back in. Now that I know that the API requests are counted against the logged in user, I shouldn't need to worry about hitting the rate limit API since each user will have 350 requests per hour.
However, I have found a few problems with this, and I have a few thoughts on solutions:
Problem: We are storing a large amount of data to cache someone's friends.
Solution: It would be best if we could cache all twitter users who are friends of one of our users in one object (or hash) and also cache only the IDs of the friends for each user (which can be grabbed with far less API calls). This would create a bit of a slowdown, but would be far less storage required. Then, whenever a user logs in, we would simply update the global friend cache with any changes (i.e. picture, name, etc.).
Problem: My application still has to store this and figure out how to parse it; it's not very organized.
Solution: Extract this functionality into a new application that creates a better API for searching. If I accomplish this, I'll post an update here with a link.