Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use streaming response ? #1071

Closed
sydneyfunnelAIO opened this issue Nov 6, 2019 · 7 comments
Closed

How to use streaming response ? #1071

sydneyfunnelAIO opened this issue Nov 6, 2019 · 7 comments
Labels
info Generic question on how to use Faraday

Comments

@sydneyfunnelAIO
Copy link

sydneyfunnelAIO commented Nov 6, 2019

Basic Info

  • Faraday Version:1.0 --pre
  • Ruby Version:2.5.6

Issue description

hello i am codding a chatbot for facebook live. and I need response live comments with a link.
yeah i know u sends me link: https://lostisland.github.io/faraday/usage/streaming
but i didnt understand how must ı use it. i tried thinks. can u send me any example for that.
i am newbie on faraday[trying just for this project].
thx again...

Steps to reproduce

image

@iMacTia
Copy link
Member

iMacTia commented Nov 6, 2019

Hi @sydneyfunnelAIO, not sure how used you are with Ruby, but basically req.options.on_data is a callback function that will be executed every time you receive a chunk of data.
A good starting point, would be to just print what you receive:

fb = 'https://streaming-graph.facebook.com'
conn = Faraday.new(fb)
conn.get('/9822...') do |req|
  req.options.on_data = Proc.new { |chunk, size| puts chunk }
end

If this works, you can then go on and change req.options.on_data to do whatever you want with chunk and size.

I hope this helps, please let me know how that goes

@iMacTia iMacTia added the info Generic question on how to use Faraday label Nov 6, 2019
@sydneyfunnelAIO
Copy link
Author

i tried but its didnt response anything.
image
my command line:
image

@iMacTia
Copy link
Member

iMacTia commented Nov 7, 2019

It would be easier to help if I could try this out, however I'm not a FB developer.
Is it possible to get a temporary access_token that I could use to try this locally?
You can send it over to me privately on Gitter

@snltd
Copy link

snltd commented Jan 28, 2020

@sydneyfunnelAIO how long are you leaving the connection open before killing it?

The problem I'm having -- possibly the same as you -- is that I get nothing for a long time, then suddenly a lot of data all at once. Hitting the same endpoint with curl produces a steady stream of data.

I haven't yet worked out why I see this pattern.

@iMacTia
Copy link
Member

iMacTia commented Feb 9, 2020

For everyone's info: I've spent some time investigating this with @sydneyfunnelAIO and we realised that even taking Faraday out of the equation and using plain Net::HTTP, the issue persists.
We then tried calling another streaming response with the same code snippet:

conn = Faraday.new('https://postman-echo.com')
conn.get('/stream/500') do |req|
  req.options.on_data = Proc.new do |chunk, size|
    puts chunk
    sleep 1
  end
end

and it worked exactly as expected. So we agreed there's something off with the Facebook API and this is not an issue in Faraday.

@iMacTia iMacTia closed this as completed Feb 9, 2020
@snltd
Copy link

snltd commented Feb 10, 2020

I made a similar investigation and came to the same conclusion. The behaviour I saw was a peculiarity of one particular endpoint.

@ioquatix
Copy link
Contributor

We've added support for streaming in socketry/async-http-faraday#41 and there is an example in the tests if that's helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info Generic question on how to use Faraday
Projects
None yet
Development

No branches or pull requests

4 participants