HTTP Streaming on Heroku with Rails 3.1 & Unicorn - heroku

Inspired by the RailsCast on HTTP Streaming, I've decided to turn it on on my server. However, the listen 3000 :tcp_nopush => false line that Ryan suggests adding to my unicorn.rb doesn't play well with Heroku (they don't let you specify a port). Is streaming supported on Heroku, and how do I turn it on?

Heroku tells your application what port to listen on via the environment variable PORT. Check for this environment variable in your unicorn.rb.
port = ENV["PORT"].to_i
listen port, :tcp_nopush => false

Related

Wakanda Server 10 on Amazon EC2, cannot listen for connections on port 8080 or secure port 4433 on all IP addresses

I have installed wakanda server on an Amazon EC2 server running ubuntu by following this utube video: https://www.youtube.com/watch?v=uSQODnB7wRU .
Now the video is for an older version but I have followed along successfully until I actually launch wakanda on the server. This is what I get in the console:
Welcome to Wakanda Server 10 build 10.187175
Publishing "DefaultSolution" solution
The solution's log file will be stored in the "/home/ubuntu/.Wakanda Server/UserCache/Wakanda Server/DefaultSolution-1882/Logs/" folder
The Administration Web Server cannot listen for connections on port 8080 or secure port 4433 on all IP addresses
You can customize the Administration Web Server's ports with the "--admin-port" and "--admin-ssl-port" options
, then when I try to log into it via the browser it says the connection dropped! Any help would be much appreciated, it seams I need to restrict the IP addresses which can access, but how?
Your wakanda server tried and failed to listen on 8080 and/or 4433
Check the following things:
Are the ports 8080 and/or 4433 used by other processes? (sudo netstat -tapen | grep :8080, if a result is found, then yes another process uses 8080. Check 4433 also)
You may found that wakanda server is already running as a service:
yes you should use this service (create and edit /etc/default/wakanda, add WAKANDA_SOLUTION_AT_STARTUP=your_path and restart with sudo /etc/init.d/wakanda restart)
or to continue starting it manually, stop the service first (sudo /etc/init.d/wakanda stop)
Has the current user the right to listen on those ports? (try running the server with sudo just to check, then use authbind or equivalent)
Can you use alternative ports? (use --admin-port and --admin-ssl-port wakanda server options)
wakanda-server --help will give you the list of options available, especially --solution=VALUE to provide the path to your solution.

Can't access Ruby server on VM from host machine

I have a VM set up running Ubuntu server 14.04. In my VM I have created the following Ruby/Sinatra app:
require 'sinatra'
set :environment, :production
set :bind, '0.0.0.0'
get '/' do
"Hello World!"
end
When I execute this using ruby hello.rb I get the following output:
[2015-03-09 16:58:34] INFO WEBrick 1.3.1
[2015-03-09 16:58:34] INFO ruby 2.1.5 (2014-11-13) [x86_64-linux]
== Sinatra/1.4.5 has taken the stage on 4567 for production with backup from WEBrick
[2015-03-09 16:58:34] INFO WEBrick::HTTPServer#start: pid=2258 port=4567
Everything seems to work fine, but when I try to access localhost:4567 from my host machine (Windows 8.1) I get a GET http://localhost:4567/ net::ERR_CONNECTION_REFUSED error (in Chrome).
If I try to access the server from within my VM (ex, by using wget http://localhost:4567) it works fine.
I also have Apache2.4 running in my VM, which works fine, but I disabled it when trying to access my Ruby server by running sudo service apache2 stop.
What could the problem be? I have no problem running regular Ruby files, and I can access my Ruby/Sinatra app if I use Apache2 with Phusion Passenger. But when I simply run ruby hello.rb I can't access it from my host machine.
localhost refers to your local host, which in the case of Windows is not the same as your Ubuntu instance.
You'll need to connect to your Ubuntu instance IP directly, whatever that is. Usually you can find out with ip addr or ifconfig.
If you need a friendly DNS name you can put in your browser, xip.io provides one.
If you're using Vagrant then you can configure port forwarding so you can still use localhost if you want. Without port forwarding you will not be able to connect indirectly.
I have the network adapter for my VM attached to NAT. I was forwarding ports 443, 22 and 80 to my VM, and accessing my server on those ports works fine. Since I was running the Ruby WEBrick server on the default port 4567, I just had to forward port 4567 from my host machine to my VM as well.
After that change, typing http://localhost:4567 into my web browser served up the content from my Ruby file.

Issues sending HTTP messages using Telnet?

I am going through exercises in the book "Sinatra Up & Running" and am trying to send HTTP messages to Sinatra using Telnet.
This is what I am trying to do
[~]$ telnet 0.0.0.0 4567
However, I get the error:
Trying 0.0.0.0...
telnet: connect to address 0.0.0.0: Connection refused
telnet: Unable to connect to remote host
Sinatra is listening on localhost:4567 instead of 0.0.0.0:4567 and I think that is indicative of the problem.
I found some documentation at http://www.sinatrarb.com/configuration.html that talks about being able to specifically configure the development environment to listen on 0.0.0.0. I passed in:
ruby server.rb -o set :bind, '0.0.0.0'
And was able to adjust where Sinatra was listening to 0.0.0.0:4567 but telnet 0.0.0.0 4567 still gives the same error messages.
When you tell the server to listen on 0.0.0.0, that isn't actually a specific address, you're really telling it to bind to all available network interfaces. To connect to it, use either 127.0.0.1 or localhost, which are special addresses that always mean "this host":
telnet 127.0.0.1 4567
If you bind Sinatra to 0.0.0.0:4567 and it is listening they you will be able to externally hit your machine via the machine's ip.
E.g. machine-01 (192.168.122.100) is running Sinatra, which is bound to 0.0.0.0:4567 then from machine-02 (192.168.122.200) you'll be able to telent 192.168.122.100:4567 (provided there are no firewalls).
As the other post suggested, if you're trying to do it all on machine-01, then you want to bind Sinatra to localhost:4567 (127.0.0.1:4567), then you will be able to telenet localhost:4567

Rails App on Port 80 on Amazon EC2 - Use Nginx or IpTables

I have a simple rails app set up on EC2 and I run it with Unicorn. I want to set it up now so that it runs on port 80. Using the -p flag with the unicorn_rails command did not work; I received an error saying permission denied:
[ec2-user#ip-10-164-110-7 current]$ unicorn_rails -p 80
F, [2013-08-02T04:41:41.441613 #3297] FATAL -- : error adding listener addr=0.0.0.0:80
/home/ec2-user/uc_social_server/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn/socket_helper.rb:147:in `initialize': Permission denied - bind(2) (Errno::EACCES)
from /home/ec2-user/uc_social_server/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn/socket_helper.rb:147:in `new'
from /home/ec2-user/uc_social_server/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn/socket_helper.rb:147:in `bind_listen'
Searching for solutions online, I keep finding posts about setting up Nginx + Unicorn. Then there are some mentions of using IpTables to redirect port 8080 traffic to port 80.
I am not sure which approach to take.
Do I need Nginx?
Is there a way ec2-user can be allowed to run the unicorn process on port 80?
Is the IpTables solution a good approach? I somehow don't think it is.
Here they apparently solved the problem you have with unicorn -p:
Unable to start unicorn on port 80 using capistrano
The other two solutions are also both going to work well.
With IPtables you can redirect the incoming traffic from port 80 to your unicorn socket listening on 8080 and back. That's happening on the TCP layer.
The nginx approach is a transparent proxy setup. All magic is happening on the HTTP layer which gives you the advantage of being able to manipulate requests and responses, set up caching etc. nginx is a lovely tool which probably beats unicorns possibilities by far (and won't really slow you down while)

Connect to Juggernaut with Ruby

require "juggernaut"
Juggernaut.publish("channel1", "Some data")
The code above works if Juggernaut is on the same server as the one running the code. What's the syntax to use a Juggernaut running on another server?
The syntax is the same. I have a juggernaut.yml config file in my config directory with settings for each environment. To run juggernaut on port 8080 of localhost in development, I have:
development:
host: 'localhost'
port: 8080
To run it on a different host, you can just change that host setting (e.g. 'jugg.someserver.com')

Resources