-2

Can anyone help me understand why do we request the API with FARADAY why not use AJAX ?? If we using Faraday what is the advantage over Ajax ?

I'm new to rails apologize if it is a basic question.

jvillian
  • 19,953
  • 5
  • 31
  • 44
Mun
  • 47
  • 2
  • 9

2 Answers2

2

Faraday is an HTTP client to do requests server to server.

Ajax is a technology to do asyncronous requests from the client to the server.

They are different things with different purposes.

arieljuod
  • 15,460
  • 2
  • 25
  • 36
2

The are not even comparable.

Faraday is a gem for performing HTTP requests from the server (to another server). Much like the Net::HTTP module from the standard library.

AJAX is used in the browser (client) to send HTTP requests asyncronously (without reloading the page).

Both can be used to fetch data from anywhere on the internet but run in completely different contexts. Which is more suited depends completly on the problem at hand.

The server is not limited by the same domain origin policy and other browser security features but doing requests on the server or proxying via the server ties up server resources and delays sending the response back.

The browser can perform a high number of asyncronous requests and do them after the inital page load which means AJAX can provide a more responsive user experience.

max
  • 96,212
  • 14
  • 104
  • 165
  • Thanks Max. Explained really well for a rails beginner. But i wanted to ask you here is as you said. Both Ajax and Faraday does the same purpose (i-e) Fetch the data from anywhere on the internet then what makes them different is just how they run in different contexts? Also request made from Server to another Server ? Could you give me example. Because i know the request made from client to Server through Ajax. But server to server? Thanks in advance. – Mun Oct 19 '18 at 18:16
  • You could for example create a feed in your Rails view that displays tweets by sending a request with Faraday to the twitter api and using the response. Not a great example - but I can't really think of anything since its such a fundamental tool. – max Oct 19 '18 at 18:25
  • You would use server side requests whenever you need data on the server (from another server) to respond to a request or even to create resources on another server. Like for example if you are doing image proccessing on the server and then upload the result to some other service. – max Oct 19 '18 at 18:29
  • So what i understood here is. Ajax and Faraday does the same work (i-e Fetch the data) but however ajax runs in completely different context (i-e) Asynchronously where as Faraday does not. But to what you said and my main doubt comes here is i can request Twitter API via- Ajax why do we specifically use Faraday?? I hope i'm making sense. @max – Mun Oct 19 '18 at 18:33
  • The difference is where the code runs. I don’t think I can explain this clearer. Thadvantage of performing a request via the server is that you can use server side templating and that you can send a request as the server and not share your credentials (on site x) with the client. You can also perform requests the brower cannot do due the same domain origin policy. This is not really a Rails thing - its fundamental to web development in general. – max Oct 19 '18 at 18:42
  • Thanks Max ! i got it. Appreciate you feedback – Mun Oct 19 '18 at 19:06