ryanb / private_pub

Handle pub/sub messaging through private channels in Rails using Faye.
MIT License
864 stars 228 forks source link

bad gateway 502 error after faye loaded on browser #132

Open lmahendra opened 5 years ago

lmahendra commented 5 years ago

I am trying to make faye work for my app. but getting 502 error after three successful requests at browser and connection halts. Below are my config details. nginx.conf =>

location /faye {
                proxy_pass         http://127.0.0.1:9292;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
                proxy_buffering off;
                proxy_redirect     off;
                proxy_http_version 1.1;
                proxy_connect_timeout      390;
                proxy_send_timeout         390;
                proxy_set_header X-Forwarded-Proto https;
                proxy_read_timeout 390;
        }

config/private_pub.yml =>

development:
  server: "http://localhost:9292/faye"
  secret_token: "oidb2d36a8c4b83db97dfh4147735829bc370036c0552e7d4eb945f9c90c4b"
test:
  server: "http://localhost:9292/faye"
  secret_token: "oidb2d36a8c4b83db97dfh4147735829bc370036c0552e7d4eb945f9c90c4b"
production:
  server: "https://www.lockated.com:443/faye"
  secret_token: "oidb2d36a8c4b83db97dfh4147735829bc370036c0552e7d4eb945f9c90c4b"
  signature_expiration: 3600 # one hour
  port: 443
  ssl_key_file: /home/ec2-user/www.key
  ssl_cert_file: /home/ec2-user/ssl_cert/2018.chained.crt
  environment: production
  rackup: private_pub.ru

faye.ru =>

require 'faye'
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)

class ServerAuth
  def incoming(message, callback)
    if message['channel'] !~ %r{^/meta/}
      if message['ext']['auth_token'] != FAYE_TOKEN
        message['error'] = 'Invalid authentication token'
      end
    end
    callback.call(message)
  end

  def outgoing(message, callback)
    if message['ext'] && message['ext']['auth_token']
      message['ext'] = {}
    end
    callback.call(message)
  end
end

faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 90)
faye_server.add_extension(ServerAuth.new)
run faye_server

private_pub.ru =>

require "bundler/setup"
require "yaml"
require "faye"
require "private_pub"

Faye::WebSocket.load_adapter('thin')

PrivatePub.load_config(File.expand_path("../config/private_pub.yml", __FILE__), "production")
run PrivatePub.faye_app

hitting below command successfully starts Thin web server RAILS_ENV=production bundle exec rackup private_pub.ru -s thin -E production

When I added below code in my view file, it shows below in Network of browser dev tools.

Request URL: https://www.mydomain.com/faye.js
Request Method: GET
Status Code: 200 OK (from disk cache)
Remote Address: xx.xx.xxx.xx:443
Referrer Policy: no-referrer-when-downgrade
Request URL: https://www.mydomain.com/faye?message=%5B%7B%22channel%22%3A%22%2Fmeta%2Fhandshake%22%2C%22version%22%3A%221.0%22%2C%22supportedConnectionTypes%22%3A%5B%22websocket%22%2C%22eventsource%22%2C%22long-polling%22%2C%22cross-origin-long-polling%22%2C%22callback-polling%22%5D%2C%22id%22%3A%221%22%7D%5D&jsonp=__jsonp1__
Request Method: GET
Status Code: 200 OK
Remote Address: xx.xx.xxx.xx:443
Referrer Policy: no-referrer-when-downgrade
Query string parameters
message: [{"channel":"/meta/handshake","version":"1.0","supportedConnectionTypes":["websocket","eventsource","long-polling","cross-origin-long-polling","callback-polling"],"id":"1"}]
jsonp: __jsonp1__
Request URL: wss://www.mydomain.com/faye
Request Method: GET
Status Code: 101 Switching Protocols
Request URL: https://www.mydomain.com/faye?message=%5B%7B%22channel%22%3A%22%2Fmeta%2Fconnect%22%2C%22clientId%22%3A%22iwuhtrmjgzgzb0cmsbvx98t9aetj3na%22%2C%22connectionType%22%3A%22callback-polling%22%2C%22id%22%3A%222%22%2C%22advice%22%3A%7B%22timeout%22%3A0%7D%7D%2C%7B%22channel%22%3A%22%2Fmeta%2Fsubscribe%22%2C%22clientId%22%3A%22iwuhtrmjgzgzb0cmsbvx98t9aetj3na%22%2C%22subscription%22%3A%22%2Fleads%2Fagent_id%3D2%22%2C%22id%22%3A%223%22%2C%22ext%22%3A%7B%22private_pub_signature%22%3A%22a5b95a0fdc2e2e9c4fcde9739e10d11a01d99f95%22%2C%22private_pub_timestamp%22%3A1534514001429%7D%7D%5D&jsonp=__jsonp2__
Request Method: GET
Status Code: 200 OK
Remote Address: xx.xx.xxx.xx:443
Referrer Policy: no-referrer-when-downgrade
Query string parameters
message: [{"channel":"/meta/connect","clientId":"iwuhtrmjgzgzb0cmsbvx98t9aetj3na","connectionType":"callback-polling","id":"2","advice":{"timeout":0}},{"channel":"/meta/subscribe","clientId":"iwuhtrmjgzgzb0cmsbvx98t9aetj3na","subscription":"/leads/agent_id=2","id":"3","ext":{"private_pub_signature":"a5b95a0fdc2e2e9c4fcde9739e10d11a01d99f95","private_pub_timestamp":1534514001429}}]
jsonp: __jsonp2__
Request URL: https://www.mydomain.com/faye?message=%5B%7B%22channel%22%3A%22%2Fmeta%2Fconnect%22%2C%22clientId%22%3A%22iwuhtrmjgzgzb0cmsbvx98t9aetj3na%22%2C%22connectionType%22%3A%22callback-polling%22%2C%22id%22%3A%224%22%7D%5D&jsonp=__jsonp3__
Request Method: GET
Status Code: 502 Bad Gateway
Remote Address: xx.xx.xxx.xx:443
Referrer Policy: no-referrer-when-downgrade
Query string parameters
message: [{"channel":"/meta/connect","clientId":"iwuhtrmjgzgzb0cmsbvx98t9aetj3na","connectionType":"callback-polling","id":"4"}]
jsonp: __jsonp3__

Nothing happens after this request. When I try to curl faye with below command, I get success message curl http://localhost:9292/faye -d 'message={"channel":"/leads/agent_id_2", "data":"hello", "ext":{"private_pub_token":"oidb2d36a8c4b83db97dfh4147735829bc370036c0552e7d4eb945f9c90c4b"}}

Response: [{"channel":"/leads/agent_id_2","successful":true}] But nothing happens at client side. Can anyone please tell me what could be the reason behing 502 bad request? or overall configuration? P.S I am using AWS & HTTPS Any help would be greatly appreciated.