CodementorIO / rest-firebase

Ruby Firebase REST API client built on top of rest-core
Apache License 2.0
107 stars 22 forks source link

Can't connect (??) #11

Closed johncox00 closed 8 years ago

johncox00 commented 8 years ago

I have an initializer:

$firebase = RestFirebase.new(site:             FB_URL,
                             secret:           FB_SECRET,
                             log_method:       method(:puts),
                             error_callback:   method(:p),
                             timeout:          10,
                             max_retries:      3,
                             retry_exceptions: [IOError, SystemCallError, Timeout::Error],
                             auth_ttl:         82800)

 es = $firebase.event_source('threads')
 es.onopen   { |sock| p sock } # Called when connected
 es.onmessage{ |event, data, sock| p event, data } # Called for each message
 es.onerror  { |error, sock| p "Streaming error: #{error}" } # Called whenever there's an error
 es.onreconnect{ |error, sock| p error; true }

When runing rails s I get the following output:

=> Booting WEBrick
=> Rails 4.2.6 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Error getting IP address: Something went wrong running an SSH command!
command : ip addr show dev eth1
err     : exit status 255
output  :
[2016-04-25 09:07:49] INFO  WEBrick 1.3.1
[2016-04-25 09:07:49] INFO  ruby 2.2.2 (2015-04-13) [x86_64-darwin14]
[2016-04-25 09:07:49] INFO  WEBrick::HTTPServer#start: pid=97403 port=3000

Any insight?

godfat commented 8 years ago

Doesn't seem to be related to rest-firebase. Could you try to run it standalone? Also perhaps checking your Internet?

johncox00 commented 8 years ago

Turns out it was something to do with docker on my machine. Really strange. Sorry for the erroneous question here. Now I just have to figure out what I'm getting a 401 when trying to .put to firebase.

godfat commented 8 years ago

No problems. Good luck!

johncox00 commented 8 years ago

I'm getting a authorization error now. Anyway I can get some direction? john@mach19.com

godfat commented 8 years ago

Could you provide detail? There are three ways to give auth. One you give secret, and rest-firebase would generate the auth for you. The other is you give secret as the auth, acting as a never expired auth. The last one is you just give the auth generated from else where. Note that normally the auth would expire in 24 hours, so you need to update auth somehow in this case.

johncox00 commented 8 years ago

Thanks for the reply. Sure. I've tried a lot of ways. Here's the latest:

$firebase_stream = RestFirebase.new( site:             FB_URL,
                                     secret:           FB_SECRET,
                                     d:                {:uid => Time.now.to_i, :app => "sectretapp"},
                                     log_method:       method(:puts),
                                     error_callback:   method(:p),
                                     timeout:          10,
                                     max_retries:      3,
                                     retry_exceptions: [IOError, SystemCallError, Timeout::Error],
                                     auth_ttl:         82800
                                  )

 $es = $firebase_stream.event_source('threads')
 $es.onopen   { |sock| puts sock }
 $es.onmessage{ |event, data, sock| puts event, data }
 $es.onerror  { |error, sock| puts "Streaming error: #{error}" }
 $es.onreconnect{ |error, sock| puts error; true }

Then I fire uprails c:

Loading development environment (Rails 4.2.6)
2.2.2 :001 > $es.start
 => #<struct RestFirebase::Client::EventSource client=#<struct RestFirebasesite="https://yet..., headers={"Accept"=>"..., query={:auth=>"eyJ..., json_request=true, max_retries=3, retry_exceptions=[IOError, Sy..., timeout=10, max_redirects=5, error_handler=#<Proc:0x007..., error_detector=#<Proc:0x007..., json_response=true, log_method=#<Method: Ob..., cache=nil, expires_in=600, d={:uid=>14616..., secret="e2IgL1YqUoU..., auth="eyJ0eXAiOiJ..., auth_ttl=82800, iat=1461675335, config_engine=nil>, path="/", query={}, opts={}, socket=nil> 
2.2.2 :002 > RestCore: spent 0.664393 Requested GET https://something.firebaseio.com/.json?auth=[omitted]
RestCore: spent 0.619017 Requested GET https://s-usc1c-nss-121.firebaseio.com/.json?auth=[omitted]&ns=something&sse=true
#<RestFirebase::Error: [401] nil from https://s-usc1c-nss-121.firebaseio.com/.json?auth=[omitted]&ns=something&sse=true>
Streaming error: [401] nil from https://s-usc1c-nss-121.firebaseio.com/.json?auth=[omitted]&ns=something&sse=true

Note the 401.

godfat commented 8 years ago

Did you set your Firebase rule? You could do that from your Firebase console. Since you're requesting threads, make sure that you have made it accessible.

johncox00 commented 8 years ago

OK...I'm an idiot. That was exactly the issue. Thank you for pointing me int he right direction. You're a gentleman and a scholar.

godfat commented 8 years ago

Haha, no problems. I think I did similar things before. They should probably give better error messages instead of generic 401.